Completed
Push — master ( c3c208...858dca )
by Henry
08:18
created

ArticleForm::render()   F

Complexity

Conditions 18
Paths 12

Size

Total Lines 402

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 197
CRAP Score 18

Importance

Changes 0
Metric Value
dl 0
loc 402
c 0
b 0
f 0
ccs 197
cts 198
cp 0.9949
rs 3.8933
cc 18
nc 12
nop 1
crap 18

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
namespace Redaxscript\Admin\View;
3
4
use Redaxscript\Admin;
5
use Redaxscript\Dater;
6
use Redaxscript\Html;
7
use Redaxscript\Module;
8
use Redaxscript\Validator;
9
use function count;
10
use function htmlspecialchars;
11
use function json_decode;
12
13
/**
14
 * children class to create the article form
15
 *
16
 * @since 3.0.0
17
 *
18
 * @package Redaxscript
19
 * @category View
20
 * @author Henry Ruhs
21
 */
22
23
class ArticleForm extends ViewAbstract
24
{
25
	/**
26
	 * render the view
27
	 *
28
	 * @since 3.0.0
29
	 *
30
	 * @param int $articleId identifier of the article
31
	 *
32
	 * @return string
33
	 */
34
35 4
	public function render(int $articleId = null) : string
36
	{
37 4
		$output = Module\Hook::trigger('adminArticleFormStart');
38 4
		$helperOption = new Helper\Option($this->_language);
39 4
		$articleModel = new Admin\Model\Article();
40 4
		$article = $articleModel->getById($articleId);
41 4
		$nameValidator = new Validator\Name();
42 4
		$aliasValidator = new Validator\Alias();
43 4
		$dater = new Dater();
44 4
		$dater->init($article->date);
45
46
		/* html element */
47
48 4
		$titleElement = new Html\Element();
49
		$titleElement
50 4
			->init('h2',
51
			[
52 4
				'class' => 'rs-admin-title-content',
53
			])
54 4
			->text($article->title ? : $this->_language->get('article_new'));
55 4
		$formElement = new Admin\Html\Form($this->_registry, $this->_language);
56 4
		$formElement->init(
57
		[
58 4
			'form' =>
59
			[
60
				'class' => 'rs-admin-js-validate rs-admin-js-alias rs-admin-fn-tab rs-admin-component-tab rs-admin-form-default rs-admin-form-article'
61
			],
62
			'button' =>
63
			[
64
				'create' =>
65
				[
66
					'name' => self::class
67
				],
68
				'save' =>
69
				[
70
					'name' => self::class
71
				]
72
			],
73
			'link' =>
74
			[
75
				'cancel' =>
76
				[
77 4
					'href' => $this->_registry->get('articlesEdit') && $this->_registry->get('articlesDelete') ? $this->_registry->get('parameterRoute') . 'admin/view/articles' : $this->_registry->get('parameterRoute') . 'admin'
78
				],
79
				'delete' =>
80
				[
81 4
					'href' => $article->id ? $this->_registry->get('parameterRoute') . 'admin/delete/articles/' . $article->id . '/' . $this->_registry->get('token') : null
82
				]
83
			]
84
		]);
85
86
		/* create the form */
87
88
		$formElement
89
90
			/* article */
91
92 4
			->radio(
93
			[
94 4
				'id' => self::class . '\Article',
95
				'class' => 'rs-admin-fn-status-tab',
96
				'name' => self::class . '\Tab',
97
				'checked' => 'checked'
98
			])
99 4
			->label($this->_language->get('article'),
0 ignored issues
show
Bug introduced by
It seems like $this->_language->get('article') 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...
100
			[
101 4
				'class' => 'rs-admin-fn-toggle-tab rs-admin-label-tab',
102
				'for' => self::class . '\Article'
103
			])
104 4
			->append('<ul class="rs-admin-fn-content-tab rs-admin-box-tab"><li>')
105 4
			->label($this->_language->get('title'),
0 ignored issues
show
Bug introduced by
It seems like $this->_language->get('title') 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...
106
			[
107 4
				'for' => 'title'
108
			])
109 4
			->text(
110
			[
111 4
				'autofocus' => 'autofocus',
112 4
				'class' => 'rs-admin-js-alias-input rs-admin-field-default rs-admin-field-text',
113 4
				'id' => 'title',
114 4
				'name' => 'title',
115 4
				'pattern' => $nameValidator->getPattern(),
116 4
				'required' => 'required',
117 4
				'value' => $article->title
118
			])
119 4
			->append('</li><li>')
120 4
			->label($this->_language->get('alias'),
0 ignored issues
show
Bug introduced by
It seems like $this->_language->get('alias') 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...
121
			[
122 4
				'for' => 'alias'
123
			])
124 4
			->text(
125
			[
126 4
				'class' => 'rs-admin-js-alias-output rs-admin-field-default rs-admin-field-text',
127 4
				'id' => 'alias',
128 4
				'name' => 'alias',
129 4
				'pattern' => $aliasValidator->getPattern(),
130 4
				'required' => 'required',
131 4
				'value' => $article->alias
132
			])
133 4
			->append('</li><li>')
134 4
			->label($this->_language->get('description'),
0 ignored issues
show
Bug introduced by
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...
135
			[
136 4
				'for' => 'description'
137
			])
138 4
			->textarea(
139
			[
140 4
				'class' => 'rs-admin-js-resize rs-admin-field-textarea rs-admin-field-small',
141 4
				'id' => 'description',
142 4
				'name' => 'description',
143 4
				'rows' => 1,
144 4
				'value' => $article->description
145
			])
146 4
			->append('</li><li>')
147 4
			->label($this->_language->get('keywords'),
0 ignored issues
show
Bug introduced by
It seems like $this->_language->get('keywords') 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...
148
			[
149 4
				'for' => 'keywords'
150
			])
151 4
			->textarea(
152
			[
153 4
				'class' => 'rs-admin-js-resize rs-admin-field-textarea rs-admin-field-small',
154 4
				'id' => 'keywords',
155 4
				'name' => 'keywords',
156 4
				'rows' => 1,
157 4
				'value' => $article->keywords
158
			])
159 4
			->append('</li><li>')
160 4
			->label($this->_language->get('robots'),
0 ignored issues
show
Bug introduced by
It seems like $this->_language->get('robots') 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...
161
			[
162 4
				'for' => 'robots'
163
			])
164 4
			->select($helperOption->getRobotArray(),
165
			[
166 4
				$article->robots
167
			],
168
			[
169 4
				'id' => 'robots',
170
				'name' => 'robots'
171
			])
172 4
			->append('</li><li>')
173 4
			->label($this->_language->get('text'),
0 ignored issues
show
Bug introduced by
It seems like $this->_language->get('text') 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...
174
			[
175 4
				'for' => 'text'
176
			])
177 4
			->textarea(
178
			[
179 4
				'class' => 'rs-admin-js-editor rs-admin-js-resize rs-admin-field-textarea',
180 4
				'id' => 'text',
181 4
				'name' => 'text',
182 4
				'required' => 'required',
183 4
				'value' => htmlspecialchars($article->text, ENT_QUOTES)
184
			])
185 4
			->append('</li></ul>')
186
187
			/* general */
188
189 4
			->radio(
190
			[
191 4
				'id' => self::class . '\General',
192
				'class' => 'rs-admin-fn-status-tab',
193
				'name' => self::class . '\Tab'
194
			])
195 4
			->label($this->_language->get('general'),
0 ignored issues
show
Bug introduced by
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...
196
			[
197 4
				'class' => 'rs-admin-fn-toggle-tab rs-admin-label-tab',
198
				'for' => self::class . '\General'
199
			])
200 4
			->append('<ul class="rs-admin-fn-content-tab rs-admin-box-tab"><li>')
201 4
			->label($this->_language->get('language'),
0 ignored issues
show
Bug introduced by
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...
202
			[
203 4
				'for' => 'language'
204
			])
205 4
			->select($helperOption->getLanguageArray(),
206
			[
207 4
				$article->language
208
			],
209
			[
210 4
				'id' => 'language',
211
				'name' => 'language'
212
			])
213 4
			->append('</li><li>')
214 4
			->label($this->_language->get('template'),
0 ignored issues
show
Bug introduced by
It seems like $this->_language->get('template') 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...
215
			[
216 4
				'for' => 'template'
217
			])
218 4
			->select($helperOption->getTemplateArray(),
219
			[
220 4
				$article->template
221
			],
222
			[
223 4
				'id' => 'template',
224
				'name' => 'template'
225
			])
226 4
			->append('</li><li>')
227 4
			->label($this->_language->get('article_sibling'),
0 ignored issues
show
Bug introduced by
It seems like $this->_language->get('article_sibling') 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...
228
			[
229 4
				'for' => 'sibling'
230
			])
231 4
			->select($helperOption->getSiblingForArticleArray($article->id),
232
			[
233 4
				$article->sibling
234
			],
235
			[
236 4
				'id' => 'sibling',
237
				'name' => 'sibling'
238
			])
239 4
			->append('</li><li>')
240 4
			->label($this->_language->get('category'),
0 ignored issues
show
Bug introduced by
It seems like $this->_language->get('category') 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...
241
			[
242 4
				'for' => 'category'
243
			])
244 4
			->select($helperOption->getCategoryArray(),
245
			[
246 4
				$article->category
247
			],
248
			[
249 4
				'id' => 'category',
250
				'name' => 'category'
251
			])
252 4
			->append('</li></ul>')
253
254
			/* customize */
255
256 4
			->radio(
257
			[
258 4
				'id' => self::class . '\Customize',
259
				'class' => 'rs-admin-fn-status-tab',
260
				'name' => self::class . '\Tab'
261
			])
262 4
			->label($this->_language->get('customize'),
0 ignored issues
show
Bug introduced by
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...
263
			[
264 4
				'class' => 'rs-admin-fn-toggle-tab rs-admin-label-tab',
265
				'for' => self::class . '\Customize'
266
			])
267 4
			->append('<ul class="rs-admin-fn-content-tab rs-admin-box-tab"><li>')
268 4
			->label($this->_language->get('headline'),
0 ignored issues
show
Bug introduced by
It seems like $this->_language->get('headline') 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...
269
			[
270 4
				'for' => 'headline'
271
			])
272 4
			->checkbox(!$article->id || $article->headline ?
273
			[
274 4
				'id' => 'headline',
275
				'class' => 'rs-admin-fn-status-switch',
276
				'name' => 'headline',
277
				'checked' => 'checked'
278
			] :
279
			[
280 4
				'id' => 'headline',
281
				'class' => 'rs-admin-fn-status-switch',
282
				'name' => 'headline'
283
			])
284 4
			->label(null,
285
			[
286 4
				'class' => 'rs-admin-label-switch',
287 4
				'for' => 'headline',
288 4
				'data-on' => $this->_language->get('enable'),
289 4
				'data-off' => $this->_language->get('disable')
290
			])
291 4
			->append('</li><li>')
292 4
			->label($this->_language->get('byline'),
0 ignored issues
show
Bug introduced by
It seems like $this->_language->get('byline') 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...
293
			[
294 4
				'for' => 'byline'
295
			])
296 4
			->checkbox(!$article->id || $article->byline ?
297
			[
298 4
				'id' => 'byline',
299
				'class' => 'rs-admin-fn-status-switch',
300
				'name' => 'byline',
301
				'checked' => 'checked'
302
			] :
303
			[
304 4
				'id' => 'byline',
305
				'class' => 'rs-admin-fn-status-switch',
306
				'name' => 'byline'
307
			])
308 4
			->label(null,
309
			[
310 4
				'class' => 'rs-admin-label-switch',
311 4
				'for' => 'byline',
312 4
				'data-on' => $this->_language->get('enable'),
313 4
				'data-off' => $this->_language->get('disable')
314
			])
315 4
			->append('</li><li>')
316 4
			->label($this->_language->get('comments'),
0 ignored issues
show
Bug introduced by
It seems like $this->_language->get('comments') 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...
317
			[
318 4
				'for' => 'comments'
319
			])
320 4
			->checkbox($article->comments ?
321
			[
322
				'id' => 'comments',
323
				'class' => 'rs-admin-fn-status-switch',
324
				'name' => 'comments',
325
				'checked' => 'checked'
326
			] :
327
			[
328 4
				'id' => 'comments',
329
				'class' => 'rs-admin-fn-status-switch',
330
				'name' => 'comments'
331
			])
332 4
			->label(null,
333
			[
334 4
				'class' => 'rs-admin-label-switch',
335 4
				'for' => 'comments',
336 4
				'data-on' => $this->_language->get('enable'),
337 4
				'data-off' => $this->_language->get('disable')
338
			])
339 4
			->append('</li><li>')
340 4
			->label($this->_language->get('status'),
0 ignored issues
show
Bug introduced by
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...
341
			[
342 4
				'for' => 'status'
343
			])
344 4
			->checkbox(!$article->id || $article->status ?
345
			[
346 4
				'id' => 'status',
347
				'class' => 'rs-admin-fn-status-switch',
348
				'name' => 'status',
349
				'checked' => 'checked'
350
			] :
351
			[
352 4
				'id' => 'status',
353
				'class' => 'rs-admin-fn-status-switch',
354
				'name' => 'status'
355
			])
356 4
			->label(null,
357
			[
358 4
				'class' => 'rs-admin-label-switch',
359 4
				'for' => 'status',
360 4
				'data-on' => $this->_language->get('publish'),
361 4
				'data-off' => $this->_language->get('unpublish')
362
			])
363 4
			->append('</li><li>')
364 4
			->label($this->_language->get('rank'),
0 ignored issues
show
Bug introduced by
It seems like $this->_language->get('rank') 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...
365
			[
366 4
				'for' => 'rank'
367
			])
368 4
			->number(
369
			[
370 4
				'id' => 'rank',
371 4
				'name' => 'rank',
372 4
				'value' => $article->id ? $article->rank : $articleModel->query()->max('rank') + 1
373
			])
374 4
			->append('</li>');
375 4
		if ($this->_registry->get('groupsEdit'))
376
		{
377
			$formElement
378 4
				->append('<li>')
379 4
				->label($this->_language->get('access'),
0 ignored issues
show
Bug introduced by
It seems like $this->_language->get('access') 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...
380
				[
381 4
					'for' => 'access'
382
				])
383 4
				->select($helperOption->getGroupArray(),
384 4
				(array)json_decode($article->access),
385
				[
386 4
					'id' => 'access',
387 4
					'name' => 'access[]',
388 4
					'multiple' => 'multiple',
389 4
					'size' => count($helperOption->getGroupArray())
390
				])
391 4
				->append('</li>');
392
		}
393
		$formElement
394 4
			->append('<li>')
395 4
			->label($this->_language->get('date'),
0 ignored issues
show
Bug introduced by
It seems like $this->_language->get('date') 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...
396
			[
397 4
				'for' => 'date'
398
			])
399 4
			->datetime(
400
			[
401 4
				'id' => 'date',
402 4
				'name' => 'date',
403 4
				'value' => $dater->formatField()
404
			])
405 4
			->append('</li></ul>')
406 4
			->hidden(
407
			[
408 4
				'name' => 'id',
409 4
				'value' => $article->id
410
			])
411 4
			->token()
412 4
			->append('<div class="rs-admin-wrapper-button">')
413 4
			->cancel();
414 4
		if ($article->id)
415
		{
416 2
			if ($this->_registry->get('articlesDelete'))
417
			{
418 1
				$formElement->delete();
419
			}
420 2
			if ($this->_registry->get('articlesEdit'))
421
			{
422 2
				$formElement->save();
423
			}
424
		}
425 2
		else if ($this->_registry->get('articlesNew'))
426
		{
427 1
			$formElement->create();
428
		}
429 4
		$formElement->append('</div>');
430
431
		/* collect output */
432
433 4
		$output .= $titleElement . $formElement;
434 4
		$output .= Module\Hook::trigger('adminArticleFormEnd');
435 4
		return $output;
436
	}
437
}
438