profile_getFieldForm()   F
last analyzed

Complexity

Conditions 45
Paths > 20000

Size

Total Lines 246
Code Lines 191

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 45
eloc 191
nc 111600
nop 2
dl 0
loc 246
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 = array(
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
        $element_select = new XoopsFormSelect(_PROFILE_AM_TYPE, 'field_type', $field->getVar('field_type', 'e'));
80
        $element_select->addOptionArray($fieldtypes);
81
82
        $form->addElement($element_select);
83
84
        switch ($field->getVar('field_type')) {
85
            case 'textbox':
86
                $valuetypes = array(
87
                    XOBJ_DTYPE_TXTBOX          => _PROFILE_AM_TXTBOX,
88
                    XOBJ_DTYPE_EMAIL           => _PROFILE_AM_EMAIL,
89
                    XOBJ_DTYPE_INT             => _PROFILE_AM_INT,
90
                    XOBJ_DTYPE_FLOAT           => _PROFILE_AM_FLOAT,
91
                    XOBJ_DTYPE_DECIMAL         => _PROFILE_AM_DECIMAL,
92
                    XOBJ_DTYPE_TXTAREA         => _PROFILE_AM_TXTAREA,
93
                    XOBJ_DTYPE_URL             => _PROFILE_AM_URL,
94
                    XOBJ_DTYPE_OTHER           => _PROFILE_AM_OTHER,
95
                    XOBJ_DTYPE_ARRAY           => _PROFILE_AM_ARRAY,
96
                    XOBJ_DTYPE_UNICODE_ARRAY   => _PROFILE_AM_UNICODE_ARRAY,
97
                    XOBJ_DTYPE_UNICODE_TXTBOX  => _PROFILE_AM_UNICODE_TXTBOX,
98
                    XOBJ_DTYPE_UNICODE_TXTAREA => _PROFILE_AM_UNICODE_TXTAREA,
99
                    XOBJ_DTYPE_UNICODE_EMAIL   => _PROFILE_AM_UNICODE_EMAIL,
100
                    XOBJ_DTYPE_UNICODE_URL     => _PROFILE_AM_UNICODE_URL);
101
102
                $type_select = new XoopsFormSelect(_PROFILE_AM_VALUETYPE, 'field_valuetype', $field->getVar('field_valuetype', 'e'));
103
                $type_select->addOptionArray($valuetypes);
104
                $form->addElement($type_select);
105
                break;
106
107
            case 'select':
108
            case 'radio':
109
                $valuetypes = array(
110
                    XOBJ_DTYPE_TXTBOX          => _PROFILE_AM_TXTBOX,
111
                    XOBJ_DTYPE_EMAIL           => _PROFILE_AM_EMAIL,
112
                    XOBJ_DTYPE_INT             => _PROFILE_AM_INT,
113
                    XOBJ_DTYPE_FLOAT           => _PROFILE_AM_FLOAT,
114
                    XOBJ_DTYPE_DECIMAL         => _PROFILE_AM_DECIMAL,
115
                    XOBJ_DTYPE_TXTAREA         => _PROFILE_AM_TXTAREA,
116
                    XOBJ_DTYPE_URL             => _PROFILE_AM_URL,
117
                    XOBJ_DTYPE_OTHER           => _PROFILE_AM_OTHER,
118
                    XOBJ_DTYPE_ARRAY           => _PROFILE_AM_ARRAY,
119
                    XOBJ_DTYPE_UNICODE_ARRAY   => _PROFILE_AM_UNICODE_ARRAY,
120
                    XOBJ_DTYPE_UNICODE_TXTBOX  => _PROFILE_AM_UNICODE_TXTBOX,
121
                    XOBJ_DTYPE_UNICODE_TXTAREA => _PROFILE_AM_UNICODE_TXTAREA,
122
                    XOBJ_DTYPE_UNICODE_EMAIL   => _PROFILE_AM_UNICODE_EMAIL,
123
                    XOBJ_DTYPE_UNICODE_URL     => _PROFILE_AM_UNICODE_URL);
124
125
                $type_select = new XoopsFormSelect(_PROFILE_AM_VALUETYPE, 'field_valuetype', $field->getVar('field_valuetype', 'e'));
126
                $type_select->addOptionArray($valuetypes);
127
                $form->addElement($type_select);
128
                break;
129
        }
130
131
        //$form->addElement(new XoopsFormRadioYN(_PROFILE_AM_NOTNULL, 'field_notnull', $field->getVar('field_notnull', 'e') ));
132
133
        if ($field->getVar('field_type') === 'select' || $field->getVar('field_type') === 'select_multi' || $field->getVar('field_type') === 'radio' || $field->getVar('field_type') === 'checkbox') {
134
            $options = $field->getVar('field_options');
135
            if (count($options) > 0) {
136
                $remove_options          = new XoopsFormCheckBox(_PROFILE_AM_REMOVEOPTIONS, 'removeOptions');
137
                $remove_options->columns = 3;
138
                asort($options);
139
                foreach (array_keys($options) as $key) {
140
                    $options[$key] .= "[{$key}]";
141
                }
142
                $remove_options->addOptionArray($options);
143
                $form->addElement($remove_options);
144
            }
145
146
            $option_text = "<table  cellspacing='1'><tr><td class='width20'>" . _PROFILE_AM_KEY . '</td><td>' . _PROFILE_AM_VALUE . '</td></tr>';
147
            for ($i = 0; $i < 3; ++$i) {
148
                $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>";
149
                $option_text .= "<tr height='3px'><td colspan='2'> </td></tr>";
150
            }
151
            $option_text .= '</table>';
152
            $form->addElement(new XoopsFormLabel(_PROFILE_AM_ADDOPTION, $option_text));
153
        }
154
    }
