Passed
Push — master ( 95e0c4...fc8ab0 )
by
unknown
44s queued 15s
created

UserForm   A

Complexity

Total Complexity 25

Size/Duplication

Total Lines 116
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 25
eloc 84
c 1
b 0
f 0
dl 0
loc 116
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
F __construct() 0 114 25
1
<?php
2
3
namespace XoopsModules\Suico\Form;
4
5
6
use XoopsModules\Suico;
7
use XoopsModules\Suico\Profile;
8
use XoopsModules\Suico\ProfileHandler;
9
use XoopsThemeForm;
10
use XoopsFormButton;
11
use XoopsFormHidden;
12
use XoopsFormLabel;
13
use XoopsFormSelectUser;
14
15
/**
16
 * Get {@link XoopsThemeForm} for editing a user
17
 *
18
 * @param \XoopsUser                   $user {@link \XoopsUser} to edit
19
 * @param Profile $profile
20
 * @param bool                        $action
21
 *
22
 */
23
24
class UserForm extends XoopsThemeForm
25
{
26
    function __construct(\XoopsUser $user, Profile $profile = null, $action = false)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
27
    {
28
        $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...
29
        if (!$action) {
30
            $action = $_SERVER['REQUEST_URI'];
31
        }
32
        if (empty($GLOBALS['xoopsConfigUser'])) {
33
            /* @var \XoopsConfigHandler $configHandler */
34
            $configHandler              = xoops_getHandler('config');
35
            $GLOBALS['xoopsConfigUser'] = $configHandler->getConfigsByCat(XOOPS_CONF_USER);
36
        }
37
        require_once $GLOBALS['xoops']->path('class/xoopsformloader.php');
38
        $title = $user->isNew() ? _AM_SUICO_ADDUSER : _US_EDITPROFILE;
39
        parent::__construct($title, 'userinfo', $action, 'post', true);
40
        /* @var ProfileHandler $profileHandler */
41
        $profileHandler = \XoopsModules\Suico\Helper::getInstance()->getHandler('Profile');
42
        // Dynamic fields
43
        if (!$profile) {
44
            /* @var ProfileHandler $profileHandler */
45
            $profileHandler = \XoopsModules\Suico\Helper::getInstance()->getHandler('Profile');
46
            $profile        = $profileHandler->get($user->getVar('uid'));
47
        }
48
        // Get fields
49
        $fields = $profileHandler->loadFields();
50
        // Get ids of fields that can be edited
51
        /* @var  \XoopsGroupPermHandler $grouppermHandler */
52
        $grouppermHandler = xoops_getHandler('groupperm');
53
        $editable_fields  = $grouppermHandler->getItemIds('profile_edit', $GLOBALS['xoopsUser']->getGroups(), $GLOBALS['xoopsModule']->getVar('mid'));
54
        if ($user->isNew() || $GLOBALS['xoopsUser']->isAdmin()) {
55
            $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...
56
                '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

56
                'element'  => new \XoopsFormText(_MD_SUICO_NICKNAME, 'uname', 25, $GLOBALS['xoopsUser']->isAdmin() ? 60 : $GLOBALS['xoopsConfigUser']['maxuname'], /** @scrutinizer ignore-type */ $user->getVar('uname', 'e')),
Loading history...
57
                'required' => 1,
58
            ];
59
            $email_text    = new \XoopsFormText('', 'email', 30, 60, $user->getVar('email'));
60
        } else {
61
            $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

61
            $elements[0][] = ['element' => new XoopsFormLabel(_MD_SUICO_NICKNAME, /** @scrutinizer ignore-type */ $user->getVar('uname')), 'required' => 0];
Loading history...
62
            $email_text    = new XoopsFormLabel('', $user->getVar('email'));
63
        }
64
        $email_tray = new \XoopsFormElementTray(_MD_SUICO_EMAILADDRESS, '<br>');
65
        $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

65
        $email_tray->addElement($email_text, /** @scrutinizer ignore-type */ ($user->isNew() || $GLOBALS['xoopsUser']->isAdmin()) ? 1 : 0);
Loading history...
66
        $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...
67
        $elements[0][] = ['element' => $email_tray, 'required' => 0];
68
        $weights[0][]  = 0;
69
        if ($GLOBALS['xoopsUser']->isAdmin() && $user->getVar('uid') != $GLOBALS['xoopsUser']->getVar('uid')) {
70
            //If the user is an admin and is editing someone else
71
            $pwd_text  = new \XoopsFormPassword('', 'password', 10, 32);
72
            $pwd_text2 = new \XoopsFormPassword('', 'vpass', 10, 32);
73
            $pwd_tray  = new \XoopsFormElementTray(_MD_SUICO_PASSWORD . '<br>' . _MD_SUICO_CONFIRMPASSWORD);
74
            $pwd_tray->addElement($pwd_text);
75
            $pwd_tray->addElement($pwd_text2);
76
            $elements[0][] = ['element' => $pwd_tray, 'required' => 0]; //cannot set an element tray required
77
            $weights[0][]  = 0;
78
            $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

78
            $level_radio   = new \XoopsFormRadio(_MD_SUICO_USERLEVEL, 'level', /** @scrutinizer ignore-type */ $user->getVar('level'));
Loading history...
79
            $level_radio->addOption(1, _MD_SUICO_ACTIVE);
80
            $level_radio->addOption(0, _MD_SUICO_INACTIVE);
81
            //$level_radio->addOption(-1, _MD_SUICO_DISABLED);
82
            $elements[0][] = ['element' => $level_radio, 'required' => 0];
83
            $weights[0][]  = 0;
84
        }
85
        $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

85
        $elements[0][]   = ['element' => new XoopsFormHidden('uid', /** @scrutinizer ignore-type */ $user->getVar('uid')), 'required' => 0];
Loading history...
86
        $weights[0][]    = 0;
87
        $elements[0][]   = ['element' => new XoopsFormHidden('op', 'save'), 'required' => 0];
88
        $weights[0][]    = 0;
89
        $categoryHandler = \XoopsModules\Suico\Helper::getInstance()->getHandler('Category');
90
        $categories      = [];
91
        $all_categories  = $categoryHandler->getObjects(null, true, false);
92
        $count_fields    = count($fields);
93
        foreach (array_keys($fields) as $i) {
94
            if (in_array($fields[$i]->getVar('field_id'), $editable_fields)) {
95
                // Set default value for user fields if available
96
                if ($user->isNew()) {
97
                    $default = $fields[$i]->getVar('field_default');
98
                    if ('' !== $default && null !== $default) {
99
                        $user->setVar($fields[$i]->getVar('field_name'), $default);
100
                    }
101
                }
102
                if (null === $profile->getVar($fields[$i]->getVar('field_name'), 'n')) {
103
                    $default = $fields[$i]->getVar('field_default', 'n');
104
                    $profile->setVar($fields[$i]->getVar('field_name'), $default);
105
                }
106
                $fieldinfo['element']  = $fields[$i]->getEditElement($user, $profile);
107
                $fieldinfo['required'] = $fields[$i]->getVar('field_required');
108
                $key                   = @$all_categories[$fields[$i]->getVar('cat_id')]['cat_weight'] * $count_fields + $fields[$i]->getVar('cat_id');
109
                $elements[$key][]      = $fieldinfo;
110
                $weights[$key][]       = $fields[$i]->getVar('field_weight');
111
                $categories[$key]      = @$all_categories[$fields[$i]->getVar('cat_id')];
112
            }
113
        }
114
        if ($GLOBALS['xoopsUser'] && $GLOBALS['xoopsUser']->isAdmin()) {
115
            xoops_loadLanguage('admin', 'profile');
116
            /* @var  \XoopsGroupPermHandler $grouppermHandler */
117
            $grouppermHandler = xoops_getHandler('groupperm');
118
            //If user has admin rights on groups
119
            include_once $GLOBALS['xoops']->path('modules/system/constants.php');
120
            if ($grouppermHandler->checkRight('system_admin', XOOPS_SYSTEM_GROUP, $GLOBALS['xoopsUser']->getGroups(), 1)) {
121
                //add group selection
122
                $group_select  = new \XoopsFormSelectGroup(_MD_SUICO_USERGROUPS, 'groups', false, $user->getGroups(), 5, true);
123
                $elements[0][] = ['element' => $group_select, 'required' => 0];
124
                //set as latest;
125
                $weights[0][] = $count_fields + 1;
126
            }
127
        }
128
        ksort($elements);
129
        foreach (array_keys($elements) as $k) {
130
            array_multisort($weights[$k], SORT_ASC, array_keys($elements[$k]), SORT_ASC, $elements[$k]);
131
            $title = isset($categories[$k]) ? $categories[$k]['cat_title'] : _MD_SUICO_DEFAULT;
132
            $desc  = isset($categories[$k]) ? $categories[$k]['cat_description'] : '';
133
            $this->addElement(new XoopsFormLabel("<h3>{$title}</h3>", $desc), false);
134
            foreach (array_keys($elements[$k]) as $i) {
135
                $this->addElement($elements[$k][$i]['element'], $elements[$k][$i]['required']);
136
            }
137
        }
138
        $this->addElement(new XoopsFormHidden('uid', $user->getVar('uid')));
139
        $this->addElement(new XoopsFormButton('', 'submit', _MD_SUICO_SAVECHANGES, 'submit'));
140
    }
141
}
142