profile_getFieldForm()   F
last analyzed

Complexity

Conditions 45
Paths > 20000

Size

Total Lines 250
Code Lines 191

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 45
eloc 191
nc 111600
nop 2
dl 0
loc 250
rs 0
c 0
b 0
f 0

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/**
3
 * Extended User Profile
4
 *
5
 * You may not change or alter any portion of this comment or credits
6
 * of supporting developers from this source code or any supporting source code
7
 * which is considered copyrighted (c) material of the original comment or credit authors.
8
 * This program is distributed in the hope that it will be useful,
9
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * @copyright       (c) 2000-2016 XOOPS Project (www.xoops.org)
13
 * @license             GNU GPL 2 (https://www.gnu.org/licenses/gpl-2.0.html)
14
 * @package             profile
15
 * @since               2.3.0
16
 * @author              Jan Pedersen
17
 * @author              Taiwen Jiang <[email protected]>
18
 */
19
20
// defined('XOOPS_ROOT_PATH') || exit("XOOPS root path not defined");
21
22
/**
23
 * Get {@link XoopsThemeForm} for adding/editing fields
24
 *
25
 * @param ProfileField $field  {@link ProfileField} object to get edit form for
26
 * @param mixed        $action URL to submit to - or false for $_SERVER['REQUEST_URI']
27
 *
28
 * @return object
29
 */
