Completed
Push — master ( b07195...7e4a95 )
by Nazar
05:05 queued 21s
created

Controller::delete_page()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 18
nc 1
nop 1
dl 0
loc 30
rs 8.8571
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
		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
		$category = $Request->route_ids(0);
218
		Page::instance()->title(
219
			$category ? Categories::instance()->get($category)['title'] : $L->root_category
220
		);
221
		return h::cs_static_pages_admin_pages_list(
222
			[
223
				'category' => $category
224
			]
225
		);
226
	}
227
	/**
228
	 * @param \cs\Request $Request
229
	 */
230
	public static function add_page ($Request) {
231
		$L = new Prefix('static_pages_');
232
		Page::instance()->title($L->adding_of_page)
233
			->content(
234
				h::{'form[is=cs-form][action=admin/Static_pages]'}(
235
					h::h2(
236
						$L->adding_of_page
237
					).
238
					h::{'table.cs-table[center] tr'}(
239
						h::th(
240
							$L->category,
241
							$L->page_title,
242
							h::info('static_pages_page_path'),
243
							h::info('static_pages_page_interface')
244
						),
245
						h::td(
246
							h::{'select[is=cs-select][full-width][name=category][size=5]'}(
247
								static::get_categories_list(),
248
								[
249
									'selected' => isset($Request->route[1]) ? (int)$Request->route[1] : 0
250
								]
251
							),
252
							h::{'input[is=cs-input-text][full-width][name=title]'}(),
253
							h::{'input[is=cs-input-text][full-width][name=path]'}(),
254
							h::{'div radio[name=interface]'}(
255
								[
256
									'checked' => 1,
257
									'value'   => [0, 1],
258
									'in'      => [$L->off, $L->on]
259
								]
260
							)
261
						)
262
					).
263
					h::{'table.cs-table[center] tr'}(
264
						h::th($L->content),
265
						h::{'td cs-editor textarea[cs-textarea][autosize][name=content]'}()
266
					).
267
					h::p(
268
						h::{'button[is=cs-button][type=submit][name=mode][value=add_page]'}(
269
							$L->save,
270
							[
271
								'tooltip' => $L->save_info
272
							]
273
						).
274
						h::{'button[is=cs-button][type=button]'}(
275
							$L->cancel,
276
							[
277
								'onclick' => 'history.go(-1);'
278
							]
279
						)
280
					)
281
				)
282
			);
283
	}
284
	/**
285
	 * @param \cs\Request $Request
286
	 */
287
	public static function edit_page ($Request) {
288
		$L        = new Prefix('static_pages_');
289
		$id       = (int)$Request->route[1];
290
		$data     = Pages::instance()->get($id);
291
		$textarea = h::{'textarea[is=cs-textarea][autosize][name=content]'}($data['content']);
292
		Page::instance()
293
			->title($L->editing_of_page($data['title']))
294
			->content(
295
				h::{'form[is=cs-form][action=admin/Static_pages]'}(
296
					h::h2(
297
						$L->editing_of_page($data['title'])
298
					).
299
					h::{'table.cs-table[center] tr'}(
300
						h::th(
301
							$L->category,
302
							$L->page_title,
303
							h::info('static_pages_page_path'),
304
							h::info('static_pages_page_interface')
305
						),
306
						h::td(
307
							h::{'select[is=cs-select][full-width][name=category][size=5]'}(
308
								static::get_categories_list(),
309
								[
310
									'selected' => $data['category']
311
								]
312
							),
313
							h::{'input[is=cs-input-text][full-width][name=title]'}(
314
								[
315
									'value' => $data['title']
316
								]
317
							),
318
							h::{'input[is=cs-input-text][full-width][name=path]'}(
319
								[
320
									'value' => $data['path']
321
								]
322
							),
323
							h::{'div radio[name=interface]'}(
324
								[
325
									'checked' => $data['interface'],
326
									'value'   => [0, 1],
327
									'in'      => [$L->off, $L->on]
328
								]
329
							)
330
						)
331
					).
332
					h::{'table.cs-table[center] tr'}(
333
						h::th($L->content),
334
						h::td(
335
							$data['interface'] ? h::cs_editor($textarea) : $textarea
336
						)
337
					).
338
					h::{'input[type=hidden][name=id]'}(
339
						[
340
							'value' => $id
341
						]
342
					).
343
					h::p(
344
						h::{'button[is=cs-button][type=submit][name=mode][value=edit_page]'}(
345
							$L->save,
346
							[
347
								'tooltip' => $L->save_info
348
							]
349
						).
350
						h::{'button[is=cs-button][type=button]'}(
351
							$L->cancel,
352
							[
353
								'onclick' => 'history.go(-1);'
354
							]
355
						)
356
					)
357
				)
358
			);
359
	}
360
	/**
361
	 * @param \cs\Request $Request
362
	 */
363
	public static function delete_page ($Request) {
364
		$L     = new Prefix('static_pages_');
365
		$id    = (int)$Request->route[1];
366
		$title = Pages::instance()->get($id)['title'];
367
		Page::instance()
368
			->title($L->deletion_of_page($title))
369
			->content(
370
				h::{'form[is=cs-form][action=admin/Static_pages]'}(
371
					h::{'h2.cs-text-center'}(
372
						$L->sure_to_delete_page($title)
373
					).
374
					h::{'input[type=hidden][name=id]'}(
375
						[
376
							'value' => $id
377
						]
378
					).
379
					h::p(
380
						h::{'button[is=cs-button][type=submit][name=mode][value=delete_page]'}(
381
							$L->yes
382
						).
383
						h::{'button[is=cs-button][type=button]'}(
384
							$L->cancel,
385
							[
386
								'onclick' => 'history.go(-1);'
387
							]
388
						)
389
					)
390
				)
391
			);
392
	}
393
	/**
394
	 * @param int|null   $current
395
	 * @param array|null $structure
396
	 * @param int        $level
397
	 *
398
	 * @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...
399
	 */
400
	protected static function get_categories_list ($current = null, $structure = null, $level = 0) {
401
		$list = [
402
			'in'    => [],
403
			'value' => []
404
		];
405
		if ($structure === null) {
406
			$structure       = Pages::instance()->get_structure();
407
			$L               = new Prefix('static_pages_');
408
			$list['in'][]    = $L->root_category;
409
			$list['value'][] = 0;
410
		} else {
411
			if ($structure['id'] == $current) {
412
				return $list;
413
			}
414
			$list['in'][]    = str_repeat('&nbsp;', $level).$structure['title'];
415
			$list['value'][] = $structure['id'];
416
		}
417
		if (!empty($structure['categories'])) {
418
			foreach ($structure['categories'] as $category) {
419
				$tmp           = static::get_categories_list($current, $category, $level + 1);
420
				$list['in']    = array_merge($list['in'], $tmp['in']);
421
				$list['value'] = array_merge($list['value'], $tmp['value']);
422
			}
423
		}
424
		return $list;
425
	}
426
}
427