Completed
Push — master ( 825b72...88de5a )
by Nazar
05:05
created

Controller   A

Complexity

Total Complexity 31

Size/Duplication

Total Lines 479
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 479
rs 9.8
c 0
b 0
f 0
wmc 31
lcom 1
cbo 6

11 Methods

Rating   Name   Duplication   Size   Complexity  
C index() 0 65 13
A browse_categories() 0 3 1
B add_category() 0 32 1
A edit_category() 0 50 1
B delete_category() 0 30 1
B browse_pages() 0 25 1
A add_page() 0 54 2
A edit_page() 0 73 2
B delete_page() 0 30 1
B get_categories_list() 0 26 5
A get_pages_rows() 0 51 3
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
		return h::cs_static_pages_admin_categories_list();
91
	}
92
	public static function add_category () {
93
		$L = new Prefix('static_pages_');
94
		Page::instance()
95
			->title($L->addition_of_page_category)
96
			->content(
97
				h::{'form[is=cs-form][action=admin/Static_pages]'}(
98
					h::h2($L->addition_of_page_category).
99
					h::label($L->parent_category).
100
					h::{'select[is=cs-select][name=parent][size=5]'}(
101
						static::get_categories_list()
102
					).
103
					h::label($L->category_title).
104
					h::{'input[is=cs-input-text][name=title]'}().
105
					h::{'label info'}('static_pages_category_path').
106
					h::{'input[is=cs-input-text][name=path]'}().
107
					h::p(
108
						h::{'button[is=cs-button][type=submit][name=mode][value=add_category]'}(
109
							$L->save,
110
							[
111
								'tooltip' => $L->save_info
112
							]
113
						).
114
						h::{'button[is=cs-button][type=button]'}(
115
							$L->cancel,
116
							[
117
								'onclick' => 'history.go(-1);'
118
							]
119
						)
120
					)
121
				)
122
			);
123
	}
124
	/**
125
	 * @param \cs\Request $Request
126
	 */
127
	public static function edit_category ($Request) {
128
		$L    = new Prefix('static_pages_');
129
		$id   = (int)$Request->route[1];
130
		$data = Categories::instance()->get($id);
131
		Page::instance()
132
			->title($L->editing_of_page_category($data['title']))
133
			->content(
134
				h::{'form[is=cs-form][action=admin/Static_pages]'}(
135
					h::h2($L->editing_of_page_category($data['title'])).
136
					h::label($L->parent_category).
137
					h::{'select[is=cs-select][name=parent][size=5]'}(
138
						static::get_categories_list($id),
139
						[
140
							'selected' => $data['parent']
141
						]
142
					).
143
					h::label($L->category_title).
144
					h::{'input[is=cs-input-text][name=title]'}(
145
						[
146
							'value' => $data['title']
147
						]
148
					).
149
					h::{'label info'}('static_pages_category_path').
150
					h::{'input[is=cs-input-text][name=path]'}(
151
						[
152
							'value' => $data['path']
153
						]
154
					).
155
					h::{'input[type=hidden][name=id]'}(
156
						[
157
							'value' => $id
158
						]
159
					).
160
					h::p(
161
						h::{'button[is=cs-button][type=submit][name=mode][value=edit_category]'}(
162
							$L->save,
163
							[
164
								'tooltip' => $L->save_info
165
							]
166
						).
167
						h::{'button[is=cs-button][type=button]'}(
168
							$L->cancel,
169
							[
170
								'onclick' => 'history.go(-1);'
171
							]
172
						)
173
					)
174
				)
175
			);
176
	}
177
	/**
178
	 * @param \cs\Request $Request
179
	 */
180
	public static function delete_category ($Request) {
181
		$L     = new Prefix('static_pages_');
182
		$id    = (int)$Request->route[1];
183
		$title = Categories::instance()->get($id)['title'];
184
		Page::instance()
185
			->title($L->deletion_of_page_category($title))
186
			->content(
187
				h::{'form[is=cs-form][action=admin/Static_pages]'}(
188
					h::{'h2.cs-text-center'}(
189
						$L->sure_to_delete_page_category($title)
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=delete_category]'}(
198
							$L->yes
199
						).
200
						h::{'button[is=cs-button][type=button]'}(
201
							$L->cancel,
202
							[
203
								'onclick' => 'history.go(-1);'
204
							]
205
						)
206
					)
207
				)
208
			);
209
	}
210
	/**
211
	 * @param \cs\Request $Request
212
	 *
213
	 * @return string
214
	 */