155
156
    if ($field->getVar('field_edit')) {
157
        switch ($field->getVar('field_type')) {
158
            case 'textbox':
159
            case 'textarea':
160
            case 'dhtml':
161
                $form->addElement(new XoopsFormText(_PROFILE_AM_MAXLENGTH, 'field_maxlength', 35, 35, $field->getVar('field_maxlength', 'e')));
162
                $form->addElement(new XoopsFormTextArea(_PROFILE_AM_DEFAULT, 'field_default', $field->getVar('field_default', 'e')));
163
                break;
164
165
            case 'checkbox':
166
            case 'select_multi':
167
                $def_value = $field->getVar('field_default', 'e') != null ? unserialize($field->getVar('field_default', 'n')) : null;
168
                $element   = new XoopsFormSelect(_PROFILE_AM_DEFAULT, 'field_default', $def_value, 8, true);
169
                $options   = $field->getVar('field_options');
170
                asort($options);
171
                // If options do not include an empty element, then add a blank option to prevent any default selection
172
                //                if (!in_array('', array_keys($options))) {
173
                if (!array_key_exists('', $options)) {
174
                    $element->addOption('', _NONE);
175
                }
176
                $element->addOptionArray($options);
177
                $form->addElement($element);
178
                break;
179
180
            case 'select':
181
            case 'radio':
182
                $def_value = $field->getVar('field_default', 'e') != null ? $field->getVar('field_default') : null;
183
                $element   = new XoopsFormSelect(_PROFILE_AM_DEFAULT, 'field_default', $def_value);
184
                $options   = $field->getVar('field_options');
185
                asort($options);
186
                // If options do not include an empty element, then add a blank option to prevent any default selection
187
                //                if (!in_array('', array_keys($options))) {
188
                if (!array_key_exists('', $options)) {
189
                    $element->addOption('', _NONE);
190
                }
191
                $element->addOptionArray($options);
192
                $form->addElement($element);
193
                break;
194
195
            case 'date':
196
                $form->addElement(new XoopsFormTextDateSelect(_PROFILE_AM_DEFAULT, 'field_default', 15, $field->getVar('field_default', 'e')));
197
                break;
198
199
            case 'longdate':
200
                $form->addElement(new XoopsFormTextDateSelect(_PROFILE_AM_DEFAULT, 'field_default', 15, strtotime($field->getVar('field_default', 'e'))));
201
                break;
202
203
            case 'datetime':
204
                $form->addElement(new XoopsFormDateTime(_PROFILE_AM_DEFAULT, 'field_default', 15, $field->getVar('field_default', 'e')));
205
                break;
206
207
            case 'yesno':
208
                $form->addElement(new XoopsFormRadioYN(_PROFILE_AM_DEFAULT, 'field_default', $field->getVar('field_default', 'e')));
209
                break;
210
211
            case 'timezone':
212
                $form->addElement(new XoopsFormSelectTimezone(_PROFILE_AM_DEFAULT, 'field_default', $field->getVar('field_default', 'e')));
213
                break;
214
215
            case 'language':
216
                $form->addElement(new XoopsFormSelectLang(_PROFILE_AM_DEFAULT, 'field_default', $field->getVar('field_default', 'e')));
217
                break;
218
219
            case 'group':
220
                $form->addElement(new XoopsFormSelectGroup(_PROFILE_AM_DEFAULT, 'field_default', true, $field->getVar('field_default', 'e')));
221
                break;
222
223
            case 'group_multi':
224
                $form->addElement(new XoopsFormSelectGroup(_PROFILE_AM_DEFAULT, 'field_default', true, unserialize($field->getVar('field_default', 'n')), 5, true));
225
                break;
226
227
            case 'theme':
228
                $form->addElement(new XoopsFormSelectTheme(_PROFILE_AM_DEFAULT, 'field_default', $field->getVar('field_default', 'e')));
229
                break;
230
231
            case 'autotext':
232
                $form->addElement(new XoopsFormTextArea(_PROFILE_AM_DEFAULT, 'field_default', $field->getVar('field_default', 'e')));
233
                break;
234
        }
235
    }
