Completed
Push — brickrouge3 ( 23d6f2...241af5 )
by Olivier
08:26
created

EditBlock::add_assets()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the Icybee package.
5
 *
6
 * (c) Olivier Laviale <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Icybee\Modules\Users\Block;
13
14
use Brickrouge\Group;
15
16
17
use Brickrouge\Document;
18
use Brickrouge\Element;
19
use Brickrouge\Form;
20
use Brickrouge\Text;
21
use Brickrouge\Widget;
22
23
use Icybee\Modules\Users\Module;
24
use Icybee\Modules\Users\User;
25
26
/**
27
 * A block to edit users.
28
 *
29
 * @property User $record
30
 * @property User $user
31
 */
32
class EditBlock extends \Icybee\Block\EditBlock
33
{
34
	static protected function add_assets(Document $document)
35
	{
36
		parent::add_assets($document);
37
38
		$document->js->add(\Icybee\Modules\Users\DIR . 'public/admin.js');
39
	}
40
41
	protected function get_permission()
42
	{
43
		$user = $this->user;
44
45
		if ($user->has_permission(Module::PERMISSION_MANAGE, $this->module))
46
		{
47
			return true;
48
		}
49
		else if ($user->uid == $this->record->uid && $user->has_permission('modify own profile'))
50
		{
51
			return true;
52
		}
53
54
		return parent::get_permission();
55
	}
56
57
	protected function get_user()
58
	{
59
		return $this->app->user;
60
	}
61
62
	protected function lazy_get_attributes()
63
	{
64
		return \ICanBoogie\array_merge_recursive(parent::lazy_get_attributes(), [
65
66
			Element::GROUPS => [
67
68
				'connection' => [ 'title' => 'Connection' ],
69
				'advanced' => [ 'title' => 'Advanced' ]
70
71
			]
72
73
		]);
74
	}
75
76
	protected function lazy_get_children()
77
	{
78
		$values = $this->values;
79
		$user = $this->user;
80
		$uid = $values[User::UID];
81
		$languages = $this->app->locale['languages'];
82
83
		uasort($languages, 'ICanBoogie\unaccent_compare_ci');
84
85
		$administer = $user->has_permission(Module::PERMISSION_MANAGE, $this->module);
86
87
		return array_merge(parent::lazy_get_children(), [
88
89
			#
90
			# name group
91
			#
92
93
			User::FIRSTNAME => new Text([
94
95
				Group::LABEL => 'firstname'
96
97
			]),
98
99
			User::LASTNAME => new Text([
100
101
				Group::LABEL => 'lastname'
102
103
			]),
104
105
			User::NICKNAME => new Text([
106
107
				Group::LABEL => 'Nickname'
108
109
			]),
110
111
			User::USERNAME => $administer ? new Text([
112
113
				Group::LABEL => 'username',
114
				Element::REQUIRED => true
115
116
			]) : null,
117
118
			User::NAME_AS => $this->create_control_for_name_as(),
119
120
			#
121
			# connection group
122
			#
123
124
			User::EMAIL => new Text([
125
126
				Group::LABEL => 'email',
127
				Element::GROUP => 'connection',
128
				Element::REQUIRED => true,
129
				Element::VALIDATION => 'email',
130
131
				'autocomplete' => 'off'
132
133
			]),
134
135
			User::PASSWORD => new Text([
136
137
				Element::LABEL => 'password',
138
				Element::LABEL_POSITION => 'above',
139
				Element::DESCRIPTION => 'password_' . ($uid ? 'update' : 'new'),
140
				Element::GROUP => 'connection',
141
142
				'autocomplete' => 'off',
143
				'type' => 'password',
144
				'value' => ''
145
146
			]),
147
148
			User::PASSWORD . '-verify' => new Text([
149
150
				Element::LABEL => 'password_confirm',
151
				Element::LABEL_POSITION => 'above',
152
				Element::DESCRIPTION => 'password_confirm',
153
				Element::GROUP => 'connection',
154
155
				'autocomplete' => 'off',
156
				'type' => 'password',
157
				'value' => ''
158
159
			]),
160
161
			User::IS_ACTIVATED => ($uid == 1 || !$administer) ? null : new Element(Element::TYPE_CHECKBOX, [
162
163
				Element::LABEL => 'is_activated',
164
				Element::GROUP => 'connection',
165
				Element::DESCRIPTION => 'is_activated'
166
167
			]),
168
169
			User::ROLES => $this->create_control_for_role(),
170
171
			User::LANGUAGE => new Element('select', [
172
173
				Group::LABEL => 'language',
174
				Element::GROUP => 'advanced',
175
				Element::DESCRIPTION => 'language',
176
				Element::OPTIONS => [ null => '' ] + $languages
177
178
			]),
179
180
			'timezone' => new Widget\TimeZone([
181
182
				Group::LABEL => 'timezone',
183
				Element::GROUP => 'advanced',
184
				Element::DESCRIPTION =>'timezone'
185
186
			]),
187
188
			User::RESTRICTED_SITES => $this->create_control_for_restricted_sites_ids()
189
190
		]);
191
	}
192
193
	protected function alter_actions(array $actions, array $params)
194
	{
195
		$actions = parent::alter_actions($actions, $params);
196
197
		$user = $this->user;
198
		$record = $this->record;
199
200
		if ($record && $record->uid == $user->uid && !$user->has_permission(Module::PERMISSION_ADMINISTER, $this->module))
201
		{
202
			unset($actions[\Icybee\OPERATION_SAVE_MODE]);
203
		}
204
205
		return $actions;
206
	}
207
208
	protected function create_control_for_role()
209
	{
210
		$user = $this->user;
211
		$uid = $this->values[User::UID];
212
213
		if ($uid == 1 || !$user->has_permission(Module::PERMISSION_ADMINISTER, $this->module))
214
		{
215
			return null;
216
		}
217
218
		$rid = [ 2 => true ];
219
220
		if ($uid)
221
		{
222
			foreach ($this->record->roles as $role)
223
			{
224
				$rid[$role->rid] = true;
225
			}
226
		}
227
228
		$options = $this->app
229
		->models['users.roles']
230
		->select('rid, name')
231
		->where('rid != 1')
232
		->order('rid')
233
		->pairs;
234
235
		return new Element(Element::TYPE_CHECKBOX_GROUP, [
236
237
			Form::LABEL => 'roles',
0 ignored issues
show
Deprecated Code introduced by
The constant Brickrouge\Form::LABEL has been deprecated.

This class constant has been deprecated.

Loading history...
238
			Element::GROUP => 'advanced',
239
			Element::OPTIONS => $options,
240
			Element::OPTIONS_DISABLED => [ 2 => true ],
241
			Element::REQUIRED => true,
242
			Element::DESCRIPTION => 'roles',
243
244
			'class' => 'framed inputs-list sortable',
245
			'value' => $rid
246
247
		]);
248
	}
249
250
	/**
251
	 * Returns the control element for the `name_as` param.
252
	 *
253
	 * @return Element
254
	 */
255
	protected function create_control_for_name_as()
256
	{
257
		$values = $this->values;
258
259
		$options = [ '<username>' ];
260
261
		if ($values[User::USERNAME])
262
		{
263
			$options[0] = $values[User::USERNAME];
264
		}
265
266
		$firstname = $values[User::FIRSTNAME];
267
268
		if ($firstname)
269
		{
270
			$options[1] = $firstname;
271
		}
272
273
		$lastname = $values[User::LASTNAME];
274
275
		if ($lastname)
276
		{
277
			$options[2] = $lastname;
278
		}
279
280
		if ($firstname && $lastname)
281
		{
282
			$options[3] = $firstname . ' ' . $lastname;
283
			$options[4] = $lastname . ' ' . $firstname;
284
		}
285
286
		$nickname = $values[User::NICKNAME];
287
288
		if ($nickname)
289
		{
290
			$options[User::NAME_AS_NICKNAME] = $nickname;
291
		}
292
293
		return new Element('select', [
294
295
			Group::LABEL => 'name_as',
296
			Element::OPTIONS => $options
297
298
		]);
299
	}
300
301
	protected function create_control_for_restricted_sites_ids()
302
	{
303
		$user = $this->user;
304
305
		if (!$user->has_permission(Module::PERMISSION_ADMINISTER, $this->module))
306
		{
307
			return null;
308
		}
309
310
		$value = [];
311
312
		if ($this->record)
313
		{
314
			$value = $this->record->restricted_sites_ids;
315
316
			if ($value)
317
			{
318
				$value = array_combine($value, array_fill(0, count($value), true));
319
			}
320
		}
321
322
		$options = $this->app
323
		->models['sites']
324
		->select('site_id, IF(admin_title != "", admin_title, concat(title, ":", language))')
325
		->order('admin_title, title')
326
		->pairs;
327
328
		if (!$options)
329
		{
330
			return null;
331
		}
332
333
		return new Element(Element::TYPE_CHECKBOX_GROUP, [
334
335
			Form::LABEL => 'site_id',
0 ignored issues
show
Deprecated Code introduced by
The constant Brickrouge\Form::LABEL has been deprecated.

This class constant has been deprecated.

Loading history...
336
			Element::OPTIONS => $options,
337
			Element::GROUP => 'advanced',
338
			Element::DESCRIPTION => 'site_id',
339
340
			'class' => 'inputs-list widget-bordered',
341
			'value' => $value
342
343
		]);
344
	}
345
}
346