215
	public static function browse_pages ($Request) {
216
		$L  = new Prefix('static_pages_');
217
		$rc = $Request->route;
218
		return
219
			h::{'table.cs-table[list]'}(
220
				h::{'tr th'}(
221
					[
222
						$L->page_title,
223
						[
224
							'style' => 'width: 80%'
225
						]
226
					],
227
					$L->action
228
				).
229
				h::{'tr| td'}(
230
					static::get_pages_rows($Request)
231
				)
232
			).
233
			h::{'p.cs-text-left a[is=cs-link-button]'}(
234
				$L->add_page,
235
				[
236
					'href' => 'admin/Static_pages/add_page/'.array_slice($rc, -1)[0]
237
				]
238
			);
239
	}
240
	/**
241
	 * @param \cs\Request $Request
242
	 */
243
	public static function add_page ($Request) {
244
		$L = new Prefix('static_pages_');
245
		Page::instance()->title($L->adding_of_page)
246
			->content(
247
				h::{'form[is=cs-form][action=admin/Static_pages]'}(
248
					h::h2(
249
						$L->adding_of_page
250
					).
251
					h::{'table.cs-table[center] tr'}(
252
						h::th(
253
							$L->category,
254
							$L->page_title,
255
							h::info('static_pages_page_path'),
256
							h::info('static_pages_page_interface')
257
						),
258
						h::td(
259
							h::{'select[is=cs-select][full-width][name=category][size=5]'}(
260
								static::get_categories_list(),
261
								[
262
									'selected' => isset($Request->route[1]) ? (int)$Request->route[1] : 0
263
								]
264
							),
265
							h::{'input[is=cs-input-text][full-width][name=title]'}(),
266
							h::{'input[is=cs-input-text][full-width][name=path]'}(),
267
							h::{'div radio[name=interface]'}(
268
								[
269
									'checked' => 1,
270
									'value'   => [0, 1],
271
									'in'      => [$L->off, $L->on]
272
								]
273
							)
274
						)
275
					).
276
					h::{'table.cs-table[center] tr'}(
277
						h::th($L->content),
278
						h::{'td cs-editor textarea[cs-textarea][autosize][name=content]'}()
279
					).
280
					h::p(
281
						h::{'button[is=cs-button][type=submit][name=mode][value=add_page]'}(
282
							$L->save,
283
							[
284
								'tooltip' => $L->save_info
285
							]
286
						).
287
						h::{'button[is=cs-button][type=button]'}(
288
							$L->cancel,
289
							[
290
								'onclick' => 'history.go(-1);'
291
							]
292
						)
293
					)
294
				)
295
			);
296
	}
297
	/**
298
	 * @param \cs\Request $Request
299
	 */
300
	public static function edit_page ($Request) {
301
		$L        = new Prefix('static_pages_');
302
		$id       = (int)$Request->route[1];
303
		$data     = Pages::instance()->get($id);
304
		$textarea = h::{'textarea[is=cs-textarea][autosize][name=content]'}($data['content']);
305
		Page::instance()
306
			->title($L->editing_of_page($data['title']))
307
			->content(
308
				h::{'form[is=cs-form][action=admin/Static_pages]'}(
309
					h::h2(
310
						$L->editing_of_page($data['title'])
311
					).
312
					h::{'table.cs-table[center] tr'}(
313
						h::th(
314
							$L->category,
315
							$L->page_title,
316
							h::info('static_pages_page_path'),
317
							h::info('static_pages_page_interface')
318
						),
319
						h::td(
320
							h::{'select[is=cs-select][full-width][name=category][size=5]'}(
321
								static::get_categories_list(),
322
								[
323
									'selected' => $data['category']
324
								]
325
							),
326
							h::{'input[is=cs-input-text][full-width][name=title]'}(
327
								[
328
									'value' => $data['title']
329
								]
330
							),
331
							h::{'input[is=cs-input-text][full-width][name=path]'}(
332
								[
333
									'value' => $data['path']
334
								]
335
							),
336
							h::{'div radio[name=interface]'}(
337
								[
338
									'checked' => $data['interface'],
339
									'value'   => [0, 1],
340
									'in'      => [$L->off, $L->on]
341
								]
342
							)
343
						)
344
					).
345
					h::{'table.cs-table[center] tr'}(
346
						h::th($L->content),
347
						h::td(
348
							$data['interface'] ? h::cs_editor($textarea) : $textarea
349
						)
350
					).
351
					h::{'input[type=hidden][name=id]'}(
352
						[
353
							'value' => $id
354
						]
355
					).
356
					h::p(
357
						h::{'button[is=cs-button][type=submit][name=mode][value=edit_page]'}(
358
							$L->save,
359
							[
360
								'tooltip' => $L->save_info
361
							]
362
						).
363
						h::{'button[is=cs-button][type=button]'}(
364
							$L->cancel,
365
							[
366
								'onclick' => 'history.go(-1);'
367
							]
368
						)
369
					)
370
				)
371
			);
372
	}