236
    /** @var XoopsGroupPermHandler $groupperm_handler */
237
    $groupperm_handler = xoops_getHandler('groupperm');
238
    $searchable_types  = array(
239
        'textbox',
240
        'select',
241
        'radio',
242
        'yesno',
243
        'date',
244
        'datetime',
245
        'timezone',
246
        'language');
247
    if (in_array($field->getVar('field_type'), $searchable_types)) {
248
        $search_groups = $groupperm_handler->getGroupIds('profile_search', $field->getVar('field_id'), $GLOBALS['xoopsModule']->getVar('mid'));
249
        $form->addElement(new XoopsFormSelectGroup(_PROFILE_AM_PROF_SEARCH, 'profile_search', true, $search_groups, 5, true));
250
    }
251
    if ($field->getVar('field_edit') || $field->isNew()) {
252
        $editable_groups = array();
253
        if (!$field->isNew()) {
254
            //Load groups
255
            $editable_groups = $groupperm_handler->getGroupIds('profile_edit', $field->getVar('field_id'), $GLOBALS['xoopsModule']->getVar('mid'));
256
        }
257
        $form->addElement(new XoopsFormSelectGroup(_PROFILE_AM_PROF_EDITABLE, 'profile_edit', false, $editable_groups, 5, true));
258
        $form->addElement(new XoopsFormRadioYN(_PROFILE_AM_REQUIRED, 'field_required', $field->getVar('field_required', 'e')));
259
        $regstep_select = new XoopsFormSelect(_PROFILE_AM_PROF_REGISTER, 'step_id', $field->getVar('step_id', 'e'));
260
        $regstep_select->addOption(0, _NO);
261
        $regstep_handler = xoops_getModuleHandler('regstep');
262
        $regstep_select->addOptionArray($regstep_handler->getList());
263
        $form->addElement($regstep_select);
264
    }
265
    $form->addElement(new XoopsFormHidden('op', 'save'));
266
    $form->addElement(new XoopsFormButton('', 'submit', _SUBMIT, 'submit'));
267
268
    $options = $field->getVar('field_options');
269
    if (count($options) > 0) {
270
        $linkText = defined('_PROFILE_AM_EDIT_OPTION_STRINGS') ? _PROFILE_AM_EDIT_OPTION_STRINGS : 'Edit Option Strings';
271
        $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>');
272
        $form->addElement($editOptionsButton);
273
    }
274
275
    return $form;
