Passed
Branch master (410c7b)
by Michael
03:30
created

yogurt_getFieldForm()   F

Complexity

Conditions 43
Paths > 20000

Size

Total Lines 230
Code Lines 186

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 186
dl 0
loc 230
rs 0
c 1
b 0
f 0
cc 43
nc 37200
nop 2

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
 * @param mixed $action
13
 * @license             GNU GPL 2 (http://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
 * @copyright       (c) 2000-2016 XOOPS Project (www.xoops.org)
19
 */
20
21
// defined('XOOPS_ROOT_PATH') || exit("XOOPS root path not defined");
22
23
/**
24
 * Get {@link XoopsThemeForm} for adding/editing fields
25
 *
26
 * @param Suico\Field $field  {@link Suico\Field} object to get edit form for
27
 * @param mixed        $action URL to submit to - or false for $_SERVER['REQUEST_URI']
28
 *
29
 * @return object
30
 */
31
32
use XoopsModules\Suico;
33
34
/**
35
 * @param Suico\Field $field
36
 * @param bool         $action
37
 * @return \XoopsThemeForm
38
 */
39
function suico_getFieldForm(Suico\Field $field, $action = false)
40
{
41
    if (!$action) {
42
        $action = $_SERVER['REQUEST_URI'];
43
    }
44
    $title = $field->isNew() ? sprintf(_AM_SUICO_ADD, _AM_SUICO_FIELD) : sprintf(_AM_SUICO_EDIT, _AM_SUICO_FIELD);
45
46
    include_once $GLOBALS['xoops']->path('class/xoopsformloader.php');
47
    $form = new XoopsThemeForm($title, 'form', $action, 'post', true);
48
49
    $form->addElement(new XoopsFormText(_AM_SUICO_TITLE, 'field_title', 35, 255, $field->getVar('field_title', 'e')));
50
    $form->addElement(new XoopsFormTextArea(_AM_SUICO_DESCRIPTION, 'field_description', $field->getVar('field_description', 'e')));
51
52
    $fieldcat_id = 0;
53
    if (!$field->isNew()) {
54
        $fieldcat_id = $field->getVar('cat_id');
55
    }
56
    $categoryHandler = \XoopsModules\Suico\Helper::getInstance()->getHandler('Category');
57
    $cat_select       = new XoopsFormSelect(_AM_SUICO_CATEGORY, 'field_category', $fieldcat_id);
58
    $cat_select->addOption(0, _AM_SUICO_DEFAULT);
59
    $cat_select->addOptionArray($categoryHandler->getList());
60
    $form->addElement($cat_select);
61
    $form->addElement(new XoopsFormText(_AM_SUICO_WEIGHT, 'field_weight', 10, 10, $field->getVar('field_weight', 'e')));
62
    if ($field->getVar('field_config') || $field->isNew()) {
63
        if (!$field->isNew()) {
64
            $form->addElement(new XoopsFormLabel(_AM_SUICO_NAME, $field->getVar('field_name')));
65
            $form->addElement(new XoopsFormHidden('id', $field->getVar('field_id')));
66
        } else {
67
            $form->addElement(new XoopsFormText(_AM_SUICO_NAME, 'field_name', 35, 255, $field->getVar('field_name', 'e')));
68
        }
69
70
        //autotext and theme left out of this one as fields of that type should never be changed (valid assumption, I think)
71
        $fieldtypes = [
72
            'checkbox'     => _AM_SUICO_CHECKBOX,
73
            'date'         => _AM_SUICO_DATE,
74
            'datetime'     => _AM_SUICO_DATETIME,
75
            'longdate'     => _AM_SUICO_LONGDATE,
76
            'group'        => _AM_SUICO_GROUP,
77
            'group_multi'  => _AM_SUICO_GROUPMULTI,
78
            'language'     => _AM_SUICO_LANGUAGE,
79
            'radio'        => _AM_SUICO_RADIO,
80
            'select'       => _AM_SUICO_SELECT,
81
            'select_multi' => _AM_SUICO_SELECTMULTI,
82
            'textarea'     => _AM_SUICO_TEXTAREA,
83
            'dhtml'        => _AM_SUICO_DHTMLTEXTAREA,
84
            'textbox'      => _AM_SUICO_TEXTBOX,
85
            'timezone'     => _AM_SUICO_TIMEZONE,
86
            'yesno'        => _AM_SUICO_YESNO,
87
        ];
88
89
        $element_select = new XoopsFormSelect(_AM_SUICO_TYPE, 'field_type', $field->getVar('field_type', 'e'));
90
        $element_select->addOptionArray($fieldtypes);
91
92
        $form->addElement($element_select);
93
94
        switch ($field->getVar('field_type')) {
95
            case 'textbox':
96
                $valuetypes = [
97
                    XOBJ_DTYPE_TXTBOX          => _AM_SUICO_TXTBOX,
98
                    XOBJ_DTYPE_EMAIL           => _AM_SUICO_EMAIL,
99
                    XOBJ_DTYPE_INT             => _AM_SUICO_INT,
100
                    XOBJ_DTYPE_FLOAT           => _AM_SUICO_FLOAT,
101
                    XOBJ_DTYPE_DECIMAL         => _AM_SUICO_DECIMAL,
102
                    XOBJ_DTYPE_TXTAREA         => _AM_SUICO_TXTAREA,
103
                    XOBJ_DTYPE_URL             => _AM_SUICO_URL,
104
                    XOBJ_DTYPE_OTHER           => _AM_SUICO_OTHER,
105
                    XOBJ_DTYPE_ARRAY           => _AM_SUICO_ARRAY,
106
                    XOBJ_DTYPE_UNICODE_ARRAY   => _AM_SUICO_UNICODE_ARRAY,
107
                    XOBJ_DTYPE_UNICODE_TXTBOX  => _AM_SUICO_UNICODE_TXTBOX,
108
                    XOBJ_DTYPE_UNICODE_TXTAREA => _AM_SUICO_UNICODE_TXTAREA,
109
                    XOBJ_DTYPE_UNICODE_EMAIL   => _AM_SUICO_UNICODE_EMAIL,
110
                    XOBJ_DTYPE_UNICODE_URL     => _AM_SUICO_UNICODE_URL,
111
                ];
112
113
                $type_select = new XoopsFormSelect(_AM_SUICO_VALUETYPE, 'field_valuetype', $field->getVar('field_valuetype', 'e'));
114
                $type_select->addOptionArray($valuetypes);
115
                $form->addElement($type_select);
116
                break;
117
            case 'select':
118
            case 'radio':
119
                $valuetypes = [
120
                    XOBJ_DTYPE_TXTBOX          => _AM_SUICO_TXTBOX,
121
                    XOBJ_DTYPE_EMAIL           => _AM_SUICO_EMAIL,
122
                    XOBJ_DTYPE_INT             => _AM_SUICO_INT,
123
                    XOBJ_DTYPE_FLOAT           => _AM_SUICO_FLOAT,
124
                    XOBJ_DTYPE_DECIMAL         => _AM_SUICO_DECIMAL,
125
                    XOBJ_DTYPE_TXTAREA         => _AM_SUICO_TXTAREA,
126
                    XOBJ_DTYPE_URL             => _AM_SUICO_URL,
127
                    XOBJ_DTYPE_OTHER           => _AM_SUICO_OTHER,
128
                    XOBJ_DTYPE_ARRAY           => _AM_SUICO_ARRAY,
129
                    XOBJ_DTYPE_UNICODE_ARRAY   => _AM_SUICO_UNICODE_ARRAY,
130
                    XOBJ_DTYPE_UNICODE_TXTBOX  => _AM_SUICO_UNICODE_TXTBOX,
131
                    XOBJ_DTYPE_UNICODE_TXTAREA => _AM_SUICO_UNICODE_TXTAREA,
132
                    XOBJ_DTYPE_UNICODE_EMAIL   => _AM_SUICO_UNICODE_EMAIL,
133
                    XOBJ_DTYPE_UNICODE_URL     => _AM_SUICO_UNICODE_URL,
134
                ];
135
136
                $type_select = new XoopsFormSelect(_AM_SUICO_VALUETYPE, 'field_valuetype', $field->getVar('field_valuetype', 'e'));
137
                $type_select->addOptionArray($valuetypes);
138
                $form->addElement($type_select);
139
                break;
140
        }
141
142
        //$form->addElement(new XoopsFormRadioYN(_AM_SUICO_NOTNULL, 'field_notnull', $field->getVar('field_notnull', 'e') ));
143
144
        if ('select' === $field->getVar('field_type') || 'select_multi' === $field->getVar('field_type') || 'radio' === $field->getVar('field_type') || 'checkbox' === $field->getVar('field_type')) {
145
            $options = $field->getVar('field_options');
146
            if (count($options) > 0) {
147
                $remove_options          = new XoopsFormCheckBox(_AM_SUICO_REMOVEOPTIONS, 'removeOptions');
148
                $remove_options->columns = 3;
149
                asort($options);
150
                foreach (array_keys($options) as $key) {
151
                    $options[$key] .= "[{$key}]";
152
                }
153
                $remove_options->addOptionArray($options);
154
                $form->addElement($remove_options);
155
            }
156
157
            $option_text = "<table  cellspacing='1'><tr><td class='width20'>" . _AM_SUICO_KEY . '</td><td>' . _AM_SUICO_VALUE . '</td></tr>';
158
            for ($i = 0; $i < 3; ++$i) {
159
                $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>";
160
                $option_text .= "<tr height='3px'><td colspan='2'> </td></tr>";
161
            }
162
            $option_text .= '</table>';
163
            $form->addElement(new XoopsFormLabel(_AM_SUICO_ADDOPTION, $option_text));
164
        }
165
    }
166
167
    if ($field->getVar('field_edit')) {
168
        switch ($field->getVar('field_type')) {
169
            case 'textbox':
170
            case 'textarea':
171
            case 'dhtml':
172
                $form->addElement(new XoopsFormText(_AM_SUICO_MAXLENGTH, 'field_maxlength', 35, 35, $field->getVar('field_maxlength', 'e')));
173
                $form->addElement(new XoopsFormTextArea(_AM_SUICO_DEFAULT, 'field_default', $field->getVar('field_default', 'e')));
174
                break;
175
            case 'checkbox':
176
            case 'select_multi':
177
                $def_value = null != $field->getVar('field_default', 'e') ? unserialize($field->getVar('field_default', 'n')) : null;
178
                $element   = new XoopsFormSelect(_AM_SUICO_DEFAULT, 'field_default', $def_value, 8, true);
179
                $options   = $field->getVar('field_options');
180
                asort($options);
181
                // If options do not include an empty element, then add a blank option to prevent any default selection
182
                //                if (!in_array('', array_keys($options))) {
183
                if (!array_key_exists('', $options)) {
184
                    $element->addOption('', _NONE);
185
                }
186
                $element->addOptionArray($options);
187
                $form->addElement($element);
188
                break;
189
            case 'select':
190
            case 'radio':
191
                $def_value = null != $field->getVar('field_default', 'e') ? $field->getVar('field_default') : null;
192
                $element   = new XoopsFormSelect(_AM_SUICO_DEFAULT, 'field_default', $def_value);
193
                $options   = $field->getVar('field_options');
194
                asort($options);
195
                // If options do not include an empty element, then add a blank option to prevent any default selection
196
                //                if (!in_array('', array_keys($options))) {
197
                if (!array_key_exists('', $options)) {
198
                    $element->addOption('', _NONE);
199
                }
200
                $element->addOptionArray($options);
201
                $form->addElement($element);
202
                break;
203
            case 'date':
204
                $form->addElement(new XoopsFormTextDateSelect(_AM_SUICO_DEFAULT, 'field_default', 15, $field->getVar('field_default', 'e')));
205
                break;
206
            case 'longdate':
207
                $form->addElement(new XoopsFormTextDateSelect(_AM_SUICO_DEFAULT, 'field_default', 15, strtotime($field->getVar('field_default', 'e'))));
208
                break;
209
            case 'datetime':
210
                $form->addElement(new XoopsFormDateTime(_AM_SUICO_DEFAULT, 'field_default', 15, $field->getVar('field_default', 'e')));
211
                break;
212
            case 'yesno':
213
                $form->addElement(new XoopsFormRadioYN(_AM_SUICO_DEFAULT, 'field_default', $field->getVar('field_default', 'e')));
214
                break;
215
            case 'timezone':
216
                $form->addElement(new XoopsFormSelectTimezone(_AM_SUICO_DEFAULT, 'field_default', $field->getVar('field_default', 'e')));
217
                break;
218
            case 'language':
219
                $form->addElement(new XoopsFormSelectLang(_AM_SUICO_DEFAULT, 'field_default', $field->getVar('field_default', 'e')));
220
                break;
221
            case 'group':
222
                $form->addElement(new XoopsFormSelectGroup(_AM_SUICO_DEFAULT, 'field_default', true, $field->getVar('field_default', 'e')));
223
                break;
224
            case 'group_multi':
225
                $form->addElement(new XoopsFormSelectGroup(_AM_SUICO_DEFAULT, 'field_default', true, unserialize($field->getVar('field_default', 'n')), 5, true));
226
                break;
227
            case 'theme':
228
                $form->addElement(new XoopsFormSelectTheme(_AM_SUICO_DEFAULT, 'field_default', $field->getVar('field_default', 'e')));
229
                break;
230
            case 'autotext':
231
                $form->addElement(new XoopsFormTextArea(_AM_SUICO_DEFAULT, 'field_default', $field->getVar('field_default', 'e')));
232
                break;
233
        }
234
    }
235
    /* @var XoopsGroupPermHandler $grouppermHandler */
236
    $grouppermHandler = xoops_getHandler('groupperm');
237
    $searchable_types  = [
238
        'textbox',
239
        'select',
240
        'radio',
241
        'yesno',
242
        'date',
243
        'datetime',
244
        'timezone',
245
        'language',
246
    ];
247
    if (in_array($field->getVar('field_type'), $searchable_types)) {
248
        $search_groups = $grouppermHandler->getGroupIds('profile_search', $field->getVar('field_id'), $GLOBALS['xoopsModule']->getVar('mid'));
249
        $form->addElement(new XoopsFormSelectGroup(_AM_SUICO_PROF_SEARCH, 'profile_search', true, $search_groups, 5, true));
250
    }
251
    if ($field->getVar('field_edit') || $field->isNew()) {
252
        $editable_groups = [];
253
        if (!$field->isNew()) {
254
            //Load groups
255
            $editable_groups = $grouppermHandler->getGroupIds('profile_edit', $field->getVar('field_id'), $GLOBALS['xoopsModule']->getVar('mid'));
256
        }
257
        $form->addElement(new XoopsFormSelectGroup(_AM_SUICO_PROF_EDITABLE, 'profile_edit', false, $editable_groups, 5, true));
258
        $form->addElement(new XoopsFormRadioYN(_AM_SUICO_REQUIRED, 'field_required', $field->getVar('field_required', 'e')));
259
        $regstep_select = new XoopsFormSelect(_AM_SUICO_PROF_REGISTER, 'step_id', $field->getVar('step_id', 'e'));
260
        $regstep_select->addOption(0, _NO);
261
        $regstepHandler = \XoopsModules\Suico\Helper::getInstance()->getHandler('Regstep');
262
        $regstep_select->addOptionArray($regstepHandler->getList());
263
        $form->addElement($regstep_select);
264
    }
265
    $form->addElement(new XoopsFormHidden('op', 'save'));
266
    $form->addElement(new XoopsFormButton('', 'submit', _SUBMIT, 'submit'));
267
268
    return $form;
269
}
270
271
/**
272
 * Get {@link XoopsThemeForm} for registering new users
273
 *
274
 * @param           $profile
275
 * @param XoopsUser $user {@link XoopsUser} to register
276
 * @param int       $step Which step we are at
277
 *
278
 * @return object
279
 * @internal param \profileRegstep $next_step
280
 */
281
function suico_getRegisterForm(XoopsUser $user, $profile, $step = null)
282
{
283
    global $opkey; // should be set in register.php
284
    if (empty($opkey)) {
285
        $opkey = 'profile_opname';
286
    }
287
    $next_opname      = 'op' . mt_rand(10000, 99999);
288
    $_SESSION[$opkey] = $next_opname;
289
290
    include_once $GLOBALS['xoops']->path('class/xoopsformloader.php');
291
    if (empty($GLOBALS['xoopsConfigUser'])) {
292
        /* @var XoopsConfigHandler $configHandler */
293
        $configHandler             = xoops_getHandler('config');
294
        $GLOBALS['xoopsConfigUser'] = $configHandler->getConfigsByCat(XOOPS_CONF_USER);
295
    }
296
    $action    = $_SERVER['REQUEST_URI'];
297
    $step_no   = $step['step_no'];
298
    $use_token = $step['step_no'] > 0; // ? true : false;
299
    $reg_form  = new XoopsThemeForm($step['step_name'], 'regform', $action, 'post', $use_token);
300
301
    if ($step['step_desc']) {
302
        $reg_form->addElement(new XoopsFormLabel('', $step['step_desc']));
303
    }
304
305
    if (1 == $step_no) {
306
        //$uname_size = $GLOBALS['xoopsConfigUser']['maxuname'] < 35 ? $GLOBALS['xoopsConfigUser']['maxuname'] : 35;
307
308
        $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...
309
            'element'  => new XoopsFormText(_MD_SUICO_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

309
            'element'  => new XoopsFormText(_MD_SUICO_NICKNAME, 'uname', 35, $GLOBALS['xoopsConfigUser']['maxuname'], /** @scrutinizer ignore-type */ $user->getVar('uname', 'e')),
Loading history...
310
            'required' => true,
311
        ];
312
        $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...
313
314
        $elements[0][] = ['element' => new XoopsFormText(_MD_SUICO_EMAILADDRESS, 'email', 35, 255, $user->getVar('email', 'e')), 'required' => true];
315
        $weights[0][]  = 0;
316
317
        $elements[0][] = ['element' => new XoopsFormPassword(_MD_SUICO_PASSWORD, 'pass', 35, 32, ''), 'required' => true];
318
        $weights[0][]  = 0;
319
320
        $elements[0][] = ['element' => new XoopsFormPassword(_US_VERIFYPASS, 'vpass', 35, 32, ''), 'required' => true];
321
        $weights[0][]  = 0;
322
    }
323
324
    // Dynamic fields
325
    $profileHandler              = \XoopsModules\Suico\Helper::getInstance()->getHandler('Profile');
326
    $fields                       = $profileHandler->loadFields();
327
    $_SESSION['profile_required'] = [];
328
    foreach (array_keys($fields) as $i) {
329
        if ($fields[$i]->getVar('step_id') == $step['step_id']) {
330
            $fieldinfo['element'] = $fields[$i]->getEditElement($user, $profile);
331
            //assign and check (=)
332
            if ($fieldinfo['required'] = $fields[$i]->getVar('field_required')) {
333
                $_SESSION['profile_required'][$fields[$i]->getVar('field_name')] = $fields[$i]->getVar('field_title');
334
            }
335
336
            $key              = $fields[$i]->getVar('cat_id');
337
            $elements[$key][] = $fieldinfo;
338
            $weights[$key][]  = $fields[$i]->getVar('field_weight');
339
        }
340
    }
341
    ksort($elements);
342
343
    // Get categories
344
    $categoryHandler = \XoopsModules\Suico\Helper::getInstance()->getHandler('Category');
345
    $categories  = $categoryHandler->getObjects(null, true, false);
0 ignored issues
show
Unused Code introduced by
The assignment to $categories is dead and can be removed.
Loading history...
346
347
    foreach (array_keys($elements) as $k) {
348
        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...
349
        //$title = isset($categories[$k]) ? $categories[$k]['cat_title'] : _MD_SUICO_DEFAULT;
350
        //$desc = isset($categories[$k]) ? $categories[$k]['cat_description'] : "";
351
        //$reg_form->insertBreak("<p>{$title}</p>{$desc}");
352
        //$reg_form->addElement(new XoopsFormLabel("<h2>".$title."</h2>", $desc), false);
353
        foreach (array_keys($elements[$k]) as $i) {
354
            $reg_form->addElement($elements[$k][$i]['element'], $elements[$k][$i]['required']);
355
        }
356
    }
357
    //end of Dynamic User fields
358
359
    if (1 == $step_no && 0 != $GLOBALS['xoopsConfigUser']['reg_dispdsclmr'] && '' != $GLOBALS['xoopsConfigUser']['reg_disclaimer']) {
360
        $disc_tray = new XoopsFormElementTray(_US_DISCLAIMER, '<br>');
361
        $disc_text = new XoopsFormLabel('', '<div class="pad5">' . $GLOBALS['myts']->displayTarea($GLOBALS['xoopsConfigUser']['reg_disclaimer'], 1) . '</div>');
362
        $disc_tray->addElement($disc_text);
363
        $agree_chk = new XoopsFormCheckBox('', 'agree_disc');
364
        $agree_chk->addOption(1, _US_IAGREE);
365
        $disc_tray->addElement($agree_chk);
366
        $reg_form->addElement($disc_tray);
367
    }
368
    global $xoopsModuleConfig;
369
    $useCaptchaAfterStep2 = $xoopsModuleConfig['profileCaptchaAfterStep1'] + 1;
370
371
    if ($step_no <= $useCaptchaAfterStep2) {
372
        $reg_form->addElement(new XoopsFormCaptcha(), true);
373
    }
374
375
    $reg_form->addElement(new XoopsFormHidden($next_opname, 'register'));
376
    $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

376
    $reg_form->addElement(new XoopsFormHidden('uid', /** @scrutinizer ignore-type */ $user->getVar('uid')));
Loading history...
377
    $reg_form->addElement(new XoopsFormHidden('step', $step_no));
378
    $reg_form->addElement(new XoopsFormButton('', 'submitButton', _SUBMIT, 'submit'));
379
380
    return $reg_form;
381
}
382
383
/**
384
 * Get {@link XoopsThemeForm} for editing a user
385
 *
386
 * @param XoopsUser                    $user {@link XoopsUser} to edit
387
 * @param \XoopsModules\Suico\Profile $profile
388
 * @param bool                         $action
389
 *
390
 * @return object
391
 */
392
function suico_getUserForm(XoopsUser $user, Suico\Profile $profile = null, $action = false)
393
{
394
    $helper = \XoopsModules\Suico\Helper::getInstance();
0 ignored issues
show
Unused Code introduced by
The assignment to $helper is dead and can be removed.
Loading history...
395
    if (!$action) {
396
        $action = $_SERVER['REQUEST_URI'];
397
    }
398
    if (empty($GLOBALS['xoopsConfigUser'])) {
399
        /* @var XoopsConfigHandler $configHandler */
400
        $configHandler             = xoops_getHandler('config');
401
        $GLOBALS['xoopsConfigUser'] = $configHandler->getConfigsByCat(XOOPS_CONF_USER);
402
    }
403
404
    require_once $GLOBALS['xoops']->path('class/xoopsformloader.php');
405
406
    $title = $user->isNew() ? _AM_SUICO_ADDUSER : _US_EDITPROFILE;
407
408
    $form = new XoopsThemeForm($title, 'userinfo', $action, 'post', true);
409
410
    /* @var ProfileHandler $profileHandler */
411
412
    $profileHandler = \XoopsModules\Suico\Helper::getInstance()->getHandler('Profile');
413
    // Dynamic fields
414
    if (!$profile) {
415
        /* @var ProfileHandler $profileHandler */
416
417
        $profileHandler = \XoopsModules\Suico\Helper::getInstance()->getHandler('Profile');
418
        $profile         = $profileHandler->get($user->getVar('uid'));
419
    }
420
    // Get fields
421
    $fields = $profileHandler->loadFields();
422
    // Get ids of fields that can be edited
423
    /* @var  XoopsGroupPermHandler $grouppermHandler */
424
    $grouppermHandler = xoops_getHandler('groupperm');
425
    $editable_fields  = $grouppermHandler->getItemIds('profile_edit', $GLOBALS['xoopsUser']->getGroups(), $GLOBALS['xoopsModule']->getVar('mid'));
426
427
    if ($user->isNew() || $GLOBALS['xoopsUser']->isAdmin()) {
428
        $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...
429
            'element'  => new XoopsFormText(_MD_SUICO_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

429
            'element'  => new XoopsFormText(_MD_SUICO_NICKNAME, 'uname', 25, $GLOBALS['xoopsUser']->isAdmin() ? 60 : $GLOBALS['xoopsConfigUser']['maxuname'], /** @scrutinizer ignore-type */ $user->getVar('uname', 'e')),
Loading history...
430
            'required' => 1,
431
        ];
432
        $email_text    = new XoopsFormText('', 'email', 30, 60, $user->getVar('email'));
433
    } else {
434
        $elements[0][] = ['element' => new XoopsFormLabel(_MD_SUICO_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

434
        $elements[0][] = ['element' => new XoopsFormLabel(_MD_SUICO_NICKNAME, /** @scrutinizer ignore-type */ $user->getVar('uname')), 'required' => 0];
Loading history...
435
        $email_text    = new XoopsFormLabel('', $user->getVar('email'));
436
    }
437
    $email_tray = new XoopsFormElementTray(_MD_SUICO_EMAILADDRESS, '<br>');
438
    $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

438
    $email_tray->addElement($email_text, /** @scrutinizer ignore-type */ ($user->isNew() || $GLOBALS['xoopsUser']->isAdmin()) ? 1 : 0);
Loading history...
439
    $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...
440
    $elements[0][] = ['element' => $email_tray, 'required' => 0];
441
    $weights[0][]  = 0;
442
443
    if ($GLOBALS['xoopsUser']->isAdmin() && $user->getVar('uid') != $GLOBALS['xoopsUser']->getVar('uid')) {
444
        //If the user is an admin and is editing someone else
445
        $pwd_text  = new XoopsFormPassword('', 'password', 10, 32);
446
        $pwd_text2 = new XoopsFormPassword('', 'vpass', 10, 32);
447
        $pwd_tray  = new XoopsFormElementTray(_MD_SUICO_PASSWORD . '<br>' . _MD_SUICO_CONFIRMPASSWORD);
448
        $pwd_tray->addElement($pwd_text);
449
        $pwd_tray->addElement($pwd_text2);
450
        $elements[0][] = ['element' => $pwd_tray, 'required' => 0]; //cannot set an element tray required
451
        $weights[0][]  = 0;
452
453
        $level_radio = new XoopsFormRadio(_MD_SUICO_USERLEVEL, 'level', $user->getVar('level'));
0 ignored issues
show
Bug introduced by
It seems like $user->getVar('level') can also be of type array and array; however, parameter $value of XoopsFormRadio::__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

453
        $level_radio = new XoopsFormRadio(_MD_SUICO_USERLEVEL, 'level', /** @scrutinizer ignore-type */ $user->getVar('level'));
Loading history...
454
        $level_radio->addOption(1, _MD_SUICO_ACTIVE);
455
        $level_radio->addOption(0, _MD_SUICO_INACTIVE);
456
        //$level_radio->addOption(-1, _MD_SUICO_DISABLED);
457
        $elements[0][] = ['element' => $level_radio, 'required' => 0];
458
        $weights[0][]  = 0;
459
    }
460
461
    $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

461
    $elements[0][] = ['element' => new XoopsFormHidden('uid', /** @scrutinizer ignore-type */ $user->getVar('uid')), 'required' => 0];
Loading history...
462
    $weights[0][]  = 0;
463
    $elements[0][] = ['element' => new XoopsFormHidden('op', 'save'), 'required' => 0];
464
    $weights[0][]  = 0;
465
466
    $categoryHandler    = \XoopsModules\Suico\Helper::getInstance()->getHandler('Category');
467
    $categories     = [];
468
    $all_categories = $categoryHandler->getObjects(null, true, false);
469
    $count_fields   = count($fields);
470
471
    foreach (array_keys($fields) as $i) {
472
        if (in_array($fields[$i]->getVar('field_id'), $editable_fields)) {
473
            // Set default value for user fields if available
474
            if ($user->isNew()) {
475
                $default = $fields[$i]->getVar('field_default');
476
                if ('' !== $default && null !== $default) {
477
                    $user->setVar($fields[$i]->getVar('field_name'), $default);
478
                }
479
            }
480
481
            if (null === $profile->getVar($fields[$i]->getVar('field_name'), 'n')) {
482
                $default = $fields[$i]->getVar('field_default', 'n');
483
                $profile->setVar($fields[$i]->getVar('field_name'), $default);
484
            }
485
486
            $fieldinfo['element']  = $fields[$i]->getEditElement($user, $profile);
487
            $fieldinfo['required'] = $fields[$i]->getVar('field_required');
488
489
            $key              = @$all_categories[$fields[$i]->getVar('cat_id')]['cat_weight'] * $count_fields + $fields[$i]->getVar('cat_id');
490
            $elements[$key][] = $fieldinfo;
491
            $weights[$key][]  = $fields[$i]->getVar('field_weight');
492
            $categories[$key] = @$all_categories[$fields[$i]->getVar('cat_id')];
493
        }
494
    }
495
496
    if ($GLOBALS['xoopsUser'] && $GLOBALS['xoopsUser']->isAdmin()) {
497
        xoops_loadLanguage('admin', 'profile');
498
        /* @var  XoopsGroupPermHandler $grouppermHandler */
499
        $grouppermHandler = xoops_getHandler('groupperm');
500
        //If user has admin rights on groups
501
        include_once $GLOBALS['xoops']->path('modules/system/constants.php');
502
        if ($grouppermHandler->checkRight('system_admin', XOOPS_SYSTEM_GROUP, $GLOBALS['xoopsUser']->getGroups(), 1)) {
503
            //add group selection
504
            $group_select  = new XoopsFormSelectGroup(_MD_SUICO_USERGROUPS, 'groups', false, $user->getGroups(), 5, true);
505
            $elements[0][] = ['element' => $group_select, 'required' => 0];
506
            //set as latest;
507
            $weights[0][] = $count_fields + 1;
508
        }
509
    }
510
511
    ksort($elements);
512
    foreach (array_keys($elements) as $k) {
513
        array_multisort($weights[$k], SORT_ASC, array_keys($elements[$k]), SORT_ASC, $elements[$k]);
514
        $title = isset($categories[$k]) ? $categories[$k]['cat_title'] : _MD_SUICO_DEFAULT;
515
        $desc  = isset($categories[$k]) ? $categories[$k]['cat_description'] : '';
516
        $form->addElement(new XoopsFormLabel("<h3>{$title}</h3>", $desc), false);
517
        foreach (array_keys($elements[$k]) as $i) {
518
            $form->addElement($elements[$k][$i]['element'], $elements[$k][$i]['required']);
519
        }
520
    }
521
522
    $form->addElement(new XoopsFormHidden('uid', $user->getVar('uid')));
523
    $form->addElement(new XoopsFormButton('', 'submit', _MD_SUICO_SAVECHANGES, 'submit'));
524
525
    return $form;
526
}
527
528
/**
529
 * Get {@link XoopsThemeForm} for editing a step
530
 *
531
 * @param \XoopsModules\Suico\Regstep $step {@link Regstep} to edit
532
 * @param bool                         $action
533
 *
534
 * @return object
535
 */
536
function suico_getStepForm(Suico\Regstep $step = null, $action = false)
537
{
538
    if (!$action) {
539
        $action = $_SERVER['REQUEST_URI'];
0 ignored issues
show
Unused Code introduced by
The assignment to $action is dead and can be removed.
Loading history...
540
    }
541
    if (empty($GLOBALS['xoopsConfigUser'])) {
542
        /* @var XoopsConfigHandler $configHandler */
543
        $configHandler             = xoops_getHandler('config');
544
        $GLOBALS['xoopsConfigUser'] = $configHandler->getConfigsByCat(XOOPS_CONF_USER);
545
    }
546
    include_once $GLOBALS['xoops']->path('class/xoopsformloader.php');
547
548
    $form = new XoopsThemeForm(_AM_SUICO_STEP, 'stepform', 'step.php', 'post', true);
549
550
    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

550
    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...
551
        $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

551
        $form->addElement(new XoopsFormHidden('id', /** @scrutinizer ignore-type */ $step->getVar('step_id')));
Loading history...
552
    }
553
    $form->addElement(new XoopsFormHidden('op', 'save'));
554
    $form->addElement(new XoopsFormText(_AM_SUICO_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

554
    $form->addElement(new XoopsFormText(_AM_SUICO_STEPNAME, 'step_name', 25, 255, /** @scrutinizer ignore-type */ $step->getVar('step_name', 'e')));
Loading history...
555
    $form->addElement(new XoopsFormText(_AM_SUICO_STEPINTRO, 'step_desc', 25, 255, $step->getVar('step_desc', 'e')));
556
    $form->addElement(new XoopsFormText(_AM_SUICO_STEPORDER, 'step_order', 10, 10, $step->getVar('step_order', 'e')));
557
    $form->addElement(new XoopsFormRadioYN(_AM_SUICO_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

557
    $form->addElement(new XoopsFormRadioYN(_AM_SUICO_STEPSAVE, 'step_save', /** @scrutinizer ignore-type */ $step->getVar('step_save', 'e')));
Loading history...
558
    $form->addElement(new XoopsFormButton('', 'submit', _SUBMIT, 'submit'));
559
560
    return $form;
561
}
562