30
function profile_getFieldForm(ProfileField $field, $action = false)
31
{
32
    if ($action === false) {
33
        $action = $_SERVER['REQUEST_URI'];
34
    }
35
    $title = $field->isNew() ? sprintf(_PROFILE_AM_ADD, _PROFILE_AM_FIELD) : sprintf(_PROFILE_AM_EDIT, _PROFILE_AM_FIELD);
36
37
    include_once $GLOBALS['xoops']->path('class/xoopsformloader.php');
38
    $form = new XoopsThemeForm($title, 'form', $action, 'post', true);
39
40
    $form->addElement(new XoopsFormText(_PROFILE_AM_TITLE, 'field_title', 35, 255, $field->getVar('field_title', 'e')));
41
    $form->addElement(new XoopsFormTextArea(_PROFILE_AM_DESCRIPTION, 'field_description', $field->getVar('field_description', 'e')));
42
43
    $fieldcat_id = 0;
44
    if (!$field->isNew()) {
45
        $fieldcat_id = $field->getVar('cat_id');
46
    }
47
    $category_handler = xoops_getModuleHandler('category');
48
    $cat_select       = new XoopsFormSelect(_PROFILE_AM_CATEGORY, 'field_category', $fieldcat_id);
49
    $cat_select->addOption(0, _PROFILE_AM_DEFAULT);
50
    $cat_select->addOptionArray($category_handler->getList());
0 ignored issues
show
Bug introduced by
The method getList() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsModuleHandler or XoopsImageHandler or XoopsRankHandler or XoopsCommentHandler or XoopsTplsetHandler or XoopsAvatarHandler or XoopsBlockHandler or XoopsImageSetHandler or XoopsPersistableObjectHandler or XoopsImagecategoryHandler. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

50
    $cat_select->addOptionArray($category_handler->/** @scrutinizer ignore-call */ getList());
Loading history...
51
    $form->addElement($cat_select);
52
    $form->addElement(new XoopsFormText(_PROFILE_AM_WEIGHT, 'field_weight', 10, 10, $field->getVar('field_weight', 'e')));
53
    if ($field->getVar('field_config') || $field->isNew()) {
54
        if (!$field->isNew()) {
55
            $form->addElement(new XoopsFormLabel(_PROFILE_AM_NAME, $field->getVar('field_name')));
56
            $form->addElement(new XoopsFormHidden('id', $field->getVar('field_id')));
57
        } else {
58
            $form->addElement(new XoopsFormText(_PROFILE_AM_NAME, 'field_name', 35, 255, $field->getVar('field_name', 'e')));
59
        }
60
61
        //autotext and theme left out of this one as fields of that type should never be changed (valid assumption, I think)
62
        $fieldtypes = [
63
            'checkbox'     => _PROFILE_AM_CHECKBOX,
64
            'date'         => _PROFILE_AM_DATE,
65
            'datetime'     => _PROFILE_AM_DATETIME,
66
            'longdate'     => _PROFILE_AM_LONGDATE,
67
            'group'        => _PROFILE_AM_GROUP,
68
            'group_multi'  => _PROFILE_AM_GROUPMULTI,
69
            'language'     => _PROFILE_AM_LANGUAGE,
70
            'radio'        => _PROFILE_AM_RADIO,
71
            'select'       => _PROFILE_AM_SELECT,
72
            'select_multi' => _PROFILE_AM_SELECTMULTI,
73
            'textarea'     => _PROFILE_AM_TEXTAREA,
74
            'dhtml'        => _PROFILE_AM_DHTMLTEXTAREA,
75
            'textbox'      => _PROFILE_AM_TEXTBOX,
76
            'timezone'     => _PROFILE_AM_TIMEZONE,
77
            'yesno'        => _PROFILE_AM_YESNO,
78
        ];
79
80
        $element_select = new XoopsFormSelect(_PROFILE_AM_TYPE, 'field_type', $field->getVar('field_type', 'e'));
81
        $element_select->addOptionArray($fieldtypes);
82
83
        $form->addElement($element_select);
84
85
        switch ($field->getVar('field_type')) {
86
            case 'textbox':
87
                $valuetypes = [
88
                    XOBJ_DTYPE_TXTBOX          => _PROFILE_AM_TXTBOX,
89
                    XOBJ_DTYPE_EMAIL           => _PROFILE_AM_EMAIL,
90
                    XOBJ_DTYPE_INT             => _PROFILE_AM_INT,
91
                    XOBJ_DTYPE_FLOAT           => _PROFILE_AM_FLOAT,
92
                    XOBJ_DTYPE_DECIMAL         => _PROFILE_AM_DECIMAL,
93
                    XOBJ_DTYPE_TXTAREA         => _PROFILE_AM_TXTAREA,
94
                    XOBJ_DTYPE_URL             => _PROFILE_AM_URL,
95
                    XOBJ_DTYPE_OTHER           => _PROFILE_AM_OTHER,
96
                    XOBJ_DTYPE_ARRAY           => _PROFILE_AM_ARRAY,
97
                    XOBJ_DTYPE_UNICODE_ARRAY   => _PROFILE_AM_UNICODE_ARRAY,
98
                    XOBJ_DTYPE_UNICODE_TXTBOX  => _PROFILE_AM_UNICODE_TXTBOX,
99
                    XOBJ_DTYPE_UNICODE_TXTAREA => _PROFILE_AM_UNICODE_TXTAREA,
100
                    XOBJ_DTYPE_UNICODE_EMAIL   => _PROFILE_AM_UNICODE_EMAIL,
101
                    XOBJ_DTYPE_UNICODE_URL     => _PROFILE_AM_UNICODE_URL,
102
                ];
103
104
                $type_select = new XoopsFormSelect(_PROFILE_AM_VALUETYPE, 'field_valuetype', $field->getVar('field_valuetype', 'e'));
105
                $type_select->addOptionArray($valuetypes);
106
                $form->addElement($type_select);
107
                break;
108
109
            case 'select':
110
            case 'radio':
111
                $valuetypes = [
112
                    XOBJ_DTYPE_TXTBOX          => _PROFILE_AM_TXTBOX,
113
                    XOBJ_DTYPE_EMAIL           => _PROFILE_AM_EMAIL,
114
                    XOBJ_DTYPE_INT             => _PROFILE_AM_INT,
115
                    XOBJ_DTYPE_FLOAT           => _PROFILE_AM_FLOAT,
116
                    XOBJ_DTYPE_DECIMAL         => _PROFILE_AM_DECIMAL,
117
                    XOBJ_DTYPE_TXTAREA         => _PROFILE_AM_TXTAREA,
118
                    XOBJ_DTYPE_URL             => _PROFILE_AM_URL,
119
                    XOBJ_DTYPE_OTHER           => _PROFILE_AM_OTHER,
120
                    XOBJ_DTYPE_ARRAY           => _PROFILE_AM_ARRAY,
121
                    XOBJ_DTYPE_UNICODE_ARRAY   => _PROFILE_AM_UNICODE_ARRAY,
122
                    XOBJ_DTYPE_UNICODE_TXTBOX  => _PROFILE_AM_UNICODE_TXTBOX,
123
                    XOBJ_DTYPE_UNICODE_TXTAREA => _PROFILE_AM_UNICODE_TXTAREA,
124
                    XOBJ_DTYPE_UNICODE_EMAIL   => _PROFILE_AM_UNICODE_EMAIL,
125
                    XOBJ_DTYPE_UNICODE_URL     => _PROFILE_AM_UNICODE_URL,
126
                ];
127
128
                $type_select = new XoopsFormSelect(_PROFILE_AM_VALUETYPE, 'field_valuetype', $field->getVar('field_valuetype', 'e'));
129
                $type_select->addOptionArray($valuetypes);
130
                $form->addElement($type_select);
131
                break;
132
        }
133
134
        //$form->addElement(new XoopsFormRadioYN(_PROFILE_AM_NOTNULL, 'field_notnull', $field->getVar('field_notnull', 'e') ));
135
136
        if ($field->getVar('field_type') === 'select' || $field->getVar('field_type') === 'select_multi' || $field->getVar('field_type') === 'radio' || $field->getVar('field_type') === 'checkbox') {
137
            $options = $field->getVar('field_options');
138
            if (count($options) > 0) {
139
                $remove_options          = new XoopsFormCheckBox(_PROFILE_AM_REMOVEOPTIONS, 'removeOptions');
140
                $remove_options->columns = 3;
141
                asort($options);
142
                foreach (array_keys($options) as $key) {
143
                    $options[$key] .= "[{$key}]";
144
                }
145
                $remove_options->addOptionArray($options);
146
                $form->addElement($remove_options);
147
            }
148
149
            $option_text = "<table  cellspacing='1'><tr><td class='width20'>" . _PROFILE_AM_KEY . '</td><td>' . _PROFILE_AM_VALUE . '</td></tr>';
150
            for ($i = 0; $i < 3; ++$i) {
151
                $option_text .= "<tr><td><input type='text' name='addOption[{$i}][key]' id='addOption[{$i}][key]' size='15' /></td><td><input type='text' name='addOption[{$i}][value]' id='addOption[{$i}][value]' size='35' /></td></tr>";
152
                $option_text .= "<tr height='3px'><td colspan='2'> </td></tr>";
153
            }
154
            $option_text .= '</table>';
155
            $form->addElement(new XoopsFormLabel(_PROFILE_AM_ADDOPTION, $option_text));
156
        }
157
    }
158
159
    if ($field->getVar('field_edit')) {
160
        switch ($field->getVar('field_type')) {
161
            case 'textbox':
162
            case 'textarea':
163
            case 'dhtml':
164
                $form->addElement(new XoopsFormText(_PROFILE_AM_MAXLENGTH, 'field_maxlength', 35, 35, $field->getVar('field_maxlength', 'e')));
165
                $form->addElement(new XoopsFormTextArea(_PROFILE_AM_DEFAULT, 'field_default', $field->getVar('field_default', 'e')));
166
                break;
167
168
            case 'checkbox':
169
            case 'select_multi':
170
                $def_value = $field->getVar('field_default', 'e') != null ? unserialize($field->getVar('field_default', 'n')) : null;
171
                $element   = new XoopsFormSelect(_PROFILE_AM_DEFAULT, 'field_default', $def_value, 8, true);
172
                $options   = $field->getVar('field_options');
173
                asort($options);
174
                // If options do not include an empty element, then add a blank option to prevent any default selection
175
                //                if (!in_array('', array_keys($options))) {
176
                if (!array_key_exists('', $options)) {
177
                    $element->addOption('', _NONE);
178
                }
179
                $element->addOptionArray($options);
180
                $form->addElement($element);
181
                break;
182
183
            case 'select':
184
            case 'radio':
185
                $def_value = $field->getVar('field_default', 'e') != null ? $field->getVar('field_default') : null;
186
                $element   = new XoopsFormSelect(_PROFILE_AM_DEFAULT, 'field_default', $def_value);
187
                $options   = $field->getVar('field_options');
188
                asort($options);
189
                // If options do not include an empty element, then add a blank option to prevent any default selection
190
                //                if (!in_array('', array_keys($options))) {
191
                if (!array_key_exists('', $options)) {
192
                    $element->addOption('', _NONE);
193
                }
194
                $element->addOptionArray($options);
195
                $form->addElement($element);
196
                break;
197
198
            case 'date':
199
                $form->addElement(new XoopsFormTextDateSelect(_PROFILE_AM_DEFAULT, 'field_default', 15, $field->getVar('field_default', 'e')));
200
                break;
201
202
            case 'longdate':
203
                $form->addElement(new XoopsFormTextDateSelect(_PROFILE_AM_DEFAULT, 'field_default', 15, strtotime($field->getVar('field_default', 'e'))));
204
                break;
205
206
            case 'datetime':
207
                $form->addElement(new XoopsFormDateTime(_PROFILE_AM_DEFAULT, 'field_default', 15, $field->getVar('field_default', 'e')));
208
                break;
209
210
            case 'yesno':
211
                $form->addElement(new XoopsFormRadioYN(_PROFILE_AM_DEFAULT, 'field_default', $field->getVar('field_default', 'e')));
212
                break;
213
214
            case 'timezone':
215
                $form->addElement(new XoopsFormSelectTimezone(_PROFILE_AM_DEFAULT, 'field_default', $field->getVar('field_default', 'e')));
216
                break;
217
218
            case 'language':
219
                $form->addElement(new XoopsFormSelectLang(_PROFILE_AM_DEFAULT, 'field_default', $field->getVar('field_default', 'e')));
220
                break;
221
222
            case 'group':
223
                $form->addElement(new XoopsFormSelectGroup(_PROFILE_AM_DEFAULT, 'field_default', true, $field->getVar('field_default', 'e')));
224
                break;
225
226
            case 'group_multi':
227
                $form->addElement(new XoopsFormSelectGroup(_PROFILE_AM_DEFAULT, 'field_default', true, unserialize($field->getVar('field_default', 'n')), 5, true));
228
                break;
229
230
            case 'theme':
231
                $form->addElement(new XoopsFormSelectTheme(_PROFILE_AM_DEFAULT, 'field_default', $field->getVar('field_default', 'e')));
232
                break;
233
234
            case 'autotext':
235
                $form->addElement(new XoopsFormTextArea(_PROFILE_AM_DEFAULT, 'field_default', $field->getVar('field_default', 'e')));
236
                break;
237
        }
238
    }
239
    /** @var XoopsGroupPermHandler $groupperm_handler */
240
    $groupperm_handler = xoops_getHandler('groupperm');
241
    $searchable_types  = [
242
        'textbox',
243
        'select',
244
        'radio',
245
        'yesno',
246
        'date',
247
        'datetime',
248
        'timezone',
249
        'language',
250
    ];
251
    if (in_array($field->getVar('field_type'), $searchable_types)) {
252
        $search_groups = $groupperm_handler->getGroupIds('profile_search', $field->getVar('field_id'), $GLOBALS['xoopsModule']->getVar('mid'));
253
        $form->addElement(new XoopsFormSelectGroup(_PROFILE_AM_PROF_SEARCH, 'profile_search', true, $search_groups, 5, true));
254
    }
255
    if ($field->getVar('field_edit') || $field->isNew()) {
256
        $editable_groups = [];
257
        if (!$field->isNew()) {
258
            //Load groups
259
            $editable_groups = $groupperm_handler->getGroupIds('profile_edit', $field->getVar('field_id'), $GLOBALS['xoopsModule']->getVar('mid'));
260
        }
261
        $form->addElement(new XoopsFormSelectGroup(_PROFILE_AM_PROF_EDITABLE, 'profile_edit', false, $editable_groups, 5, true));
262
        $form->addElement(new XoopsFormRadioYN(_PROFILE_AM_REQUIRED, 'field_required', $field->getVar('field_required', 'e')));
263
        $regstep_select = new XoopsFormSelect(_PROFILE_AM_PROF_REGISTER, 'step_id', $field->getVar('step_id', 'e'));
264
        $regstep_select->addOption(0, _NO);
265
        $regstep_handler = xoops_getModuleHandler('regstep');
266
        $regstep_select->addOptionArray($regstep_handler->getList());
267
        $form->addElement($regstep_select);
268
    }
269
    $form->addElement(new XoopsFormHidden('op', 'save'));
270
    $form->addElement(new XoopsFormButton('', 'submit', _SUBMIT, 'submit'));
271
272
    $options = $field->getVar('field_options');
273
    if (count($options) > 0) {
274
        $linkText = defined('_PROFILE_AM_EDIT_OPTION_STRINGS') ? _PROFILE_AM_EDIT_OPTION_STRINGS : 'Edit Option Strings';
275
        $editOptionsButton = new XoopsFormLabel('', '<a href="' . $action . '&op=edit-option-strings"><i class="fa fa-fw fa-2x fa-language" aria-hidden="true"></i> ' . $linkText . '</a>');
276
        $form->addElement($editOptionsButton);
277
    }
278
279
    return $form;
280
}
281
282
function profile_getFieldOptionForm(ProfileField $field, $action = false)
283
{
284
    if ($action === false) {
285
        $action = ''; // $_SERVER['REQUEST_URI'];
286
    }
287
    $title = sprintf(_PROFILE_AM_EDIT, _PROFILE_AM_FIELD);
288
289
    include_once $GLOBALS['xoops']->path('class/xoopsformloader.php');
290
    $form = new XoopsThemeForm($title, 'form', $action, 'post', true);
291
292
    $form->addElement(new XoopsFormLabel(_PROFILE_AM_TITLE, $field->getVar('field_title', 'e')));
293
294
    $options = $field->getVar('field_options');
295
    foreach($options as $name => $value) {
296
        $form->addElement(new XoopsFormText($name, "field_options[$name]", 80, 255, $value));
297
    }
298
299
    $form->addElement(new XoopsFormHidden('op', 'save-option-strings'));
300
    $form->addElement(new XoopsFormButton('', 'submit', _SUBMIT, 'submit'));
301
302
    return $form;
303
}
304
305
/**
306
 * Get {@link XoopsThemeForm} for registering new users
307
 *
308
 * @param XoopsUser $user
309
 * @param           $profile
310
 * @param XoopsUser $user {@link XoopsUser} to register
311
 * @param int       $step Which step we are at
312
 *
313
 * @internal param \profileRegstep $next_step
314
 * @return object
315
 */