276
}
277
278
function profile_getFieldOptionForm(ProfileField $field, $action = false)
279
{
280
    if ($action === false) {
281
        $action = ''; // $_SERVER['REQUEST_URI'];
282
    }
283
    $title = sprintf(_PROFILE_AM_EDIT, _PROFILE_AM_FIELD);
284
285
    include_once $GLOBALS['xoops']->path('class/xoopsformloader.php');
286
    $form = new XoopsThemeForm($title, 'form', $action, 'post', true);
287
288
    $form->addElement(new XoopsFormLabel(_PROFILE_AM_TITLE, $field->getVar('field_title', 'e')));
289
290
    $options = $field->getVar('field_options');
291
    foreach($options as $name=>$value) {
292
        $form->addElement(new XoopsFormText($name, "field_options[$name]", 80, 255, $value));
293
    }
294
295
    $form->addElement(new XoopsFormHidden('op', 'save-option-strings'));
296
    $form->addElement(new XoopsFormButton('', 'submit', _SUBMIT, 'submit'));
297
298
    return $form;
299
}
300
301
/**
302
 * Get {@link XoopsThemeForm} for registering new users
303
 *
304
 * @param XoopsUser $user
305
 * @param           $profile
306
 * @param XoopsUser $user {@link XoopsUser} to register
307
 * @param int       $step Which step we are at
308
 *
309
 * @internal param \profileRegstep $next_step
310
 * @return object
311
 */
