Completed
Push — master ( 0b670a...40cd7e )
by Nazar
04:20
created

Controller::edit_category()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 50
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 28
nc 1
nop 1
dl 0
loc 50
rs 9.3333
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package   Static Pages
4
 * @category  modules
5
 * @author    Nazar Mokrynskyi <[email protected]>
6
 * @copyright Copyright (c) 2011-2016, Nazar Mokrynskyi
7
 * @license   MIT License, see license.txt
8
 */
9
namespace cs\modules\Static_pages\admin;
10
use
11
	h,
12
	cs\Language\Prefix,
13
	cs\Page,
14
	cs\modules\Static_pages\Pages,
15
	cs\modules\Static_pages\Categories;
16
17
class Controller {
18
	/**
19
	 * @param \cs\Request $Request
20
	 */
21
	public static function index ($Request) {
22
		$L          = new Prefix('static_pages_');
23
		$Page       = Page::instance();
24
		$Pages      = Pages::instance();
25
		$Categories = Categories::instance();
26
		switch ($Request->data('mode')) {
27
			case 'add_category':
28
				if ($Categories->add($Request->data['parent'], $Request->data['title'], $Request->data['path'])) {
29
					$Page->success($L->changes_saved);
30
				} else {
31
					$Page->warning($L->changes_save_error);
32
				}
33
				break;
34
			case 'edit_category':
35
				if ($Categories->set($Request->data['id'], $Request->data['parent'], $Request->data['title'], $Request->data['path'])) {
36
					$Page->success($L->changes_saved);
37
				} else {
38
					$Page->warning($L->changes_save_error);
39
				}
40
				break;
41
			case 'delete_category':
42
				if ($Categories->del($Request->data['id'])) {
43
					$Page->success($L->changes_saved);
44
				} else {
45
					$Page->warning($L->changes_save_error);
46
				}
47
				break;
48
			case 'add_page':
49
				if ($Pages->add(
50
					$Request->data['category'],
51
					$Request->data['title'],
52
					$Request->data['path'],
53
					$Request->data['content'],
54
					$Request->data['interface']
55
				)
56
				) {
57
					$Page->success($L->changes_saved);
58
				} else {
59
					$Page->warning($L->changes_save_error);
60
				}
61
				break;
62
			case 'edit_page':
63
				if ($Pages->set(
64
					$Request->data['id'],
65
					$Request->data['category'],
66
					$Request->data['title'],
67
					$Request->data['path'],
68
					$Request->data['content'],
69
					$Request->data['interface']
70
				)
71
				) {
72
					$Page->success($L->changes_saved);
73
				} else {
74
					$Page->warning($L->changes_save_error);
75
				}
76
				break;
77
			case 'delete_page':
78
				if ($Pages->del($Request->data['id'])) {
79
					$Page->success($L->changes_saved);
80
				} else {
81
					$Page->warning($L->changes_save_error);
82
				}
83
				break;
84
		}
85
	}
86
	/**
87
	 * @return string
88
	 */
89
	public static function browse_categories () {
90
		$L = new Prefix('static_pages_');
91
		return
92
			h::{'table.cs-table[list]'}(
93
				h::{'tr th'}(
94
					[
95
						$L->pages_category,
96
						[
97
							'style' => 'width: 80%'
98
						]
99
					],
100
					$L->action
101
				).
102
				h::{'tr| td'}(
103
					static::get_categories_rows()
104
				)
105
			).
106
			h::{'p.cs-text-left'}($L->index_page_path).
107
			h::{'p.cs-text-left a[is=cs-link-button]'}(
108
				[
109
					$L->add_category,
110
					[
111
						'href' => 'admin/Static_pages/add_category'
112
					]
113
				],
114
				[
115
					$L->add_page,
116
					[
117
						'href' => 'admin/Static_pages/add_page'
118
					]
119
				]
120
			);
121
	}
122
	/**
123
	 * @param \cs\Request $Request
124
	 */
125
	public static function add_category ($Request) {
126
		$L = new Prefix('static_pages_');
127
		Page::instance()
128
			->title($L->addition_of_page_category)
129
			->content(
130
				h::{'form[is=cs-form][action=admin/Static_pages]'}(
131
					h::h2($L->addition_of_page_category).
132
					h::label($L->parent_category).
133
					h::{'select[is=cs-select][name=parent][size=5]'}(
134
						static::get_categories_list(),
135
						[
136
							'selected' => isset($Request->route[1]) ? (int)$Request->route[1] : 0
137
						]
138
					).
139
					h::label($L->category_title).
140
					h::{'input[is=cs-input-text][name=title]'}().
141
					h::{'label info'}('static_pages_category_path').
142
					h::{'input[is=cs-input-text][name=path]'}().
143
					h::p(
144
						h::{'button[is=cs-button][type=submit][name=mode][value=add_category]'}(
145
							$L->save,
146
							[
147
								'tooltip' => $L->save_info
148
							]
149
						).
150
						h::{'button[is=cs-button][type=button]'}(
151
							$L->cancel,
152
							[
153
								'onclick' => 'history.go(-1);'
154
							]
155
						)
156
					)
157
				)
158
			);
159
	}
160
	/**
161
	 * @param \cs\Request $Request
162
	 */
163
	public static function edit_category ($Request) {
164
		$L    = new Prefix('static_pages_');
165
		$id   = (int)$Request->route[1];
166
		$data = Categories::instance()->get($id);
167
		Page::instance()
168
			->title($L->editing_of_page_category($data['title']))
169
			->content(
170
				h::{'form[is=cs-form][action=admin/Static_pages]'}(
171
					h::h2($L->editing_of_page_category($data['title'])).
172
					h::label($L->parent_category).
173
					h::{'select[is=cs-select][name=parent][size=5]'}(
174
						static::get_categories_list($id),
175
						[
176
							'selected' => $data['parent']
177
						]
178
					).
179
					h::label($L->category_title).
180
					h::{'input[is=cs-input-text][name=title]'}(
181
						[
182
							'value' => $data['title']
183
						]
184
					).
185
					h::{'label info'}('static_pages_category_path').
186
					h::{'input[is=cs-input-text][name=path]'}(
187
						[
188
							'value' => $data['path']
189
						]
190
					).
191
					h::{'input[type=hidden][name=id]'}(
192
						[
193
							'value' => $id
194
						]
195
					).
196
					h::p(
197
						h::{'button[is=cs-button][type=submit][name=mode][value=edit_category]'}(
198
							$L->save,
199
							[
200
								'tooltip' => $L->save_info
201
							]
202
						).
203
						h::{'button[is=cs-button][type=button]'}(
204
							$L->cancel,
205
							[
206
								'onclick' => 'history.go(-1);'
207
							]
208
						)
209
					)
210
				)
211
			);
212
	}
213
	/**
214
	 * @param \cs\Request $Request
215
	 */
216
	public static function delete_category ($Request) {
217
		$L     = new Prefix('static_pages_');
218
		$id    = (int)$Request->route[1];
219
		$title = Categories::instance()->get($id)['title'];
220
		Page::instance()
221
			->title($L->deletion_of_page_category($title))
222
			->content(
223
				h::{'form[is=cs-form][action=admin/Static_pages]'}(
224
					h::{'h2.cs-text-center'}(
225
						$L->sure_to_delete_page_category($title)
226
					).
227
					h::{'input[type=hidden][name=id]'}(
228
						[
229
							'value' => $id
230
						]
231
					).
232
					h::p(
233
						h::{'button[is=cs-button][type=submit][name=mode][value=delete_category]'}(
234
							$L->yes
235
						).
236
						h::{'button[is=cs-button][type=button]'}(
237
							$L->cancel,
238
							[
239
								'onclick' => 'history.go(-1);'
240
							]
241
						)
242
					)
243
				)
244
			);
245
	}
246
	/**
247
	 * @param \cs\Request $Request
248
	 *
249
	 * @return string
250
	 */
251
	public static function browse_pages ($Request) {
252
		$L  = new Prefix('static_pages_');
253
		$rc = $Request->route;
254
		return
255
			h::{'table.cs-table[list]'}(
256
				h::{'tr th'}(
257
					[
258
						$L->page_title,
259
						[
260
							'style' => 'width: 80%'
261
						]
262
					],
263
					$L->action
264
				).
265
				h::{'tr| td'}(
266
					static::get_pages_rows($Request)
267
				)
268
			).
269
			h::{'p.cs-text-left a[is=cs-link-button]'}(
270
				$L->add_page,
271
				[
272
					'href' => 'admin/Static_pages/add_page/'.array_slice($rc, -1)[0]
273
				]
274
			);
275
	}
276
	/**
277
	 * @param \cs\Request $Request
278
	 */
279
	public static function add_page ($Request) {
280
		$L = new Prefix('static_pages_');
281
		Page::instance()->title($L->adding_of_page)
282
			->content(
283
				h::{'form[is=cs-form][action=admin/Static_pages]'}(
284
					h::h2(
285
						$L->adding_of_page
286
					).
287
					h::{'table.cs-table[center] tr'}(
288
						h::th(
289
							$L->category,
290
							$L->page_title,
291
							h::info('static_pages_page_path'),
292
							h::info('static_pages_page_interface')
293
						),
294
						h::td(
295
							h::{'select[is=cs-select][full-width][name=category][size=5]'}(
296
								static::get_categories_list(),
297
								[
298
									'selected' => isset($Request->route[1]) ? (int)$Request->route[1] : 0
299
								]
300
							),
301
							h::{'input[is=cs-input-text][full-width][name=title]'}(),
302
							h::{'input[is=cs-input-text][full-width][name=path]'}(),
303
							h::{'div radio[name=interface]'}(
304
								[
305
									'checked' => 1,
306
									'value'   => [0, 1],
307
									'in'      => [$L->off, $L->on]
308
								]
309
							)
310
						)
311
					).
312
					h::{'table.cs-table[center] tr'}(
313
						h::th($L->content),
314
						h::{'td cs-editor textarea[cs-textarea][autosize][name=content]'}()
315
					).
316
					h::p(
317
						h::{'button[is=cs-button][type=submit][name=mode][value=add_page]'}(
318
							$L->save,
319
							[
320
								'tooltip' => $L->save_info
321
							]
322
						).
323
						h::{'button[is=cs-button][type=button]'}(
324
							$L->cancel,
325
							[
326
								'onclick' => 'history.go(-1);'
327
							]
328
						)
329
					)
330
				)
331
			);
332
	}
333
	/**
334
	 * @param \cs\Request $Request
335
	 */
336
	public static function edit_page ($Request) {
337
		$L        = new Prefix('static_pages_');
338
		$id       = (int)$Request->route[1];
339
		$data     = Pages::instance()->get($id);
340
		$textarea = h::{'textarea[is=cs-textarea][autosize][name=content]'}($data['content']);
341
		Page::instance()
342
			->title($L->editing_of_page($data['title']))
343
			->content(
344
				h::{'form[is=cs-form][action=admin/Static_pages]'}(
345
					h::h2(
346
						$L->editing_of_page($data['title'])
347
					).
348
					h::{'table.cs-table[center] tr'}(
349
						h::th(
350
							$L->category,
351
							$L->page_title,
352
							h::info('static_pages_page_path'),
353
							h::info('static_pages_page_interface')
354
						),
355
						h::td(
356
							h::{'select[is=cs-select][full-width][name=category][size=5]'}(
357
								static::get_categories_list(),
358
								[
359
									'selected' => $data['category']
360
								]
361
							),
362
							h::{'input[is=cs-input-text][full-width][name=title]'}(
363
								[
364
									'value' => $data['title']
365
								]
366
							),
367
							h::{'input[is=cs-input-text][full-width][name=path]'}(
368
								[
369
									'value' => $data['path']
370
								]
371
							),
372
							h::{'div radio[name=interface]'}(
373
								[
374
									'checked' => $data['interface'],
375
									'value'   => [0, 1],
376
									'in'      => [$L->off, $L->on]
377
								]
378
							)
379
						)
380
					).
381
					h::{'table.cs-table[center] tr'}(
382
						h::th($L->content),
383
						h::td(
384
							$data['interface'] ? h::cs_editor($textarea) : $textarea
385
						)
386
					).
387
					h::{'input[type=hidden][name=id]'}(
388
						[
389
							'value' => $id
390
						]
391
					).
392
					h::p(
393
						h::{'button[is=cs-button][type=submit][name=mode][value=edit_page]'}(
394
							$L->save,
395
							[
396
								'tooltip' => $L->save_info
397
							]
398
						).
399
						h::{'button[is=cs-button][type=button]'}(
400
							$L->cancel,
401
							[
402
								'onclick' => 'history.go(-1);'
403
							]
404
						)
405
					)
406
				)
407
			);
408
	}
409
	/**
410
	 * @param \cs\Request $Request
411
	 */
412
	public static function delete_page ($Request) {
413
		$L     = new Prefix('static_pages_');
414
		$id    = (int)$Request->route[1];
415
		$title = Pages::instance()->get($id)['title'];
416
		Page::instance()
417
			->title($L->deletion_of_page($title))
418
			->content(
419
				h::{'form[is=cs-form][action=admin/Static_pages]'}(
420
					h::{'h2.cs-text-center'}(
421
						$L->sure_to_delete_page($title)
422
					).
423
					h::{'input[type=hidden][name=id]'}(
424
						[
425
							'value' => $id
426
						]
427
					).
428
					h::p(
429
						h::{'button[is=cs-button][type=submit][name=mode][value=delete_page]'}(
430
							$L->yes
431
						).
432
						h::{'button[is=cs-button][type=button]'}(
433
							$L->cancel,
434
							[
435
								'onclick' => 'history.go(-1);'
436
							]
437
						)
438
					)
439
				)
440
			);
441
	}
442
	/**
443
	 * @param array|null $structure
444
	 * @param int        $level
445
	 * @param array      $parent_categories
446
	 *
447
	 * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<array|string>[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
448
	 */
449
	protected static function get_categories_rows ($structure = null, $level = 0, $parent_categories = []) {
450
		$L    = new Prefix('static_pages_');
451
		$root = false;
452
		if ($structure === null) {
453
			$structure          = Pages::instance()->get_structure();
454
			$structure['title'] = $L->root_category;
455
			$root               = true;
456
		}
457
		$parent_categories[] = $structure['id'];
458
		$content             = [
459
			[
460
				[
461
					h::a(
462
						$structure['title'].
463
						h::{'b.cs-static-pages-count'}(
464
							count($structure['pages']),
465
							[
466
								'tooltip' => $L->pages_in_category
467
							]
468
						),
469
						[
470
							'href' => 'admin/Static_pages/browse_pages/'.implode('/', $parent_categories)
471
						]
472
					),
473
					[
474
						'class' => "cs-static-pages-padding-left-$level"
475
					]
476
				],
477
				h::{'a[is=cs-link-button][icon=plus]'}(
478
					[
479
						'href'    => "admin/Static_pages/add_category/$structure[id]",
480
						'tooltip' => $L->add_subcategory
481
					]
482
				).
483
				h::{'a[is=cs-link-button][icon=file-text]'}(
484
					[
485
						'href'    => "admin/Static_pages/add_page/$structure[id]",
486
						'tooltip' => $L->add_page
487
					]
488
				).
489
				(!$root ?
490
					h::{'a[is=cs-link-button][icon=pencil]'}(
491
						[
492
							'href'    => "admin/Static_pages/edit_category/$structure[id]",
493
							'tooltip' => $L->edit
494
						]
495
					).
496
					h::{'a[is=cs-link-button][icon=trash]'}(
497
						[
498
							'href'    => "admin/Static_pages/delete_category/$structure[id]",
499
							'tooltip' => $L->delete
500
						]
501
					)
502
					: false
503
				)
504
			]
505
		];
506
		if (!empty($structure['categories'])) {
507
			foreach ($structure['categories'] as $category) {
508
				$content = array_merge($content, static::get_categories_rows($category, $level + 1, $parent_categories));
509
			}
510
		}
511
		return $content;
512
	}
513
	/**
514
	 * @param int|null   $current
515
	 * @param array|null $structure
516
	 * @param int        $level
517
	 *
518
	 * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,array>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
519
	 */
520
	protected static function get_categories_list ($current = null, $structure = null, $level = 0) {
521
		$list = [
522
			'in'    => [],
523
			'value' => []
524
		];
525
		if ($structure === null) {
526
			$structure       = Pages::instance()->get_structure();
527
			$L               = new Prefix('static_pages_');
528
			$list['in'][]    = $L->root_category;
529
			$list['value'][] = 0;
530
		} else {
531
			if ($structure['id'] == $current) {
532
				return $list;
533
			}
534
			$list['in'][]    = str_repeat('&nbsp;', $level).$structure['title'];
535
			$list['value'][] = $structure['id'];
536
		}
537
		if (!empty($structure['categories'])) {
538
			foreach ($structure['categories'] as $category) {
539
				$tmp           = static::get_categories_list($current, $category, $level + 1);
540
				$list['in']    = array_merge($list['in'], $tmp['in']);
541
				$list['value'] = array_merge($list['value'], $tmp['value']);
542
			}
543
		}
544
		return $list;
545
	}
546
	/**
547
	 * @param \cs\Request $Request
548
	 *
549
	 * @return array
550
	 */
551
	protected static function get_pages_rows ($Request) {
552
		$L          = new Prefix('static_pages_');
553
		$Pages      = Pages::instance();
554
		$Categories = Categories::instance();
555
		$categories = array_slice($Request->route, 2);
556
		$structure  = $Pages->get_structure();
557
		$path       = [];
558
		if (!empty($categories)) {
559
			foreach ($categories as $category) {
560
				$category = $Categories->get($category)['path'];
561
				if (isset($structure['categories'][$category])) {
562
					$structure = $structure['categories'][$category];
563
					$path[]    = $structure['path'];
564
				}
565
			}
566
		}
567
		Page::instance()->title($structure['id'] == 0 ? $L->root_category : $structure['title']);
568
		$path    = !empty($path) ? implode('/', $path).'/' : '';
569
		$content = [];
570
		if (!empty($structure['pages'])) {
571
			foreach ($structure['pages'] as &$page) {
572
				$page      = $Pages->get($page);
573
				$content[] = [
574
					[
575
						h::a(
576
							$page['title'],
577
							[
578
								'href' => $path.$page['path']
579
							]
580
						),
581
						[
582
							'class' => 'cs-static-pages-padding-left-0'
583
						]
584
					],
585
					h::{'a[is=cs-link-button][icon=file-text]'}(
586
						[
587
							'href'    => "admin/Static_pages/edit_page/$page[id]",
588
							'tooltip' => $L->edit
589
						]
590
					).
591
					h::{'a[is=cs-link-button][icon=trash]'}(
592
						[
593
							'href'    => "admin/Static_pages/delete_page/$page[id]",
594
							'tooltip' => $L->delete
595
						]
596
					)
597
				];
598
			}
599
			unset($page);
600
		}
601
		return $content;
602
	}
603
}
604