316
function profile_getRegisterForm(XoopsUser $user, $profile, $step = null)
317
{
318
    global $opkey; // should be set in register.php
319
    if (empty($opkey)) {
320
        $opkey = 'profile_opname';
321
    }
322
    $next_opname      = 'op' . mt_rand(10000, 99999);
323
    $_SESSION[$opkey] = $next_opname;
324
325
    include_once $GLOBALS['xoops']->path('class/xoopsformloader.php');
326
    if (empty($GLOBALS['xoopsConfigUser'])) {
327
        /** @var XoopsConfigHandler $config_handler */
328
        $config_handler             = xoops_getHandler('config');
329
        $GLOBALS['xoopsConfigUser'] = $config_handler->getConfigsByCat(XOOPS_CONF_USER);
330
    }
331
    $action    = $_SERVER['REQUEST_URI'];
332
    $step_no   = $step['step_no'];
333
    $use_token = $step['step_no'] > 0;// ? true : false;
334
    $reg_form  = new XoopsThemeForm($step['step_name'], 'regform', $action, 'post', $use_token);
335
336
    if ($step['step_desc']) {
337
        $reg_form->addElement(new XoopsFormLabel('', $step['step_desc']));
338
    }
339
340
    if ($step_no == 1) {
341
        //$uname_size = $GLOBALS['xoopsConfigUser']['maxuname'] < 35 ? $GLOBALS['xoopsConfigUser']['maxuname'] : 35;
342
343
        $elements[0][] = [
0 ignored issues
show
Comprehensibility Best Practice introduced by
$elements was never initialized. Although not strictly required by PHP, it is generally a good practice to add $elements = array(); before regardless.
Loading history...
344
            'element'  => new XoopsFormText(_US_NICKNAME, 'uname', 35, $GLOBALS['xoopsConfigUser']['maxuname'], $user->getVar('uname', 'e')),
0 ignored issues
show
Bug introduced by
It seems like $user->getVar('uname', 'e') can also be of type array and array; however, parameter $value of XoopsFormText::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

344
            'element'  => new XoopsFormText(_US_NICKNAME, 'uname', 35, $GLOBALS['xoopsConfigUser']['maxuname'], /** @scrutinizer ignore-type */ $user->getVar('uname', 'e')),
Loading history...
345
            'required' => true,
346
            'description' => sprintf(_US_DESCRIPTIONMIN, $GLOBALS['xoopsConfigUser']['minuname']) . '<br>' . sprintf(_US_DESCRIPTIONMAX, $GLOBALS['xoopsConfigUser']['maxuname']),
347
        ];
348
        $weights[0][]  = 0;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$weights was never initialized. Although not strictly required by PHP, it is generally a good practice to add $weights = array(); before regardless.
Loading history...
349
350
        $elements[0][] = ['element' => new XoopsFormText(_US_EMAIL, 'email', 35, 255, $user->getVar('email', 'e')), 'required' => true];
351
        $weights[0][]  = 0;
352
353
        $elements[0][] = [
354
            'element' => new XoopsFormPassword(_US_PASSWORD, 'pass', 35, 32, ''),
355
            'required' => true,
356
            'description' => sprintf(_US_DESCRIPTIONMIN, $GLOBALS['xoopsConfigUser']['minpass']),
357
        ];
358
        $weights[0][]  = 0;
359
360
        $elements[0][] = ['element' => new XoopsFormPassword(_US_VERIFYPASS, 'vpass', 35, 32, ''), 'required' => true];
361
        $weights[0][]  = 0;
362
    }
363
364
    // Dynamic fields
365
    $profile_handler              = xoops_getModuleHandler('profile');
366
    $fields                       = $profile_handler->loadFields();
0 ignored issues
show
Bug introduced by
The method loadFields() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of XoopsObjectHandler such as XoopsPersistableObjectHandler. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

366
    /** @scrutinizer ignore-call */ 
367
    $fields                       = $profile_handler->loadFields();
Loading history...
367
    $_SESSION['profile_required'] = [];
368
    foreach (array_keys($fields) as $i) {
369
        if ($fields[$i]->getVar('step_id') == $step['step_id']) {
370
            $fieldinfo['element'] = $fields[$i]->getEditElement($user, $profile);
371
            //assign and check (=)
372
            if ($fieldinfo['required'] = $fields[$i]->getVar('field_required')) {
373
                $_SESSION['profile_required'][$fields[$i]->getVar('field_name')] = $fields[$i]->getVar('field_title');
374
            }
375
376
            $key              = $fields[$i]->getVar('cat_id');
377
            $elements[$key][] = $fieldinfo;
378
            $weights[$key][]  = $fields[$i]->getVar('field_weight');
379
        }
380
    }
381
    ksort($elements);
382
383
    // Get categories
384
    $cat_handler = xoops_getModuleHandler('category');
385
    $categories  = $cat_handler->getObjects(null, true, false);
0 ignored issues
show
Unused Code introduced by
The assignment to $categories is dead and can be removed.
Loading history...
Bug introduced by
The method getObjects() does not exist on XoopsObjectHandler. It seems like you code against a sub-type of said class. However, the method does not exist in XoopsRankHandler or XoUserHandler. Are you sure you never get one of those? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

385
    /** @scrutinizer ignore-call */ 
386
    $categories  = $cat_handler->getObjects(null, true, false);
Loading history...
386
387
    foreach (array_keys($elements) as $k) {
388
        array_multisort($weights[$k], SORT_ASC, array_keys($elements[$k]), SORT_ASC, $elements[$k]);
0 ignored issues
show
Bug introduced by
SORT_ASC cannot be passed to array_multisort() as the parameter $rest expects a reference. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

388
        array_multisort($weights[$k], /** @scrutinizer ignore-type */ SORT_ASC, array_keys($elements[$k]), SORT_ASC, $elements[$k]);
Loading history...
Bug introduced by
array_keys($elements[$k]) cannot be passed to array_multisort() as the parameter $rest expects a reference. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

388
        array_multisort($weights[$k], SORT_ASC, /** @scrutinizer ignore-type */ array_keys($elements[$k]), SORT_ASC, $elements[$k]);
Loading history...
Comprehensibility Best Practice introduced by
The variable $weights does not seem to be defined for all execution paths leading up to this point.
Loading history...
389
        //$title = isset($categories[$k]) ? $categories[$k]['cat_title'] : _PROFILE_MA_DEFAULT;
390
        //$desc = isset($categories[$k]) ? $categories[$k]['cat_description'] : "";
391
        //$reg_form->insertBreak("<p>{$title}</p>{$desc}");
392
        //$reg_form->addElement(new XoopsFormLabel("<h2>".$title."</h2>", $desc), false);
393
        foreach (array_keys($elements[$k]) as $i) {
394
            if (array_key_exists('description', $elements[$k][$i])) {
395
                $element = $elements[$k][$i]['element'];
396
                $element->setDescription($elements[$k][$i]['description']);
397
                $reg_form->addElement($element, $elements[$k][$i]['required']);
398
                unset($element);
399
            } else {
400
                $reg_form->addElement($elements[$k][$i]['element'], $elements[$k][$i]['required']);
401
            }
402
        }
403
    }
404
    //end of Dynamic User fields
405
406
    if ($step_no == 1 && $GLOBALS['xoopsConfigUser']['reg_dispdsclmr'] != 0 && $GLOBALS['xoopsConfigUser']['reg_disclaimer'] != '') {
407
        $disc_tray = new XoopsFormElementTray(_US_DISCLAIMER, '<br>');
408
        $disc_text = new XoopsFormLabel('', "<div class=\"pad5\">" . $GLOBALS['myts']->displayTarea($GLOBALS['xoopsConfigUser']['reg_disclaimer'], 1) . '</div>');
409
        $disc_tray->addElement($disc_text);
410
        $agree_chk = new XoopsFormCheckBox('', 'agree_disc');
411
        $agree_chk->addOption(1, _US_IAGREE);
412
        $disc_tray->addElement($agree_chk);
413
        $reg_form->addElement($disc_tray);
414
    }
415
    global $xoopsModuleConfig;
416
    $useCaptchaAfterStep2 = $xoopsModuleConfig['profileCaptchaAfterStep1'];
417
418
    if ($step_no == 1) {
419
        $reg_form->addElement(new XoopsFormCaptcha(), true);
420
    } elseif($useCaptchaAfterStep2 == 1) {
421
        $reg_form->addElement(new XoopsFormCaptcha(), true);
422
    }
423
424
    $reg_form->addElement(new XoopsFormHidden($next_opname, 'register'));
425
    $reg_form->addElement(new XoopsFormHidden('uid', $user->getVar('uid')));
0 ignored issues
show
Bug introduced by
It seems like $user->getVar('uid') can also be of type array and array; however, parameter $value of XoopsFormHidden::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

425
    $reg_form->addElement(new XoopsFormHidden('uid', /** @scrutinizer ignore-type */ $user->getVar('uid')));
Loading history...
426
    $reg_form->addElement(new XoopsFormHidden('step', $step_no));
427
    $reg_form->addElement(new XoopsFormButton('', 'submitButton', _SUBMIT, 'submit'));
428
429
    return $reg_form;
430
}
431
432
/**
433
 * Get {@link XoopsThemeForm} for editing a user
434
 *
435
 * @param XoopsUser           $user {@link XoopsUser} to edit
436
 * @param ProfileProfile|XoopsObject|null $profile
437
 * @param bool                $action
438
 *
439
 * @return object
440
 */