312
function profile_getRegisterForm(XoopsUser $user, $profile, $step = null)
313
{
314
    global $opkey; // should be set in register.php
315
    if (empty($opkey)) {
316
        $opkey = 'profile_opname';
317
    }
318
    $next_opname      = 'op' . mt_rand(10000, 99999);
319
    $_SESSION[$opkey] = $next_opname;
320
321
    include_once $GLOBALS['xoops']->path('class/xoopsformloader.php');
322
    if (empty($GLOBALS['xoopsConfigUser'])) {
323
        /** @var XoopsConfigHandler $config_handler */
324
        $config_handler             = xoops_getHandler('config');
325
        $GLOBALS['xoopsConfigUser'] = $config_handler->getConfigsByCat(XOOPS_CONF_USER);
326
    }
327
    $action    = $_SERVER['REQUEST_URI'];
328
    $step_no   = $step['step_no'];
329
    $use_token = $step['step_no'] > 0;// ? true : false;
330
    $reg_form  = new XoopsThemeForm($step['step_name'], 'regform', $action, 'post', $use_token);
331
332
    if ($step['step_desc']) {
333
        $reg_form->addElement(new XoopsFormLabel('', $step['step_desc']));
334
    }
335
336
    if ($step_no == 1) {
337
        //$uname_size = $GLOBALS['xoopsConfigUser']['maxuname'] < 35 ? $GLOBALS['xoopsConfigUser']['maxuname'] : 35;
338
339
        $elements[0][] = array(
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...
340
            '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

340
            'element'  => new XoopsFormText(_US_NICKNAME, 'uname', 35, $GLOBALS['xoopsConfigUser']['maxuname'], /** @scrutinizer ignore-type */ $user->getVar('uname', 'e')),
Loading history...
341
            'required' => true,
342
			'description' => sprintf(_US_DESCRIPTIONMIN, $GLOBALS['xoopsConfigUser']['minuname']) . '<br>' . sprintf(_US_DESCRIPTIONMAX, $GLOBALS['xoopsConfigUser']['maxuname']));
343
        $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...
344
345
        $elements[0][] = array('element' => new XoopsFormText(_US_EMAIL, 'email', 35, 255, $user->getVar('email', 'e')), 'required' => true);
346
        $weights[0][]  = 0;
347
348
        $elements[0][] = array(
349
			'element' => new XoopsFormPassword(_US_PASSWORD, 'pass', 35, 32, ''),
350
			'required' => true,
351
			'description' => sprintf(_US_DESCRIPTIONMIN, $GLOBALS['xoopsConfigUser']['minpass']));
352
        $weights[0][]  = 0;
353
354
        $elements[0][] = array('element' => new XoopsFormPassword(_US_VERIFYPASS, 'vpass', 35, 32, ''), 'required' => true);
355
        $weights[0][]  = 0;
356
    }
357
358
    // Dynamic fields
359
    $profile_handler              = xoops_getModuleHandler('profile');
360
    $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

360
    /** @scrutinizer ignore-call */ 
361
    $fields                       = $profile_handler->loadFields();
Loading history...
361
    $_SESSION['profile_required'] = array();
362
    foreach (array_keys($fields) as $i) {
363
        if ($fields[$i]->getVar('step_id') == $step['step_id']) {
364
            $fieldinfo['element'] = $fields[$i]->getEditElement($user, $profile);
365
            //assign and check (=)
366
            if ($fieldinfo['required'] = $fields[$i]->getVar('field_required')) {
367
                $_SESSION['profile_required'][$fields[$i]->getVar('field_name')] = $fields[$i]->getVar('field_title');
368
            }
369
370
            $key              = $fields[$i]->getVar('cat_id');
371
            $elements[$key][] = $fieldinfo;
372
            $weights[$key][]  = $fields[$i]->getVar('field_weight');
373
        }
374
    }
375
    ksort($elements);
376
377
    // Get categories
378
    $cat_handler = xoops_getModuleHandler('category');
379
    $categories  = $cat_handler->getObjects(null, true, false);
0 ignored issues
show
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

379
    /** @scrutinizer ignore-call */ 
380
    $categories  = $cat_handler->getObjects(null, true, false);
Loading history...
Unused Code introduced by
The assignment to $categories is dead and can be removed.
Loading history...
380
381
    foreach (array_keys($elements) as $k) {
382
        array_multisort($weights[$k], SORT_ASC, array_keys($elements[$k]), SORT_ASC, $elements[$k]);
0 ignored issues
show
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...
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

382
        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

382
        array_multisort($weights[$k], /** @scrutinizer ignore-type */ SORT_ASC, array_keys($elements[$k]), SORT_ASC, $elements[$k]);
Loading history...
383
        //$title = isset($categories[$k]) ? $categories[$k]['cat_title'] : _PROFILE_MA_DEFAULT;
384
        //$desc = isset($categories[$k]) ? $categories[$k]['cat_description'] : "";
385
        //$reg_form->insertBreak("<p>{$title}</p>{$desc}");
386
        //$reg_form->addElement(new XoopsFormLabel("<h2>".$title."</h2>", $desc), false);
387
        foreach (array_keys($elements[$k]) as $i) {
388
			if (array_key_exists('description', $elements[$k][$i])){
389
				$element = $elements[$k][$i]['element'];
390
				$element->setDescription($elements[$k][$i]['description']);
391
				$reg_form->addElement($element, $elements[$k][$i]['required']);
392
				unset($element);
393
			} else {
394
				$reg_form->addElement($elements[$k][$i]['element'], $elements[$k][$i]['required']);
395
			}
396
        }
397
    }
398
    //end of Dynamic User fields
399
400
    if ($step_no == 1 && $GLOBALS['xoopsConfigUser']['reg_dispdsclmr'] != 0 && $GLOBALS['xoopsConfigUser']['reg_disclaimer'] != '') {
401
        $disc_tray = new XoopsFormElementTray(_US_DISCLAIMER, '<br>');
402
        $disc_text = new XoopsFormLabel('', "<div class=\"pad5\">" . $GLOBALS['myts']->displayTarea($GLOBALS['xoopsConfigUser']['reg_disclaimer'], 1) . '</div>');
403
        $disc_tray->addElement($disc_text);
404
        $agree_chk = new XoopsFormCheckBox('', 'agree_disc');
405
        $agree_chk->addOption(1, _US_IAGREE);
406
        $disc_tray->addElement($agree_chk);
407
        $reg_form->addElement($disc_tray);
408
    }
409
    global $xoopsModuleConfig;
410
    $useCaptchaAfterStep2 = $xoopsModuleConfig['profileCaptchaAfterStep1'];
411
	
412
	if ($step_no == 1) {
413
        $reg_form->addElement(new XoopsFormCaptcha(), true);
414
    } elseif($useCaptchaAfterStep2 == 1){
415
		$reg_form->addElement(new XoopsFormCaptcha(), true);
416
	}
417
418
    $reg_form->addElement(new XoopsFormHidden($next_opname, 'register'));
419
    $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

419
    $reg_form->addElement(new XoopsFormHidden('uid', /** @scrutinizer ignore-type */ $user->getVar('uid')));
Loading history...
420
    $reg_form->addElement(new XoopsFormHidden('step', $step_no));
421
    $reg_form->addElement(new XoopsFormButton('', 'submitButton', _SUBMIT, 'submit'));
422
423
    return $reg_form;
424
}
425
426
/**
427
 * Get {@link XoopsThemeForm} for editing a user
428
 *
429
 * @param XoopsUser           $user {@link XoopsUser} to edit
430
 * @param ProfileProfile|XoopsObject|null $profile
431
 * @param bool                $action
432
 *
433
 * @return object
434
 */
