Completed
Push — master ( 9027b3...432a29 )
by Henry
12:53 queued 02:53
created

includes/Admin/View/UserForm.php (11 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Redaxscript\Admin\View;
3
4
use Redaxscript\Admin;
5
use Redaxscript\Html;
6
use Redaxscript\Module;
7
use Redaxscript\Validator;
8
use function count;
9
use function json_decode;
10
11
/**
12
 * children class to create the user form
13
 *
14
 * @since 3.0.0
15
 *
16
 * @package Redaxscript
17
 * @category View
18
 * @author Henry Ruhs
19
 */
20
21
class UserForm extends ViewAbstract
22
{
23
	/**
24
	 * render the view
25
	 *
26
	 * @since 3.0.0
27
	 *
28
	 * @param int $userId identifier of the user
29
	 *
30
	 * @return string
31
	 */
32
33 4
	public function render(int $userId = null) : string
34
	{
35 4
		$output = Module\Hook::trigger('adminUserFormStart');
36 4
		$userModel = new Admin\Model\User();
37 4
		$user = $userModel->getById($userId);
38 4
		$nameValidator = new Validator\Name();
39 4
		$userValidator = new Validator\User();
40 4
		$passwordValidator = new Validator\Password();
41 4
		$helperOption = new Helper\Option($this->_language);
42
43
		/* html element */
44
45 4
		$titleElement = new Html\Element();
46
		$titleElement
47 4
			->init('h2',
48
			[
49 4
				'class' => 'rs-admin-title-content',
50
			])
51 4
			->text($user->id ? $user->name : $this->_language->get('user_new'));
52 4
		$formElement = new Admin\Html\Form($this->_registry, $this->_language);
53 4
		$formElement->init(
54
		[
55
			'form' =>
56
			[
57 4
				'class' => 'rs-admin-js-validate rs-admin-fn-tab rs-admin-component-tab rs-admin-form-default rs-admin-form-user'
58
			],
59
			'button' =>
60
			[
61
				'create' =>
62
				[
63
					'name' => self::class
64
				],
65
				'save' =>
66
				[
67
					'name' => self::class
68
				]
69
			],
70
			'link' =>
71
			[
72
				'cancel' =>
73
				[
74 4
					'href' => $this->_registry->get('usersEdit') && $this->_registry->get('usersDelete') ? $this->_registry->get('parameterRoute') . 'admin/view/users' : $this->_registry->get('parameterRoute') . 'admin'
75
				],
76
				'delete' =>
77
				[
78 4
					'href' => $user->id ? $this->_registry->get('parameterRoute') . 'admin/delete/users/' . $user->id . '/' . $this->_registry->get('token') : null
79
				]
80
			]
81
		]);
82
83
		/* create the form */
84
85
		$formElement
86
87
			/* user */
88
89 4
			->radio(
90
			[
91 4
				'id' => self::class . '\User',
92
				'class' => 'rs-admin-fn-status-tab',
93
				'name' => self::class . '\Tab',
94
				'checked' => 'checked'
95
			])
96 4
			->label($this->_language->get('user'),
0 ignored issues
show
It seems like $this->_language->get('user') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
97
			[
98 4
				'class' => 'rs-admin-fn-toggle-tab rs-admin-label-tab',
99
				'for' => self::class . '\User'
100
			])
101 4
			->append('<ul class="rs-admin-fn-content-tab rs-admin-box-tab"><li>')
102 4
			->label($this->_language->get('name'),
0 ignored issues
show
It seems like $this->_language->get('name') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
103
			[
104 4
				'for' => 'name'
105
			])
106 4
			->text(
107
			[
108 4
				'autofocus' => 'autofocus',
109 4
				'id' => 'name',
110 4
				'name' => 'name',
111 4
				'pattern' => $nameValidator->getPattern(),
112 4
				'required' => 'required',
113 4
				'value' => $user->name
114
			])
115 4
			->append('</li><li>')
116 4
			->label($this->_language->get('user'),
0 ignored issues
show
It seems like $this->_language->get('user') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
117
			[
118 4
				'for' => 'user'
119
			])
120 4
			->text(
121
			[
122 4
				'id' => 'user',
123 4
				'name' => 'user',
124 4
				'pattern' => $userValidator->getPattern(),
125 4
				'required' => 'required',
126 4
				'value' => $user->user
127
			])
128 4
			->append('</li><li>')
129 4
			->label($this->_language->get('description'),
0 ignored issues
show
It seems like $this->_language->get('description') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
130
			[
131 4
				'for' => 'description'
132
			])
133 4
			->textarea(
134
			[
135 4
				'class' => 'rs-admin-js-resize rs-admin-field-textarea rs-admin-field-small',
136 4
				'id' => 'description',
137 4
				'name' => 'description',
138 4
				'rows' => 1,
139 4
				'value' => $user->description
140
			])
141 4
			->append('</li><li>')
142 4
			->label($this->_language->get('password'),
0 ignored issues
show
It seems like $this->_language->get('password') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
143
			[
144 4
				'for' => 'password'
145
			])
146 4
			->password(!$user->id ?
147
			[
148 2
				'id' => 'password',
149 2
				'name' => 'password',
150 2
				'pattern' => $passwordValidator->getPattern(),
151 2
				'autocomplete' => 'new-password',
152 2
				'required' => 'required'
153
			] :
154
			[
155 2
				'id' => 'password',
156 2
				'name' => 'password',
157 2
				'pattern' => $passwordValidator->getPattern(),
158 4
				'autocomplete' => 'new-password'
159
			])
160 4
			->append('</li><li>')
161 4
			->label($this->_language->get('email'),
0 ignored issues
show
It seems like $this->_language->get('email') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
162
			[
163 4
				'for' => 'email'
164
			])
165 4
			->email(
166
			[
167 4
				'id' => 'email',
168 4
				'name' => 'email',
169 4
				'required' => 'required',
170 4
				'value' => $user->email
171
			])
172 4
			->append('</li></ul>')
173
174
			/* general */
175
176 4
			->radio(
177
			[
178 4
				'id' => self::class . '\General',
179
				'class' => 'rs-admin-fn-status-tab',
180
				'name' => self::class . '\Tab'
181
			])
182 4
			->label($this->_language->get('general'),
0 ignored issues
show
It seems like $this->_language->get('general') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
183
			[
184 4
				'class' => 'rs-admin-fn-toggle-tab rs-admin-label-tab',
185
				'for' => self::class . '\General'
186
			])
187 4
			->append('<ul class="rs-admin-fn-content-tab rs-admin-box-tab"><li>')
188 4
			->label($this->_language->get('language'),
0 ignored issues
show
It seems like $this->_language->get('language') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
189
			[
190 4
				'for' => 'language'
191
			])
192 4
			->select($helperOption->getLanguageArray(),
193
			[
194 4
				$user->language
195
			],
196
			[
197 4
				'id' => 'language',
198
				'name' => 'language'
199
			])
200 4
			->append('</li></ul>');
201 4
		if (!$user->id || $user->id > 1)
202
		{
203
			$formElement
204
205
				/* customize */
206
207 3
				->radio(
208
				[
209 3
					'id' => self::class . '\Customize',
210
					'class' => 'rs-admin-fn-status-tab',
211
					'name' => self::class . '\Tab'
212
				])
213 3
				->label($this->_language->get('customize'),
0 ignored issues
show
It seems like $this->_language->get('customize') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
214
				[
215 3
					'class' => 'rs-admin-fn-toggle-tab rs-admin-label-tab',
216
					'for' => self::class . '\Customize'
217
				])
218 3
				->append('<ul class="rs-admin-fn-content-tab rs-admin-box-tab"><li>')
219 3
				->label($this->_language->get('status'),
0 ignored issues
show
It seems like $this->_language->get('status') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
220
				[
221 3
					'for' => 'status'
222
				])
223 3
				->checkbox(!$user->id || $user->status ?
224
				[
225 3
					'id' => 'status',
226
					'class' => 'rs-admin-fn-status-switch',
227
					'name' => 'status',
228
					'checked' => 'checked'
229
				] :
230
				[
231 3
					'id' => 'status',
232
					'class' => 'rs-admin-fn-status-switch',
233
					'name' => 'status'
234
				])
235 3
				->label(null,
236
				[
237 3
					'class' => 'rs-admin-label-switch',
238 3
					'for' => 'status',
239 3
					'data-on' => $this->_language->get('enable'),
240 3
					'data-off' => $this->_language->get('disable')
241
				])
242 3
				->append('</li>');
243 3
			if ($this->_registry->get('groupsEdit'))
244
			{
245
				$formElement
246 1
					->append('<li>')
247 1
					->label($this->_language->get('groups'),
0 ignored issues
show
It seems like $this->_language->get('groups') targeting Redaxscript\Language::get() can also be of type array; however, Redaxscript\Html\Form::label() does only seem to accept null|string, maybe add an additional type check?

This check looks at variables that are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
248
					[
249 1
						'for' => 'groups'
250
					])
251 1
					->select($helperOption->getGroupArray(),
252 1
					(array)json_decode($user->groups),
253
					[
254 1
						'id' => 'groups',
255 1
						'name' => 'groups[]',
256 1
						'multiple' => 'multiple',
257 1
						'size' => count($helperOption->getGroupArray())
258
					])
259 1
					->append('</li>');
260
			}
261 3
			$formElement->append('</ul>');
262
		}
263
		$formElement
264 4
			->hidden(
265
			[
266 4
				'name' => 'id',
267 4
				'value' => $user->id
268
			])
269 4
			->token()
270 4
			->append('<div class="rs-admin-wrapper-button">')
271 4
			->cancel();
272 4
		if ($user->id)
273
		{
274 2
			if (($this->_registry->get('usersDelete') || $this->_registry->get('myId') === $user->id) && $user->id > 1)
275
			{
276 1
				$formElement->delete();
277
			}
278 2
			if ($this->_registry->get('usersEdit') || $this->_registry->get('myId') === $user->id)
279
			{
280 2
				$formElement->save();
281
			}
282
		}
283 2
		else if ($this->_registry->get('usersNew'))
284
		{
285 1
			$formElement->create();
286
		}
287 4
		$formElement->append('</div>');
288
289
		/* collect output */
290
291 4
		$output .= $titleElement . $formElement;
292 4
		$output .= Module\Hook::trigger('adminUserFormEnd');
293 4
		return $output;
294
	}
295
}
296