441
function profile_getUserForm(XoopsUser $user, ?ProfileProfile $profile = null, $action = false)
442
{
443
    if ($action === false) {
444
        $action = $_SERVER['REQUEST_URI'];
445
    }
446
    if (empty($GLOBALS['xoopsConfigUser'])) {
447
        /** @var XoopsConfigHandler $config_handler */
448
        $config_handler             = xoops_getHandler('config');
449
        $GLOBALS['xoopsConfigUser'] = $config_handler->getConfigsByCat(XOOPS_CONF_USER);
450
    }
451
452
    include_once $GLOBALS['xoops']->path('class/xoopsformloader.php');
453
454
    $title = $user->isNew() ? _PROFILE_AM_ADDUSER : _US_EDITPROFILE;
455
456
    $form = new XoopsThemeForm($title, 'userinfo', $action, 'post', true);
457
    /** @var ProfileProfileHandler $profile_handler */
458
    $profile_handler = xoops_getModuleHandler('profile');
459
    // Dynamic fields
460
    if (!$profile) {
461
        /** @var ProfileProfileHandler $profile_handler */
462
        $profile_handler = xoops_getModuleHandler('profile', 'profile');
463
        $profile         = $profile_handler->get($user->getVar('uid'));
464
    }
465
    // Get fields
466
    $fields = $profile_handler->loadFields();
467
    // Get ids of fields that can be edited
468
    /** @var  XoopsGroupPermHandler $gperm_handler */
469
    $gperm_handler   = xoops_getHandler('groupperm');
470
    $editable_fields = $gperm_handler->getItemIds('profile_edit', $GLOBALS['xoopsUser']->getGroups(), $GLOBALS['xoopsModule']->getVar('mid'));
471
472
    if ($user->isNew() || $GLOBALS['xoopsUser']->isAdmin()) {
473
        $elements[0][] = [
0 ignored issues
show
Comprehensibility Best Practice introduced by
$elements was never initialized. Although not strictly required by PHP, it is generally a good practice to add $elements = array(); before regardless.
Loading history...
474
            'element'  => new XoopsFormText(_US_NICKNAME, 'uname', 25, $GLOBALS['xoopsUser']->isAdmin() ? 60 : $GLOBALS['xoopsConfigUser']['maxuname'], $user->getVar('uname', 'e')),
0 ignored issues
show
Bug introduced by
It seems like $user->getVar('uname', 'e') can also be of type array and array; however, parameter $value of XoopsFormText::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

474
            'element'  => new XoopsFormText(_US_NICKNAME, 'uname', 25, $GLOBALS['xoopsUser']->isAdmin() ? 60 : $GLOBALS['xoopsConfigUser']['maxuname'], /** @scrutinizer ignore-type */ $user->getVar('uname', 'e')),
Loading history...
475
            'required' => 1,
476
        ];
477
        $email_text    = new XoopsFormText('', 'email', 30, 60, $user->getVar('email'));
478
    } else {
479
        $elements[0][] = ['element' => new XoopsFormLabel(_US_NICKNAME, $user->getVar('uname')), 'required' => 0];
0 ignored issues
show
Bug introduced by
It seems like $user->getVar('uname') can also be of type array and array; however, parameter $value of XoopsFormLabel::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

479
        $elements[0][] = ['element' => new XoopsFormLabel(_US_NICKNAME, /** @scrutinizer ignore-type */ $user->getVar('uname')), 'required' => 0];
Loading history...
480
        $email_text    = new XoopsFormLabel('', $user->getVar('email'));
481
    }
482
    $email_tray = new XoopsFormElementTray(_US_EMAIL, '<br>');
483
    $email_tray->addElement($email_text, ($user->isNew() || $GLOBALS['xoopsUser']->isAdmin()) ? 1 : 0);
0 ignored issues
show
Bug introduced by
$user->isNew() || $GLOBA...er']->isAdmin() ? 1 : 0 of type integer is incompatible with the type boolean expected by parameter $required of XoopsFormElementTray::addElement(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

483
    $email_tray->addElement($email_text, /** @scrutinizer ignore-type */ ($user->isNew() || $GLOBALS['xoopsUser']->isAdmin()) ? 1 : 0);
Loading history...
484
    $weights[0][]  = 0;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$weights was never initialized. Although not strictly required by PHP, it is generally a good practice to add $weights = array(); before regardless.
Loading history...
485
    $elements[0][] = ['element' => $email_tray, 'required' => 0];
486
    $weights[0][]  = 0;
487
488
    if ($GLOBALS['xoopsUser']->isAdmin() && $user->getVar('uid') != $GLOBALS['xoopsUser']->getVar('uid')) {
489
        //If the user is an admin and is editing someone else
490
        $pwd_text  = new XoopsFormPassword('', 'password', 10, 32);
491
        $pwd_text2 = new XoopsFormPassword('', 'vpass', 10, 32);
492
        $pwd_tray  = new XoopsFormElementTray(_US_PASSWORD . '<br>' . _US_TYPEPASSTWICE);
493
        $pwd_tray->addElement($pwd_text);
494
        $pwd_tray->addElement($pwd_text2);
495
        $elements[0][] = ['element' => $pwd_tray, 'required' => 0]; //cannot set an element tray required
496
        $weights[0][]  = 0;
497
498
        $level_radio = new XoopsFormRadio(_PROFILE_MA_USERLEVEL, 'level', (string) $user->getVar('level'));
499
        $level_radio->addOption(1, _PROFILE_MA_ACTIVE);
500
        $level_radio->addOption(0, _PROFILE_MA_INACTIVE);
501
        //$level_radio->addOption(-1, _PROFILE_MA_DISABLED);
502
        $elements[0][] = ['element' => $level_radio, 'required' => 0];
503
        $weights[0][]  = 0;
504
    }
505
506
    $elements[0][] = ['element' => new XoopsFormHidden('uid', $user->getVar('uid')), 'required' => 0];
0 ignored issues
show
Bug introduced by
It seems like $user->getVar('uid') can also be of type array and array; however, parameter $value of XoopsFormHidden::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

506
    $elements[0][] = ['element' => new XoopsFormHidden('uid', /** @scrutinizer ignore-type */ $user->getVar('uid')), 'required' => 0];
Loading history...
507
    $weights[0][]  = 0;
508
    $elements[0][] = ['element' => new XoopsFormHidden('op', 'save'), 'required' => 0];
509
    $weights[0][]  = 0;
510
511
    $cat_handler    = xoops_getModuleHandler('category');
512
    $categories     = [];
513
    $all_categories = $cat_handler->getObjects(null, true, false);
514
    $count_fields   = count($fields);
515
516
    foreach (array_keys($fields) as $i) {
517
        if (in_array($fields[$i]->getVar('field_id'), $editable_fields)) {
518
            // Set default value for user fields if available
519
            if ($user->isNew()) {
520
                $default = $fields[$i]->getVar('field_default');
521
                if ($default !== '' && $default !== null) {
522
                    $user->setVar($fields[$i]->getVar('field_name'), $default);
523
                }
524
            }
525
526
            if ($profile->getVar($fields[$i]->getVar('field_name'), 'n') === null) {
527
                $default = $fields[$i]->getVar('field_default', 'n');
528
                $profile->setVar($fields[$i]->getVar('field_name'), $default);
529
            }
530
531
            $fieldinfo['element']  = $fields[$i]->getEditElement($user, $profile);
532
            $fieldinfo['required'] = $fields[$i]->getVar('field_required');
533
534
            $key              = isset($all_categories[$fields[$i]->getVar('cat_id')]['cat_weight']) ? (int) ($all_categories[$fields[$i]->getVar('cat_id')]['cat_weight'] * $count_fields) + $fields[$i]->getVar('cat_id') : 0;
535
            $elements[$key][] = $fieldinfo;
536
            $weights[$key][]  = $fields[$i]->getVar('field_weight');
537
            $categories[$key] = $all_categories[$fields[$i]->getVar('cat_id')] ?? null;
538
        }
539
    }
540
541
    if ($GLOBALS['xoopsUser'] && $GLOBALS['xoopsUser']->isAdmin()) {
542
        xoops_loadLanguage('admin', 'profile');
543
        /** @var  XoopsGroupPermHandler $gperm_handler */
544
        $gperm_handler = xoops_getHandler('groupperm');
545
        //If user has admin rights on groups
546
        include_once $GLOBALS['xoops']->path('modules/system/constants.php');
547
        if ($gperm_handler->checkRight('system_admin', XOOPS_SYSTEM_GROUP, $GLOBALS['xoopsUser']->getGroups(), 1)) {
548
            //add group selection
549
            $group_select  = new XoopsFormSelectGroup(_US_GROUPS, 'groups', false, $user->getGroups(), 5, true);
550
            $elements[0][] = ['element' => $group_select, 'required' => 0];
551
            //set as latest;
552
            $weights[0][] = $count_fields + 1;
553
        }
554
    }
555
556
    ksort($elements);
557
    foreach (array_keys($elements) as $k) {
558
        array_multisort($weights[$k], SORT_ASC, array_keys($elements[$k]), SORT_ASC, $elements[$k]);
0 ignored issues
show
Bug introduced by
array_keys($elements[$k]) cannot be passed to array_multisort() as the parameter $rest expects a reference. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

558
        array_multisort($weights[$k], SORT_ASC, /** @scrutinizer ignore-type */ array_keys($elements[$k]), SORT_ASC, $elements[$k]);
Loading history...
Bug introduced by
SORT_ASC cannot be passed to array_multisort() as the parameter $rest expects a reference. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

558
        array_multisort($weights[$k], /** @scrutinizer ignore-type */ SORT_ASC, array_keys($elements[$k]), SORT_ASC, $elements[$k]);
Loading history...
559
        $title = isset($categories[$k]) ? $categories[$k]['cat_title'] : _PROFILE_MA_DEFAULT;
560
        $desc  = isset($categories[$k]) ? $categories[$k]['cat_description'] : '';
561
        $form->addElement(new XoopsFormLabel("<h3>{$title}</h3>", $desc), false);
562
        foreach (array_keys($elements[$k]) as $i) {
563
            $form->addElement($elements[$k][$i]['element'], $elements[$k][$i]['required']);
564
        }
565
    }
566
567
    $form->addElement(new XoopsFormHidden('uid', $user->getVar('uid')));
568
    $form->addElement(new XoopsFormButton('', 'submit', _US_SAVECHANGES, 'submit'));
569
570
    return $form;
571
}
572
573
/**
574
 * Get {@link XoopsThemeForm} for editing a step
575
 *
576
 * @param ProfileRegstep|null $step {@link ProfileRegstep} to edit
577
 * @param bool                $action
578
 *
579
 * @return object
580
 */