435
function profile_getUserForm(XoopsUser $user, ProfileProfile $profile = null, $action = false)
436
{
437
    if ($action === false) {
438
        $action = $_SERVER['REQUEST_URI'];
439
    }
440
    if (empty($GLOBALS['xoopsConfigUser'])) {
441
        /** @var XoopsConfigHandler $config_handler */
442
        $config_handler             = xoops_getHandler('config');
443
        $GLOBALS['xoopsConfigUser'] = $config_handler->getConfigsByCat(XOOPS_CONF_USER);
444
    }
445
446
    include_once $GLOBALS['xoops']->path('class/xoopsformloader.php');
447
448
    $title = $user->isNew() ? _PROFILE_AM_ADDUSER : _US_EDITPROFILE;
449
450
    $form = new XoopsThemeForm($title, 'userinfo', $action, 'post', true);
451
    /** @var ProfileProfileHandler $profile_handler */
452
    $profile_handler = xoops_getModuleHandler('profile');
453
    // Dynamic fields
454
    if (!$profile) {
455
        /** @var ProfileProfileHandler $profile_handler */
456
        $profile_handler = xoops_getModuleHandler('profile', 'profile');
457
        $profile         = $profile_handler->get($user->getVar('uid'));
458
    }
459
    // Get fields
460
    $fields = $profile_handler->loadFields();
461
    // Get ids of fields that can be edited
462
    /** @var  XoopsGroupPermHandler $gperm_handler */
463
    $gperm_handler   = xoops_getHandler('groupperm');
464
    $editable_fields = $gperm_handler->getItemIds('profile_edit', $GLOBALS['xoopsUser']->getGroups(), $GLOBALS['xoopsModule']->getVar('mid'));
465
466
    if ($user->isNew() || $GLOBALS['xoopsUser']->isAdmin()) {
467
        $elements[0][] = array(
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...
468
            '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

468
            'element'  => new XoopsFormText(_US_NICKNAME, 'uname', 25, $GLOBALS['xoopsUser']->isAdmin() ? 60 : $GLOBALS['xoopsConfigUser']['maxuname'], /** @scrutinizer ignore-type */ $user->getVar('uname', 'e')),
Loading history...
469
            'required' => 1);
470
        $email_text    = new XoopsFormText('', 'email', 30, 60, $user->getVar('email'));
471
    } else {
472
        $elements[0][] = array('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

472
        $elements[0][] = array('element' => new XoopsFormLabel(_US_NICKNAME, /** @scrutinizer ignore-type */ $user->getVar('uname')), 'required' => 0);
Loading history...
473
        $email_text    = new XoopsFormLabel('', $user->getVar('email'));
474
    }
475
    $email_tray = new XoopsFormElementTray(_US_EMAIL, '<br>');
476
    $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

476
    $email_tray->addElement($email_text, /** @scrutinizer ignore-type */ ($user->isNew() || $GLOBALS['xoopsUser']->isAdmin()) ? 1 : 0);
Loading history...
477
    $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...
478
    $elements[0][] = array('element' => $email_tray, 'required' => 0);
479
    $weights[0][]  = 0;
480
481
    if ($GLOBALS['xoopsUser']->isAdmin() && $user->getVar('uid') != $GLOBALS['xoopsUser']->getVar('uid')) {
482
        //If the user is an admin and is editing someone else
483
        $pwd_text  = new XoopsFormPassword('', 'password', 10, 32);
484
        $pwd_text2 = new XoopsFormPassword('', 'vpass', 10, 32);
485
        $pwd_tray  = new XoopsFormElementTray(_US_PASSWORD . '<br>' . _US_TYPEPASSTWICE);
486
        $pwd_tray->addElement($pwd_text);
487
        $pwd_tray->addElement($pwd_text2);
488
        $elements[0][] = array('element' => $pwd_tray, 'required' => 0); //cannot set an element tray required
489
        $weights[0][]  = 0;
490
491
        $level_radio = new XoopsFormRadio(_PROFILE_MA_USERLEVEL, 'level', (string)$user->getVar('level'));
492
        $level_radio->addOption(1, _PROFILE_MA_ACTIVE);
493
        $level_radio->addOption(0, _PROFILE_MA_INACTIVE);
494
        //$level_radio->addOption(-1, _PROFILE_MA_DISABLED);
495
        $elements[0][] = array('element' => $level_radio, 'required' => 0);
496
        $weights[0][]  = 0;
497
    }
498
499
    $elements[0][] = array('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

499
    $elements[0][] = array('element' => new XoopsFormHidden('uid', /** @scrutinizer ignore-type */ $user->getVar('uid')), 'required' => 0);
Loading history...
500
    $weights[0][]  = 0;
501
    $elements[0][] = array('element' => new XoopsFormHidden('op', 'save'), 'required' => 0);
502
    $weights[0][]  = 0;
503
504
    $cat_handler    = xoops_getModuleHandler('category');
505
    $categories     = array();
506
    $all_categories = $cat_handler->getObjects(null, true, false);
507
    $count_fields   = count($fields);
508
509
    foreach (array_keys($fields) as $i) {
510
        if (in_array($fields[$i]->getVar('field_id'), $editable_fields)) {
511
            // Set default value for user fields if available
512
            if ($user->isNew()) {
513
                $default = $fields[$i]->getVar('field_default');
514
                if ($default !== '' && $default !== null) {
515
                    $user->setVar($fields[$i]->getVar('field_name'), $default);
516
                }
517
            }
518
519
            if ($profile->getVar($fields[$i]->getVar('field_name'), 'n') === null) {
520
                $default = $fields[$i]->getVar('field_default', 'n');
521
                $profile->setVar($fields[$i]->getVar('field_name'), $default);
522
            }
523
524
            $fieldinfo['element']  = $fields[$i]->getEditElement($user, $profile);
525
            $fieldinfo['required'] = $fields[$i]->getVar('field_required');
526
527
            $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;
528
            $elements[$key][] = $fieldinfo;
529
            $weights[$key][]  = $fields[$i]->getVar('field_weight');
530
            $categories[$key] = isset($all_categories[$fields[$i]->getVar('cat_id')]) ? $all_categories[$fields[$i]->getVar('cat_id')] : null;
531
        }
532
    }
533
534
    if ($GLOBALS['xoopsUser'] && $GLOBALS['xoopsUser']->isAdmin()) {
535
        xoops_loadLanguage('admin', 'profile');
536
        /** @var  XoopsGroupPermHandler $gperm_handler */
537
        $gperm_handler = xoops_getHandler('groupperm');
538
        //If user has admin rights on groups
539
        include_once $GLOBALS['xoops']->path('modules/system/constants.php');
540
        if ($gperm_handler->checkRight('system_admin', XOOPS_SYSTEM_GROUP, $GLOBALS['xoopsUser']->getGroups(), 1)) {
541
            //add group selection
542
            $group_select  = new XoopsFormSelectGroup(_US_GROUPS, 'groups', false, $user->getGroups(), 5, true);
543
            $elements[0][] = array('element' => $group_select, 'required' => 0);
544
            //set as latest;
545
            $weights[0][] = $count_fields + 1;
546
        }
547
    }
548
549
    ksort($elements);
550
    foreach (array_keys($elements) as $k) {
551
        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

551
        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

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

588
    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...
589
        $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

589
        $form->addElement(new XoopsFormHidden('id', /** @scrutinizer ignore-type */ $step->getVar('step_id')));
Loading history...
590
    }
591
    $form->addElement(new XoopsFormHidden('op', 'save'));
592
    $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

592
    $form->addElement(new XoopsFormText(_PROFILE_AM_STEPNAME, 'step_name', 25, 255, /** @scrutinizer ignore-type */ $step->getVar('step_name', 'e')));
Loading history...
593
    $form->addElement(new XoopsFormText(_PROFILE_AM_STEPINTRO, 'step_desc', 25, 255, $step->getVar('step_desc', 'e')));
594
    $form->addElement(new XoopsFormText(_PROFILE_AM_STEPORDER, 'step_order', 10, 10, $step->getVar('step_order', 'e')));
595
    $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

595
    $form->addElement(new XoopsFormRadioYN(_PROFILE_AM_STEPSAVE, 'step_save', /** @scrutinizer ignore-type */ $step->getVar('step_save', 'e')));
Loading history...
596
    $form->addElement(new XoopsFormButton('', 'submit', _SUBMIT, 'submit'));
597
598
    return $form;
599
}
600