Completed
Push — master ( 96a033...7625bb )
by Henry
07:07
created

ExtraForm::render()   D

Complexity

Conditions 15
Paths 12

Size

Total Lines 313

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 240

Importance

Changes 0
Metric Value
dl 0
loc 313
ccs 0
cts 140
cp 0
rs 4.7333
c 0
b 0
f 0
cc 15
nc 12
nop 1
crap 240

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