1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Icybee\Modules\Users;
13
14 use Brickrouge\Group;
15
16 use Icybee\Modules\Users\User;
17
18 use Brickrouge\Document;
19 use Brickrouge\Element;
20 use Brickrouge\Form;
21 use Brickrouge\Text;
22 use Brickrouge\Widget;
23
24 25 26
27 class EditBlock extends \Icybee\EditBlock
28 {
29 static protected function add_assets(Document $document)
30 {
31 parent::add_assets($document);
32
33 $document->js->add(DIR . 'public/admin.js');
34 }
35
36 protected function get_permission()
37 {
38 global $core;
39
40 $user = $core->user;
41
42 if ($user->has_permission(Module::PERMISSION_MANAGE, $this->module))
43 {
44 return true;
45 }
46 else if ($user->uid == $this->record->uid && $user->has_permission('modify own profile'))
47 {
48 return true;
49 }
50
51 return parent::get_permission();
52 }
53
54 protected function lazy_get_attributes()
55 {
56 return \ICanBoogie\array_merge_recursive(parent::lazy_get_attributes(), [
57
58 Element::GROUPS => [
59
60 'connection' => [ 'title' => 'Connection' ],
61 'advanced' => [ 'title' => 'Advanced' ]
62
63 ]
64
65 ]);
66 }
67
68 protected function lazy_get_children()
69 {
70 global $core;
71
72 $values = $this->values;
73
74
75
76
77
78 $user = $core->user;
79 $permission = $this->permission;
80 $uid = $values[User::UID];
81
82
83
84
85
86 $languages = $core->locale['languages'];
87
88 uasort($languages, 'ICanBoogie\unaccent_compare_ci');
89
90 $administer = $user->has_permission(Module::PERMISSION_MANAGE, $this->module);
91
92 return array_merge(parent::lazy_get_children(), [
93
94
95
96
97
98 User::FIRSTNAME => new Text([
99
100 Group::LABEL => 'firstname'
101
102 ]),
103
104 User::LASTNAME => new Text([
105
106 Group::LABEL => 'lastname'
107
108 ]),
109
110 User::NICKNAME => new Text([
111
112 Group::LABEL => 'Nickname'
113
114 ]),
115
116 User::USERNAME => $administer ? new Text([
117
118 Group::LABEL => 'username',
119 Element::REQUIRED => true
120
121 ]) : null,
122
123 User::NAME_AS => $this->create_control_for_name_as(),
124
125
126
127
128
129 User::EMAIL => new Text([
130
131 Group::LABEL => 'email',
132 Element::GROUP => 'connection',
133 Element::REQUIRED => true,
134
135 'autocomplete' => 'off'
136
137 ]),
138
139 User::PASSWORD => new Text([
140
141 Element::LABEL => 'password',
142 Element::LABEL_POSITION => 'above',
143 Element::DESCRIPTION => 'password_' . ($uid ? 'update' : 'new'),
144 Element::GROUP => 'connection',
145
146 'autocomplete' => 'off',
147 'type' => 'password',
148 'value' => ''
149
150 ]),
151
152 User::PASSWORD . '-verify' => new Text([
153
154 Element::LABEL => 'password_confirm',
155 Element::LABEL_POSITION => 'above',
156 Element::DESCRIPTION => 'password_confirm',
157 Element::GROUP => 'connection',
158
159 'autocomplete' => 'off',
160 'type' => 'password',
161 'value' => ''
162
163 ]),
164
165 User::IS_ACTIVATED => ($uid == 1 || !$administer) ? null : new Element(Element::TYPE_CHECKBOX, [
166
167 Element::LABEL => 'is_activated',
168 Element::GROUP => 'connection',
169 Element::DESCRIPTION => 'is_activated'
170
171 ]),
172
173 User::ROLES => $this->create_control_for_role(),
174
175 User::LANGUAGE => new Element('select', [
176
177 Group::LABEL => 'language',
178 Element::GROUP => 'advanced',
179 Element::DESCRIPTION => 'language',
180 Element::OPTIONS => array(null => '') + $languages
181
182 ]),
183
184 'timezone' => new Widget\TimeZone([
185
186 Group::LABEL => 'timezone',
187 Element::GROUP => 'advanced',
188 Element::DESCRIPTION => "Si la zone horaire n'est pas définie celle
189 du site sera utilisée à la place."
190
191 ]),
192
193 User::RESTRICTED_SITES => $this->create_control_for_restricted_sites_ids()
194
195 ]);
196 }
197
198 protected function alter_actions(array $actions, array $params)
199 {
200 global $core;
201
202 $actions = parent::alter_actions($actions, $params);
203
204 $user = $core->user;
205 $record = $this->record;
206
207 if ($record && $record->uid == $user->uid && !$user->has_permission(Module::PERMISSION_ADMINISTER, $this->module))
208 {
209 unset($actions[\Icybee\OPERATION_SAVE_MODE]);
210 }
211
212 return $actions;
213 }
214
215 protected function create_control_for_role()
216 {
217 global $core;
218
219 $user = $core->user;
220 $uid = $this->values[User::UID];
221
222 if ($uid == 1 || !$user->has_permission(Module::PERMISSION_ADMINISTER, $this->module))
223 {
224 return;
225 }
226
227 $rid = [ 2 => true ];
228
229 if ($uid)
230 {
231 $record = $this->module->model[$uid];
232
233 foreach ($record->roles as $role)
234 {
235 $rid[$role->rid] = true;
236 }
237 }
238
239 $options = $core->models['users.roles']
240 ->select('rid, name')
241 ->where('rid != 1')
242 ->order('rid')
243 ->pairs;
244
245 return new Element(Element::TYPE_CHECKBOX_GROUP, [
246
247 Form::LABEL => 'roles',
248 Element::GROUP => 'advanced',
249 Element::OPTIONS => $options,
250 Element::OPTIONS_DISABLED => [ 2 => true ],
251 Element::REQUIRED => true,
252 Element::DESCRIPTION => 'roles',
253
254 'class' => 'framed inputs-list sortable',
255 'value' => $rid
256
257 ]);
258 }
259
260 261 262 263 264 265 266
267 protected function create_control_for_name_as()
268 {
269 $values = $this->values;
270
271 $options = [ '<username>' ];
272
273 if ($values[User::USERNAME])
274 {
275 $options[0] = $values[User::USERNAME];
276 }
277
278 $firstname = $values[User::FIRSTNAME];
279
280 if ($firstname)
281 {
282 $options[1] = $firstname;
283 }
284
285 $lastname = $values[User::LASTNAME];
286
287 if ($lastname)
288 {
289 $options[2] = $lastname;
290 }
291
292 if ($firstname && $lastname)
293 {
294 $options[3] = $firstname . ' ' . $lastname;
295 $options[4] = $lastname . ' ' . $firstname;
296 }
297
298 $nickname = $values[User::NICKNAME];
299
300 if ($nickname)
301 {
302 $options[User::NAME_AS_NICKNAME] = $nickname;
303 }
304
305 return new Element('select', [
306
307 Group::LABEL => 'name_as',
308 Element::OPTIONS => $options
309
310 ]);
311 }
312
313 protected function create_control_for_restricted_sites_ids()
314 {
315 global $core;
316
317 $user = $core->user;
318
319 if (!$user->has_permission(Module::PERMISSION_ADMINISTER, $this->module))
320 {
321 return;
322 }
323
324 $value = [];
325
326 if ($this->record)
327 {
328 $value = $this->record->restricted_sites_ids;
329
330 if ($value)
331 {
332 $value = array_combine($value, array_fill(0, count($value), true));
333 }
334 }
335
336 $options = $core->models['sites']->select('siteid, IF(admin_title != "", admin_title, concat(title, ":", language))')->order('admin_title, title')->pairs;
337
338 if (!$options)
339 {
340 return;
341 }
342
343 return new Element(Element::TYPE_CHECKBOX_GROUP, [
344
345 Form::LABEL => 'siteid',
346 Element::OPTIONS => $options,
347 Element::GROUP => 'advanced',
348 Element::DESCRIPTION => 'siteid',
349
350 'class' => 'inputs-list widget-bordered',
351 'value' => $value
352
353 ]);
354 }
355 }