Completed
Push — master ( de9e2d...f59dfb )
by Henry
08:37
created

ArticleForm::render()   C

Complexity

Conditions 14
Paths 12

Size

Total Lines 355

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 174
CRAP Score 14

Importance

Changes 0
Metric Value
dl 0
loc 355
ccs 174
cts 174
cp 1
rs 5.0133
c 0
b 0
f 0
cc 14
nc 12
nop 1
crap 14

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