Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 705
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 1
acp_profile
0.00% covered (danger)
0.00%
0 / 703
0.00% covered (danger)
0.00%
0 / 4
32580
0.00% covered (danger)
0.00%
0 / 1
 main
0.00% covered (danger)
0.00%
0 / 461
0.00% covered (danger)
0.00%
0 / 1
11342
 build_language_options
0.00% covered (danger)
0.00%
0 / 53
0.00% covered (danger)
0.00%
0 / 1
650
 save_profile_field
0.00% covered (danger)
0.00%
0 / 167
0.00% covered (danger)
0.00%
0 / 1
1722
 update_insert
0.00% covered (danger)
0.00%
0 / 22
0.00% covered (danger)
0.00%
0 / 1
72
1<?php
2/**
3*
4* This file is part of the phpBB Forum Software package.
5*
6* @copyright (c) phpBB Limited <https://www.phpbb.com>
7* @license GNU General Public License, version 2 (GPL-2.0)
8*
9* For full copyright and license information, please see
10* the docs/CREDITS.txt file.
11*
12*/
13
14/**
15* @ignore
16*/
17if (!defined('IN_PHPBB'))
18{
19    exit;
20}
21
22class acp_profile
23{
24    var $u_action;
25
26    var $edit_lang_id;
27    var $lang_defs;
28
29    /**
30     * @var \phpbb\di\service_collection
31     */
32    protected $type_collection;
33
34    function main($id, $mode)
35    {
36        global $config, $db, $user, $template;
37        global $phpbb_root_path, $phpEx;
38        global $request, $phpbb_container, $phpbb_log, $phpbb_dispatcher;
39
40        if (!function_exists('generate_smilies'))
41        {
42            include($phpbb_root_path . 'includes/functions_posting.' . $phpEx);
43        }
44
45        if (!function_exists('user_get_id_name'))
46        {
47            include($phpbb_root_path . 'includes/functions_user.' . $phpEx);
48        }
49
50        $user->add_lang(array('ucp', 'acp/profile'));
51        $this->tpl_name = 'acp_profile';
52        $this->page_title = 'ACP_CUSTOM_PROFILE_FIELDS';
53
54        $field_id = $request->variable('field_id', 0);
55        $action = (isset($_POST['create'])) ? 'create' : $request->variable('action', '');
56
57        $error = array();
58
59        $form_key = 'acp_profile';
60        add_form_key($form_key);
61
62        if (!$field_id && in_array($action, array('delete','activate', 'deactivate', 'move_up', 'move_down', 'edit')))
63        {
64            trigger_error($user->lang['NO_FIELD_ID'] . adm_back_link($this->u_action), E_USER_WARNING);
65        }
66
67        /* @var $cp \phpbb\profilefields\manager */
68        $cp = $phpbb_container->get('profilefields.manager');
69        $this->type_collection = $phpbb_container->get('profilefields.type_collection');
70
71        // Build Language array
72        // Based on this, we decide which elements need to be edited later and which language items are missing
73        $this->lang_defs = array();
74
75        $sql = 'SELECT lang_id, lang_iso
76            FROM ' . LANG_TABLE . '
77            ORDER BY lang_english_name';
78        $result = $db->sql_query($sql);
79
80        while ($row = $db->sql_fetchrow($result))
81        {
82            // Make some arrays with all available languages
83            $this->lang_defs['id'][$row['lang_id']] = $row['lang_iso'];
84            $this->lang_defs['iso'][$row['lang_iso']] = $row['lang_id'];
85        }
86        $db->sql_freeresult($result);
87
88        $sql = 'SELECT field_id, lang_id
89            FROM ' . PROFILE_LANG_TABLE . '
90            ORDER BY lang_id';
91        $result = $db->sql_query($sql);
92
93        while ($row = $db->sql_fetchrow($result))
94        {
95            // Which languages are available for each item
96            $this->lang_defs['entry'][$row['field_id']][] = $row['lang_id'];
97        }
98        $db->sql_freeresult($result);
99
100        // Have some fields been defined?
101        if (isset($this->lang_defs['entry']))
102        {
103            foreach ($this->lang_defs['entry'] as $field_ident => $field_ary)
104            {
105                // Fill an array with the languages that are missing for each field
106                $this->lang_defs['diff'][$field_ident] = array_diff(array_values($this->lang_defs['iso']), $field_ary);
107            }
108        }
109
110        switch ($action)
111        {
112            case 'delete':
113
114                if (confirm_box(true))
115                {
116                    $sql = 'SELECT field_ident
117                        FROM ' . PROFILE_FIELDS_TABLE . "
118                        WHERE field_id = $field_id";
119                    $result = $db->sql_query($sql);
120                    $field_ident = (string) $db->sql_fetchfield('field_ident');
121                    $db->sql_freeresult($result);
122
123                    $db->sql_transaction('begin');
124
125                    $db->sql_query('DELETE FROM ' . PROFILE_FIELDS_TABLE . " WHERE field_id = $field_id");
126                    $db->sql_query('DELETE FROM ' . PROFILE_FIELDS_LANG_TABLE . " WHERE field_id = $field_id");
127                    $db->sql_query('DELETE FROM ' . PROFILE_LANG_TABLE . " WHERE field_id = $field_id");
128
129                    /* @var $db_tools \phpbb\db\tools\tools_interface */
130                    $db_tools = $phpbb_container->get('dbal.tools');
131                    $db_tools->sql_column_remove(PROFILE_FIELDS_DATA_TABLE, 'pf_' . $field_ident);
132
133                    $order = 0;
134
135                    $sql = 'SELECT *
136                        FROM ' . PROFILE_FIELDS_TABLE . '
137                        ORDER BY field_order';
138                    $result = $db->sql_query($sql);
139
140                    while ($row = $db->sql_fetchrow($result))
141                    {
142                        $order++;
143                        if ($row['field_order'] != $order)
144                        {
145                            $sql = 'UPDATE ' . PROFILE_FIELDS_TABLE . "
146                                SET field_order = $order
147                                WHERE field_id = {$row['field_id']}";
148                            $db->sql_query($sql);
149                        }
150                    }
151                    $db->sql_freeresult($result);
152
153                    $db->sql_transaction('commit');
154
155                    $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_PROFILE_FIELD_REMOVED', false, array($field_ident));
156                    trigger_error($user->lang['REMOVED_PROFILE_FIELD'] . adm_back_link($this->u_action));
157                }
158                else
159                {
160                    confirm_box(false, 'DELETE_PROFILE_FIELD', build_hidden_fields(array(
161                        'i'            => $id,
162                        'mode'        => $mode,
163                        'action'    => $action,
164                        'field_id'    => $field_id,
165                    )));
166                }
167
168            break;
169
170            case 'activate':
171
172                if (!check_link_hash($request->variable('hash', ''), 'acp_profile'))
173                {
174                    trigger_error($user->lang['FORM_INVALID'] . adm_back_link($this->u_action), E_USER_WARNING);
175                }
176
177                $sql = 'SELECT lang_id
178                    FROM ' . LANG_TABLE . "
179                    WHERE lang_iso = '" . $db->sql_escape($config['default_lang']) . "'";
180                $result = $db->sql_query($sql);
181                $default_lang_id = (int) $db->sql_fetchfield('lang_id');
182                $db->sql_freeresult($result);
183
184                if (!in_array($default_lang_id, $this->lang_defs['entry'][$field_id]))
185                {
186                    trigger_error($user->lang['DEFAULT_LANGUAGE_NOT_FILLED'] . adm_back_link($this->u_action), E_USER_WARNING);
187                }
188
189                $sql = 'UPDATE ' . PROFILE_FIELDS_TABLE . "
190                    SET field_active = 1
191                    WHERE field_id = $field_id";
192                $db->sql_query($sql);
193
194                $sql = 'SELECT field_ident
195                    FROM ' . PROFILE_FIELDS_TABLE . "
196                    WHERE field_id = $field_id";
197                $result = $db->sql_query($sql);
198                $field_ident = (string) $db->sql_fetchfield('field_ident');
199                $db->sql_freeresult($result);
200
201                $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_PROFILE_FIELD_ACTIVATE', false, array($field_ident));
202
203                if ($request->is_ajax())
204                {
205                    $json_response = new \phpbb\json_response();
206                    $json_response->send(array(
207                        'text'    => $user->lang('DEACTIVATE'),
208                    ));
209                }
210
211                trigger_error($user->lang['PROFILE_FIELD_ACTIVATED'] . adm_back_link($this->u_action));
212
213            break;
214
215            case 'deactivate':
216
217                if (!check_link_hash($request->variable('hash', ''), 'acp_profile'))
218                {
219                    trigger_error($user->lang['FORM_INVALID'] . adm_back_link($this->u_action), E_USER_WARNING);
220                }
221
222                $sql = 'UPDATE ' . PROFILE_FIELDS_TABLE . "
223                    SET field_active = 0
224                    WHERE field_id = $field_id";
225                $db->sql_query($sql);
226
227                $sql = 'SELECT field_ident
228                    FROM ' . PROFILE_FIELDS_TABLE . "
229                    WHERE field_id = $field_id";
230                $result = $db->sql_query($sql);
231                $field_ident = (string) $db->sql_fetchfield('field_ident');
232                $db->sql_freeresult($result);
233
234                if ($request->is_ajax())
235                {
236                    $json_response = new \phpbb\json_response();
237                    $json_response->send(array(
238                        'text'    => $user->lang('ACTIVATE'),
239                    ));
240                }
241
242                $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_PROFILE_FIELD_DEACTIVATE', false, array($field_ident));
243
244                trigger_error($user->lang['PROFILE_FIELD_DEACTIVATED'] . adm_back_link($this->u_action));
245
246            break;
247
248            case 'move_up':
249            case 'move_down':
250
251                if (!check_link_hash($request->variable('hash', ''), 'acp_profile'))
252                {
253                    trigger_error($user->lang['FORM_INVALID'] . adm_back_link($this->u_action), E_USER_WARNING);
254                }
255
256                $sql = 'SELECT field_order
257                    FROM ' . PROFILE_FIELDS_TABLE . "
258                    WHERE field_id = $field_id";
259                $result = $db->sql_query($sql);
260                $field_order = $db->sql_fetchfield('field_order');
261                $db->sql_freeresult($result);
262
263                if ($field_order === false || ($field_order == 0 && $action == 'move_up'))
264                {
265                    break;
266                }
267                $field_order = (int) $field_order;
268                $order_total = $field_order * 2 + (($action == 'move_up') ? -1 : 1);
269
270                $sql = 'UPDATE ' . PROFILE_FIELDS_TABLE . "
271                    SET field_order = $order_total - field_order
272                    WHERE field_order IN ($field_order" . (($action == 'move_up') ? $field_order - 1 : $field_order + 1) . ')';
273                $db->sql_query($sql);
274
275                if ($request->is_ajax())
276                {
277                    $json_response = new \phpbb\json_response;
278                    $json_response->send(array(
279                        'success'    => (bool) $db->sql_affectedrows(),
280                    ));
281                }
282
283            break;
284
285            case 'create':
286            case 'edit':
287
288                $step = $request->variable('step', 1);
289
290                $submit = (isset($_REQUEST['next']) || isset($_REQUEST['prev'])) ? true : false;
291                $save = (isset($_REQUEST['save'])) ? true : false;
292
293                // The language id of default language
294                $this->edit_lang_id = $this->lang_defs['iso'][$config['default_lang']];
295
296                // We are editing... we need to grab basic things
297                if ($action == 'edit')
298                {
299                    $sql = 'SELECT l.*, f.*
300                        FROM ' . PROFILE_LANG_TABLE . ' l, ' . PROFILE_FIELDS_TABLE . ' f
301                        WHERE l.lang_id = ' . $this->edit_lang_id . "
302                            AND f.field_id = $field_id
303                            AND l.field_id = f.field_id";
304                    $result = $db->sql_query($sql);
305                    $field_row = $db->sql_fetchrow($result);
306                    $db->sql_freeresult($result);
307
308                    if (!$field_row)
309                    {
310                        // Some admin changed the default language?
311                        $sql = 'SELECT l.*, f.*
312                            FROM ' . PROFILE_LANG_TABLE . ' l, ' . PROFILE_FIELDS_TABLE . ' f
313                            WHERE l.lang_id <> ' . $this->edit_lang_id . "
314                            AND f.field_id = $field_id
315                            AND l.field_id = f.field_id";
316                        $result = $db->sql_query($sql);
317                        $field_row = $db->sql_fetchrow($result);
318                        $db->sql_freeresult($result);
319
320                        if (!$field_row)
321                        {
322                            trigger_error($user->lang['FIELD_NOT_FOUND'] . adm_back_link($this->u_action), E_USER_WARNING);
323                        }
324
325                        $this->edit_lang_id = $field_row['lang_id'];
326                    }
327                    $field_type = $field_row['field_type'];
328                    $profile_field = $this->type_collection[$field_type];
329
330                    // Get language entries
331                    $sql = 'SELECT *
332                        FROM ' . PROFILE_FIELDS_LANG_TABLE . '
333                        WHERE lang_id = ' . $this->edit_lang_id . "
334                            AND field_id = $field_id
335                        ORDER BY option_id ASC";
336                    $result = $db->sql_query($sql);
337
338                    $lang_options = array();
339                    while ($row = $db->sql_fetchrow($result))
340                    {
341                        $lang_options[$row['option_id']] = $row['lang_value'];
342                    }
343                    $db->sql_freeresult($result);
344
345                    $s_hidden_fields = '<input type="hidden" name="field_id" value="' . $field_id . '" />';
346                }
347                else // action = create
348                {
349                    // We are adding a new field, define basic params
350                    $lang_options = array();
351
352                    $field_type = $request->variable('field_type', '');
353
354                    if (!isset($this->type_collection[$field_type]))
355                    {
356                        trigger_error($user->lang['NO_FIELD_TYPE'] . adm_back_link($this->u_action), E_USER_WARNING);
357                    }
358
359                    $profile_field = $this->type_collection[$field_type];
360                    $field_row = array_merge($profile_field->get_default_option_values(), array(
361                        'field_ident'        => str_replace(' ', '_', utf8_clean_string($request->variable('field_ident', '', true))),
362                        'field_required'    => 0,
363                        'field_show_novalue'=> 0,
364                        'field_hide'        => 0,
365                        'field_show_profile'=> 0,
366                        'field_no_view'        => 0,
367                        'field_show_on_reg'    => 0,
368                        'field_show_on_pm'    => 0,
369                        'field_show_on_vt'    => 0,
370                        'field_show_on_ml'    => 0,
371                        'field_is_contact'    => 0,
372                        'field_contact_desc'=> '',
373                        'field_contact_url'    => '',
374                        'lang_name'            => $request->variable('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_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'),
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                // Visibility Options...
390                $visibility_ary = array(
391                    'field_required',
392                    'field_show_novalue',
393                    'field_show_on_reg',
394                    'field_show_on_pm',
395                    'field_show_on_vt',
396                    'field_show_on_ml',
397                    'field_show_profile',
398                    'field_hide',
399                    'field_is_contact',
400                );
401
402                /**
403                * Event to add initialization for new profile field table fields
404                *
405                * @event core.acp_profile_create_edit_init
406                * @var    string    action            create|edit
407                * @var    int        step            Configuration step (1|2|3)
408                * @var    bool    submit            Form has been submitted
409                * @var    bool    save            Configuration should be saved
410                * @var    string    field_type        Type of the field we are dealing with
411                * @var    array    field_row        Array of data about the field
412                * @var    array    exclude            Array of excluded fields by step
413                * @var    array    visibility_ary    Array of fields that are visibility related
414                * @since 3.1.6-RC1
415                */
416                $vars = array(
417                    'action',
418                    'step',
419                    'submit',
420                    'save',
421                    'field_type',
422                    'field_row',
423                    'exclude',
424                    'visibility_ary',
425                );
426                extract($phpbb_dispatcher->trigger_event('core.acp_profile_create_edit_init', compact($vars)));
427
428                $options = $profile_field->prepare_options_form($exclude, $visibility_ary);
429
430                $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']);
431                $cp->vars['lang_name']            = $request->variable('lang_name', $field_row['lang_name'], true);
432                $cp->vars['lang_explain']        = $request->variable('lang_explain', $field_row['lang_explain'], true);
433                $cp->vars['lang_default_value']    = $request->variable('lang_default_value', $field_row['lang_default_value'], true);
434                $cp->vars['field_contact_desc']    = $request->variable('field_contact_desc', $field_row['field_contact_desc'], true);
435                $cp->vars['field_contact_url']    = $request->variable('field_contact_url', $field_row['field_contact_url'], true);
436
437                foreach ($visibility_ary as $val)
438                {
439                    $cp->vars[$val] = ($submit || $save) ? $request->variable($val, 0) : $field_row[$val];
440                }
441
442                $cp->vars['field_no_view'] = $request->variable('field_no_view', (int) $field_row['field_no_view']);
443
444                // If the user has submitted a form with options (i.e. dropdown field)
445                if ($options)
446                {
447                    $exploded_options = (is_array($options)) ? $options : explode("\n", $options);
448
449                    if (count($exploded_options) == count($lang_options) || $action == 'create')
450                    {
451                        // The number of options in the field is equal to the number of options already in the database
452                        // Or we are creating a new dropdown list.
453                        $cp->vars['lang_options'] = $exploded_options;
454                    }
455                    else if ($action == 'edit')
456                    {
457                        // Changing the number of options? (We remove and re-create the option fields)
458                        $cp->vars['lang_options'] = $exploded_options;
459                    }
460                }
461                else
462                {
463                    $cp->vars['lang_options'] = $lang_options;
464                }
465
466                // step 2
467                foreach ($exclude[2] as $key)
468                {
469                    $var = $request->variable($key, $field_row[$key], true);
470
471                    $field_data = $cp->vars;
472                    $var = $profile_field->get_excluded_options($key, $action, $var, $field_data, 2);
473                    $cp->vars = $field_data;
474
475                    $cp->vars[$key] = $var;
476                }
477
478                // step 3 - all arrays
479                if ($action == 'edit')
480                {
481                    // Get language entries
482                    $sql = 'SELECT *
483                        FROM ' . PROFILE_FIELDS_LANG_TABLE . '
484                        WHERE lang_id <> ' . $this->edit_lang_id . "
485                            AND field_id = $field_id
486                        ORDER BY option_id ASC";
487                    $result = $db->sql_query($sql);
488
489                    $l_lang_options = [];
490                    while ($row = $db->sql_fetchrow($result))
491                    {
492                        $l_lang_options[$row['lang_id']][$row['option_id']] = $row['lang_value'];
493                    }
494                    $db->sql_freeresult($result);
495
496                    $sql = 'SELECT lang_id, lang_name, lang_explain, lang_default_value
497                        FROM ' . PROFILE_LANG_TABLE . '
498                        WHERE lang_id <> ' . $this->edit_lang_id . "
499                            AND field_id = $field_id
500                        ORDER BY lang_id ASC";
501                    $result = $db->sql_query($sql);
502
503                    $l_lang_name = $l_lang_explain = $l_lang_default_value = [];
504                    while ($row = $db->sql_fetchrow($result))
505                    {
506                        $l_lang_name[$row['lang_id']] = $row['lang_name'];
507                        $l_lang_explain[$row['lang_id']] = $row['lang_explain'];
508                        $l_lang_default_value[$row['lang_id']] = $row['lang_default_value'];
509                    }
510                    $db->sql_freeresult($result);
511                }
512
513                foreach ($exclude[3] as $key)
514                {
515                    $cp->vars[$key] = $request->variable($key, array(0 => ''), true);
516
517                    if (!$cp->vars[$key] && $action == 'edit')
518                    {
519                        $cp->vars[$key] = ${$key};
520                    }
521
522                    $field_data = $cp->vars;
523                    $var = $profile_field->get_excluded_options($key, $action, $var, $field_data, 3);
524                    $cp->vars = $field_data;
525                }
526
527                // Check for general issues in every step
528                if ($submit) //  && $step == 1
529                {
530                    // Check values for step 1
531                    if ($cp->vars['field_ident'] == '')
532                    {
533                        $error[] = $user->lang['EMPTY_FIELD_IDENT'];
534                    }
535
536                    if (!preg_match('/^[a-z_]+$/', $cp->vars['field_ident']))
537                    {
538                        $error[] = $user->lang['INVALID_CHARS_FIELD_IDENT'];
539                    }
540
541                    if (strlen($cp->vars['field_ident']) > 17)
542                    {
543                        $error[] = $user->lang['INVALID_FIELD_IDENT_LEN'];
544                    }
545
546                    if ($cp->vars['lang_name'] == '')
547                    {
548                        $error[] = $user->lang['EMPTY_USER_FIELD_NAME'];
549                    }
550
551                    $error = $profile_field->validate_options_on_submit($error, $cp->vars);
552
553                    // Check for already existing field ident
554                    if ($action != 'edit')
555                    {
556                        $sql = 'SELECT field_ident
557                            FROM ' . PROFILE_FIELDS_TABLE . "
558                            WHERE field_ident = '" . $db->sql_escape($cp->vars['field_ident']) . "'";
559                        $result = $db->sql_query($sql);
560                        $row = $db->sql_fetchrow($result);
561                        $db->sql_freeresult($result);
562
563                        if ($row)
564                        {
565                            $error[] = $user->lang['FIELD_IDENT_ALREADY_EXIST'];
566                        }
567                    }
568                }
569
570                if (count($error))
571                {
572                    $submit = false;
573                }
574                else
575                {
576                    $step = (isset($_REQUEST['next'])) ? $step + 1 : ((isset($_REQUEST['prev'])) ? $step - 1 : $step);
577                }
578
579                // Build up the specific hidden fields
580                foreach ($exclude as $num => $key_ary)
581                {
582                    if ($num == $step)
583                    {
584                        continue;
585                    }
586
587                    $_new_key_ary = array();
588
589                    $field_data = $cp->vars;
590                    foreach ($key_ary as $key)
591                    {
592                        $var = $profile_field->prepare_hidden_fields($step, $key, $action, $field_data);
593                        if ($var !== null)
594                        {
595                            $_new_key_ary[$key] = $var;
596                        }
597                    }
598                    $cp->vars = $field_data;
599
600                    $s_hidden_fields .= build_hidden_fields($_new_key_ary);
601                }
602
603                if (!count($error))
604                {
605                    if (($step == 3 && (count($this->lang_defs['iso']) == 1 || $save)) || ($action == 'edit' && $save))
606                    {
607                        if (!check_form_key($form_key))
608                        {
609                            trigger_error($user->lang['FORM_INVALID'] . adm_back_link($this->u_action), E_USER_WARNING);
610                        }
611
612                        $this->save_profile_field($cp, $field_type, $action);
613                    }
614                }
615
616                $template->assign_vars(array(
617                    'S_EDIT'            => true,
618                    'S_EDIT_MODE'        => ($action == 'edit') ? true : false,
619                    'ERROR_MSG'            => (count($error)) ? implode('<br />', $error) : '',
620
621                    'L_TITLE'            => $user->lang['STEP_' . $step . '_TITLE_' . strtoupper($action)],
622                    'L_EXPLAIN'            => $user->lang['STEP_' . $step . '_EXPLAIN_' . strtoupper($action)],
623
624                    'U_ACTION'            => $this->u_action . "&amp;action=$action&amp;step=$step",
625                    'U_BACK'            => $this->u_action)
626                );
627
628                // Now go through the steps
629                switch ($step)
630                {
631                    // Create basic options - only small differences between field types
632                    case 1:
633                        $template_vars = array(
634                            'S_STEP_ONE'        => true,
635                            'S_FIELD_REQUIRED'    => ($cp->vars['field_required']) ? true : false,
636                            'S_FIELD_SHOW_NOVALUE'=> ($cp->vars['field_show_novalue']) ? true : false,
637                            'S_SHOW_ON_REG'        => ($cp->vars['field_show_on_reg']) ? true : false,
638                            'S_SHOW_ON_PM'        => ($cp->vars['field_show_on_pm']) ? true : false,
639                            'S_SHOW_ON_VT'        => ($cp->vars['field_show_on_vt']) ? true : false,
640                            'S_SHOW_ON_MEMBERLIST'=> ($cp->vars['field_show_on_ml']) ? true : false,
641                            'S_FIELD_HIDE'        => ($cp->vars['field_hide']) ? true : false,
642                            'S_SHOW_PROFILE'    => ($cp->vars['field_show_profile']) ? true : false,
643                            'S_FIELD_NO_VIEW'    => ($cp->vars['field_no_view']) ? true : false,
644                            'S_FIELD_CONTACT'    => $cp->vars['field_is_contact'],
645                            'FIELD_CONTACT_DESC'=> $cp->vars['field_contact_desc'],
646                            'FIELD_CONTACT_URL'    => $cp->vars['field_contact_url'],
647
648                            'L_LANG_SPECIFIC'    => sprintf($user->lang['LANG_SPECIFIC_OPTIONS'], $config['default_lang']),
649                            'FIELD_TYPE'        => $profile_field->get_name(),
650                            'FIELD_IDENT'        => $cp->vars['field_ident'],
651                            'LANG_NAME'            => $cp->vars['lang_name'],
652                            'LANG_EXPLAIN'        => $cp->vars['lang_explain'],
653                        );
654
655                        $field_data = $cp->vars;
656                        $profile_field->display_options($template_vars, $field_data);
657                        $cp->vars = $field_data;
658
659                        // Build common create options
660                        $template->assign_vars($template_vars);
661                    break;
662
663                    case 2:
664
665                        $template->assign_vars(array(
666                            'S_STEP_TWO'        => true,
667                            'L_NEXT_STEP'            => (count($this->lang_defs['iso']) == 1) ? $user->lang['SAVE'] : $user->lang['PROFILE_LANG_OPTIONS'])
668                        );
669
670                        // Build options based on profile type
671                        $options = $profile_field->get_options($this->lang_defs['iso'][$config['default_lang']], $cp->vars);
672
673                        foreach ($options as $option_ary)
674                        {
675                            $template->assign_block_vars('option', $option_ary);
676                        }
677
678                    break;
679
680                    // Define remaining language variables
681                    case 3:
682
683                        $template->assign_var('S_STEP_THREE', true);
684                        $options = $this->build_language_options($cp, $field_type, $action);
685
686                        foreach ($options as $lang_id => $lang_ary)
687                        {
688                            $template->assign_block_vars('options', array(
689                                'LANGUAGE'        => sprintf($user->lang[(($lang_id == $this->edit_lang_id) ? 'DEFAULT_' : '') . 'ISO_LANGUAGE'], $lang_ary['lang_iso']))
690                            );
691
692                            foreach ($lang_ary['fields'] as $field_ident => $field_ary)
693                            {
694                                $template->assign_block_vars('options.field', array(
695                                    'L_TITLE'        => $field_ary['TITLE'],
696                                    'L_EXPLAIN'        => (isset($field_ary['EXPLAIN'])) ? $field_ary['EXPLAIN'] : '',
697                                    'FIELD'            => $field_ary['FIELD'])
698                                );
699                            }
700                        }
701
702                    break;
703                }
704
705                $field_data = $cp->vars;
706                /**
707                * Event to add template variables for new profile field table fields
708                *
709                * @event core.acp_profile_create_edit_after
710                * @var    string    action            create|edit
711                * @var    int        step            Configuration step (1|2|3)
712                * @var    bool    submit            Form has been submitted
713                * @var    bool    save            Configuration should be saved
714                * @var    string    field_type        Type of the field we are dealing with
715                * @var    array    field_data        Array of data about the field
716                * @var    array    s_hidden_fields    Array of hidden fields in case this needs modification
717                * @var    array    options            Array of options specific to this step
718                * @since 3.1.6-RC1
719                */
720                $vars = array(
721                    'action',
722                    'step',
723                    'submit',
724                    'save',
725                    'field_type',
726                    'field_data',
727                    's_hidden_fields',
728                    'options',
729                );
730                extract($phpbb_dispatcher->trigger_event('core.acp_profile_create_edit_after', compact($vars)));
731
732                $template->assign_vars(array(
733                    'S_HIDDEN_FIELDS'    => $s_hidden_fields)
734                );
735
736                return;
737
738            break;
739        }
740
741        $tpl_name = $this->tpl_name;
742        $page_title = $this->page_title;
743        $u_action = $this->u_action;
744
745        /**
746        * Event to handle actions on the ACP profile fields page
747        *
748        * @event core.acp_profile_action
749        * @var    string    action        Action that is being performed
750        * @var    string    tpl_name    Template file to load
751        * @var    string    page_title    Page title
752        * @var    string    u_action    The URL we are at, read only
753        * @since 3.2.2-RC1
754        */
755        $vars = array(
756            'action',
757            'tpl_name',
758            'page_title',
759            'u_action',
760        );
761        extract($phpbb_dispatcher->trigger_event('core.acp_profile_action', compact($vars)));
762
763        $this->tpl_name = $tpl_name;
764        $this->page_title = $page_title;
765        unset($u_action);
766
767        $sql = 'SELECT *
768            FROM ' . PROFILE_FIELDS_TABLE . '
769            ORDER BY field_order';
770        $result = $db->sql_query($sql);
771
772        $s_one_need_edit = false;
773        while ($row = $db->sql_fetchrow($result))
774        {
775            $active_lang = (!$row['field_active']) ? 'ACTIVATE' : 'DEACTIVATE';
776            $active_value = (!$row['field_active']) ? 'activate' : 'deactivate';
777            $id = $row['field_id'];
778
779            $s_need_edit = (count($this->lang_defs['diff'][$row['field_id']])) ? true : false;
780
781            if ($s_need_edit)
782            {
783                $s_one_need_edit = true;
784            }
785
786            if (!isset($this->type_collection[$row['field_type']]))
787            {
788                continue;
789            }
790            $profile_field = $this->type_collection[$row['field_type']];
791
792            $field_block = array(
793                'FIELD_IDENT'        => $row['field_ident'],
794                'FIELD_TYPE'        => $profile_field->get_name(),
795
796                'L_ACTIVATE_DEACTIVATE'        => $user->lang[$active_lang],
797                'U_ACTIVATE_DEACTIVATE'        => $this->u_action . "&amp;action=$active_value&amp;field_id=$id" . '&amp;hash=' . generate_link_hash('acp_profile'),
798                'U_EDIT'                    => $this->u_action . "&amp;action=edit&amp;field_id=$id",
799                'U_TRANSLATE'                => $this->u_action . "&amp;action=edit&amp;field_id=$id&amp;step=3",
800                'U_DELETE'                    => $this->u_action . "&amp;action=delete&amp;field_id=$id",
801                'U_MOVE_UP'                    => $this->u_action . "&amp;action=move_up&amp;field_id=$id" . '&amp;hash=' . generate_link_hash('acp_profile'),
802                'U_MOVE_DOWN'                => $this->u_action . "&amp;action=move_down&amp;field_id=$id" . '&amp;hash=' . generate_link_hash('acp_profile'),
803
804                'S_NEED_EDIT'                => $s_need_edit,
805            );
806
807            /**
808            * Event to modify profile field data before it is assigned to the template
809            *
810            * @event core.acp_profile_modify_profile_row
811            * @var    array    row                Array with data for the current profile field
812            * @var    array    field_block        Template data that is being assigned to the 'fields' block
813            * @var    object    profile_field    A profile field instance, implements \phpbb\profilefields\type\type_base
814            * @since 3.2.2-RC1
815            */
816            $vars = array(
817                'row',
818                'field_block',
819                'profile_field',
820            );
821            extract($phpbb_dispatcher->trigger_event('core.acp_profile_modify_profile_row', compact($vars)));
822
823            $template->assign_block_vars('fields', $field_block);
824        }
825        $db->sql_freeresult($result);
826
827        // At least one option field needs editing?
828        if ($s_one_need_edit)
829        {
830            $template->assign_var('S_NEED_EDIT', true);
831        }
832
833        $s_select_type = '';
834        foreach ($this->type_collection as $key => $profile_field)
835        {
836            $s_select_type .= '<option value="' . $key . '">' . $profile_field->get_name() . '</option>';
837        }
838
839        $template->assign_vars(array(
840            'U_ACTION'            => $this->u_action,
841            'S_TYPE_OPTIONS'    => $s_select_type,
842        ));
843    }
844
845    /**
846    * Build all Language specific options
847    */
848    function build_language_options($cp, $field_type, $action = 'create')
849    {
850        global $user, $config, $db, $request;
851
852        $default_lang_id = (!empty($this->edit_lang_id)) ? $this->edit_lang_id : $this->lang_defs['iso'][$config['default_lang']];
853
854        $sql = 'SELECT lang_id, lang_iso
855            FROM ' . LANG_TABLE . '
856            WHERE lang_id <> ' . (int) $default_lang_id . '
857            ORDER BY lang_english_name';
858        $result = $db->sql_query($sql);
859
860        $languages = array();
861        while ($row = $db->sql_fetchrow($result))
862        {
863            $languages[$row['lang_id']] = $row['lang_iso'];
864        }
865        $db->sql_freeresult($result);
866
867        $profile_field = $this->type_collection[$field_type];
868        $options = $profile_field->get_language_options($cp->vars);
869
870        $lang_options = array();
871
872        foreach ($options as $field => $field_type)
873        {
874            $lang_options[1]['lang_iso'] = $this->lang_defs['id'][$default_lang_id];
875            $lang_options[1]['fields'][$field] = array(
876                'TITLE'        => $user->lang['CP_' . strtoupper($field)],
877                'FIELD'        => '<dd>' . ((is_array($cp->vars[$field])) ? implode('<br />', $cp->vars[$field]) : bbcode_nl2br($cp->vars[$field])) . '</dd>'
878            );
879
880            if (isset($user->lang['CP_' . strtoupper($field) . '_EXPLAIN']))
881            {
882                $lang_options[1]['fields'][$field]['EXPLAIN'] = $user->lang['CP_' . strtoupper($field) . '_EXPLAIN'];
883            }
884        }
885
886        foreach ($languages as $lang_id => $lang_iso)
887        {
888            $lang_options[$lang_id]['lang_iso'] = $lang_iso;
889            foreach ($options as $field => $field_type)
890            {
891                $value = ($action == 'create') ? $request->variable('l_' . $field, array(0 => ''), true) : $cp->vars['l_' . $field];
892                if ($field == 'lang_options')
893                {
894                    $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];
895
896                    switch ($field_type)
897                    {
898                        case 'two_options':
899
900                            $lang_options[$lang_id]['fields'][$field] = array(
901                                'TITLE'        => $user->lang['CP_' . strtoupper($field)],
902                                'FIELD'        => '
903                                            <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>
904                                            <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>'
905                            );
906                        break;
907
908                        case 'optionfield':
909                            $value = ((isset($value[$lang_id])) ? ((is_array($value[$lang_id])) ?  implode("\n", $value[$lang_id]) : $value[$lang_id]) : implode("\n", $var));
910                            $lang_options[$lang_id]['fields'][$field] = array(
911                                'TITLE'        => $user->lang['CP_' . strtoupper($field)],
912                                'FIELD'        => '<dd><textarea name="l_' . $field . '[' . $lang_id . ']" rows="7" cols="80">' . $value . '</textarea></dd>'
913                            );
914                        break;
915                    }
916
917                    if (isset($user->lang['CP_' . strtoupper($field) . '_EXPLAIN']))
918                    {
919                        $lang_options[$lang_id]['fields'][$field]['EXPLAIN'] = $user->lang['CP_' . strtoupper($field) . '_EXPLAIN'];
920                    }
921                }
922                else
923                {
924                    $var = ($action == 'create' || !is_array($cp->vars[$field])) ? $cp->vars[$field] : $cp->vars[$field][$lang_id];
925
926                    $lang_options[$lang_id]['fields'][$field] = array(
927                        'TITLE'        => $user->lang['CP_' . strtoupper($field)],
928                        '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>'
929                    );
930
931                    if (isset($user->lang['CP_' . strtoupper($field) . '_EXPLAIN']))
932                    {
933                        $lang_options[$lang_id]['fields'][$field]['EXPLAIN'] = $user->lang['CP_' . strtoupper($field) . '_EXPLAIN'];
934                    }
935                }
936            }
937        }
938
939        return $lang_options;
940    }
941
942    /**
943    * Save Profile Field
944    */
945    function save_profile_field($cp, $field_type, $action = 'create')
946    {
947        global $db, $config, $user, $phpbb_container, $phpbb_log, $request, $phpbb_dispatcher;
948
949        $field_id = $request->variable('field_id', 0);
950
951        // Collect all information, if something is going wrong, abort the operation
952        $profile_sql = $profile_lang = $empty_lang = $profile_lang_fields = array();
953
954        $default_lang_id = (!empty($this->edit_lang_id)) ? $this->edit_lang_id : $this->lang_defs['iso'][$config['default_lang']];
955
956        if ($action == 'create')
957        {
958            $sql = 'SELECT MAX(field_order) as max_field_order
959                FROM ' . PROFILE_FIELDS_TABLE;
960            $result = $db->sql_query($sql);
961            $new_field_order = (int) $db->sql_fetchfield('max_field_order');
962            $db->sql_freeresult($result);
963
964            $field_ident = $cp->vars['field_ident'];
965        }
966
967        // Save the field
968        $profile_fields = array(
969            'field_length'            => $cp->vars['field_length'],
970            'field_minlen'            => $cp->vars['field_minlen'],
971            'field_maxlen'            => $cp->vars['field_maxlen'],
972            'field_novalue'            => $cp->vars['field_novalue'],
973            'field_default_value'    => $cp->vars['field_default_value'],
974            'field_validation'        => $cp->vars['field_validation'],
975            'field_required'        => $cp->vars['field_required'],
976            'field_show_novalue'    => $cp->vars['field_show_novalue'],
977            'field_show_on_reg'        => $cp->vars['field_show_on_reg'],
978            'field_show_on_pm'        => $cp->vars['field_show_on_pm'],
979            'field_show_on_vt'        => $cp->vars['field_show_on_vt'],
980            'field_show_on_ml'        => $cp->vars['field_show_on_ml'],
981            'field_hide'            => $cp->vars['field_hide'],
982            'field_show_profile'    => $cp->vars['field_show_profile'],
983            'field_no_view'            => $cp->vars['field_no_view'],
984            'field_is_contact'        => $cp->vars['field_is_contact'],
985            'field_contact_desc'    => $cp->vars['field_contact_desc'],
986            'field_contact_url'        => $cp->vars['field_contact_url'],
987        );
988
989        $field_data = $cp->vars;
990        /**
991        * Event to modify profile field configuration data before saving to database
992        *
993        * @event core.acp_profile_create_edit_save_before
994        * @var    string    action            create|edit
995        * @var    string    field_type        Type of the field we are dealing with
996        * @var    array    field_data        Array of data about the field
997        * @var    array    profile_fields    Array of fields to be sent to the database
998        * @since 3.1.6-RC1
999        */
1000        $vars = array(
1001            'action',
1002            'field_type',
1003            'field_data',
1004            'profile_fields',
1005        );
1006        extract($phpbb_dispatcher->trigger_event('core.acp_profile_create_edit_save_before', compact($vars)));
1007
1008        if ($action == 'create')
1009        {
1010            $profile_fields += array(
1011                'field_type'        => $field_type,
1012                'field_ident'        => $field_ident,
1013                'field_name'        => $field_ident,
1014                'field_order'        => $new_field_order + 1,
1015                'field_active'        => 1
1016            );
1017
1018            $sql = 'INSERT INTO ' . PROFILE_FIELDS_TABLE . ' ' . $db->sql_build_array('INSERT', $profile_fields);
1019            $db->sql_query($sql);
1020
1021            $field_id = $db->sql_nextid();
1022        }
1023        else
1024        {
1025            $sql = 'UPDATE ' . PROFILE_FIELDS_TABLE . '
1026                SET ' . $db->sql_build_array('UPDATE', $profile_fields) . "
1027                WHERE field_id = $field_id";
1028            $db->sql_query($sql);
1029        }
1030
1031        $profile_field = $this->type_collection[$field_type];
1032
1033        if ($action == 'create')
1034        {
1035            $field_ident = 'pf_' . $field_ident;
1036            /* @var $db_tools \phpbb\db\tools\tools_interface */
1037            $db_tools = $phpbb_container->get('dbal.tools');
1038            $db_tools->sql_column_add(PROFILE_FIELDS_DATA_TABLE, $field_ident, array($profile_field->get_database_column_type(), null));
1039        }
1040
1041        $sql_ary = array(
1042            'lang_name'                => $cp->vars['lang_name'],
1043            'lang_explain'            => $cp->vars['lang_explain'],
1044            'lang_default_value'    => $cp->vars['lang_default_value']
1045        );
1046
1047        if ($action == 'create')
1048        {
1049            $sql_ary['field_id'] = $field_id;
1050            $sql_ary['lang_id'] = $default_lang_id;
1051
1052            $profile_sql[] = 'INSERT INTO ' . PROFILE_LANG_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
1053        }
1054        else
1055        {
1056            $this->update_insert(PROFILE_LANG_TABLE, $sql_ary, array('field_id' => $field_id, 'lang_id' => $default_lang_id));
1057        }
1058
1059        if (is_array($cp->vars['l_lang_name']) && count($cp->vars['l_lang_name']))
1060        {
1061            foreach ($cp->vars['l_lang_name'] as $lang_id => $data)
1062            {
1063                if (($cp->vars['lang_name'] != '' && $cp->vars['l_lang_name'][$lang_id] == '')
1064                    || ($cp->vars['lang_explain'] != '' && $cp->vars['l_lang_explain'][$lang_id] == '')
1065                    || ($cp->vars['lang_default_value'] != '' && $cp->vars['l_lang_default_value'][$lang_id] == ''))
1066                {
1067                    $empty_lang[$lang_id] = true;
1068                    break;
1069                }
1070
1071                if (!isset($empty_lang[$lang_id]))
1072                {
1073                    $profile_lang[] = array(
1074                        'field_id'        => $field_id,
1075                        'lang_id'        => $lang_id,
1076                        'lang_name'        => $cp->vars['l_lang_name'][$lang_id],
1077                        'lang_explain'    => (isset($cp->vars['l_lang_explain'][$lang_id])) ? $cp->vars['l_lang_explain'][$lang_id] : '',
1078                        'lang_default_value'    => (isset($cp->vars['l_lang_default_value'][$lang_id])) ? $cp->vars['l_lang_default_value'][$lang_id] : ''
1079                    );
1080                }
1081            }
1082
1083            foreach ($empty_lang as $lang_id => $NULL)
1084            {
1085                $sql = 'DELETE FROM ' . PROFILE_LANG_TABLE . "
1086                    WHERE field_id = $field_id
1087                    AND lang_id = " . (int) $lang_id;
1088                $db->sql_query($sql);
1089            }
1090        }
1091
1092        $cp->vars = $profile_field->get_language_options_input($cp->vars);
1093
1094        if ($cp->vars['lang_options'])
1095        {
1096            if (!is_array($cp->vars['lang_options']))
1097            {
1098                $cp->vars['lang_options'] = explode("\n", $cp->vars['lang_options']);
1099            }
1100
1101            if ($action != 'create')
1102            {
1103                $sql = 'DELETE FROM ' . PROFILE_FIELDS_LANG_TABLE . "
1104                    WHERE field_id = $field_id
1105                        AND lang_id = " . (int) $default_lang_id;
1106                $db->sql_query($sql);
1107            }
1108
1109            foreach ($cp->vars['lang_options'] as $option_id => $value)
1110            {
1111                $sql_ary = array(
1112                    'field_type'    => $field_type,
1113                    'lang_value'    => $value
1114                );
1115
1116                if ($action == 'create')
1117                {
1118                    $sql_ary['field_id'] = $field_id;
1119                    $sql_ary['lang_id'] = $default_lang_id;
1120                    $sql_ary['option_id'] = (int) $option_id;
1121
1122                    $profile_sql[] = 'INSERT INTO ' . PROFILE_FIELDS_LANG_TABLE . ' ' . $db->sql_build_array('INSERT', $sql_ary);
1123                }
1124                else
1125                {
1126                    $this->update_insert(PROFILE_FIELDS_LANG_TABLE, $sql_ary, array(
1127                        'field_id'    => $field_id,
1128                        'lang_id'    => (int) $default_lang_id,
1129                        'option_id'    => (int) $option_id)
1130                    );
1131                }
1132            }
1133        }
1134
1135        if (is_array($cp->vars['l_lang_options']) && count($cp->vars['l_lang_options']))
1136        {
1137            $empty_lang = array();
1138
1139            foreach ($cp->vars['l_lang_options'] as $lang_id => $lang_ary)
1140            {
1141                if (!is_array($lang_ary))
1142                {
1143                    $lang_ary = explode("\n", $lang_ary);
1144                }
1145
1146                if (count($lang_ary) != count($cp->vars['lang_options']))
1147                {
1148                    $empty_lang[$lang_id] = true;
1149                }
1150
1151                if (!isset($empty_lang[$lang_id]))
1152                {
1153                    if ($action != 'create')
1154                    {
1155                        $sql = 'DELETE FROM ' . PROFILE_FIELDS_LANG_TABLE . "
1156                            WHERE field_id = $field_id
1157                            AND lang_id = " . (int) $lang_id;
1158                        $db->sql_query($sql);
1159                    }
1160
1161                    foreach ($lang_ary as $option_id => $value)
1162                    {
1163                        $profile_lang_fields[] = array(
1164                            'field_id'        => (int) $field_id,
1165                            'lang_id'        => (int) $lang_id,
1166                            'option_id'        => (int) $option_id,
1167                            'field_type'    => $field_type,
1168                            'lang_value'    => $value
1169                        );
1170                    }
1171                }
1172            }
1173
1174            foreach ($empty_lang as $lang_id => $NULL)
1175            {
1176                $sql = 'DELETE FROM ' . PROFILE_FIELDS_LANG_TABLE . "
1177                    WHERE field_id = $field_id
1178                    AND lang_id = " . (int) $lang_id;
1179                $db->sql_query($sql);
1180            }
1181        }
1182
1183        foreach ($profile_lang as $sql)
1184        {
1185            if ($action == 'create')
1186            {
1187                $profile_sql[] = 'INSERT INTO ' . PROFILE_LANG_TABLE . ' ' . $db->sql_build_array('INSERT', $sql);
1188            }
1189            else
1190            {
1191                $lang_id = $sql['lang_id'];
1192                unset($sql['lang_id'], $sql['field_id']);
1193
1194                $this->update_insert(PROFILE_LANG_TABLE, $sql, array('lang_id' => (int) $lang_id, 'field_id' => $field_id));
1195            }
1196        }
1197
1198        if (count($profile_lang_fields))
1199        {
1200            foreach ($profile_lang_fields as $sql)
1201            {
1202                if ($action == 'create')
1203                {
1204                    $profile_sql[] = 'INSERT INTO ' . PROFILE_FIELDS_LANG_TABLE . ' ' . $db->sql_build_array('INSERT', $sql);
1205                }
1206                else
1207                {
1208                    $lang_id = $sql['lang_id'];
1209                    $option_id = $sql['option_id'];
1210                    unset($sql['lang_id'], $sql['field_id'], $sql['option_id']);
1211
1212                    $this->update_insert(PROFILE_FIELDS_LANG_TABLE, $sql, array(
1213                        'lang_id'    => $lang_id,
1214                        'field_id'    => $field_id,
1215                        'option_id'    => $option_id)
1216                    );
1217                }
1218            }
1219        }
1220
1221        $db->sql_transaction('begin');
1222
1223        if ($action == 'create')
1224        {
1225            foreach ($profile_sql as $sql)
1226            {
1227                $db->sql_query($sql);
1228            }
1229        }
1230
1231        $db->sql_transaction('commit');
1232
1233        if ($action == 'edit')
1234        {
1235            $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_PROFILE_FIELD_EDIT', false, array($cp->vars['field_ident'] . ':' . $cp->vars['lang_name']));
1236            trigger_error($user->lang['CHANGED_PROFILE_FIELD'] . adm_back_link($this->u_action));
1237        }
1238        else
1239        {
1240            $phpbb_log->add('admin', $user->data['user_id'], $user->ip, 'LOG_PROFILE_FIELD_CREATE', false, array(substr($field_ident, 3) . ':' . $cp->vars['lang_name']));
1241            trigger_error($user->lang['ADDED_PROFILE_FIELD'] . adm_back_link($this->u_action));
1242        }
1243    }
1244
1245    /**
1246    * Update, then insert if not successfull
1247    */
1248    function update_insert($table, $sql_ary, $where_fields)
1249    {
1250        global $db;
1251
1252        $where_sql = array();
1253        $check_key = '';
1254
1255        foreach ($where_fields as $key => $value)
1256        {
1257            $check_key = (!$check_key) ? $key : $check_key;
1258            $where_sql[] = $key . ' = ' . ((is_string($value)) ? "'" . $db->sql_escape($value) . "'" : (int) $value);
1259        }
1260
1261        if (!count($where_sql))
1262        {
1263            return;
1264        }
1265
1266        $sql = "SELECT $check_key
1267            FROM $table
1268            WHERE " . implode(' AND ', $where_sql);
1269        $result = $db->sql_query($sql);
1270        $row = $db->sql_fetchrow($result);
1271        $db->sql_freeresult($result);
1272
1273        if (!$row)
1274        {
1275            $sql_ary = array_merge($where_fields, $sql_ary);
1276
1277            if (count($sql_ary))
1278            {
1279                $db->sql_query("INSERT INTO $table " . $db->sql_build_array('INSERT', $sql_ary));
1280            }
1281        }
1282        else
1283        {
1284            if (count($sql_ary))
1285            {
1286                $sql = "UPDATE $table SET " . $db->sql_build_array('UPDATE', $sql_ary) . '
1287                    WHERE ' . implode(' AND ', $where_sql);
1288                $db->sql_query($sql);
1289            }
1290        }
1291    }
1292}