1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace XoopsModules\Suico\Form; |
4
|
|
|
|
5
|
|
|
use XoopsModules\Suico\{ |
6
|
|
|
Helper, |
7
|
|
|
Profile, |
8
|
|
|
ProfileHandler |
9
|
|
|
}; |
10
|
|
|
use XoopsThemeForm; |
11
|
|
|
use XoopsFormButton; |
12
|
|
|
use XoopsFormHidden; |
13
|
|
|
use XoopsFormLabel; |
14
|
|
|
use XoopsFormSelectUser; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Get {@link XoopsThemeForm} for editing a user |
18
|
|
|
* |
19
|
|
|
* @param \XoopsUser $user {@link \XoopsUser} to edit |
20
|
|
|
* @param Profile $profile |
21
|
|
|
* @param bool $action |
22
|
|
|
* |
23
|
|
|
*/ |
24
|
|
|
class UserForm extends XoopsThemeForm |
25
|
|
|
{ |
26
|
|
|
/** |
27
|
|
|
* UserForm constructor. |
28
|
|
|
* @param \XoopsUser $user |
29
|
|
|
* @param \XoopsModules\Suico\Profile|null $profile |
30
|
|
|
* @param bool $action |
31
|
|
|
*/ |
32
|
|
|
public function __construct(\XoopsUser $user, Profile $profile = null, $action = false) |
33
|
|
|
{ |
34
|
|
|
$helper = Helper::getInstance(); |
|
|
|
|
35
|
|
|
if (!$action) { |
36
|
|
|
$action = $_SERVER['REQUEST_URI']; |
37
|
|
|
} |
38
|
|
|
if (empty($GLOBALS['xoopsConfigUser'])) { |
39
|
|
|
/** @var \XoopsConfigHandler $configHandler */ |
40
|
|
|
$configHandler = \xoops_getHandler('config'); |
41
|
|
|
$GLOBALS['xoopsConfigUser'] = $configHandler->getConfigsByCat(\XOOPS_CONF_USER); |
42
|
|
|
} |
43
|
|
|
require_once $GLOBALS['xoops']->path('class/xoopsformloader.php'); |
44
|
|
|
$title = $user->isNew() ? \_AM_SUICO_ADDUSER : \_US_EDITPROFILE; |
45
|
|
|
parent::__construct($title, 'userinfo', $action, 'post', true); |
46
|
|
|
/** @var ProfileHandler $profileHandler */ |
47
|
|
|
$profileHandler = Helper::getInstance()->getHandler('Profile'); |
48
|
|
|
// Dynamic fields |
49
|
|
|
if (!$profile) { |
50
|
|
|
/** @var ProfileHandler $profileHandler */ |
51
|
|
|
$profileHandler = Helper::getInstance()->getHandler('Profile'); |
52
|
|
|
$profile = $profileHandler->get($user->getVar('uid')); |
53
|
|
|
} |
54
|
|
|
// Get fields |
55
|
|
|
$fields = $profileHandler->loadFields(); |
56
|
|
|
// Get ids of fields that can be edited |
57
|
|
|
/** @var \XoopsGroupPermHandler $grouppermHandler */ |
58
|
|
|
$grouppermHandler = \xoops_getHandler('groupperm'); |
59
|
|
|
$editable_fields = $grouppermHandler->getItemIds('profile_edit', $GLOBALS['xoopsUser']->getGroups(), $GLOBALS['xoopsModule']->getVar('mid')); |
60
|
|
|
if ($user->isNew() || $GLOBALS['xoopsUser']->isAdmin()) { |
61
|
|
|
$elements[0][] = [ |
|
|
|
|
62
|
|
|
'element' => new \XoopsFormText(\_MD_SUICO_NICKNAME, 'uname', 25, $GLOBALS['xoopsUser']->isAdmin() ? 60 : $GLOBALS['xoopsConfigUser']['maxuname'], $user->getVar('uname', 'e')), |
|
|
|
|
63
|
|
|
'required' => 1, |
64
|
|
|
]; |
65
|
|
|
$email_text = new \XoopsFormText('', 'email', 30, 60, $user->getVar('email')); |
66
|
|
|
} else { |
67
|
|
|
$elements[0][] = ['element' => new XoopsFormLabel(\_MD_SUICO_NICKNAME, $user->getVar('uname')), 'required' => 0]; |
|
|
|
|
68
|
|
|
$email_text = new XoopsFormLabel('', $user->getVar('email')); |
69
|
|
|
} |
70
|
|
|
$email_tray = new \XoopsFormElementTray(\_MD_SUICO_EMAILADDRESS, '<br>'); |
71
|
|
|
$email_tray->addElement($email_text, ($user->isNew() || $GLOBALS['xoopsUser']->isAdmin()) ? 1 : 0); |
|
|
|
|
72
|
|
|
$weights[0][] = 0; |
|
|
|
|
73
|
|
|
$elements[0][] = ['element' => $email_tray, 'required' => 0]; |
74
|
|
|
$weights[0][] = 0; |
75
|
|
|
if ($GLOBALS['xoopsUser']->isAdmin() && $user->getVar('uid') != $GLOBALS['xoopsUser']->getVar('uid')) { |
76
|
|
|
//If the user is an admin and is editing someone else |
77
|
|
|
$pwd_text = new \XoopsFormPassword('', 'password', 10, 32); |
78
|
|
|
$pwd_text2 = new \XoopsFormPassword('', 'vpass', 10, 32); |
79
|
|
|
$pwd_tray = new \XoopsFormElementTray(\_MD_SUICO_PASSWORD . '<br>' . \_MD_SUICO_CONFIRMPASSWORD); |
80
|
|
|
$pwd_tray->addElement($pwd_text); |
81
|
|
|
$pwd_tray->addElement($pwd_text2); |
82
|
|
|
$elements[0][] = ['element' => $pwd_tray, 'required' => 0]; //cannot set an element tray required |
83
|
|
|
$weights[0][] = 0; |
84
|
|
|
$level_radio = new \XoopsFormRadio(\_MD_SUICO_USERLEVEL, 'level', $user->getVar('level')); |
|
|
|
|
85
|
|
|
$level_radio->addOption(1, \_MD_SUICO_ACTIVE); |
86
|
|
|
$level_radio->addOption(0, \_MD_SUICO_INACTIVE); |
87
|
|
|
//$level_radio->addOption(-1, _MD_SUICO_DISABLED); |
88
|
|
|
$elements[0][] = ['element' => $level_radio, 'required' => 0]; |
89
|
|
|
$weights[0][] = 0; |
90
|
|
|
} |
91
|
|
|
$elements[0][] = ['element' => new XoopsFormHidden('uid', $user->getVar('uid')), 'required' => 0]; |
|
|
|
|
92
|
|
|
$weights[0][] = 0; |
93
|
|
|
$elements[0][] = ['element' => new XoopsFormHidden('op', 'save'), 'required' => 0]; |
94
|
|
|
$weights[0][] = 0; |
95
|
|
|
$categoryHandler = Helper::getInstance()->getHandler('Category'); |
96
|
|
|
$categories = []; |
97
|
|
|
$all_categories = $categoryHandler->getObjects(null, true, false); |
98
|
|
|
$count_fields = \count($fields); |
99
|
|
|
foreach (\array_keys($fields) as $i) { |
100
|
|
|
if (\in_array($fields[$i]->getVar('field_id'), $editable_fields, true)) { |
101
|
|
|
// Set default value for user fields if available |
102
|
|
|
if ($user->isNew()) { |
103
|
|
|
$default = $fields[$i]->getVar('field_default'); |
104
|
|
|
if ('' !== $default && null !== $default) { |
105
|
|
|
$user->setVar($fields[$i]->getVar('field_name'), $default); |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
if (null === $profile->getVar($fields[$i]->getVar('field_name'), 'n')) { |
109
|
|
|
$default = $fields[$i]->getVar('field_default', 'n'); |
110
|
|
|
$profile->setVar($fields[$i]->getVar('field_name'), $default); |
111
|
|
|
} |
112
|
|
|
$fieldinfo['element'] = $fields[$i]->getEditElement($user, $profile); |
113
|
|
|
$fieldinfo['required'] = $fields[$i]->getVar('field_required'); |
114
|
|
|
$key = @$all_categories[$fields[$i]->getVar('cat_id')]['cat_weight'] * $count_fields + $fields[$i]->getVar('cat_id'); |
115
|
|
|
$elements[$key][] = $fieldinfo; |
116
|
|
|
$weights[$key][] = $fields[$i]->getVar('field_weight'); |
117
|
|
|
$categories[$key] = @$all_categories[$fields[$i]->getVar('cat_id')]; |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
if ($GLOBALS['xoopsUser'] && $GLOBALS['xoopsUser']->isAdmin()) { |
121
|
|
|
\xoops_loadLanguage('admin', 'profile'); |
122
|
|
|
/** @var \XoopsGroupPermHandler $grouppermHandler */ |
123
|
|
|
$grouppermHandler = \xoops_getHandler('groupperm'); |
124
|
|
|
//If user has admin rights on groups |
125
|
|
|
require_once $GLOBALS['xoops']->path('modules/system/constants.php'); |
126
|
|
|
if ($grouppermHandler->checkRight('system_admin', \XOOPS_SYSTEM_GROUP, $GLOBALS['xoopsUser']->getGroups(), 1)) { |
127
|
|
|
//add group selection |
128
|
|
|
$group_select = new \XoopsFormSelectGroup(\_MD_SUICO_USERGROUPS, 'groups', false, $user->getGroups(), 5, true); |
129
|
|
|
$elements[0][] = ['element' => $group_select, 'required' => 0]; |
130
|
|
|
//set as latest; |
131
|
|
|
$weights[0][] = $count_fields + 1; |
132
|
|
|
} |
133
|
|
|
} |
134
|
|
|
\ksort($elements); |
135
|
|
|
foreach (\array_keys($elements) as $k) { |
136
|
|
|
\array_multisort($weights[$k], \SORT_ASC, \array_keys($elements[$k]), \SORT_ASC, $elements[$k]); |
|
|
|
|
137
|
|
|
$title = isset($categories[$k]) ? $categories[$k]['cat_title'] : \_MD_SUICO_DEFAULT; |
138
|
|
|
$desc = isset($categories[$k]) ? $categories[$k]['cat_description'] : ''; |
139
|
|
|
$this->addElement(new XoopsFormLabel("<h3>{$title}</h3>", $desc), false); |
140
|
|
|
foreach (\array_keys($elements[$k]) as $i) { |
141
|
|
|
$this->addElement($elements[$k][$i]['element'], $elements[$k][$i]['required']); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
$this->addElement(new XoopsFormHidden('uid', $user->getVar('uid'))); |
145
|
|
|
$this->addElement(new XoopsFormButton('', 'submit', \_MD_SUICO_SAVECHANGES, 'submit')); |
146
|
|
|
} |
147
|
|
|
} |
148
|
|
|
|