581
function profile_getStepForm(?ProfileRegstep $step = null, $action = false)
582
{
583
    if ($action === false) {
584
        $action = $_SERVER['REQUEST_URI'];
0 ignored issues
show
Unused Code introduced by
The assignment to $action is dead and can be removed.
Loading history...
585
    }
586
    if (empty($GLOBALS['xoopsConfigUser'])) {
587
        /** @var XoopsConfigHandler $config_handler */
588
        $config_handler             = xoops_getHandler('config');
589
        $GLOBALS['xoopsConfigUser'] = $config_handler->getConfigsByCat(XOOPS_CONF_USER);
590
    }
591
    include_once $GLOBALS['xoops']->path('class/xoopsformloader.php');
592
593
    $form = new XoopsThemeForm(_PROFILE_AM_STEP, 'stepform', 'step.php', 'post', true);
594
595
    if (!$step->isNew()) {
0 ignored issues
show
Bug introduced by
The method isNew() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

595
    if (!$step->/** @scrutinizer ignore-call */ isNew()) {

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
596
        $form->addElement(new XoopsFormHidden('id', $step->getVar('step_id')));
0 ignored issues
show
Bug introduced by
It seems like $step->getVar('step_id') can also be of type array and array; however, parameter $value of XoopsFormHidden::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

596
        $form->addElement(new XoopsFormHidden('id', /** @scrutinizer ignore-type */ $step->getVar('step_id')));
Loading history...
597
    }
598
    $form->addElement(new XoopsFormHidden('op', 'save'));
599
    $form->addElement(new XoopsFormText(_PROFILE_AM_STEPNAME, 'step_name', 25, 255, $step->getVar('step_name', 'e')));
0 ignored issues
show
Bug introduced by
It seems like $step->getVar('step_name', 'e') can also be of type array and array; however, parameter $value of XoopsFormText::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

599
    $form->addElement(new XoopsFormText(_PROFILE_AM_STEPNAME, 'step_name', 25, 255, /** @scrutinizer ignore-type */ $step->getVar('step_name', 'e')));
Loading history...
600
    $form->addElement(new XoopsFormText(_PROFILE_AM_STEPINTRO, 'step_desc', 25, 255, $step->getVar('step_desc', 'e')));
601
    $form->addElement(new XoopsFormText(_PROFILE_AM_STEPORDER, 'step_order', 10, 10, $step->getVar('step_order', 'e')));
602
    $form->addElement(new XoopsFormRadioYN(_PROFILE_AM_STEPSAVE, 'step_save', $step->getVar('step_save', 'e')));
0 ignored issues
show
Bug introduced by
It seems like $step->getVar('step_save', 'e') can also be of type array and array; however, parameter $value of XoopsFormRadioYN::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

602
    $form->addElement(new XoopsFormRadioYN(_PROFILE_AM_STEPSAVE, 'step_save', /** @scrutinizer ignore-type */ $step->getVar('step_save', 'e')));
Loading history...
603
    $form->addElement(new XoopsFormButton('', 'submit', _SUBMIT, 'submit'));
604
605
    return $form;
606
}
607