Verzeichnisstruktur phpBB-3.2.0


Veröffentlicht
06.01.2017

So funktioniert es


Auf das letzte Element klicken. Dies geht jeweils ein Schritt zurück

Auf das Icon klicken, dies öffnet das Verzeichnis. Nochmal klicken schließt das Verzeichnis.
Auf den Verzeichnisnamen klicken, dies zeigt nur das Verzeichnis mit Inhalt an

      (Beispiel Datei-Icons)

Auf das Icon klicken um den Quellcode anzuzeigen

acp_profile.php

Zuletzt modifiziert: 09.10.2024, 12:52 - Dateigröße: 38.81 KiB


 0001  <?php
 0002  /**
 0003  *
 0004  * This file is part of the phpBB Forum Software package.
 0005  *
 0006  * @copyright (c) phpBB Limited <https://www.phpbb.com>
 0007  * @license GNU General Public License, version 2 (GPL-2.0)
 0008  *
 0009  * For full copyright and license information, please see
 0010  * the docs/CREDITS.txt file.
 0011  *
 0012  */
 0013   
 0014  /**
 0015  * @ignore
 0016  */
 0017  if (!defined('IN_PHPBB'))
 0018  {
 0019      exit;
 0020  }
 0021   
 0022  class acp_profile
 0023  {
 0024      var $u_action;
 0025   
 0026      var $edit_lang_id;
 0027      var $lang_defs;
 0028   
 0029      /**
 0030       * @var \phpbb\di\service_collection
 0031       */
 0032      protected $type_collection;
 0033   
 0034      function main($id, $mode)
 0035      {
 0036          global $config, $db, $user, $template;
 0037          global $phpbb_root_path, $phpEx;
 0038          global $request, $phpbb_container, $phpbb_log, $phpbb_dispatcher;
 0039   
 0040          if (!function_exists('generate_smilies'))
 0041          {
 0042              include($phpbb_root_path . 'includes/functions_posting.' . $phpEx);
 0043          }
 0044   
 0045          if (!function_exists('user_get_id_name'))
 0046          {
 0047              include($phpbb_root_path . 'includes/functions_user.' . $phpEx);
 0048          }
 0049   
 0050          $user->add_lang(array('ucp', 'acp/profile'));
 0051          $this->tpl_name = 'acp_profile';
 0052          $this->page_title = 'ACP_CUSTOM_PROFILE_FIELDS';
 0053   
 0054          $field_id = $request->variable('field_id', 0);
 0055          $action = (isset($_POST['create'])) ? 'create' : $request->variable('action', '');
 0056   
 0057          $error = array();
 0058   
 0059          $form_key = 'acp_profile';
 0060          add_form_key($form_key);
 0061   
 0062          if (!$field_id && in_array($action, array('delete','activate', 'deactivate', 'move_up', 'move_down', 'edit')))
 0063          {
 0064              trigger_error($user->lang['NO_FIELD_ID'] . adm_back_link($this->u_action), E_USER_WARNING);
 0065          }
 0066   
 0067          /* @var $cp \phpbb\profilefields\manager */
 0068          $cp = $phpbb_container->get('profilefields.manager');
 0069          $this->type_collection = $phpbb_container->get('profilefields.type_collection');
 0070   
 0071          // Build Language array
 0072          // Based on this, we decide which elements need to be edited later and which language items are missing
 0073          $this->lang_defs = array();
 0074   
 0075          $sql = 'SELECT lang_id, lang_iso
 0076              FROM ' . LANG_TABLE . '
 0077              ORDER BY lang_english_name';
 0078          $result = $db->sql_query($sql);
 0079   
 0080          while ($row = $db->sql_fetchrow($result))
 0081          {
 0082              // Make some arrays with all available languages
 0083              $this->lang_defs['id'][$row['lang_id']] = $row['lang_iso'];
 0084              $this->lang_defs['iso'][$row['lang_iso']] = $row['lang_id'];
 0085          }
 0086          $db->sql_freeresult($result);
 0087   
 0088          $sql = 'SELECT field_id, lang_id
 0089              FROM ' . PROFILE_LANG_TABLE . '
 0090              ORDER BY lang_id';
 0091          $result = $db->sql_query($sql);
 0092   
 0093          while ($row = $db->sql_fetchrow($result))
 0094          {
 0095              // Which languages are available for each item
 0096              $this->lang_defs['entry'][$row['field_id']][] = $row['lang_id'];
 0097          }
 0098          $db->sql_freeresult($result);
 0099   
 0100          // Have some fields been defined?
 0101          if (isset($this->lang_defs['entry']))
 0102          {
 0103              foreach ($this->lang_defs['entry'] as $field_ident => $field_ary)
 0104              {
 0105                  // Fill an array with the languages that are missing for each field
 0106                  $this->lang_defs['diff'][$field_ident] = array_diff(array_values($this->lang_defs['iso']), $field_ary);
 0107              }
 0108          }
 0109   
 0110          switch ($action)
 0111          {
 0112              case 'delete':
 0113   
 0114                  if (confirm_box(true))
 0115                  {
 0116                      $sql = 'SELECT field_ident
 0117                          FROM ' . PROFILE_FIELDS_TABLE . "
 0118                          WHERE field_id = $field_id";
 0119                      $result = $db->sql_query($sql);
 0120                      $field_ident = (string) $db->sql_fetchfield('field_ident');
 0121                      $db->sql_freeresult($result);
 0122   
 0123                      $db->sql_transaction('begin');
 0124   
 0125                      $db->sql_query('DELETE FROM ' . PROFILE_FIELDS_TABLE . " WHERE field_id = $field_id");
 0126                      $db->sql_query('DELETE FROM ' . PROFILE_FIELDS_LANG_TABLE . " WHERE field_id = $field_id");
 0127                      $db->sql_query('DELETE FROM ' . PROFILE_LANG_TABLE . " WHERE field_id = $field_id");
 0128   
 0129                      /* @var $db_tools \phpbb\db\tools\tools_interface */
 0130                      $db_tools = $phpbb_container->get('dbal.tools');
 0131                      $db_tools->sql_column_remove(PROFILE_FIELDS_DATA_TABLE, 'pf_' . $field_ident);
 0132   
 0133                      $order = 0;
 0134   
 0135                      $sql = 'SELECT *
 0136                          FROM ' . PROFILE_FIELDS_TABLE . '
 0137                          ORDER BY field_order';
 0138                      $result = $db->sql_query($sql);
 0139   
 0140                      while ($row = $db->sql_fetchrow($result))
 0141                      {
 0142                          $order++;
 0143                          if ($row['field_order'] != $order)
 0144                          {
 0145                              $sql = 'UPDATE ' . PROFILE_FIELDS_TABLE . "
 0146                                  SET field_order = $order
 0147                                  WHERE field_id = {$row['field_id']}";
 0148                              $db->sql_query($sql);
 0149                          }
 0150                      }
 0151                      $db->sql_freeresult($result);
 0152   
 0153                      $db->sql_transaction('commit');
 0154   
 0155                      $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_PROFILE_FIELD_REMOVED', false, array($field_ident));
 0156                      trigger_error($user->lang['REMOVED_PROFILE_FIELD'] . adm_back_link($this->u_action));
 0157                  }
 0158                  else
 0159                  {
 0160                      confirm_box(false, 'DELETE_PROFILE_FIELD', build_hidden_fields(array(
 0161                          'i'            => $id,
 0162                          'mode'        => $mode,
 0163                          'action'    => $action,
 0164                          'field_id'    => $field_id,
 0165                      )));
 0166                  }
 0167   
 0168              break;
 0169   
 0170              case 'activate':
 0171   
 0172                  if (!check_link_hash($request->variable('hash', ''), 'acp_profile'))
 0173                  {
 0174                      trigger_error($user->lang['FORM_INVALID'] . adm_back_link($this->u_action), E_USER_WARNING);
 0175                  }
 0176   
 0177                  $sql = 'SELECT lang_id
 0178                      FROM ' . LANG_TABLE . "
 0179                      WHERE lang_iso = '" . $db->sql_escape($config['default_lang']) . "'";
 0180                  $result = $db->sql_query($sql);
 0181                  $default_lang_id = (int) $db->sql_fetchfield('lang_id');
 0182                  $db->sql_freeresult($result);
 0183   
 0184                  if (!in_array($default_lang_id, $this->lang_defs['entry'][$field_id]))
 0185                  {
 0186                      trigger_error($user->lang['DEFAULT_LANGUAGE_NOT_FILLED'] . adm_back_link($this->u_action), E_USER_WARNING);
 0187                  }
 0188   
 0189                  $sql = 'UPDATE ' . PROFILE_FIELDS_TABLE . "
 0190                      SET field_active = 1
 0191                      WHERE field_id = $field_id";
 0192                  $db->sql_query($sql);
 0193   
 0194                  $sql = 'SELECT field_ident
 0195                      FROM ' . PROFILE_FIELDS_TABLE . "
 0196                      WHERE field_id = $field_id";
 0197                  $result = $db->sql_query($sql);
 0198                  $field_ident = (string) $db->sql_fetchfield('field_ident');
 0199                  $db->sql_freeresult($result);
 0200   
 0201                  $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_PROFILE_FIELD_ACTIVATE', false, array($field_ident));
 0202   
 0203                  if ($request->is_ajax())
 0204                  {
 0205                      $json_response = new \phpbb\json_response();
 0206                      $json_response->send(array(
 0207                          'text'    => $user->lang('DEACTIVATE'),
 0208                      ));
 0209                  }
 0210   
 0211                  trigger_error($user->lang['PROFILE_FIELD_ACTIVATED'] . adm_back_link($this->u_action));
 0212   
 0213              break;
 0214   
 0215              case 'deactivate':
 0216   
 0217                  if (!check_link_hash($request->variable('hash', ''), 'acp_profile'))
 0218                  {
 0219                      trigger_error($user->lang['FORM_INVALID'] . adm_back_link($this->u_action), E_USER_WARNING);
 0220                  }
 0221   
 0222                  $sql = 'UPDATE ' . PROFILE_FIELDS_TABLE . "
 0223                      SET field_active = 0
 0224                      WHERE field_id = $field_id";
 0225                  $db->sql_query($sql);
 0226   
 0227                  $sql = 'SELECT field_ident
 0228                      FROM ' . PROFILE_FIELDS_TABLE . "
 0229                      WHERE field_id = $field_id";
 0230                  $result = $db->sql_query($sql);
 0231                  $field_ident = (string) $db->sql_fetchfield('field_ident');
 0232                  $db->sql_freeresult($result);
 0233   
 0234                  if ($request->is_ajax())
 0235                  {
 0236                      $json_response = new \phpbb\json_response();
 0237                      $json_response->send(array(
 0238                          'text'    => $user->lang('ACTIVATE'),
 0239                      ));
 0240                  }
 0241   
 0242                  $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_PROFILE_FIELD_DEACTIVATE', false, array($field_ident));
 0243   
 0244                  trigger_error($user->lang['PROFILE_FIELD_DEACTIVATED'] . adm_back_link($this->u_action));
 0245   
 0246              break;
 0247   
 0248              case 'move_up':
 0249              case 'move_down':
 0250   
 0251                  if (!check_link_hash($request->variable('hash', ''), 'acp_profile'))
 0252                  {
 0253                      trigger_error($user->lang['FORM_INVALID'] . adm_back_link($this->u_action), E_USER_WARNING);
 0254                  }
 0255   
 0256                  $sql = 'SELECT field_order
 0257                      FROM ' . PROFILE_FIELDS_TABLE . "
 0258                      WHERE field_id = $field_id";
 0259                  $result = $db->sql_query($sql);
 0260                  $field_order = $db->sql_fetchfield('field_order');
 0261                  $db->sql_freeresult($result);
 0262   
 0263                  if ($field_order === false || ($field_order == 0 && $action == 'move_up'))
 0264                  {
 0265                      break;
 0266                  }
 0267                  $field_order = (int) $field_order;
 0268                  $order_total = $field_order * 2 + (($action == 'move_up') ? -1 : 1);
 0269   
 0270                  $sql = 'UPDATE ' . PROFILE_FIELDS_TABLE . "
 0271                      SET field_order = $order_total - field_order
 0272                      WHERE field_order IN ($field_order, " . (($action == 'move_up') ? $field_order - 1 : $field_order + 1) . ')';
 0273                  $db->sql_query($sql);
 0274   
 0275                  if ($request->is_ajax())
 0276                  {
 0277                      $json_response = new \phpbb\json_response;
 0278                      $json_response->send(array(
 0279                          'success'    => (bool) $db->sql_affectedrows(),
 0280                      ));
 0281                  }
 0282   
 0283              break;
 0284   
 0285              case 'create':
 0286              case 'edit':
 0287   
 0288                  $step = $request->variable('step', 1);
 0289   
 0290                  $submit = (isset($_REQUEST['next']) || isset($_REQUEST['prev'])) ? true : false;
 0291                  $save = (isset($_REQUEST['save'])) ? true : false;
 0292   
 0293                  // The language id of default language
 0294                  $this->edit_lang_id = $this->lang_defs['iso'][$config['default_lang']];
 0295   
 0296                  // We are editing... we need to grab basic things
 0297                  if ($action == 'edit')
 0298                  {
 0299                      $sql = 'SELECT l.*, f.*
 0300                          FROM ' . PROFILE_LANG_TABLE . ' l, ' . PROFILE_FIELDS_TABLE . ' f
 0301                          WHERE l.lang_id = ' . $this->edit_lang_id . "
 0302                              AND f.field_id = $field_id
 0303                              AND l.field_id = f.field_id";
 0304                      $result = $db->sql_query($sql);
 0305                      $field_row = $db->sql_fetchrow($result);
 0306                      $db->sql_freeresult($result);
 0307   
 0308                      if (!$field_row)
 0309                      {
 0310                          // Some admin changed the default language?
 0311                          $sql = 'SELECT l.*, f.*
 0312                              FROM ' . PROFILE_LANG_TABLE . ' l, ' . PROFILE_FIELDS_TABLE . ' f
 0313                              WHERE l.lang_id <> ' . $this->edit_lang_id . "
 0314                              AND f.field_id = $field_id
 0315                              AND l.field_id = f.field_id";
 0316                          $result = $db->sql_query($sql);
 0317                          $field_row = $db->sql_fetchrow($result);
 0318                          $db->sql_freeresult($result);
 0319   
 0320                          if (!$field_row)
 0321                          {
 0322                              trigger_error($user->lang['FIELD_NOT_FOUND'] . adm_back_link($this->u_action), E_USER_WARNING);
 0323                          }
 0324   
 0325                          $this->edit_lang_id = $field_row['lang_id'];
 0326                      }
 0327                      $field_type = $field_row['field_type'];
 0328                      $profile_field = $this->type_collection[$field_type];
 0329   
 0330                      // Get language entries
 0331                      $sql = 'SELECT *
 0332                          FROM ' . PROFILE_FIELDS_LANG_TABLE . '
 0333                          WHERE lang_id = ' . $this->edit_lang_id . "
 0334                              AND field_id = $field_id
 0335                          ORDER BY option_id ASC";
 0336                      $result = $db->sql_query($sql);
 0337   
 0338                      $lang_options = array();
 0339                      while ($row = $db->sql_fetchrow($result))
 0340                      {
 0341                          $lang_options[$row['option_id']] = $row['lang_value'];
 0342                      }
 0343                      $db->sql_freeresult($result);
 0344   
 0345                      $s_hidden_fields = '<input type="hidden" name="field_id" value="' . $field_id . '" />';
 0346                  }
 0347                  else
 0348                  {
 0349                      // We are adding a new field, define basic params
 0350                      $lang_options = $field_row = array();
 0351   
 0352                      $field_type = $request->variable('field_type', '');
 0353   
 0354                      if (!isset($this->type_collection[$field_type]))
 0355                      {
 0356                          trigger_error($user->lang['NO_FIELD_TYPE'] . adm_back_link($this->u_action), E_USER_WARNING);
 0357                      }
 0358   
 0359                      $profile_field = $this->type_collection[$field_type];
 0360                      $field_row = array_merge($profile_field->get_default_option_values(), array(
 0361                          'field_ident'        => str_replace(' ', '_', utf8_clean_string($request->variable('field_ident', '', true))),
 0362                          'field_required'    => 0,
 0363                          'field_show_novalue'=> 0,
 0364                          'field_hide'        => 0,
 0365                          'field_show_profile'=> 0,
 0366                          'field_no_view'        => 0,
 0367                          'field_show_on_reg'    => 0,
 0368                          'field_show_on_pm'    => 0,
 0369                          'field_show_on_vt'    => 0,
 0370                          'field_show_on_ml'    => 0,
 0371                          'field_is_contact'    => 0,
 0372                          'field_contact_desc'=> '',
 0373                          'field_contact_url'    => '',
 0374                          'lang_name'            => $request->variable('field_ident', '', true),
 0375                          'lang_explain'        => '',
 0376                          'lang_default_value'=> '')
 0377                      );
 0378   
 0379                      $s_hidden_fields = '<input type="hidden" name="field_type" value="' . $field_type . '" />';
 0380                  }
 0381   
 0382                  // $exclude contains the data we gather in each step
 0383                  $exclude = array(
 0384                      1    => array('field_ident', 'lang_name', 'lang_explain', 'field_option_none', 'field_show_on_reg', 'field_show_on_pm', 'field_show_on_vt', 'field_show_on_ml', 'field_required', 'field_show_novalue', 'field_hide', 'field_show_profile', 'field_no_view', 'field_is_contact', 'field_contact_desc', 'field_contact_url'),
 0385                      2    => array('field_length', 'field_maxlen', 'field_minlen', 'field_validation', 'field_novalue', 'field_default_value'),
 0386                      3    => array('l_lang_name', 'l_lang_explain', 'l_lang_default_value', 'l_lang_options')
 0387                  );
 0388   
 0389                  // Visibility Options...
 0390                  $visibility_ary = array(
 0391                      'field_required',
 0392                      'field_show_novalue',
 0393                      'field_show_on_reg',
 0394                      'field_show_on_pm',
 0395                      'field_show_on_vt',
 0396                      'field_show_on_ml',
 0397                      'field_show_profile',
 0398                      'field_hide',
 0399                      'field_is_contact',
 0400                  );
 0401   
 0402                  /**
 0403                  * Event to add initialization for new profile field table fields
 0404                  *
 0405                  * @event core.acp_profile_create_edit_init
 0406                  * @var    string    action            create|edit
 0407                  * @var    int        step            Configuration step (1|2|3)
 0408                  * @var    bool    submit            Form has been submitted
 0409                  * @var    bool    save            Configuration should be saved
 0410                  * @var    string    field_type        Type of the field we are dealing with
 0411                  * @var    array    field_row        Array of data about the field
 0412                  * @var    array    exclude            Array of excluded fields by step
 0413                  * @var    array    visibility_ary    Array of fields that are visibility related
 0414                  * @since 3.1.6-RC1
 0415                  */
 0416                  $vars = array(
 0417                      'action',
 0418                      'step',
 0419                      'submit',
 0420                      'save',
 0421                      'field_type',
 0422                      'field_row',
 0423                      'exclude',
 0424                      'visibility_ary',
 0425                  );
 0426                  extract($phpbb_dispatcher->trigger_event('core.acp_profile_create_edit_init', compact($vars)));
 0427   
 0428                  $options = $profile_field->prepare_options_form($exclude, $visibility_ary);
 0429   
 0430                  $cp->vars['field_ident']        = ($action == 'create' && $step == 1) ? utf8_clean_string($request->variable('field_ident', $field_row['field_ident'], true)) : $request->variable('field_ident', $field_row['field_ident']);
 0431                  $cp->vars['lang_name']            = $request->variable('lang_name', $field_row['lang_name'], true);
 0432                  $cp->vars['lang_explain']        = $request->variable('lang_explain', $field_row['lang_explain'], true);
 0433                  $cp->vars['lang_default_value']    = $request->variable('lang_default_value', $field_row['lang_default_value'], true);
 0434                  $cp->vars['field_contact_desc']    = $request->variable('field_contact_desc', $field_row['field_contact_desc'], true);
 0435                  $cp->vars['field_contact_url']    = $request->variable('field_contact_url', $field_row['field_contact_url'], true);
 0436   
 0437                  foreach ($visibility_ary as $val)
 0438                  {
 0439                      $cp->vars[$val] = ($submit || $save) ? $request->variable($val, 0) : $field_row[$val];
 0440                  }
 0441   
 0442                  $cp->vars['field_no_view'] = $request->variable('field_no_view', (int) $field_row['field_no_view']);
 0443   
 0444                  // If the user has submitted a form with options (i.e. dropdown field)
 0445                  if ($options)
 0446                  {
 0447                      $exploded_options = (is_array($options)) ? $options : explode("\n", $options);
 0448   
 0449                      if (sizeof($exploded_options) == sizeof($lang_options) || $action == 'create')
 0450                      {
 0451                          // The number of options in the field is equal to the number of options already in the database
 0452                          // Or we are creating a new dropdown list.
 0453                          $cp->vars['lang_options'] = $exploded_options;
 0454                      }
 0455                      else if ($action == 'edit')
 0456                      {
 0457                          // Changing the number of options? (We remove and re-create the option fields)
 0458                          $cp->vars['lang_options'] = $exploded_options;
 0459                      }
 0460                  }
 0461                  else
 0462                  {
 0463                      $cp->vars['lang_options'] = $lang_options;
 0464                  }
 0465   
 0466                  // step 2
 0467                  foreach ($exclude[2] as $key)
 0468                  {
 0469                      $var = $request->variable($key, $field_row[$key], true);
 0470   
 0471                      $field_data = $cp->vars;
 0472                      $var = $profile_field->get_excluded_options($key, $action, $var, $field_data, 2);
 0473                      $cp->vars = $field_data;
 0474   
 0475                      $cp->vars[$key] = $var;
 0476                  }
 0477   
 0478                  // step 3 - all arrays
 0479                  if ($action == 'edit')
 0480                  {
 0481                      // Get language entries
 0482                      $sql = 'SELECT *
 0483                          FROM ' . PROFILE_FIELDS_LANG_TABLE . '
 0484                          WHERE lang_id <> ' . $this->edit_lang_id . "
 0485                              AND field_id = $field_id
 0486                          ORDER BY option_id ASC";
 0487                      $result = $db->sql_query($sql);
 0488   
 0489                      $l_lang_options = array();
 0490                      while ($row = $db->sql_fetchrow($result))
 0491                      {
 0492                          $l_lang_options[$row['lang_id']][$row['option_id']] = $row['lang_value'];
 0493                      }
 0494                      $db->sql_freeresult($result);
 0495   
 0496                      $sql = 'SELECT lang_id, lang_name, lang_explain, lang_default_value
 0497                          FROM ' . PROFILE_LANG_TABLE . '
 0498                          WHERE lang_id <> ' . $this->edit_lang_id . "
 0499                              AND field_id = $field_id
 0500                          ORDER BY lang_id ASC";
 0501                      $result = $db->sql_query($sql);
 0502   
 0503                      $l_lang_name = $l_lang_explain = $l_lang_default_value = array();
 0504                      while ($row = $db->sql_fetchrow($result))
 0505                      {
 0506                          $l_lang_name[$row['lang_id']] = $row['lang_name'];
 0507                          $l_lang_explain[$row['lang_id']] = $row['lang_explain'];
 0508                          $l_lang_default_value[$row['lang_id']] = $row['lang_default_value'];
 0509                      }
 0510                      $db->sql_freeresult($result);
 0511                  }
 0512   
 0513                  foreach ($exclude[3] as $key)
 0514                  {
 0515                      $cp->vars[$key] = $request->variable($key, array(0 => ''), true);
 0516   
 0517                      if (!$cp->vars[$key] && $action == 'edit')
 0518                      {
 0519                          $cp->vars[$key] = ${$key};
 0520                      }
 0521   
 0522                      $field_data = $cp->vars;
 0523                      $var = $profile_field->get_excluded_options($key, $action, $var, $field_data, 3);
 0524                      $cp->vars = $field_data;
 0525                  }
 0526   
 0527                  // Check for general issues in every step
 0528                  if ($submit) //  && $step == 1
 0529                  {
 0530                      // Check values for step 1
 0531                      if ($cp->vars['field_ident'] == '')
 0532                      {
 0533                          $error[] = $user->lang['EMPTY_FIELD_IDENT'];
 0534                      }
 0535   
 0536                      if (!preg_match('/^[a-z_]+$/', $cp->vars['field_ident']))
 0537                      {
 0538                          $error[] = $user->lang['INVALID_CHARS_FIELD_IDENT'];
 0539                      }
 0540   
 0541                      if (strlen($cp->vars['field_ident']) > 17)
 0542                      {
 0543                          $error[] = $user->lang['INVALID_FIELD_IDENT_LEN'];
 0544                      }
 0545   
 0546                      if ($cp->vars['lang_name'] == '')
 0547                      {
 0548                          $error[] = $user->lang['EMPTY_USER_FIELD_NAME'];
 0549                      }
 0550   
 0551                      $error = $profile_field->validate_options_on_submit($error, $cp->vars);
 0552   
 0553                      // Check for already existing field ident
 0554                      if ($action != 'edit')
 0555                      {
 0556                          $sql = 'SELECT field_ident
 0557                              FROM ' . PROFILE_FIELDS_TABLE . "
 0558                              WHERE field_ident = '" . $db->sql_escape($cp->vars['field_ident']) . "'";
 0559                          $result = $db->sql_query($sql);
 0560                          $row = $db->sql_fetchrow($result);
 0561                          $db->sql_freeresult($result);
 0562   
 0563                          if ($row)
 0564                          {
 0565                              $error[] = $user->lang['FIELD_IDENT_ALREADY_EXIST'];
 0566                          }
 0567                      }
 0568                  }
 0569   
 0570                  if (sizeof($error))
 0571                  {
 0572                      $submit = false;
 0573                  }
 0574                  else
 0575                  {
 0576                      $step = (isset($_REQUEST['next'])) ? $step + 1 : ((isset($_REQUEST['prev'])) ? $step - 1 : $step);
 0577                  }
 0578   
 0579                  // Build up the specific hidden fields
 0580                  foreach ($exclude as $num => $key_ary)
 0581                  {
 0582                      if ($num == $step)
 0583                      {
 0584                          continue;
 0585                      }
 0586   
 0587                      $_new_key_ary = array();
 0588   
 0589                      $field_data = $cp->vars;
 0590                      foreach ($key_ary as $key)
 0591                      {
 0592                          $var = $profile_field->prepare_hidden_fields($step, $key, $action, $field_data);
 0593                          if ($var !== null)
 0594                          {
 0595                              $_new_key_ary[$key] = $var;
 0596                          }
 0597                      }
 0598                      $cp->vars = $field_data;
 0599   
 0600                      $s_hidden_fields .= build_hidden_fields($_new_key_ary);
 0601                  }
 0602   
 0603                  if (!sizeof($error))
 0604                  {
 0605                      if (($step == 3 && (sizeof($this->lang_defs['iso']) == 1 || $save)) || ($action == 'edit' && $save))
 0606                      {
 0607                          if (!check_form_key($form_key))
 0608                          {
 0609                              trigger_error($user->lang['FORM_INVALID'] . adm_back_link($this->u_action), E_USER_WARNING);
 0610                          }
 0611   
 0612                          $this->save_profile_field($cp, $field_type, $action);
 0613                      }
 0614                  }
 0615   
 0616                  $template->assign_vars(array(
 0617                      'S_EDIT'            => true,
 0618                      'S_EDIT_MODE'        => ($action == 'edit') ? true : false,
 0619                      'ERROR_MSG'            => (sizeof($error)) ? implode('<br />', $error) : '',
 0620   
 0621                      'L_TITLE'            => $user->lang['STEP_' . $step . '_TITLE_' . strtoupper($action)],
 0622                      'L_EXPLAIN'            => $user->lang['STEP_' . $step . '_EXPLAIN_' . strtoupper($action)],
 0623   
 0624                      'U_ACTION'            => $this->u_action . "&amp;action=$action&amp;step=$step",
 0625                      'U_BACK'            => $this->u_action)
 0626                  );
 0627   
 0628                  // Now go through the steps
 0629                  switch ($step)
 0630                  {
 0631                      // Create basic options - only small differences between field types
 0632                      case 1:
 0633                          $template_vars = array(
 0634                              'S_STEP_ONE'        => true,
 0635                              'S_FIELD_REQUIRED'    => ($cp->vars['field_required']) ? true : false,
 0636                              'S_FIELD_SHOW_NOVALUE'=> ($cp->vars['field_show_novalue']) ? true : false,
 0637                              'S_SHOW_ON_REG'        => ($cp->vars['field_show_on_reg']) ? true : false,
 0638                              'S_SHOW_ON_PM'        => ($cp->vars['field_show_on_pm']) ? true : false,
 0639                              'S_SHOW_ON_VT'        => ($cp->vars['field_show_on_vt']) ? true : false,
 0640                              'S_SHOW_ON_MEMBERLIST'=> ($cp->vars['field_show_on_ml']) ? true : false,
 0641                              'S_FIELD_HIDE'        => ($cp->vars['field_hide']) ? true : false,
 0642                              'S_SHOW_PROFILE'    => ($cp->vars['field_show_profile']) ? true : false,
 0643                              'S_FIELD_NO_VIEW'    => ($cp->vars['field_no_view']) ? true : false,
 0644                              'S_FIELD_CONTACT'    => $cp->vars['field_is_contact'],
 0645                              'FIELD_CONTACT_DESC'=> $cp->vars['field_contact_desc'],
 0646                              'FIELD_CONTACT_URL'    => $cp->vars['field_contact_url'],
 0647   
 0648                              'L_LANG_SPECIFIC'    => sprintf($user->lang['LANG_SPECIFIC_OPTIONS'], $config['default_lang']),
 0649                              'FIELD_TYPE'        => $profile_field->get_name(),
 0650                              'FIELD_IDENT'        => $cp->vars['field_ident'],
 0651                              'LANG_NAME'            => $cp->vars['lang_name'],
 0652                              'LANG_EXPLAIN'        => $cp->vars['lang_explain'],
 0653                          );
 0654   
 0655                          $field_data = $cp->vars;
 0656                          $profile_field->display_options($template_vars, $field_data);
 0657                          $cp->vars = $field_data;
 0658   
 0659                          // Build common create options
 0660                          $template->assign_vars($template_vars);
 0661                      break;
 0662   
 0663                      case 2:
 0664   
 0665                          $template->assign_vars(array(
 0666                              'S_STEP_TWO'        => true,
 0667                              'L_NEXT_STEP'            => (sizeof($this->lang_defs['iso']) == 1) ? $user->lang['SAVE'] : $user->lang['PROFILE_LANG_OPTIONS'])
 0668                          );
 0669   
 0670                          // Build options based on profile type
 0671                          $options = $profile_field->get_options($this->lang_defs['iso'][$config['default_lang']], $cp->vars);
 0672   
 0673                          foreach ($options as $num => $option_ary)
 0674                          {
 0675                              $template->assign_block_vars('option', $option_ary);
 0676                          }
 0677   
 0678                      break;
 0679   
 0680                      // Define remaining language variables
 0681                      case 3:
 0682   
 0683                          $template->assign_var('S_STEP_THREE', true);
 0684                          $options = $this->build_language_options($cp, $field_type, $action);
 0685   
 0686                          foreach ($options as $lang_id => $lang_ary)
 0687                          {
 0688                              $template->assign_block_vars('options', array(
 0689                                  'LANGUAGE'        => sprintf($user->lang[(($lang_id == $this->edit_lang_id) ? 'DEFAULT_' : '') . 'ISO_LANGUAGE'], $lang_ary['lang_iso']))
 0690                              );
 0691   
 0692                              foreach ($lang_ary['fields'] as $field_ident => $field_ary)
 0693                              {
 0694                                  $template->assign_block_vars('options.field', array(
 0695                                      'L_TITLE'        => $field_ary['TITLE'],
 0696                                      'L_EXPLAIN'        => (isset($field_ary['EXPLAIN'])) ? $field_ary['EXPLAIN'] : '',
 0697                                      'FIELD'            => $field_ary['FIELD'])
 0698                                  );
 0699                              }
 0700                          }
 0701   
 0702                      break;
 0703                  }
 0704   
 0705                  $field_data = $cp->vars;
 0706                  /**
 0707                  * Event to add template variables for new profile field table fields
 0708                  *
 0709                  * @event core.acp_profile_create_edit_after
 0710                  * @var    string    action            create|edit
 0711                  * @var    int        step            Configuration step (1|2|3)
 0712                  * @var    bool    submit            Form has been submitted
 0713                  * @var    bool    save            Configuration should be saved
 0714                  * @var    string    field_type        Type of the field we are dealing with
 0715                  * @var    array    field_data        Array of data about the field
 0716                  * @var    array    s_hidden_fields    Array of hidden fields in case this needs modification
 0717                  * @var    array    options            Array of options specific to this step
 0718                  * @since 3.1.6-RC1
 0719                  */
 0720                  $vars = array(
 0721                      'action',
 0722                      'step',
 0723                      'submit',
 0724                      'save',
 0725                      'field_type',
 0726                      'field_data',
 0727                      's_hidden_fields',
 0728                      'options',
 0729                  );
 0730                  extract($phpbb_dispatcher->trigger_event('core.acp_profile_create_edit_after', compact($vars)));
 0731   
 0732                  $template->assign_vars(array(
 0733                      'S_HIDDEN_FIELDS'    => $s_hidden_fields)
 0734                  );
 0735   
 0736                  return;
 0737   
 0738              break;
 0739          }
 0740   
 0741          $sql = 'SELECT *
 0742              FROM ' . PROFILE_FIELDS_TABLE . '
 0743              ORDER BY field_order';
 0744          $result = $db->sql_query($sql);
 0745   
 0746          $s_one_need_edit = false;
 0747          while ($row = $db->sql_fetchrow($result))
 0748          {
 0749              $active_lang = (!$row['field_active']) ? 'ACTIVATE' : 'DEACTIVATE';
 0750              $active_value = (!$row['field_active']) ? 'activate' : 'deactivate';
 0751              $id = $row['field_id'];
 0752   
 0753              $s_need_edit = (sizeof($this->lang_defs['diff'][$row['field_id']])) ? true : false;
 0754   
 0755              if ($s_need_edit)
 0756              {
 0757                  $s_one_need_edit = true;
 0758              }
 0759   
 0760              if (!isset($this->type_collection[$row['field_type']]))
 0761              {
 0762                  continue;
 0763              }
 0764              $profile_field = $this->type_collection[$row['field_type']];
 0765              $template->assign_block_vars('fields', array(
 0766                  'FIELD_IDENT'        => $row['field_ident'],
 0767                  'FIELD_TYPE'        => $profile_field->get_name(),
 0768   
 0769                  'L_ACTIVATE_DEACTIVATE'        => $user->lang[$active_lang],
 0770                  'U_ACTIVATE_DEACTIVATE'        => $this->u_action . "&amp;action=$active_value&amp;field_id=$id" . '&amp;hash=' . generate_link_hash('acp_profile'),
 0771                  'U_EDIT'                    => $this->u_action . "&amp;action=edit&amp;field_id=$id",
 0772                  'U_TRANSLATE'                => $this->u_action . "&amp;action=edit&amp;field_id=$id&amp;step=3",
 0773                  'U_DELETE'                    => $this->u_action . "&amp;action=delete&amp;field_id=$id",
 0774                  'U_MOVE_UP'                    => $this->u_action . "&amp;action=move_up&amp;field_id=$id" . '&amp;hash=' . generate_link_hash('acp_profile'),
 0775                  'U_MOVE_DOWN'                => $this->u_action . "&amp;action=move_down&amp;field_id=$id" . '&amp;hash=' . generate_link_hash('acp_profile'),
 0776   
 0777                  'S_NEED_EDIT'                => $s_need_edit)
 0778              );
 0779          }
 0780          $db->sql_freeresult($result);
 0781   
 0782          // At least one option field needs editing?
 0783          if ($s_one_need_edit)
 0784          {
 0785              $template->assign_var('S_NEED_EDIT', true);
 0786          }
 0787   
 0788          $s_select_type = '';
 0789          foreach ($this->type_collection as $key => $profile_field)
 0790          {
 0791              $s_select_type .= '<option value="' . $key . '">' . $profile_field->get_name() . '</option>';
 0792          }
 0793   
 0794          $template->assign_vars(array(
 0795              'U_ACTION'            => $this->u_action,
 0796              'S_TYPE_OPTIONS'    => $s_select_type,
 0797          ));
 0798      }
 0799   
 0800      /**
 0801      * Build all Language specific options
 0802      */
 0803      function build_language_options(&$cp, $field_type, $action = 'create')
 0804      {
 0805          global $user, $config, $db, $request;
 0806   
 0807          $default_lang_id = (!empty($this->edit_lang_id)) ? $this->edit_lang_id : $this->lang_defs['iso'][$config['default_lang']];
 0808   
 0809          $sql = 'SELECT lang_id, lang_iso
 0810              FROM ' . LANG_TABLE . '
 0811              WHERE lang_id <> ' . (int) $default_lang_id . '
 0812              ORDER BY lang_english_name';
 0813          $result = $db->sql_query($sql);
 0814   
 0815          $languages = array();
 0816          while ($row = $db->sql_fetchrow($result))
 0817          {
 0818              $languages[$row['lang_id']] = $row['lang_iso'];
 0819          }
 0820          $db->sql_freeresult($result);
 0821   
 0822          $profile_field = $this->type_collection[$field_type];
 0823          $options = $profile_field->get_language_options($cp->vars);
 0824   
 0825          $lang_options = array();
 0826   
 0827          foreach ($options as $field => $field_type)
 0828          {
 0829              $lang_options[1]['lang_iso'] = $this->lang_defs['id'][$default_lang_id];
 0830              $lang_options[1]['fields'][$field] = array(
 0831                  'TITLE'        => $user->lang['CP_' . strtoupper($field)],
 0832                  'FIELD'        => '<dd>' . ((is_array($cp->vars[$field])) ? implode('<br />', $cp->vars[$field]) : bbcode_nl2br($cp->vars[$field])) . '</dd>'
 0833              );
 0834   
 0835              if (isset($user->lang['CP_' . strtoupper($field) . '_EXPLAIN']))
 0836              {
 0837                  $lang_options[1]['fields'][$field]['EXPLAIN'] = $user->lang['CP_' . strtoupper($field) . '_EXPLAIN'];
 0838              }
 0839          }
 0840   
 0841          foreach ($languages as $lang_id => $lang_iso)
 0842          {
 0843              $lang_options[$lang_id]['lang_iso'] = $lang_iso;
 0844              foreach ($options as $field => $field_type)
 0845              {
 0846                  $value = ($action == 'create') ? $request->variable('l_' . $field, array(0 => ''), true) : $cp->vars['l_' . $field];
 0847                  if ($field == 'lang_options')
 0848                  {
 0849                      $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];
 0850   
 0851                      switch ($field_type)
 0852                      {
 0853                          case 'two_options':
 0854   
 0855                              $lang_options[$lang_id]['fields'][$field] = array(
 0856                                  'TITLE'        => $user->lang['CP_' . strtoupper($field)],
 0857                                  'FIELD'        => '
 0858                                              <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>
 0859                                              <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>'
 0860                              );
 0861                          break;
 0862   
 0863                          case 'optionfield':
 0864                              $value = ((isset($value[$lang_id])) ? ((is_array($value[$lang_id])) ?  implode("\n", $value[$lang_id]) : $value[$lang_id]) : implode("\n", $var));
 0865                              $lang_options[$lang_id]['fields'][$field] = array(
 0866                                  'TITLE'        => $user->lang['CP_' . strtoupper($field)],
 0867                                  'FIELD'        => '<dd><textarea name="l_' . $field . '[' . $lang_id . ']" rows="7" cols="80">' . $value . '</textarea></dd>'
 0868                              );
 0869                          break;
 0870                      }
 0871   
 0872                      if (isset($user->lang['CP_' . strtoupper($field) . '_EXPLAIN']))
 0873                      {
 0874                          $lang_options[$lang_id]['fields'][$field]['EXPLAIN'] = $user->lang['CP_' . strtoupper($field) . '_EXPLAIN'];
 0875                      }
 0876                  }
 0877                  else
 0878                  {
 0879                      $var = ($action == 'create' || !is_array($cp->vars[$field])) ? $cp->vars[$field] : $cp->vars[$field][$lang_id];
 0880   
 0881                      $lang_options[$lang_id]['fields'][$field] = array(
 0882                          'TITLE'        => $user->lang['CP_' . strtoupper($field)],
 0883                          '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>'
 0884                      );
 0885   
 0886                      if (isset($user->lang['CP_' . strtoupper($field) . '_EXPLAIN']))
 0887                      {
 0888                          $lang_options[$lang_id]['fields'][$field]['EXPLAIN'] = $user->lang['CP_' . strtoupper($field) . '_EXPLAIN'];
 0889                      }
 0890                  }
 0891              }
 0892          }
 0893   
 0894          return $lang_options;
 0895      }
 0896   
 0897      /**
 0898      * Save Profile Field
 0899      */
 0900      function save_profile_field(&$cp, $field_type, $action = 'create')
 0901      {
 0902          global $db, $config, $user, $phpbb_container, $phpbb_log, $request, $phpbb_dispatcher;
 0903   
 0904          $field_id = $request->variable('field_id', 0);
 0905   
 0906          // Collect all information, if something is going wrong, abort the operation
 0907          $profile_sql = $profile_lang = $empty_lang = $profile_lang_fields = array();
 0908   
 0909          $default_lang_id = (!empty($this->edit_lang_id)) ? $this->edit_lang_id : $this->lang_defs['iso'][$config['default_lang']];
 0910   
 0911          if ($action == 'create')
 0912          {
 0913              $sql = 'SELECT MAX(field_order) as max_field_order
 0914                  FROM ' . PROFILE_FIELDS_TABLE;
 0915              $result = $db->sql_query($sql);
 0916              $new_field_order = (int) $db->sql_fetchfield('max_field_order');
 0917              $db->sql_freeresult($result);
 0918   
 0919              $field_ident = $cp->vars['field_ident'];
 0920          }
 0921   
 0922          // Save the field
 0923          $profile_fields = array(
 0924              'field_length'            => $cp->vars['field_length'],
 0925              'field_minlen'            => $cp->vars['field_minlen'],
 0926              'field_maxlen'            => $cp->vars['field_maxlen'],
 0927              'field_novalue'            => $cp->vars['field_novalue'],
 0928              'field_default_value'    => $cp->vars['field_default_value'],
 0929              'field_validation'        => $cp->vars['field_validation'],
 0930              'field_required'        => $cp->vars['field_required'],
 0931              'field_show_novalue'    => $cp->vars['field_show_novalue'],
 0932              'field_show_on_reg'        => $cp->vars['field_show_on_reg'],
 0933              'field_show_on_pm'        => $cp->vars['field_show_on_pm'],
 0934              'field_show_on_vt'        => $cp->vars['field_show_on_vt'],
 0935              'field_show_on_ml'        => $cp->vars['field_show_on_ml'],
 0936              'field_hide'            => $cp->vars['field_hide'],
 0937              'field_show_profile'    => $cp->vars['field_show_profile'],
 0938              'field_no_view'            => $cp->vars['field_no_view'],
 0939              'field_is_contact'        => $cp->vars['field_is_contact'],
 0940              'field_contact_desc'    => $cp->vars['field_contact_desc'],
 0941              'field_contact_url'        => $cp->vars['field_contact_url'],
 0942          );
 0943   
 0944          $field_data = $cp->vars;
 0945          /**
 0946          * Event to modify profile field configuration data before saving to database
 0947          *
 0948          * @event core.acp_profile_create_edit_save_before
 0949          * @var    string    action            create|edit
 0950          * @var    string    field_type        Type of the field we are dealing with
 0951          * @var    array    field_data        Array of data about the field
 0952          * @var    array    profile_fields    Array of fields to be sent to the database
 0953          * @since 3.1.6-RC1
 0954          */
 0955          $vars = array(
 0956              'action',
 0957              'field_type',
 0958              'field_data',
 0959              'profile_fields',
 0960          );
 0961          extract($phpbb_dispatcher->trigger_event('core.acp_profile_create_edit_save_before', compact($vars)));
 0962   
 0963          if ($action == 'create')
 0964          {
 0965              $profile_fields += array(
 0966                  'field_type'        => $field_type,
 0967                  'field_ident'        => $field_ident,
 0968                  'field_name'        => $field_ident,
 0969                  'field_order'        => $new_field_order + 1,
 0970                  'field_active'        => 1
 0971              );
 0972   
 0973              $sql = 'INSERT INTO ' . PROFILE_FIELDS_TABLE . ' ' . $db->sql_build_array('INSERT', $profile_fields);
 0974              $db->sql_query($sql);
 0975   
 0976              $field_id = $db->sql_nextid();
 0977          }
 0978          else
 0979          {
 0980              $sql = 'UPDATE ' . PROFILE_FIELDS_TABLE . '
 0981                  SET ' . $db->sql_build_array('UPDATE', $profile_fields) . "
 0982                  WHERE field_id = $field_id";
 0983              $db->sql_query($sql);
 0984          }
 0985   
 0986          $profile_field = $this->type_collection[$field_type];
 0987   
 0988          if ($action == 'create')
 0989          {
 0990              $field_ident = 'pf_' . $field_ident;
 0991              /* @var $db_tools \phpbb\db\tools\tools_interface */
 0992              $db_tools = $phpbb_container->get('dbal.tools');
 0993              $db_tools->sql_column_add(PROFILE_FIELDS_DATA_TABLE, $field_ident, array($profile_field->get_database_column_type(), null));
 0994          }
 0995   
 0996          $sql_ary = array(
 0997              'lang_name'                => $cp->vars['lang_name'],
 0998              'lang_explain'            => $cp->vars['lang_explain'],
 0999              'lang_default_value'    => $cp->vars['lang_default_value']
 1000          );
 1001   
 1002          if ($action == 'create')
 1003          {
 1004              $sql_ary['field_id'] = $field_id;
 1005              $sql_ary['lang_id'] = $default_lang_id;
 1006   
 1007              $profile_sql[] = 'INSERT INTO ' . PROFILE_LANG_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
 1008          }
 1009          else
 1010          {
 1011              $this->update_insert(PROFILE_LANG_TABLE, $sql_ary, array('field_id' => $field_id, 'lang_id' => $default_lang_id));
 1012          }
 1013   
 1014          if (is_array($cp->vars['l_lang_name']) && sizeof($cp->vars['l_lang_name']))
 1015          {
 1016              foreach ($cp->vars['l_lang_name'] as $lang_id => $data)
 1017              {
 1018                  if (($cp->vars['lang_name'] != '' && $cp->vars['l_lang_name'][$lang_id] == '')
 1019                      || ($cp->vars['lang_explain'] != '' && $cp->vars['l_lang_explain'][$lang_id] == '')
 1020                      || ($cp->vars['lang_default_value'] != '' && $cp->vars['l_lang_default_value'][$lang_id] == ''))
 1021                  {
 1022                      $empty_lang[$lang_id] = true;
 1023                      break;
 1024                  }
 1025   
 1026                  if (!isset($empty_lang[$lang_id]))
 1027                  {
 1028                      $profile_lang[] = array(
 1029                          'field_id'        => $field_id,
 1030                          'lang_id'        => $lang_id,
 1031                          'lang_name'        => $cp->vars['l_lang_name'][$lang_id],
 1032                          'lang_explain'    => (isset($cp->vars['l_lang_explain'][$lang_id])) ? $cp->vars['l_lang_explain'][$lang_id] : '',
 1033                          'lang_default_value'    => (isset($cp->vars['l_lang_default_value'][$lang_id])) ? $cp->vars['l_lang_default_value'][$lang_id] : ''
 1034                      );
 1035                  }
 1036              }
 1037   
 1038              foreach ($empty_lang as $lang_id => $NULL)
 1039              {
 1040                  $sql = 'DELETE FROM ' . PROFILE_LANG_TABLE . "
 1041                      WHERE field_id = $field_id
 1042                      AND lang_id = " . (int) $lang_id;
 1043                  $db->sql_query($sql);
 1044              }
 1045          }
 1046   
 1047          $cp->vars = $profile_field->get_language_options_input($cp->vars);
 1048   
 1049          if ($cp->vars['lang_options'])
 1050          {
 1051              if (!is_array($cp->vars['lang_options']))
 1052              {
 1053                  $cp->vars['lang_options'] = explode("\n", $cp->vars['lang_options']);
 1054              }
 1055   
 1056              if ($action != 'create')
 1057              {
 1058                  $sql = 'DELETE FROM ' . PROFILE_FIELDS_LANG_TABLE . "
 1059                      WHERE field_id = $field_id
 1060                          AND lang_id = " . (int) $default_lang_id;
 1061                  $db->sql_query($sql);
 1062              }
 1063   
 1064              foreach ($cp->vars['lang_options'] as $option_id => $value)
 1065              {
 1066                  $sql_ary = array(
 1067                      'field_type'    => $field_type,
 1068                      'lang_value'    => $value
 1069                  );
 1070   
 1071                  if ($action == 'create')
 1072                  {
 1073                      $sql_ary['field_id'] = $field_id;
 1074                      $sql_ary['lang_id'] = $default_lang_id;
 1075                      $sql_ary['option_id'] = (int) $option_id;
 1076   
 1077                      $profile_sql[] = 'INSERT INTO ' . PROFILE_FIELDS_LANG_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
 1078                  }
 1079                  else
 1080                  {
 1081                      $this->update_insert(PROFILE_FIELDS_LANG_TABLE, $sql_ary, array(
 1082                          'field_id'    => $field_id,
 1083                          'lang_id'    => (int) $default_lang_id,
 1084                          'option_id'    => (int) $option_id)
 1085                      );
 1086                  }
 1087              }
 1088          }
 1089   
 1090          if (is_array($cp->vars['l_lang_options']) && sizeof($cp->vars['l_lang_options']))
 1091          {
 1092              $empty_lang = array();
 1093   
 1094              foreach ($cp->vars['l_lang_options'] as $lang_id => $lang_ary)
 1095              {
 1096                  if (!is_array($lang_ary))
 1097                  {
 1098                      $lang_ary = explode("\n", $lang_ary);
 1099                  }
 1100   
 1101                  if (sizeof($lang_ary) != sizeof($cp->vars['lang_options']))
 1102                  {
 1103                      $empty_lang[$lang_id] = true;
 1104                  }
 1105   
 1106                  if (!isset($empty_lang[$lang_id]))
 1107                  {
 1108                      if ($action != 'create')
 1109                      {
 1110                          $sql = 'DELETE FROM ' . PROFILE_FIELDS_LANG_TABLE . "
 1111                              WHERE field_id = $field_id
 1112                              AND lang_id = " . (int) $lang_id;
 1113                          $db->sql_query($sql);
 1114                      }
 1115   
 1116                      foreach ($lang_ary as $option_id => $value)
 1117                      {
 1118                          $profile_lang_fields[] = array(
 1119                              'field_id'        => (int) $field_id,
 1120                              'lang_id'        => (int) $lang_id,
 1121                              'option_id'        => (int) $option_id,
 1122                              'field_type'    => $field_type,
 1123                              'lang_value'    => $value
 1124                          );
 1125                      }
 1126                  }
 1127              }
 1128   
 1129              foreach ($empty_lang as $lang_id => $NULL)
 1130              {
 1131                  $sql = 'DELETE FROM ' . PROFILE_FIELDS_LANG_TABLE . "
 1132                      WHERE field_id = $field_id
 1133                      AND lang_id = " . (int) $lang_id;
 1134                  $db->sql_query($sql);
 1135              }
 1136          }
 1137   
 1138          foreach ($profile_lang as $sql)
 1139          {
 1140              if ($action == 'create')
 1141              {
 1142                  $profile_sql[] = 'INSERT INTO ' . PROFILE_LANG_TABLE . ' ' . $db->sql_build_array('INSERT', $sql);
 1143              }
 1144              else
 1145              {
 1146                  $lang_id = $sql['lang_id'];
 1147                  unset($sql['lang_id'], $sql['field_id']);
 1148   
 1149                  $this->update_insert(PROFILE_LANG_TABLE, $sql, array('lang_id' => (int) $lang_id, 'field_id' => $field_id));
 1150              }
 1151          }
 1152   
 1153          if (sizeof($profile_lang_fields))
 1154          {
 1155              foreach ($profile_lang_fields as $sql)
 1156              {
 1157                  if ($action == 'create')
 1158                  {
 1159                      $profile_sql[] = 'INSERT INTO ' . PROFILE_FIELDS_LANG_TABLE . ' ' . $db->sql_build_array('INSERT', $sql);
 1160                  }
 1161                  else
 1162                  {
 1163                      $lang_id = $sql['lang_id'];
 1164                      $option_id = $sql['option_id'];
 1165                      unset($sql['lang_id'], $sql['field_id'], $sql['option_id']);
 1166   
 1167                      $this->update_insert(PROFILE_FIELDS_LANG_TABLE, $sql, array(
 1168                          'lang_id'    => $lang_id,
 1169                          'field_id'    => $field_id,
 1170                          'option_id'    => $option_id)
 1171                      );
 1172                  }
 1173              }
 1174          }
 1175   
 1176          $db->sql_transaction('begin');
 1177   
 1178          if ($action == 'create')
 1179          {
 1180              foreach ($profile_sql as $sql)
 1181              {
 1182                  $db->sql_query($sql);
 1183              }
 1184          }
 1185   
 1186          $db->sql_transaction('commit');
 1187   
 1188          if ($action == 'edit')
 1189          {
 1190              $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_PROFILE_FIELD_EDIT', false, array($cp->vars['field_ident'] . ':' . $cp->vars['lang_name']));
 1191              trigger_error($user->lang['CHANGED_PROFILE_FIELD'] . adm_back_link($this->u_action));
 1192          }
 1193          else
 1194          {
 1195              $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_PROFILE_FIELD_CREATE', false, array(substr($field_ident, 3) . ':' . $cp->vars['lang_name']));
 1196              trigger_error($user->lang['ADDED_PROFILE_FIELD'] . adm_back_link($this->u_action));
 1197          }
 1198      }
 1199   
 1200      /**
 1201      * Update, then insert if not successfull
 1202      */
 1203      function update_insert($table, $sql_ary, $where_fields)
 1204      {
 1205          global $db;
 1206   
 1207          $where_sql = array();
 1208          $check_key = '';
 1209   
 1210          foreach ($where_fields as $key => $value)
 1211          {
 1212              $check_key = (!$check_key) ? $key : $check_key;
 1213              $where_sql[] = $key . ' = ' . ((is_string($value)) ? "'" . $db->sql_escape($value) . "'" : (int) $value);
 1214          }
 1215   
 1216          if (!sizeof($where_sql))
 1217          {
 1218              return;
 1219          }
 1220   
 1221          $sql = "SELECT $check_key
 1222              FROM $table
 1223              WHERE " . implode(' AND ', $where_sql);
 1224          $result = $db->sql_query($sql);
 1225          $row = $db->sql_fetchrow($result);
 1226          $db->sql_freeresult($result);
 1227   
 1228          if (!$row)
 1229          {
 1230              $sql_ary = array_merge($where_fields, $sql_ary);
 1231   
 1232              if (sizeof($sql_ary))
 1233              {
 1234                  $db->sql_query("INSERT INTO $table " . $db->sql_build_array('INSERT', $sql_ary));
 1235              }
 1236          }
 1237          else
 1238          {
 1239              if (sizeof($sql_ary))
 1240              {
 1241                  $sql = "UPDATE $table SET " . $db->sql_build_array('UPDATE', $sql_ary) . '
 1242                      WHERE ' . implode(' AND ', $where_sql);
 1243                  $db->sql_query($sql);
 1244              }
 1245          }
 1246      }
 1247  }
 1248