373
	/**
374
	 * @param \cs\Request $Request
375
	 */
376
	public static function delete_page ($Request) {
377
		$L     = new Prefix('static_pages_');
378
		$id    = (int)$Request->route[1];
379
		$title = Pages::instance()->get($id)['title'];
380
		Page::instance()
381
			->title($L->deletion_of_page($title))
382
			->content(
383
				h::{'form[is=cs-form][action=admin/Static_pages]'}(
384
					h::{'h2.cs-text-center'}(
385
						$L->sure_to_delete_page($title)
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=delete_page]'}(
394
							$L->yes
395
						).
396
						h::{'button[is=cs-button][type=button]'}(
397
							$L->cancel,
398
							[
399
								'onclick' => 'history.go(-1);'
400
							]
401
						)
402
					)
403
				)
404
			);
405
	}
406
	/**
407
	 * @param int|null   $current
408
	 * @param array|null $structure
409
	 * @param int        $level
410
	 *
411
	 * @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...
412
	 */
413
	protected static function get_categories_list ($current = null, $structure = null, $level = 0) {
414
		$list = [
415
			'in'    => [],
416
			'value' => []
417
		];
418
		if ($structure === null) {
419
			$structure       = Pages::instance()->get_structure();
420
			$L               = new Prefix('static_pages_');
421
			$list['in'][]    = $L->root_category;
422
			$list['value'][] = 0;
423
		} else {
424
			if ($structure['id'] == $current) {
425
				return $list;
426
			}
427
			$list['in'][]    = str_repeat('&nbsp;', $level).$structure['title'];
428
			$list['value'][] = $structure['id'];
429
		}
430
		if (!empty($structure['categories'])) {
431
			foreach ($structure['categories'] as $category) {
432
				$tmp           = static::get_categories_list($current, $category, $level + 1);
433
				$list['in']    = array_merge($list['in'], $tmp['in']);
434
				$list['value'] = array_merge($list['value'], $tmp['value']);
435
			}
436
		}
437
		return $list;
438
	}
439
	/**
440
	 * @param \cs\Request $Request
441
	 *
442
	 * @return array
443
	 */
444
	protected static function get_pages_rows ($Request) {
445
		$L          = new Prefix('static_pages_');
446
		$Page       = Page::instance();
447
		$Pages      = Pages::instance();
448
		$Categories = Categories::instance();
449
		$category   = $Request->route_ids(0);
450
		if (!$category) {
451
			$category = [
452
				'id'         => 0,
453
				'full_title' => $L->root_category,
454
				'full_path'  => ''
455
			];
456
		} else {
457
			/**
458
			 * @var array $category
459
			 */
460
			$category = $Categories->get($category);
461
		}
462
		$Page->title($category['full_title']);
463
		$path    = $category['full_path'];
464
		$content = [];
465
		/** @noinspection ForeachSourceInspection */
466
		foreach ($Pages->get($Pages->get_for_category($category['id'])) as $page) {
1 ignored issue
show
Bug introduced by
The expression $Pages->get($Pages->get_...egory($category['id'])) of type array|false is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
467
			$content[] = [
468
				[
469
					h::a(
470
						$page['title'],
471
						[
472
							'href' => $path.$page['path']
473
						]
474
					),
475
					[
476
						'class' => 'cs-static-pages-padding-left-0'
477
					]
478
				],
479
				h::{'a[is=cs-link-button][icon=file-text]'}(
480
					[
481
						'href'    => "admin/Static_pages/edit_page/$page[id]",
482
						'tooltip' => $L->edit
483
					]
484
				).
485
				h::{'a[is=cs-link-button][icon=trash]'}(
486
					[
487
						'href'    => "admin/Static_pages/delete_page/$page[id]",
488
						'tooltip' => $L->delete
489
					]
490
				)
491
			];
492
		}
493
		return $content;
494
	}
495
}
496