[ Index ]

PHP Cross Reference of Unnamed Project

title

Body

[close]

/includes/acp/ -> acp_profile.php (source)

   1  <?php
   2  /**
   3  *
   4  * @package acp
   5  * @version $Id$
   6  * @copyright (c) 2005 phpBB Group
   7  * @license http://opensource.org/licenses/gpl-license.php GNU Public License
   8  *
   9  */
  10  
  11  /**
  12  * @ignore
  13  */
  14  if (!defined('IN_PHPBB'))
  15  {
  16      exit;
  17  }
  18  
  19  /**
  20  * @package acp
  21  */
  22  class acp_profile
  23  {
  24      var $u_action;
  25  
  26      var $edit_lang_id;
  27      var $lang_defs;
  28  
  29  	function main($id, $mode)
  30      {
  31          global $config, $db, $user, $auth, $template, $cache;
  32          global $phpbb_root_path, $phpbb_admin_path, $phpEx, $table_prefix;
  33  
  34          include($phpbb_root_path . 'includes/functions_posting.' . $phpEx);
  35          include($phpbb_root_path . 'includes/functions_user.' . $phpEx);
  36          include($phpbb_root_path . 'includes/functions_profile_fields.' . $phpEx);
  37  
  38          $user->add_lang(array('ucp', 'acp/profile'));
  39          $this->tpl_name = 'acp_profile';
  40          $this->page_title = 'ACP_CUSTOM_PROFILE_FIELDS';
  41  
  42          $action = (isset($_POST['create'])) ? 'create' : request_var('action', '');
  43  
  44          $error = array();
  45          $s_hidden_fields = '';
  46  
  47          // Define some default values for each field type
  48          $default_values = array(
  49              FIELD_STRING    => array('field_length' => 10, 'field_minlen' => 0, 'field_maxlen' => 20, 'field_validation' => '.*', 'field_novalue' => '', 'field_default_value' => ''),
  50              FIELD_TEXT        => array('field_length' => '5|80', 'field_minlen' => 0, 'field_maxlen' => 1000, 'field_validation' => '.*', 'field_novalue' => '', 'field_default_value' => ''),
  51              FIELD_INT        => array('field_length' => 5, 'field_minlen' => 0, 'field_maxlen' => 100, 'field_validation' => '', 'field_novalue' => 0, 'field_default_value' => 0),
  52              FIELD_DATE        => array('field_length' => 10, 'field_minlen' => 10, 'field_maxlen' => 10, 'field_validation' => '', 'field_novalue' => ' 0- 0-   0', 'field_default_value' => ' 0- 0-   0'),
  53              FIELD_BOOL        => array('field_length' => 1, 'field_minlen' => 0, 'field_maxlen' => 0, 'field_validation' => '', 'field_novalue' => 0, 'field_default_value' => 0),
  54              FIELD_DROPDOWN    => array('field_length' => 0, 'field_minlen' => 0, 'field_maxlen' => 5, 'field_validation' => '', 'field_novalue' => 0, 'field_default_value' => 0),
  55          );
  56  
  57          $cp = new custom_profile_admin();
  58  
  59          // Build Language array
  60          // Based on this, we decide which elements need to be edited later and which language items are missing
  61          $this->lang_defs = array();
  62  
  63          $sql = 'SELECT lang_id, lang_iso
  64              FROM ' . LANG_TABLE . '
  65              ORDER BY lang_english_name';
  66          $result = $db->sql_query($sql);
  67  
  68          while ($row = $db->sql_fetchrow($result))
  69          {
  70              // Make some arrays with all available languages
  71              $this->lang_defs['id'][$row['lang_id']] = $row['lang_iso'];
  72              $this->lang_defs['iso'][$row['lang_iso']] = $row['lang_id'];
  73          }
  74          $db->sql_freeresult($result);
  75  
  76          $sql = 'SELECT field_id, lang_id
  77              FROM ' . PROFILE_LANG_TABLE . '
  78              ORDER BY lang_id';
  79          $result = $db->sql_query($sql);
  80  
  81          while ($row = $db->sql_fetchrow($result))
  82          {
  83              // Which languages are available for each item
  84              $this->lang_defs['entry'][$row['field_id']][] = $row['lang_id'];
  85          }
  86          $db->sql_freeresult($result);
  87  
  88          // Have some fields been defined?
  89          if (isset($this->lang_defs['entry']))
  90          {
  91              foreach ($this->lang_defs['entry'] as $field_id => $field_ary)
  92              {
  93                  // Fill an array with the languages that are missing for each field
  94                  $this->lang_defs['diff'][$field_id] = array_diff(array_values($this->lang_defs['iso']), $field_ary);
  95              }
  96          }
  97  
  98          switch ($action)
  99          {
 100              case 'delete':
 101                  $field_id = request_var('field_id', 0);
 102  
 103                  if (!$field_id)
 104                  {
 105                      trigger_error($user->lang['NO_FIELD_ID'] . adm_back_link($this->u_action), E_USER_WARNING);
 106                  }
 107  
 108                  if (confirm_box(true))
 109                  {
 110                      $sql = 'SELECT field_ident
 111                          FROM ' . PROFILE_FIELDS_TABLE . "
 112                          WHERE field_id = $field_id";
 113                      $result = $db->sql_query($sql);
 114                      $field_ident = (string) $db->sql_fetchfield('field_ident');
 115                      $db->sql_freeresult($result);
 116  
 117                      $db->sql_transaction('begin');
 118  
 119                      $db->sql_query('DELETE FROM ' . PROFILE_FIELDS_TABLE . " WHERE field_id = $field_id");
 120                      $db->sql_query('DELETE FROM ' . PROFILE_FIELDS_LANG_TABLE . " WHERE field_id = $field_id");
 121                      $db->sql_query('DELETE FROM ' . PROFILE_LANG_TABLE . " WHERE field_id = $field_id");
 122  
 123                      switch ($db->sql_layer)
 124                      {
 125                          case 'sqlite':
 126                              $sql = "SELECT sql
 127                                  FROM sqlite_master
 128                                  WHERE type = 'table'
 129                                      AND name = '" . PROFILE_FIELDS_DATA_TABLE . "'
 130                                  ORDER BY type DESC, name;";
 131                              $result = $db->sql_query($sql);
 132                              $row = $db->sql_fetchrow($result);
 133                              $db->sql_freeresult($result);
 134  
 135                              // Create a temp table and populate it, destroy the existing one
 136                              $db->sql_query(preg_replace('#CREATE\s+TABLE\s+"?' . PROFILE_FIELDS_DATA_TABLE . '"?#i', 'CREATE TEMPORARY TABLE ' . PROFILE_FIELDS_DATA_TABLE . '_temp', $row['sql']));
 137                              $db->sql_query('INSERT INTO ' . PROFILE_FIELDS_DATA_TABLE . '_temp SELECT * FROM ' . PROFILE_FIELDS_DATA_TABLE);
 138                              $db->sql_query('DROP TABLE ' . PROFILE_FIELDS_DATA_TABLE);
 139  
 140                              preg_match('#\((.*)\)#s', $row['sql'], $matches);
 141  
 142                              $new_table_cols = trim($matches[1]);
 143                              $old_table_cols = preg_split('/,(?=[\\sa-z])/im', $new_table_cols);
 144                              $column_list = array();
 145  
 146                              foreach ($old_table_cols as $declaration)
 147                              {
 148                                  $entities = preg_split('#\s+#', trim($declaration));
 149  
 150                                  if ($entities[0] == 'PRIMARY')
 151                                  {
 152                                      continue;
 153                                  }
 154  
 155                                  if ($entities[0] !== 'pf_' . $field_ident)
 156                                  {
 157                                      $column_list[] = $entities[0];
 158                                  }
 159                              }
 160  
 161                              $columns = implode(',', $column_list);
 162  
 163                              $new_table_cols = preg_replace('/' . 'pf_' . $field_ident . '[^,]+,/', '', $new_table_cols);
 164  
 165                              // create a new table and fill it up. destroy the temp one
 166                              $db->sql_query('CREATE TABLE ' . PROFILE_FIELDS_DATA_TABLE . ' (' . $new_table_cols . ');');
 167                              $db->sql_query('INSERT INTO ' . PROFILE_FIELDS_DATA_TABLE . ' (' . $columns . ') SELECT ' . $columns . ' FROM ' . PROFILE_FIELDS_DATA_TABLE . '_temp;');
 168                              $db->sql_query('DROP TABLE ' . PROFILE_FIELDS_DATA_TABLE . '_temp');
 169                          break;
 170  
 171                          default:
 172                              $db->sql_query('ALTER TABLE ' . PROFILE_FIELDS_DATA_TABLE . " DROP COLUMN pf_$field_ident");
 173                      }
 174  
 175                      $order = 0;
 176  
 177                      $sql = 'SELECT *
 178                          FROM ' . PROFILE_FIELDS_TABLE . '
 179                          ORDER BY field_order';
 180                      $result = $db->sql_query($sql);
 181  
 182                      while ($row = $db->sql_fetchrow($result))
 183                      {
 184                          $order++;
 185                          if ($row['field_order'] != $order)
 186                          {
 187                              $sql = 'UPDATE ' . PROFILE_FIELDS_TABLE . "
 188                                  SET field_order = $order
 189                                  WHERE field_id = {$row['field_id']}";
 190                              $db->sql_query($sql);
 191                          }
 192                      }
 193                      $db->sql_freeresult($result);
 194  
 195                      $db->sql_transaction('commit');
 196  
 197                      add_log('admin', 'LOG_PROFILE_FIELD_REMOVED', $field_ident);
 198                      trigger_error($user->lang['REMOVED_PROFILE_FIELD'] . adm_back_link($this->u_action));
 199                  }
 200                  else
 201                  {
 202                      confirm_box(false, 'DELETE_PROFILE_FIELD', build_hidden_fields(array(
 203                          'i'            => $id,
 204                          'mode'        => $mode,
 205                          'action'    => $action,
 206                          'field_id'    => $field_id,
 207                      )));
 208                  }
 209  
 210              break;
 211  
 212              case 'activate':
 213                  $field_id = request_var('field_id', 0);
 214  
 215                  if (!$field_id)
 216                  {
 217                      trigger_error($user->lang['NO_FIELD_ID'] . adm_back_link($this->u_action), E_USER_WARNING);
 218                  }
 219  
 220                  $sql = 'SELECT lang_id
 221                      FROM ' . LANG_TABLE . "
 222                      WHERE lang_iso = '" . $db->sql_escape($config['default_lang']) . "'";
 223                  $result = $db->sql_query($sql);
 224                  $default_lang_id = (int) $db->sql_fetchfield('lang_id');
 225                  $db->sql_freeresult($result);
 226  
 227                  if (!in_array($default_lang_id, $this->lang_defs['entry'][$field_id]))
 228                  {
 229                      trigger_error($user->lang['DEFAULT_LANGUAGE_NOT_FILLED'] . adm_back_link($this->u_action), E_USER_WARNING);
 230                  }
 231  
 232                  $sql = 'UPDATE ' . PROFILE_FIELDS_TABLE . "
 233                      SET field_active = 1
 234                      WHERE field_id = $field_id";
 235                  $db->sql_query($sql);
 236  
 237                  $sql = 'SELECT field_ident
 238                      FROM ' . PROFILE_FIELDS_TABLE . "
 239                      WHERE field_id = $field_id";
 240                  $result = $db->sql_query($sql);
 241                  $field_ident = (string) $db->sql_fetchfield('field_ident');
 242                  $db->sql_freeresult($result);
 243  
 244                  add_log('admin', 'LOG_PROFILE_FIELD_ACTIVATE', $field_ident);
 245                  trigger_error($user->lang['PROFILE_FIELD_ACTIVATED'] . adm_back_link($this->u_action));
 246  
 247              break;
 248  
 249              case 'deactivate':
 250                  $field_id = request_var('field_id', 0);
 251  
 252                  if (!$field_id)
 253                  {
 254                      trigger_error($user->lang['NO_FIELD_ID'] . adm_back_link($this->u_action), E_USER_WARNING);
 255                  }
 256  
 257                  $sql = 'UPDATE ' . PROFILE_FIELDS_TABLE . "
 258                      SET field_active = 0
 259                      WHERE field_id = $field_id";
 260                  $db->sql_query($sql);
 261  
 262                  $sql = 'SELECT field_ident
 263                      FROM ' . PROFILE_FIELDS_TABLE . "
 264                      WHERE field_id = $field_id";
 265                  $result = $db->sql_query($sql);
 266                  $field_ident = (string) $db->sql_fetchfield('field_ident');
 267                  $db->sql_freeresult($result);
 268  
 269                  add_log('admin', 'LOG_PROFILE_FIELD_DEACTIVATE', $field_ident);
 270                  trigger_error($user->lang['PROFILE_FIELD_DEACTIVATED'] . adm_back_link($this->u_action));
 271  
 272              break;
 273  
 274              case 'move_up':
 275              case 'move_down':
 276                  $field_order = request_var('order', 0);
 277                  $order_total = $field_order * 2 + (($action == 'move_up') ? -1 : 1);
 278  
 279                  $sql = 'UPDATE ' . PROFILE_FIELDS_TABLE . "
 280                      SET field_order = $order_total - field_order
 281                      WHERE field_order IN ($field_order, " . (($action == 'move_up') ? $field_order - 1 : $field_order + 1) . ')';
 282                  $db->sql_query($sql);
 283  
 284              break;
 285  
 286              case 'create':
 287              case 'edit':
 288  
 289                  $field_id = request_var('field_id', 0);
 290                  $step = request_var('step', 1);
 291  
 292                  $submit = (isset($_REQUEST['next']) || isset($_REQUEST['prev'])) ? true : false;
 293                  $save = (isset($_REQUEST['save'])) ? true : false;
 294  
 295                  // The language id of default language
 296                  $this->edit_lang_id = $this->lang_defs['iso'][$config['default_lang']];
 297  
 298                  // We are editing... we need to grab basic things
 299                  if ($action == 'edit')
 300                  {
 301                      if (!$field_id)
 302                      {
 303                          trigger_error($user->lang['NO_FIELD_ID'] . adm_back_link($this->u_action), E_USER_WARNING);
 304                      }
 305  
 306                      $sql = 'SELECT l.*, f.*
 307                          FROM ' . PROFILE_LANG_TABLE . ' l, ' . PROFILE_FIELDS_TABLE . ' f
 308                          WHERE l.lang_id = ' . $this->edit_lang_id . "
 309                              AND f.field_id = $field_id
 310                              AND l.field_id = f.field_id";
 311                      $result = $db->sql_query($sql);
 312                      $field_row = $db->sql_fetchrow($result);
 313                      $db->sql_freeresult($result);
 314  
 315                      if (!$field_row)
 316                      {
 317                          // Some admin changed the default language?
 318                          $sql = 'SELECT l.*, f.*
 319                              FROM ' . PROFILE_LANG_TABLE . ' l, ' . PROFILE_FIELDS_TABLE . ' f
 320                              WHERE l.lang_id <> ' . $this->edit_lang_id . "
 321                              AND f.field_id = $field_id
 322                              AND l.field_id = f.field_id";
 323                          $result = $db->sql_query($sql);
 324                          $field_row = $db->sql_fetchrow($result);
 325                          $db->sql_freeresult($result);
 326  
 327                          if (!$field_row)
 328                          {
 329                              trigger_error($user->lang['FIELD_NOT_FOUND'] . adm_back_link($this->u_action), E_USER_WARNING);
 330                          }
 331  
 332                          $this->edit_lang_id = $field_row['lang_id'];
 333                      }
 334                      $field_type = $field_row['field_type'];
 335  
 336                      // Get language entries
 337                      $sql = 'SELECT *
 338                          FROM ' . PROFILE_FIELDS_LANG_TABLE . '
 339                          WHERE lang_id = ' . $this->edit_lang_id . "
 340                              AND field_id = $field_id
 341                          ORDER BY option_id ASC";
 342                      $result = $db->sql_query($sql);
 343  
 344                      $lang_options = array();
 345                      while ($row = $db->sql_fetchrow($result))
 346                      {
 347                          $lang_options[$row['option_id']] = $row['lang_value'];
 348                      }
 349                      $db->sql_freeresult($result);
 350  
 351                      $s_hidden_fields = '<input type="hidden" name="field_id" value="' . $field_id . '" />';
 352                  }
 353                  else
 354                  {
 355                      // We are adding a new field, define basic params
 356                      $lang_options = $field_row = array();
 357  
 358                      $field_type = request_var('field_type', 0);
 359  
 360                      if (!$field_type)
 361                      {
 362                          trigger_error($user->lang['NO_FIELD_TYPE'] . adm_back_link($this->u_action), E_USER_WARNING);
 363                      }
 364  
 365                      $field_row = array_merge($default_values[$field_type], array(
 366                          'field_ident'        => str_replace(' ', '_', utf8_clean_string(request_var('field_ident', '', true))),
 367                          'field_required'    => 0,
 368                          'field_show_novalue'=> 0,
 369                          'field_hide'        => 0,
 370                          'field_show_profile'=> 0,
 371                          'field_no_view'        => 0,
 372                          'field_show_on_reg'    => 0,
 373                          'field_show_on_vt'    => 0,
 374                          'lang_name'            => utf8_normalize_nfc(request_var('field_ident', '', true)),
 375                          'lang_explain'        => '',
 376                          'lang_default_value'=> '')
 377                      );
 378  
 379                      $s_hidden_fields = '<input type="hidden" name="field_type" value="' . $field_type . '" />';
 380                  }
 381  
 382                  // $exclude contains the data we gather in each step
 383                  $exclude = array(
 384                      1    => array('field_ident', 'lang_name', 'lang_explain', 'field_option_none', 'field_show_on_reg', 'field_show_on_vt', 'field_required', 'field_show_novalue', 'field_hide', 'field_show_profile', 'field_no_view'),
 385                      2    => array('field_length', 'field_maxlen', 'field_minlen', 'field_validation', 'field_novalue', 'field_default_value'),
 386                      3    => array('l_lang_name', 'l_lang_explain', 'l_lang_default_value', 'l_lang_options')
 387                  );
 388  
 389                  // Text-based fields require the lang_default_value to be excluded
 390                  if ($field_type == FIELD_STRING || $field_type == FIELD_TEXT)
 391                  {
 392                      $exclude[1][] = 'lang_default_value';
 393                  }
 394  
 395                  // option-specific fields require lang_options to be excluded
 396                  if ($field_type == FIELD_BOOL || $field_type == FIELD_DROPDOWN)
 397                  {
 398                      $exclude[1][] = 'lang_options';
 399                  }
 400  
 401                  $cp->vars['field_ident']        = ($action == 'create' && $step == 1) ? utf8_clean_string(request_var('field_ident', $field_row['field_ident'], true)) : request_var('field_ident', $field_row['field_ident']);
 402                  $cp->vars['lang_name']            = utf8_normalize_nfc(request_var('lang_name', $field_row['lang_name'], true));
 403                  $cp->vars['lang_explain']        = utf8_normalize_nfc(request_var('lang_explain', $field_row['lang_explain'], true));
 404                  $cp->vars['lang_default_value']    = utf8_normalize_nfc(request_var('lang_default_value', $field_row['lang_default_value'], true));
 405  
 406                  // Visibility Options...
 407                  $visibility_ary = array(
 408                      'field_required',
 409                      'field_show_novalue',
 410                      'field_show_on_reg',
 411                      'field_show_on_vt',
 412                      'field_show_profile',
 413                      'field_hide',
 414                  );
 415  
 416                  foreach ($visibility_ary as $val)
 417                  {
 418                      $cp->vars[$val] = ($submit || $save) ? request_var($val, 0) : $field_row[$val];
 419                  }
 420  
 421                  $cp->vars['field_no_view'] = request_var('field_no_view', (int) $field_row['field_no_view']);
 422  
 423                  // A boolean field expects an array as the lang options
 424                  if ($field_type == FIELD_BOOL)
 425                  {
 426                      $options = utf8_normalize_nfc(request_var('lang_options', array(''), true));
 427                  }
 428                  else
 429                  {
 430                      $options = utf8_normalize_nfc(request_var('lang_options', '', true));
 431                  }
 432  
 433                  // If the user has submitted a form with options (i.e. dropdown field)
 434                  if ($options)
 435                  {
 436                      $exploded_options = (is_array($options)) ? $options : explode("\n", $options);
 437  
 438                      if (sizeof($exploded_options) == sizeof($lang_options) || $action == 'create')
 439                      {
 440                          // The number of options in the field is equal to the number of options already in the database
 441                          // Or we are creating a new dropdown list.
 442                          $cp->vars['lang_options'] = $exploded_options;
 443                      }
 444                      else if ($action == 'edit')
 445                      {
 446                          // Changing the number of options? (We remove and re-create the option fields)
 447                          $cp->vars['lang_options'] = $exploded_options;
 448                      }
 449                  }
 450                  else
 451                  {
 452                      $cp->vars['lang_options'] = $lang_options;
 453                  }
 454  
 455                  // step 2
 456                  foreach ($exclude[2] as $key)
 457                  {
 458                      $var = utf8_normalize_nfc(request_var($key, $field_row[$key], true));
 459  
 460                      // Manipulate the intended variables a little bit if needed
 461                      if ($field_type == FIELD_DROPDOWN && $key == 'field_maxlen')
 462                      {
 463                          // Get the number of options if this key is 'field_maxlen'
 464                          $var = sizeof(explode("\n", utf8_normalize_nfc(request_var('lang_options', '', true))));
 465                      }
 466                      else if ($field_type == FIELD_TEXT && $key == 'field_length')
 467                      {
 468                          if (isset($_REQUEST['rows']))
 469                          {
 470                              $cp->vars['rows'] = request_var('rows', 0);
 471                              $cp->vars['columns'] = request_var('columns', 0);
 472                              $var = $cp->vars['rows'] . '|' . $cp->vars['columns'];
 473                          }
 474                          else
 475                          {
 476                              $row_col = explode('|', $var);
 477                              $cp->vars['rows'] = $row_col[0];
 478                              $cp->vars['columns'] = $row_col[1];
 479                          }
 480                      }
 481                      else if ($field_type == FIELD_DATE && $key == 'field_default_value')
 482                      {
 483                          $always_now = request_var('always_now', -1);
 484  
 485                          if ($always_now == 1 || ($always_now === -1 && $var == 'now'))
 486                          {
 487                              $now = getdate();
 488  
 489                              $cp->vars['field_default_value_day'] = $now['mday'];
 490                              $cp->vars['field_default_value_month'] = $now['mon'];
 491                              $cp->vars['field_default_value_year'] = $now['year'];
 492                              $var = $_POST['field_default_value'] = 'now';
 493                          }
 494                          else
 495                          {
 496                              if (isset($_REQUEST['field_default_value_day']))
 497                              {
 498                                  $cp->vars['field_default_value_day'] = request_var('field_default_value_day', 0);
 499                                  $cp->vars['field_default_value_month'] = request_var('field_default_value_month', 0);
 500                                  $cp->vars['field_default_value_year'] = request_var('field_default_value_year', 0);
 501                                  $var = $_POST['field_default_value'] = sprintf('%2d-%2d-%4d', $cp->vars['field_default_value_day'], $cp->vars['field_default_value_month'], $cp->vars['field_default_value_year']);
 502                              }
 503                              else
 504                              {
 505                                  list($cp->vars['field_default_value_day'], $cp->vars['field_default_value_month'], $cp->vars['field_default_value_year']) = explode('-', $var);
 506                              }
 507                          }
 508                      }
 509                      else if ($field_type == FIELD_BOOL && $key == 'field_default_value')
 510                      {
 511                          // 'field_length' == 1 defines radio buttons. Possible values are 1 or 2 only.
 512                          // 'field_length' == 2 defines checkbox. Possible values are 0 or 1 only.
 513                          // If we switch the type on step 2, we have to adjust field value.
 514                          // 1 is a common value for the checkbox and radio buttons.
 515  
 516                          // Adjust unchecked checkbox value.
 517                          // If we return or save settings from 2nd/3rd page
 518                          // and the checkbox is unchecked, set the value to 0.
 519                          if (isset($_REQUEST['step']) && !isset($_REQUEST[$key]))
 520                          {
 521                              $var = 0;
 522                          }
 523  
 524                          // If we switch to the checkbox type but former radio buttons value was 2,
 525                          // which is not the case for the checkbox, set it to 0 (unchecked).
 526                          if ($cp->vars['field_length'] == 2 && $var == 2)
 527                          {
 528                              $var = 0;
 529                          }
 530                          // If we switch to the radio buttons but the former checkbox value was 0,
 531                          // which is not the case for the radio buttons, set it to 0.
 532                          else if ($cp->vars['field_length'] == 1 && $var == 0)
 533                          {
 534                              $var = 2;
 535                          }
 536                      }
 537                      else if ($field_type == FIELD_INT && $key == 'field_default_value')
 538                      {
 539                          // Permit an empty string
 540                          if ($action == 'create' && request_var('field_default_value', '') === '')
 541                          {
 542                              $var = '';
 543                          }
 544                      }
 545  
 546                      $cp->vars[$key] = $var;
 547                  }
 548  
 549                  // step 3 - all arrays
 550                  if ($action == 'edit')
 551                  {
 552                      // Get language entries
 553                      $sql = 'SELECT *
 554                          FROM ' . PROFILE_FIELDS_LANG_TABLE . '
 555                          WHERE lang_id <> ' . $this->edit_lang_id . "
 556                              AND field_id = $field_id
 557                          ORDER BY option_id ASC";
 558                      $result = $db->sql_query($sql);
 559  
 560                      $l_lang_options = array();
 561                      while ($row = $db->sql_fetchrow($result))
 562                      {
 563                          $l_lang_options[$row['lang_id']][$row['option_id']] = $row['lang_value'];
 564                      }
 565                      $db->sql_freeresult($result);
 566  
 567  
 568                      $sql = 'SELECT lang_id, lang_name, lang_explain, lang_default_value
 569                          FROM ' . PROFILE_LANG_TABLE . '
 570                          WHERE lang_id <> ' . $this->edit_lang_id . "
 571                              AND field_id = $field_id
 572                          ORDER BY lang_id ASC";
 573                      $result = $db->sql_query($sql);
 574  
 575                      $l_lang_name = $l_lang_explain = $l_lang_default_value = array();
 576                      while ($row = $db->sql_fetchrow($result))
 577                      {
 578                          $l_lang_name[$row['lang_id']] = $row['lang_name'];
 579                          $l_lang_explain[$row['lang_id']] = $row['lang_explain'];
 580                          $l_lang_default_value[$row['lang_id']] = $row['lang_default_value'];
 581                      }
 582                      $db->sql_freeresult($result);
 583                  }
 584  
 585                  foreach ($exclude[3] as $key)
 586                  {
 587                      $cp->vars[$key] = utf8_normalize_nfc(request_var($key, array(0 => ''), true));
 588  
 589                      if (!$cp->vars[$key] && $action == 'edit')
 590                      {
 591                          $cp->vars[$key] = $$key;
 592                      }
 593                      else if ($key == 'l_lang_options' && $field_type == FIELD_BOOL)
 594                      {
 595                          $cp->vars[$key] = utf8_normalize_nfc(request_var($key, array(0 => array('')), true));
 596                      }
 597                      else if ($key == 'l_lang_options' && is_array($cp->vars[$key]))
 598                      {
 599                          foreach ($cp->vars[$key] as $lang_id => $options)
 600                          {
 601                              $cp->vars[$key][$lang_id] = explode("\n", $options);
 602                          }
 603  
 604                      }
 605                  }
 606  
 607                  // Check for general issues in every step
 608                  if ($submit) //  && $step == 1
 609                  {
 610                      // Check values for step 1
 611                      if ($cp->vars['field_ident'] == '')
 612                      {
 613                          $error[] = $user->lang['EMPTY_FIELD_IDENT'];
 614                      }
 615  
 616                      if (!preg_match('/^[a-z_]+$/', $cp->vars['field_ident']))
 617                      {
 618                          $error[] = $user->lang['INVALID_CHARS_FIELD_IDENT'];
 619                      }
 620  
 621                      if (strlen($cp->vars['field_ident']) > 17)
 622                      {
 623                          $error[] = $user->lang['INVALID_FIELD_IDENT_LEN'];
 624                      }
 625  
 626                      if ($cp->vars['lang_name'] == '')
 627                      {
 628                          $error[] = $user->lang['EMPTY_USER_FIELD_NAME'];
 629                      }
 630  
 631                      if ($field_type == FIELD_DROPDOWN && !sizeof($cp->vars['lang_options']))
 632                      {
 633                          $error[] = $user->lang['NO_FIELD_ENTRIES'];
 634                      }
 635  
 636                      if ($field_type == FIELD_BOOL && (empty($cp->vars['lang_options'][0]) || empty($cp->vars['lang_options'][1])))
 637                      {
 638                          $error[] = $user->lang['NO_FIELD_ENTRIES'];
 639                      }
 640  
 641                      // Check for already existing field ident
 642                      if ($action != 'edit')
 643                      {
 644                          $sql = 'SELECT field_ident
 645                              FROM ' . PROFILE_FIELDS_TABLE . "
 646                              WHERE field_ident = '" . $db->sql_escape($cp->vars['field_ident']) . "'";
 647                          $result = $db->sql_query($sql);
 648                          $row = $db->sql_fetchrow($result);
 649                          $db->sql_freeresult($result);
 650  
 651                          if ($row)
 652                          {
 653                              $error[] = $user->lang['FIELD_IDENT_ALREADY_EXIST'];
 654                          }
 655                      }
 656                  }
 657  
 658                  $step = (isset($_REQUEST['next'])) ? $step + 1 : ((isset($_REQUEST['prev'])) ? $step - 1 : $step);
 659  
 660                  if (sizeof($error))
 661                  {
 662                      $step--;
 663                      $submit = false;
 664                  }
 665  
 666                  // Build up the specific hidden fields
 667                  foreach ($exclude as $num => $key_ary)
 668                  {
 669                      if ($num == $step)
 670                      {
 671                          continue;
 672                      }
 673  
 674                      $_new_key_ary = array();
 675  
 676                      foreach ($key_ary as $key)
 677                      {
 678                          if ($field_type == FIELD_TEXT && $key == 'field_length' && isset($_REQUEST['rows']))
 679                          {
 680                              $cp->vars['rows'] = request_var('rows', 0);
 681                              $cp->vars['columns'] = request_var('columns', 0);
 682                              $_new_key_ary[$key] = $cp->vars['rows'] . '|' . $cp->vars['columns'];
 683                          }
 684                          else if ($field_type == FIELD_DATE && $key == 'field_default_value')
 685                          {
 686                              $always_now = request_var('always_now', 0);
 687  
 688                              if ($always_now)
 689                              {
 690                                  $_new_key_ary[$key] = 'now';
 691                              }
 692                              else if (isset($_REQUEST['field_default_value_day']))
 693                              {
 694                                  $cp->vars['field_default_value_day'] = request_var('field_default_value_day', 0);
 695                                  $cp->vars['field_default_value_month'] = request_var('field_default_value_month', 0);
 696                                  $cp->vars['field_default_value_year'] = request_var('field_default_value_year', 0);
 697                                  $_new_key_ary[$key]  = sprintf('%2d-%2d-%4d', $cp->vars['field_default_value_day'], $cp->vars['field_default_value_month'], $cp->vars['field_default_value_year']);
 698                              }
 699                          }
 700                          else if ($field_type == FIELD_BOOL && $key == 'l_lang_options' && isset($_REQUEST['l_lang_options']))
 701                          {
 702                              $_new_key_ary[$key] = utf8_normalize_nfc(request_var($key, array(array('')), true));
 703                          }
 704                          else if ($field_type == FIELD_BOOL && $key == 'field_default_value')
 705                          {
 706                              $_new_key_ary[$key] =  request_var($key, $cp->vars[$key]);
 707                          }
 708                          else
 709                          {
 710                              if (!isset($_REQUEST[$key]))
 711                              {
 712                                  $var = false;
 713                              }
 714                              else if ($key == 'field_ident' && isset($cp->vars[$key]))
 715                              {
 716                                  $_new_key_ary[$key]= $cp->vars[$key];
 717                              }
 718                              else
 719                              {
 720                                  $_new_key_ary[$key] = (is_array($_REQUEST[$key])) ? utf8_normalize_nfc(request_var($key, array(''), true)) : utf8_normalize_nfc(request_var($key, '', true));
 721                              }
 722                          }
 723                      }
 724  
 725                      $s_hidden_fields .= build_hidden_fields($_new_key_ary);
 726                  }
 727  
 728                  if (!sizeof($error))
 729                  {
 730                      if ($step == 3 && (sizeof($this->lang_defs['iso']) == 1 || $save))
 731                      {
 732                          $this->save_profile_field($cp, $field_type, $action);
 733                      }
 734                      else if ($action == 'edit' && $save)
 735                      {
 736                          $this->save_profile_field($cp, $field_type, $action);
 737                      }
 738                  }
 739  
 740                  $template->assign_vars(array(
 741                      'S_EDIT'            => true,
 742                      'S_EDIT_MODE'        => ($action == 'edit') ? true : false,
 743                      'ERROR_MSG'            => (sizeof($error)) ? implode('<br />', $error) : '',
 744  
 745                      'L_TITLE'            => $user->lang['STEP_' . $step . '_TITLE_' . strtoupper($action)],
 746                      'L_EXPLAIN'            => $user->lang['STEP_' . $step . '_EXPLAIN_' . strtoupper($action)],
 747  
 748                      'U_ACTION'            => $this->u_action . "&amp;action=$action&amp;step=$step",
 749                      'U_BACK'            => $this->u_action)
 750                  );
 751  
 752                  // Now go through the steps
 753                  switch ($step)
 754                  {
 755                      // Create basic options - only small differences between field types
 756                      case 1:
 757  
 758                          // Build common create options
 759                          $template->assign_vars(array(
 760                              'S_STEP_ONE'        => true,
 761                              'S_FIELD_REQUIRED'    => ($cp->vars['field_required']) ? true : false,
 762                              'S_FIELD_SHOW_NOVALUE'=> ($cp->vars['field_show_novalue']) ? true : false,
 763                              'S_SHOW_ON_REG'        => ($cp->vars['field_show_on_reg']) ? true : false,
 764                              'S_SHOW_ON_VT'        => ($cp->vars['field_show_on_vt']) ? true : false,
 765                              'S_FIELD_HIDE'        => ($cp->vars['field_hide']) ? true : false,
 766                              'S_SHOW_PROFILE'    => ($cp->vars['field_show_profile']) ? true : false,
 767                              'S_FIELD_NO_VIEW'    => ($cp->vars['field_no_view']) ? true : false,
 768  
 769                              'L_LANG_SPECIFIC'    => sprintf($user->lang['LANG_SPECIFIC_OPTIONS'], $config['default_lang']),
 770                              'FIELD_TYPE'        => $user->lang['FIELD_' . strtoupper($cp->profile_types[$field_type])],
 771                              'FIELD_IDENT'        => $cp->vars['field_ident'],
 772                              'LANG_NAME'            => $cp->vars['lang_name'],
 773                              'LANG_EXPLAIN'        => $cp->vars['lang_explain'])
 774                          );
 775  
 776                          // String and Text needs to set default values here...
 777                          if ($field_type == FIELD_STRING || $field_type == FIELD_TEXT)
 778                          {
 779                              $template->assign_vars(array(
 780                                  'S_TEXT'        => ($field_type == FIELD_TEXT) ? true : false,
 781                                  'S_STRING'        => ($field_type == FIELD_STRING) ? true : false,
 782  
 783                                  'L_DEFAULT_VALUE_EXPLAIN'    => $user->lang[strtoupper($cp->profile_types[$field_type]) . '_DEFAULT_VALUE_EXPLAIN'],
 784                                  'LANG_DEFAULT_VALUE'        => $cp->vars['lang_default_value'])
 785                              );
 786                          }
 787  
 788                          if ($field_type == FIELD_BOOL || $field_type == FIELD_DROPDOWN)
 789                          {
 790                              // Initialize these array elements if we are creating a new field
 791                              if (!sizeof($cp->vars['lang_options']))
 792                              {
 793                                  if ($field_type == FIELD_BOOL)
 794                                  {
 795                                      // No options have been defined for a boolean field.
 796                                      $cp->vars['lang_options'][0] = '';
 797                                      $cp->vars['lang_options'][1] = '';
 798                                  }
 799                                  else
 800                                  {
 801                                      // No options have been defined for the dropdown menu
 802                                      $cp->vars['lang_options'] = array();
 803                                  }
 804                              }
 805  
 806                              $template->assign_vars(array(
 807                                  'S_BOOL'        => ($field_type == FIELD_BOOL) ? true : false,
 808                                  'S_DROPDOWN'    => ($field_type == FIELD_DROPDOWN) ? true : false,
 809  
 810                                  'L_LANG_OPTIONS_EXPLAIN'    => $user->lang[strtoupper($cp->profile_types[$field_type]) . '_ENTRIES_EXPLAIN'],
 811                                  'LANG_OPTIONS'                => ($field_type == FIELD_DROPDOWN) ? implode("\n", $cp->vars['lang_options']) : '',
 812                                  'FIRST_LANG_OPTION'            => ($field_type == FIELD_BOOL) ? $cp->vars['lang_options'][0] : '',
 813                                  'SECOND_LANG_OPTION'        => ($field_type == FIELD_BOOL) ? $cp->vars['lang_options'][1] : '')
 814                              );
 815                          }
 816  
 817                      break;
 818  
 819                      case 2:
 820  
 821                          $template->assign_vars(array(
 822                              'S_STEP_TWO'        => true,
 823                              'L_NEXT_STEP'            => (sizeof($this->lang_defs['iso']) == 1) ? $user->lang['SAVE'] : $user->lang['PROFILE_LANG_OPTIONS'])
 824                          );
 825  
 826                          // Build options based on profile type
 827                          $function = 'get_' . $cp->profile_types[$field_type] . '_options';
 828                          $options = $cp->$function();
 829  
 830                          foreach ($options as $num => $option_ary)
 831                          {
 832                              $template->assign_block_vars('option', $option_ary);
 833                          }
 834  
 835                      break;
 836  
 837                      // Define remaining language variables
 838                      case 3:
 839  
 840                          $template->assign_var('S_STEP_THREE', true);
 841                          $options = $this->build_language_options($cp, $field_type, $action);
 842  
 843                          foreach ($options as $lang_id => $lang_ary)
 844                          {
 845                              $template->assign_block_vars('options', array(
 846                                  'LANGUAGE'        => sprintf($user->lang[(($lang_id == $this->edit_lang_id) ? 'DEFAULT_' : '') . 'ISO_LANGUAGE'], $lang_ary['lang_iso']))
 847                              );
 848  
 849                              foreach ($lang_ary['fields'] as $field_ident => $field_ary)
 850                              {
 851                                  $template->assign_block_vars('options.field', array(
 852                                      'L_TITLE'        => $field_ary['TITLE'],
 853                                      'L_EXPLAIN'        => (isset($field_ary['EXPLAIN'])) ? $field_ary['EXPLAIN'] : '',
 854                                      'FIELD'            => $field_ary['FIELD'])
 855                                  );
 856                              }
 857                          }
 858  
 859                      break;
 860                  }
 861  
 862                  $template->assign_vars(array(
 863                      'S_HIDDEN_FIELDS'    => $s_hidden_fields)
 864                  );
 865  
 866                  return;
 867  
 868              break;
 869          }
 870  
 871          $sql = 'SELECT *
 872              FROM ' . PROFILE_FIELDS_TABLE . '
 873              ORDER BY field_order';
 874          $result = $db->sql_query($sql);
 875  
 876          $s_one_need_edit = false;
 877          while ($row = $db->sql_fetchrow($result))
 878          {
 879              $active_lang = (!$row['field_active']) ? 'ACTIVATE' : 'DEACTIVATE';
 880              $active_value = (!$row['field_active']) ? 'activate' : 'deactivate';
 881              $id = $row['field_id'];
 882  
 883              $s_need_edit = (sizeof($this->lang_defs['diff'][$row['field_id']])) ? true : false;
 884  
 885              if ($s_need_edit)
 886              {
 887                  $s_one_need_edit = true;
 888              }
 889  
 890              $template->assign_block_vars('fields', array(
 891                  'FIELD_IDENT'        => $row['field_ident'],
 892                  'FIELD_TYPE'        => $user->lang['FIELD_' . strtoupper($cp->profile_types[$row['field_type']])],
 893  
 894                  'L_ACTIVATE_DEACTIVATE'        => $user->lang[$active_lang],
 895                  'U_ACTIVATE_DEACTIVATE'        => $this->u_action . "&amp;action=$active_value&amp;field_id=$id",
 896                  'U_EDIT'                    => $this->u_action . "&amp;action=edit&amp;field_id=$id",
 897                  'U_TRANSLATE'                => $this->u_action . "&amp;action=edit&amp;field_id=$id&amp;step=3",
 898                  'U_DELETE'                    => $this->u_action . "&amp;action=delete&amp;field_id=$id",
 899                  'U_MOVE_UP'                    => $this->u_action . "&amp;action=move_up&amp;order={$row['field_order']}",
 900                  'U_MOVE_DOWN'                => $this->u_action . "&amp;action=move_down&amp;order={$row['field_order']}",
 901  
 902                  'S_NEED_EDIT'                => $s_need_edit)
 903              );
 904          }
 905          $db->sql_freeresult($result);
 906  
 907          // At least one option field needs editing?
 908          if ($s_one_need_edit)
 909          {
 910              $template->assign_var('S_NEED_EDIT', true);
 911          }
 912  
 913          $s_select_type = '';
 914          foreach ($cp->profile_types as $key => $value)
 915          {
 916              $s_select_type .= '<option value="' . $key . '">' . $user->lang['FIELD_' . strtoupper($value)] . '</option>';
 917          }
 918  
 919          $template->assign_vars(array(
 920              'U_ACTION'            => $this->u_action,
 921              'S_TYPE_OPTIONS'    => $s_select_type)
 922          );
 923      }
 924  
 925      /**
 926      * Build all Language specific options
 927      */
 928  	function build_language_options(&$cp, $field_type, $action = 'create')
 929      {
 930          global $user, $config, $db;
 931  
 932          $default_lang_id = (!empty($this->edit_lang_id)) ? $this->edit_lang_id : $this->lang_defs['iso'][$config['default_lang']];
 933  
 934          $sql = 'SELECT lang_id, lang_iso
 935              FROM ' . LANG_TABLE . '
 936              WHERE lang_id <> ' . (int) $default_lang_id . '
 937              ORDER BY lang_english_name';
 938          $result = $db->sql_query($sql);
 939  
 940          $languages = array();
 941          while ($row = $db->sql_fetchrow($result))
 942          {
 943              $languages[$row['lang_id']] = $row['lang_iso'];
 944          }
 945          $db->sql_freeresult($result);
 946  
 947          $options = array();
 948          $options['lang_name'] = 'string';
 949          if ($cp->vars['lang_explain'])
 950          {
 951              $options['lang_explain'] = 'text';
 952          }
 953  
 954          switch ($field_type)
 955          {
 956              case FIELD_BOOL:
 957                  $options['lang_options'] = 'two_options';
 958              break;
 959  
 960              case FIELD_DROPDOWN:
 961                  $options['lang_options'] = 'optionfield';
 962              break;
 963  
 964              case FIELD_TEXT:
 965              case FIELD_STRING:
 966                  if (strlen($cp->vars['lang_default_value']))
 967                  {
 968                      $options['lang_default_value'] = ($field_type == FIELD_STRING) ? 'string' : 'text';
 969                  }
 970              break;
 971          }
 972  
 973          $lang_options = array();
 974  
 975          foreach ($options as $field => $field_type)
 976          {
 977              $lang_options[1]['lang_iso'] = $this->lang_defs['id'][$default_lang_id];
 978              $lang_options[1]['fields'][$field] = array(
 979                  'TITLE'        => $user->lang['CP_' . strtoupper($field)],
 980                  'FIELD'        => '<dd>' . ((is_array($cp->vars[$field])) ? implode('<br />', $cp->vars[$field]) : bbcode_nl2br($cp->vars[$field])) . '</dd>'
 981              );
 982  
 983              if (isset($user->lang['CP_' . strtoupper($field) . '_EXPLAIN']))
 984              {
 985                  $lang_options[1]['fields'][$field]['EXPLAIN'] = $user->lang['CP_' . strtoupper($field) . '_EXPLAIN'];
 986              }
 987          }
 988  
 989          foreach ($languages as $lang_id => $lang_iso)
 990          {
 991              $lang_options[$lang_id]['lang_iso'] = $lang_iso;
 992              foreach ($options as $field => $field_type)
 993              {
 994                  $value = ($action == 'create') ? utf8_normalize_nfc(request_var('l_' . $field, array(0 => ''), true)) : $cp->vars['l_' . $field];
 995                  if ($field == 'lang_options')
 996                  {
 997                      $var = (!isset($cp->vars['l_lang_options'][$lang_id]) || !is_array($cp->vars['l_lang_options'][$lang_id])) ? $cp->vars['lang_options'] : $cp->vars['l_lang_options'][$lang_id];
 998  
 999                      switch ($field_type)
1000                      {
1001                          case 'two_options':
1002  
1003                              $lang_options[$lang_id]['fields'][$field] = array(
1004                                  'TITLE'        => $user->lang['CP_' . strtoupper($field)],
1005                                  'FIELD'        => '
1006                                              <dd><input class="medium" name="l_' . $field . '[' . $lang_id . '][]" value="' . ((isset($value[$lang_id][0])) ? $value[$lang_id][0] : $var[0]) . '" /> ' . $user->lang['FIRST_OPTION'] . '</dd>
1007                                              <dd><input class="medium" name="l_' . $field . '[' . $lang_id . '][]" value="' . ((isset($value[$lang_id][1])) ? $value[$lang_id][1] : $var[1]) . '" /> ' . $user->lang['SECOND_OPTION'] . '</dd>'
1008                              );
1009                          break;
1010  
1011                          case 'optionfield':
1012                              $value = ((isset($value[$lang_id])) ? ((is_array($value[$lang_id])) ?  implode("\n", $value[$lang_id]) : $value[$lang_id]) : implode("\n", $var));
1013                              $lang_options[$lang_id]['fields'][$field] = array(
1014                                  'TITLE'        => $user->lang['CP_' . strtoupper($field)],
1015                                  'FIELD'        => '<dd><textarea name="l_' . $field . '[' . $lang_id . ']" rows="7" cols="80">' . $value . '</textarea></dd>'
1016                              );
1017                          break;
1018                      }
1019  
1020                      if (isset($user->lang['CP_' . strtoupper($field) . '_EXPLAIN']))
1021                      {
1022                          $lang_options[$lang_id]['fields'][$field]['EXPLAIN'] = $user->lang['CP_' . strtoupper($field) . '_EXPLAIN'];
1023                      }
1024                  }
1025                  else
1026                  {
1027                      $var = ($action == 'create' || !is_array($cp->vars[$field])) ? $cp->vars[$field] : $cp->vars[$field][$lang_id];
1028  
1029                      $lang_options[$lang_id]['fields'][$field] = array(
1030                          'TITLE'        => $user->lang['CP_' . strtoupper($field)],
1031                          'FIELD'        => ($field_type == 'string') ? '<dd><input class="medium" type="text" name="l_' . $field . '[' . $lang_id . ']" value="' . ((isset($value[$lang_id])) ? $value[$lang_id] : $var) . '" /></dd>' : '<dd><textarea name="l_' . $field . '[' . $lang_id . ']" rows="3" cols="80">' . ((isset($value[$lang_id])) ? $value[$lang_id] : $var) . '</textarea></dd>'
1032                      );
1033  
1034                      if (isset($user->lang['CP_' . strtoupper($field) . '_EXPLAIN']))
1035                      {
1036                          $lang_options[$lang_id]['fields'][$field]['EXPLAIN'] = $user->lang['CP_' . strtoupper($field) . '_EXPLAIN'];
1037                      }
1038                  }
1039              }
1040          }
1041  
1042          return $lang_options;
1043      }
1044  
1045      /**
1046      * Save Profile Field
1047      */
1048  	function save_profile_field(&$cp, $field_type, $action = 'create')
1049      {
1050          global $db, $config, $user;
1051  
1052          $field_id = request_var('field_id', 0);
1053  
1054          // Collect all information, if something is going wrong, abort the operation
1055          $profile_sql = $profile_lang = $empty_lang = $profile_lang_fields = array();
1056  
1057          $default_lang_id = (!empty($this->edit_lang_id)) ? $this->edit_lang_id : $this->lang_defs['iso'][$config['default_lang']];
1058  
1059          if ($action == 'create')
1060          {
1061              $sql = 'SELECT MAX(field_order) as max_field_order
1062                  FROM ' . PROFILE_FIELDS_TABLE;
1063              $result = $db->sql_query($sql);
1064              $new_field_order = (int) $db->sql_fetchfield('max_field_order');
1065              $db->sql_freeresult($result);
1066  
1067              $field_ident = $cp->vars['field_ident'];
1068          }
1069  
1070          // Save the field
1071          $profile_fields = array(
1072              'field_length'            => $cp->vars['field_length'],
1073              'field_minlen'            => $cp->vars['field_minlen'],
1074              'field_maxlen'            => $cp->vars['field_maxlen'],
1075              'field_novalue'            => $cp->vars['field_novalue'],
1076              'field_default_value'    => $cp->vars['field_default_value'],
1077              'field_validation'        => $cp->vars['field_validation'],
1078              'field_required'        => $cp->vars['field_required'],
1079              'field_show_novalue'    => $cp->vars['field_show_novalue'],
1080              'field_show_on_reg'        => $cp->vars['field_show_on_reg'],
1081              'field_show_on_vt'        => $cp->vars['field_show_on_vt'],
1082              'field_hide'            => $cp->vars['field_hide'],
1083              'field_show_profile'    => $cp->vars['field_show_profile'],
1084              'field_no_view'            => $cp->vars['field_no_view']
1085          );
1086  
1087          if ($action == 'create')
1088          {
1089              $profile_fields += array(
1090                  'field_type'        => $field_type,
1091                  'field_ident'        => $field_ident,
1092                  'field_name'        => $field_ident,
1093                  'field_order'        => $new_field_order + 1,
1094                  'field_active'        => 1
1095              );
1096  
1097              $sql = 'INSERT INTO ' . PROFILE_FIELDS_TABLE . ' ' . $db->sql_build_array('INSERT', $profile_fields);
1098              $db->sql_query($sql);
1099  
1100              $field_id = $db->sql_nextid();
1101          }
1102          else
1103          {
1104              $sql = 'UPDATE ' . PROFILE_FIELDS_TABLE . '
1105                  SET ' . $db->sql_build_array('UPDATE', $profile_fields) . "
1106                  WHERE field_id = $field_id";
1107              $db->sql_query($sql);
1108          }
1109  
1110          if ($action == 'create')
1111          {
1112              $field_ident = 'pf_' . $field_ident;
1113              $profile_sql[] = $this->add_field_ident($field_ident, $field_type);
1114          }
1115  
1116          $sql_ary = array(
1117              'lang_name'                => $cp->vars['lang_name'],
1118              'lang_explain'            => $cp->vars['lang_explain'],
1119              'lang_default_value'    => $cp->vars['lang_default_value']
1120          );
1121  
1122          if ($action == 'create')
1123          {
1124              $sql_ary['field_id'] = $field_id;
1125              $sql_ary['lang_id'] = $default_lang_id;
1126  
1127              $profile_sql[] = 'INSERT INTO ' . PROFILE_LANG_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
1128          }
1129          else
1130          {
1131              $this->update_insert(PROFILE_LANG_TABLE, $sql_ary, array('field_id' => $field_id, 'lang_id' => $default_lang_id));
1132          }
1133  
1134          if (is_array($cp->vars['l_lang_name']) && sizeof($cp->vars['l_lang_name']))
1135          {
1136              foreach ($cp->vars['l_lang_name'] as $lang_id => $data)
1137              {
1138                  if (($cp->vars['lang_name'] != '' && $cp->vars['l_lang_name'][$lang_id] == '')
1139                      || ($cp->vars['lang_explain'] != '' && $cp->vars['l_lang_explain'][$lang_id] == '')
1140                      || ($cp->vars['lang_default_value'] != '' && $cp->vars['l_lang_default_value'][$lang_id] == ''))
1141                  {
1142                      $empty_lang[$lang_id] = true;
1143                      break;
1144                  }
1145  
1146                  if (!isset($empty_lang[$lang_id]))
1147                  {
1148                      $profile_lang[] = array(
1149                          'field_id'        => $field_id,
1150                          'lang_id'        => $lang_id,
1151                          'lang_name'        => $cp->vars['l_lang_name'][$lang_id],
1152                          'lang_explain'    => (isset($cp->vars['l_lang_explain'][$lang_id])) ? $cp->vars['l_lang_explain'][$lang_id] : '',
1153                          'lang_default_value'    => (isset($cp->vars['l_lang_default_value'][$lang_id])) ? $cp->vars['l_lang_default_value'][$lang_id] : ''
1154                      );
1155                  }
1156              }
1157  
1158              foreach ($empty_lang as $lang_id => $NULL)
1159              {
1160                  $sql = 'DELETE FROM ' . PROFILE_LANG_TABLE . "
1161                      WHERE field_id = $field_id
1162                      AND lang_id = " . (int) $lang_id;
1163                  $db->sql_query($sql);
1164              }
1165          }
1166  
1167          // These are always arrays because the key is the language id...
1168          $cp->vars['l_lang_name']            = utf8_normalize_nfc(request_var('l_lang_name', array(0 => ''), true));
1169          $cp->vars['l_lang_explain']            = utf8_normalize_nfc(request_var('l_lang_explain', array(0 => ''), true));
1170          $cp->vars['l_lang_default_value']    = utf8_normalize_nfc(request_var('l_lang_default_value', array(0 => ''), true));
1171  
1172          if ($field_type != FIELD_BOOL)
1173          {
1174              $cp->vars['l_lang_options']            = utf8_normalize_nfc(request_var('l_lang_options', array(0 => ''), true));
1175          }
1176          else
1177          {
1178              /**
1179              * @todo check if this line is correct...
1180              $cp->vars['l_lang_default_value']    = request_var('l_lang_default_value', array(0 => array('')), true);
1181              */
1182              $cp->vars['l_lang_options']    = utf8_normalize_nfc(request_var('l_lang_options', array(0 => array('')), true));
1183          }
1184  
1185          if ($cp->vars['lang_options'])
1186          {
1187              if (!is_array($cp->vars['lang_options']))
1188              {
1189                  $cp->vars['lang_options'] = explode("\n", $cp->vars['lang_options']);
1190              }
1191  
1192              if ($action != 'create')
1193              {
1194                  $sql = 'DELETE FROM ' . PROFILE_FIELDS_LANG_TABLE . "
1195                      WHERE field_id = $field_id
1196                          AND lang_id = " . (int) $default_lang_id;
1197                  $db->sql_query($sql);
1198              }
1199  
1200              foreach ($cp->vars['lang_options'] as $option_id => $value)
1201              {
1202                  $sql_ary = array(
1203                      'field_type'    => (int) $field_type,
1204                      'lang_value'    => $value
1205                  );
1206  
1207                  if ($action == 'create')
1208                  {
1209                      $sql_ary['field_id'] = $field_id;
1210                      $sql_ary['lang_id'] = $default_lang_id;
1211                      $sql_ary['option_id'] = (int) $option_id;
1212  
1213                      $profile_sql[] = 'INSERT INTO ' . PROFILE_FIELDS_LANG_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
1214                  }
1215                  else
1216                  {
1217                      $this->update_insert(PROFILE_FIELDS_LANG_TABLE, $sql_ary, array(
1218                          'field_id'    => $field_id,
1219                          'lang_id'    => (int) $default_lang_id,
1220                          'option_id'    => (int) $option_id)
1221                      );
1222                  }
1223              }
1224          }
1225  
1226          if (is_array($cp->vars['l_lang_options']) && sizeof($cp->vars['l_lang_options']))
1227          {
1228              $empty_lang = array();
1229  
1230              foreach ($cp->vars['l_lang_options'] as $lang_id => $lang_ary)
1231              {
1232                  if (!is_array($lang_ary))
1233                  {
1234                      $lang_ary = explode("\n", $lang_ary);
1235                  }
1236  
1237                  if (sizeof($lang_ary) != sizeof($cp->vars['lang_options']))
1238                  {
1239                      $empty_lang[$lang_id] = true;
1240                  }
1241  
1242                  if (!isset($empty_lang[$lang_id]))
1243                  {
1244                      if ($action != 'create')
1245                      {
1246                          $sql = 'DELETE FROM ' . PROFILE_FIELDS_LANG_TABLE . "
1247                              WHERE field_id = $field_id
1248                              AND lang_id = " . (int) $lang_id;
1249                          $db->sql_query($sql);
1250                      }
1251  
1252                      foreach ($lang_ary as $option_id => $value)
1253                      {
1254                          $profile_lang_fields[] = array(
1255                              'field_id'        => (int) $field_id,
1256                              'lang_id'        => (int) $lang_id,
1257                              'option_id'        => (int) $option_id,
1258                              'field_type'    => (int) $field_type,
1259                              'lang_value'    => $value
1260                          );
1261                      }
1262                  }
1263              }
1264  
1265              foreach ($empty_lang as $lang_id => $NULL)
1266              {
1267                  $sql = 'DELETE FROM ' . PROFILE_FIELDS_LANG_TABLE . "
1268                      WHERE field_id = $field_id
1269                      AND lang_id = " . (int) $lang_id;
1270                  $db->sql_query($sql);
1271              }
1272          }
1273  
1274          foreach ($profile_lang as $sql)
1275          {
1276              if ($action == 'create')
1277              {
1278                  $profile_sql[] = 'INSERT INTO ' . PROFILE_LANG_TABLE . ' ' . $db->sql_build_array('INSERT', $sql);
1279              }
1280              else
1281              {
1282                  $lang_id = $sql['lang_id'];
1283                  unset($sql['lang_id'], $sql['field_id']);
1284  
1285                  $this->update_insert(PROFILE_LANG_TABLE, $sql, array('lang_id' => (int) $lang_id, 'field_id' => $field_id));
1286              }
1287          }
1288  
1289          if (sizeof($profile_lang_fields))
1290          {
1291              foreach ($profile_lang_fields as $sql)
1292              {
1293                  if ($action == 'create')
1294                  {
1295                      $profile_sql[] = 'INSERT INTO ' . PROFILE_FIELDS_LANG_TABLE . ' ' . $db->sql_build_array('INSERT', $sql);
1296                  }
1297                  else
1298                  {
1299                      $lang_id = $sql['lang_id'];
1300                      $option_id = $sql['option_id'];
1301                      unset($sql['lang_id'], $sql['field_id'], $sql['option_id']);
1302  
1303                      $this->update_insert(PROFILE_FIELDS_LANG_TABLE, $sql, array(
1304                          'lang_id'    => $lang_id,
1305                          'field_id'    => $field_id,
1306                          'option_id'    => $option_id)
1307                      );
1308                  }
1309              }
1310          }
1311  
1312  
1313          $db->sql_transaction('begin');
1314  
1315          if ($action == 'create')
1316          {
1317              foreach ($profile_sql as $sql)
1318              {
1319                  $db->sql_query($sql);
1320              }
1321          }
1322  
1323          $db->sql_transaction('commit');
1324  
1325          if ($action == 'edit')
1326          {
1327              add_log('admin', 'LOG_PROFILE_FIELD_EDIT', $cp->vars['field_ident'] . ':' . $cp->vars['lang_name']);
1328              trigger_error($user->lang['CHANGED_PROFILE_FIELD'] . adm_back_link($this->u_action));
1329          }
1330          else
1331          {
1332              add_log('admin', 'LOG_PROFILE_FIELD_CREATE', substr($field_ident, 3) . ':' . $cp->vars['lang_name']);
1333              trigger_error($user->lang['ADDED_PROFILE_FIELD'] . adm_back_link($this->u_action));
1334          }
1335      }
1336  
1337      /**
1338      * Update, then insert if not successfull
1339      */
1340  	function update_insert($table, $sql_ary, $where_fields)
1341      {
1342          global $db;
1343  
1344          $where_sql = array();
1345          $check_key = '';
1346  
1347          foreach ($where_fields as $key => $value)
1348          {
1349              $check_key = (!$check_key) ? $key : $check_key;
1350              $where_sql[] = $key . ' = ' . ((is_string($value)) ? "'" . $db->sql_escape($value) . "'" : (int) $value);
1351          }
1352  
1353          if (!sizeof($where_sql))
1354          {
1355              return;
1356          }
1357  
1358          $sql = "SELECT $check_key
1359              FROM $table
1360              WHERE " . implode(' AND ', $where_sql);
1361          $result = $db->sql_query($sql);
1362          $row = $db->sql_fetchrow($result);
1363          $db->sql_freeresult($result);
1364  
1365          if (!$row)
1366          {
1367              $sql_ary = array_merge($where_fields, $sql_ary);
1368  
1369              if (sizeof($sql_ary))
1370              {
1371                  $db->sql_query("INSERT INTO $table " . $db->sql_build_array('INSERT', $sql_ary));
1372              }
1373          }
1374          else
1375          {
1376              if (sizeof($sql_ary))
1377              {
1378                  $sql = "UPDATE $table SET " . $db->sql_build_array('UPDATE', $sql_ary) . '
1379                      WHERE ' . implode(' AND ', $where_sql);
1380                  $db->sql_query($sql);
1381              }
1382          }
1383      }
1384  
1385      /**
1386      * Return sql statement for adding a new field ident (profile field) to the profile fields data table
1387      */
1388  	function add_field_ident($field_ident, $field_type)
1389      {
1390          global $db;
1391  
1392          switch ($db->sql_layer)
1393          {
1394              case 'mysql':
1395              case 'mysql4':
1396              case 'mysqli':
1397  
1398                  // We are defining the biggest common value, because of the possibility to edit the min/max values of each field.
1399                  $sql = 'ALTER TABLE ' . PROFILE_FIELDS_DATA_TABLE . " ADD `$field_ident` ";
1400  
1401                  switch ($field_type)
1402                  {
1403                      case FIELD_STRING:
1404                          $sql .= ' VARCHAR(255) ';
1405                      break;
1406  
1407                      case FIELD_DATE:
1408                          $sql .= 'VARCHAR(10) ';
1409                      break;
1410  
1411                      case FIELD_TEXT:
1412                          $sql .= "TEXT";
1413          //                        ADD {$field_ident}_bbcode_uid VARCHAR(5) NOT NULL,
1414          //                        ADD {$field_ident}_bbcode_bitfield INT(11) UNSIGNED";
1415                      break;
1416  
1417                      case FIELD_BOOL:
1418                          $sql .= 'TINYINT(2) ';
1419                      break;
1420  
1421                      case FIELD_DROPDOWN:
1422                          $sql .= 'MEDIUMINT(8) ';
1423                      break;
1424  
1425                      case FIELD_INT:
1426                          $sql .= 'BIGINT(20) ';
1427                      break;
1428                  }
1429  
1430              break;
1431  
1432              case 'sqlite':
1433  
1434                  switch ($field_type)
1435                  {
1436                      case FIELD_STRING:
1437                          $type = ' VARCHAR(255) ';
1438                      break;
1439  
1440                      case FIELD_DATE:
1441                          $type = 'VARCHAR(10) ';
1442                      break;
1443  
1444                      case FIELD_TEXT:
1445                          $type = "TEXT(65535)";
1446          //                        ADD {$field_ident}_bbcode_uid VARCHAR(5) NOT NULL,
1447          //                        ADD {$field_ident}_bbcode_bitfield INT(11) UNSIGNED";
1448                      break;
1449  
1450                      case FIELD_BOOL:
1451                          $type = 'TINYINT(2) ';
1452                      break;
1453  
1454                      case FIELD_DROPDOWN:
1455                          $type = 'MEDIUMINT(8) ';
1456                      break;
1457  
1458                      case FIELD_INT:
1459                          $type = 'BIGINT(20) ';
1460                      break;
1461                  }
1462  
1463                  // We are defining the biggest common value, because of the possibility to edit the min/max values of each field.
1464                  if (version_compare(sqlite_libversion(), '3.0') == -1)
1465                  {
1466                      $sql = "SELECT sql
1467                          FROM sqlite_master
1468                          WHERE type = 'table'
1469                              AND name = '" . PROFILE_FIELDS_DATA_TABLE . "'
1470                          ORDER BY type DESC, name;";
1471                      $result = $db->sql_query($sql);
1472                      $row = $db->sql_fetchrow($result);
1473                      $db->sql_freeresult($result);
1474  
1475                      // Create a temp table and populate it, destroy the existing one
1476                      $db->sql_query(preg_replace('#CREATE\s+TABLE\s+"?' . PROFILE_FIELDS_DATA_TABLE . '"?#i', 'CREATE TEMPORARY TABLE ' . PROFILE_FIELDS_DATA_TABLE . '_temp', $row['sql']));
1477                      $db->sql_query('INSERT INTO ' . PROFILE_FIELDS_DATA_TABLE . '_temp SELECT * FROM ' . PROFILE_FIELDS_DATA_TABLE);
1478                      $db->sql_query('DROP TABLE ' . PROFILE_FIELDS_DATA_TABLE);
1479  
1480                      preg_match('#\((.*)\)#s', $row['sql'], $matches);
1481  
1482                      $new_table_cols = trim($matches[1]);
1483                      $old_table_cols = explode(',', $new_table_cols);
1484                      $column_list = array();
1485  
1486                      foreach ($old_table_cols as $declaration)
1487                      {
1488                          $entities = preg_split('#\s+#', trim($declaration));
1489                          if ($entities[0] == 'PRIMARY')
1490                          {
1491                              continue;
1492                          }
1493                          $column_list[] = $entities[0];
1494                      }
1495  
1496                      $columns = implode(',', $column_list);
1497  
1498                      $new_table_cols = $field_ident . ' ' . $type . ',' . $new_table_cols;
1499  
1500                      // create a new table and fill it up. destroy the temp one
1501                      $db->sql_query('CREATE TABLE ' . PROFILE_FIELDS_DATA_TABLE . ' (' . $new_table_cols . ');');
1502                      $db->sql_query('INSERT INTO ' . PROFILE_FIELDS_DATA_TABLE . ' (' . $columns . ') SELECT ' . $columns . ' FROM ' . PROFILE_FIELDS_DATA_TABLE . '_temp;');
1503                      $db->sql_query('DROP TABLE ' . PROFILE_FIELDS_DATA_TABLE . '_temp');
1504                  }
1505                  else
1506                  {
1507                      $sql = 'ALTER TABLE ' . PROFILE_FIELDS_DATA_TABLE . " ADD $field_ident [$type]";
1508                  }
1509  
1510              break;
1511  
1512              case 'mssql':
1513              case 'mssql_odbc':
1514              case 'mssqlnative':
1515  
1516                  // We are defining the biggest common value, because of the possibility to edit the min/max values of each field.
1517                  $sql = 'ALTER TABLE [' . PROFILE_FIELDS_DATA_TABLE . "] ADD [$field_ident] ";
1518  
1519                  switch ($field_type)
1520                  {
1521                      case FIELD_STRING:
1522                          $sql .= ' [VARCHAR] (255) ';
1523                      break;
1524  
1525                      case FIELD_DATE:
1526                          $sql .= '[VARCHAR] (10) ';
1527                      break;
1528  
1529                      case FIELD_TEXT:
1530                          $sql .= "[TEXT]";
1531          //                        ADD {$field_ident}_bbcode_uid [VARCHAR] (5) NOT NULL,
1532          //                        ADD {$field_ident}_bbcode_bitfield [INT] UNSIGNED";
1533                      break;
1534  
1535                      case FIELD_BOOL:
1536                      case FIELD_DROPDOWN:
1537                          $sql .= '[INT] ';
1538                      break;
1539  
1540                      case FIELD_INT:
1541                          $sql .= '[FLOAT] ';
1542                      break;
1543                  }
1544  
1545              break;
1546  
1547              case 'postgres':
1548  
1549                  // We are defining the biggest common value, because of the possibility to edit the min/max values of each field.
1550                  $sql = 'ALTER TABLE ' . PROFILE_FIELDS_DATA_TABLE . " ADD COLUMN \"$field_ident\" ";
1551  
1552                  switch ($field_type)
1553                  {
1554                      case FIELD_STRING:
1555                          $sql .= ' VARCHAR(255) ';
1556                      break;
1557  
1558                      case FIELD_DATE:
1559                          $sql .= 'VARCHAR(10) ';
1560                      break;
1561  
1562                      case FIELD_TEXT:
1563                          $sql .= "TEXT";
1564          //                        ADD {$field_ident}_bbcode_uid VARCHAR(5) NOT NULL,
1565          //                        ADD {$field_ident}_bbcode_bitfield INT4 UNSIGNED";
1566                      break;
1567  
1568                      case FIELD_BOOL:
1569                          $sql .= 'INT2 ';
1570                      break;
1571  
1572                      case FIELD_DROPDOWN:
1573                          $sql .= 'INT4 ';
1574                      break;
1575  
1576                      case FIELD_INT:
1577                          $sql .= 'INT8 ';
1578                      break;
1579                  }
1580  
1581              break;
1582  
1583              case 'firebird':
1584  
1585                  // We are defining the biggest common value, because of the possibility to edit the min/max values of each field.
1586                  $sql = 'ALTER TABLE ' . PROFILE_FIELDS_DATA_TABLE . ' ADD "' . strtoupper($field_ident) . '" ';
1587  
1588                  switch ($field_type)
1589                  {
1590                      case FIELD_STRING:
1591                          $sql .= ' VARCHAR(255) ';
1592                      break;
1593  
1594                      case FIELD_DATE:
1595                          $sql .= 'VARCHAR(10) ';
1596                      break;
1597  
1598                      case FIELD_TEXT:
1599                          $sql .= "BLOB SUB_TYPE TEXT";
1600          //                        ADD {$field_ident}_bbcode_uid VARCHAR(5) NOT NULL,
1601          //                        ADD {$field_ident}_bbcode_bitfield INTEGER UNSIGNED";
1602                      break;
1603  
1604                      case FIELD_BOOL:
1605                      case FIELD_DROPDOWN:
1606                          $sql .= 'INTEGER ';
1607                      break;
1608  
1609                      case FIELD_INT:
1610                          $sql .= 'DOUBLE PRECISION ';
1611                      break;
1612                  }
1613  
1614              break;
1615  
1616              case 'oracle':
1617  
1618                  // We are defining the biggest common value, because of the possibility to edit the min/max values of each field.
1619                  $sql = 'ALTER TABLE ' . PROFILE_FIELDS_DATA_TABLE . " ADD $field_ident ";
1620  
1621                  switch ($field_type)
1622                  {
1623                      case FIELD_STRING:
1624                          $sql .= ' VARCHAR2(255) ';
1625                      break;
1626  
1627                      case FIELD_DATE:
1628                          $sql .= 'VARCHAR2(10) ';
1629                      break;
1630  
1631                      case FIELD_TEXT:
1632                          $sql .= "CLOB";
1633          //                        ADD {$field_ident}_bbcode_uid VARCHAR2(5) NOT NULL,
1634          //                        ADD {$field_ident}_bbcode_bitfield NUMBER(11) UNSIGNED";
1635                      break;
1636  
1637                      case FIELD_BOOL:
1638                          $sql .= 'NUMBER(2) ';
1639                      break;
1640  
1641                      case FIELD_DROPDOWN:
1642                          $sql .= 'NUMBER(8) ';
1643                      break;
1644  
1645                      case FIELD_INT:
1646                          $sql .= 'NUMBER(20) ';
1647                      break;
1648                  }
1649  
1650              break;
1651          }
1652  
1653          return $sql;
1654      }
1655  }
1656  
1657  ?>


Generated: Wed Oct 2 15:03:47 2013 Cross-referenced by PHPXref 0.7.1