Completed
Push — master ( 4eb4a8...a52438 )
by Henry
07:48
created

includes/Admin/View/ArticleForm.php (1 issue)

Labels
Severity

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\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);
0 ignored issues
show
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected T_OBJECT_OPERATOR
Loading history...
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?->id ? $article?->title : $this->_language->get('article_new'));
55 4
		$formElement = new Admin\Html\Form($this->_registry, $this->_language);
56 4
		$formElement->init(
57
		[
58
			'form' =>
59
			[
60 4
				'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'),
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'),
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'),
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'),
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'),
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'),
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'),
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'),
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'),
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'),
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'),
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'),
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'),
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'),
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'),
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'),
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'),
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'),
365
			[
366 4
				'for' => 'rank'
367
			])
368 4
			->number(
369
			[
370 4
				'id' => 'rank',
371 4
				'name' => 'rank',
372 4
				'value' => $article?->title ? $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'),
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'),
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
		if ($article?->id)
407
		{
408 4
			$formElement
409 4
				->hidden(
410
				[
411 4
					'name' => 'id',
412 4
					'value' => $article?->id
413 4
				]);
414 4
		}
415
		$formElement
416 2
			->token()
417
			->append('<div class="rs-admin-wrapper-button">')
418 1
			->cancel();
419
		if ($article?->id)
420 2
		{
421
			if ($this->_registry->get('articlesDelete'))
422 2
			{
423
				$formElement->delete();
424
			}
425 2
			if ($this->_registry->get('articlesEdit'))
426
			{
427 1
				$formElement->save();
428
			}
429 4
		}
430
		else if ($this->_registry->get('articlesNew'))
431
		{
432
			$formElement->create();
433 4
		}
434 4
		$formElement->append('</div>');
435 4
436
		/* collect output */
437
438
		$output .= $titleElement . $formElement;
439
		$output .= Module\Hook::trigger('adminArticleFormEnd');
440
		return $output;
441
	}
442
}
443