Completed
Push — master ( 7e4a95...8da695 )
by Nazar
04:15
created

Controller   A

Complexity

Total Complexity 23

Size/Duplication

Total Lines 330
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 6

Importance

Changes 0
Metric Value
dl 0
loc 330
rs 10
c 0
b 0
f 0
wmc 23
lcom 1
cbo 6

8 Methods

Rating   Name   Duplication   Size   Complexity  
C index() 0 51 9
A browse_categories() 0 3 1
B add_category() 0 32 1
A edit_category() 0 50 1
A browse_pages() 0 12 2
A add_page() 0 54 2
A edit_page() 0 73 2
B get_categories_list() 0 26 5
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 'add_page':
42
				if ($Pages->add(
43
					$Request->data['category'],
44
					$Request->data['title'],
45
					$Request->data['path'],
46
					$Request->data['content'],
47
					$Request->data['interface']
48
				)
49
				) {
50
					$Page->success($L->changes_saved);
51
				} else {
52
					$Page->warning($L->changes_save_error);
53
				}
54
				break;
55
			case 'edit_page':
56
				if ($Pages->set(
57
					$Request->data['id'],
58
					$Request->data['category'],
59
					$Request->data['title'],
60
					$Request->data['path'],
61
					$Request->data['content'],
62
					$Request->data['interface']
63
				)
64
				) {
65
					$Page->success($L->changes_saved);
66
				} else {
67
					$Page->warning($L->changes_save_error);
68
				}
69
				break;
70
		}
71
	}
72
	/**
73
	 * @return string
74
	 */
75
	public static function browse_categories () {
76
		return h::cs_static_pages_admin_categories_list();
77
	}
78
	public static function add_category () {
79
		$L = new Prefix('static_pages_');
80
		Page::instance()
81
			->title($L->addition_of_page_category)
82
			->content(
83
				h::{'form[is=cs-form][action=admin/Static_pages]'}(
84
					h::h2($L->addition_of_page_category).
85
					h::label($L->parent_category).
86
					h::{'select[is=cs-select][name=parent][size=5]'}(
87
						static::get_categories_list()
88
					).
89
					h::label($L->category_title).
90
					h::{'input[is=cs-input-text][name=title]'}().
91
					h::{'label info'}('static_pages_category_path').
92
					h::{'input[is=cs-input-text][name=path]'}().
93
					h::p(
94
						h::{'button[is=cs-button][type=submit][name=mode][value=add_category]'}(
95
							$L->save,
96
							[
97
								'tooltip' => $L->save_info
98
							]
99
						).
100
						h::{'button[is=cs-button][type=button]'}(
101
							$L->cancel,
102
							[
103
								'onclick' => 'history.go(-1);'
104
							]
105
						)
106
					)
107
				)
108
			);
109
	}
110
	/**
111
	 * @param \cs\Request $Request
112
	 */
113
	public static function edit_category ($Request) {
114
		$L    = new Prefix('static_pages_');
115
		$id   = (int)$Request->route[1];
116
		$data = Categories::instance()->get($id);
117
		Page::instance()
118
			->title($L->editing_of_page_category($data['title']))
119
			->content(
120
				h::{'form[is=cs-form][action=admin/Static_pages]'}(
121
					h::h2($L->editing_of_page_category($data['title'])).
122
					h::label($L->parent_category).
123
					h::{'select[is=cs-select][name=parent][size=5]'}(
124
						static::get_categories_list($id),
125
						[
126
							'selected' => $data['parent']
127
						]
128
					).
129
					h::label($L->category_title).
130
					h::{'input[is=cs-input-text][name=title]'}(
131
						[
132
							'value' => $data['title']
133
						]
134
					).
135
					h::{'label info'}('static_pages_category_path').
136
					h::{'input[is=cs-input-text][name=path]'}(
137
						[
138
							'value' => $data['path']
139
						]
140
					).
141
					h::{'input[type=hidden][name=id]'}(
142
						[
143
							'value' => $id
144
						]
145
					).
146
					h::p(
147
						h::{'button[is=cs-button][type=submit][name=mode][value=edit_category]'}(
148
							$L->save,
149
							[
150
								'tooltip' => $L->save_info
151
							]
152
						).
153
						h::{'button[is=cs-button][type=button]'}(
154
							$L->cancel,
155
							[
156
								'onclick' => 'history.go(-1);'
157
							]
158
						)
159
					)
160
				)
161
			);
162
	}
163
	/**
164
	 * @param \cs\Request $Request
165
	 *
166
	 * @return string
167
	 */
168
	public static function browse_pages ($Request) {
169
		$L        = new Prefix('static_pages_');
170
		$category = $Request->route_ids(0);
171
		Page::instance()->title(
172
			$category ? Categories::instance()->get($category)['title'] : $L->root_category
173
		);
174
		return h::cs_static_pages_admin_pages_list(
175
			[
176
				'category' => $category
177
			]
178
		);
179
	}
180
	/**
181
	 * @param \cs\Request $Request
182
	 */
183
	public static function add_page ($Request) {
184
		$L = new Prefix('static_pages_');
185
		Page::instance()->title($L->adding_of_page)
186
			->content(
187
				h::{'form[is=cs-form][action=admin/Static_pages]'}(
188
					h::h2(
189
						$L->adding_of_page
190
					).
191
					h::{'table.cs-table[center] tr'}(
192
						h::th(
193
							$L->category,
194
							$L->page_title,
195
							h::info('static_pages_page_path'),
196
							h::info('static_pages_page_interface')
197
						),
198
						h::td(
199
							h::{'select[is=cs-select][full-width][name=category][size=5]'}(
200
								static::get_categories_list(),
201
								[
202
									'selected' => isset($Request->route[1]) ? (int)$Request->route[1] : 0
203
								]
204
							),
205
							h::{'input[is=cs-input-text][full-width][name=title]'}(),
206
							h::{'input[is=cs-input-text][full-width][name=path]'}(),
207
							h::{'div radio[name=interface]'}(
208
								[
209
									'checked' => 1,
210
									'value'   => [0, 1],
211
									'in'      => [$L->off, $L->on]
212
								]
213
							)
214
						)
215
					).
216
					h::{'table.cs-table[center] tr'}(
217
						h::th($L->content),
218
						h::{'td cs-editor textarea[cs-textarea][autosize][name=content]'}()
219
					).
220
					h::p(
221
						h::{'button[is=cs-button][type=submit][name=mode][value=add_page]'}(
222
							$L->save,
223
							[
224
								'tooltip' => $L->save_info
225
							]
226
						).
227
						h::{'button[is=cs-button][type=button]'}(
228
							$L->cancel,
229
							[
230
								'onclick' => 'history.go(-1);'
231
							]
232
						)
233
					)
234
				)
235
			);
236
	}
237
	/**
238
	 * @param \cs\Request $Request
239
	 */
240
	public static function edit_page ($Request) {
241
		$L        = new Prefix('static_pages_');
242
		$id       = (int)$Request->route[1];
243
		$data     = Pages::instance()->get($id);
244
		$textarea = h::{'textarea[is=cs-textarea][autosize][name=content]'}($data['content']);
245
		Page::instance()
246
			->title($L->editing_of_page($data['title']))
247
			->content(
248
				h::{'form[is=cs-form][action=admin/Static_pages]'}(
249
					h::h2(
250
						$L->editing_of_page($data['title'])
251
					).
252
					h::{'table.cs-table[center] tr'}(
253
						h::th(
254
							$L->category,
255
							$L->page_title,
256
							h::info('static_pages_page_path'),
257
							h::info('static_pages_page_interface')
258
						),
259
						h::td(
260
							h::{'select[is=cs-select][full-width][name=category][size=5]'}(
261
								static::get_categories_list(),
262
								[
263
									'selected' => $data['category']
264
								]
265
							),
266
							h::{'input[is=cs-input-text][full-width][name=title]'}(
267
								[
268
									'value' => $data['title']
269
								]
270
							),
271
							h::{'input[is=cs-input-text][full-width][name=path]'}(
272
								[
273
									'value' => $data['path']
274
								]
275
							),
276
							h::{'div radio[name=interface]'}(
277
								[
278
									'checked' => $data['interface'],
279
									'value'   => [0, 1],
280
									'in'      => [$L->off, $L->on]
281
								]
282
							)
283
						)
284
					).
285
					h::{'table.cs-table[center] tr'}(
286
						h::th($L->content),
287
						h::td(
288
							$data['interface'] ? h::cs_editor($textarea) : $textarea
289
						)
290
					).
291
					h::{'input[type=hidden][name=id]'}(
292
						[
293
							'value' => $id
294
						]
295
					).
296
					h::p(
297
						h::{'button[is=cs-button][type=submit][name=mode][value=edit_page]'}(
298
							$L->save,
299
							[
300
								'tooltip' => $L->save_info
301
							]
302
						).
303
						h::{'button[is=cs-button][type=button]'}(
304
							$L->cancel,
305
							[
306
								'onclick' => 'history.go(-1);'
307
							]
308
						)
309
					)
310
				)
311
			);
312
	}
313
	/**
314
	 * @param int|null   $current
315
	 * @param array|null $structure
316
	 * @param int        $level
317
	 *
318
	 * @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...
319
	 */
320
	protected static function get_categories_list ($current = null, $structure = null, $level = 0) {
321
		$list = [
322
			'in'    => [],
323
			'value' => []
324
		];
325
		if ($structure === null) {
326
			$structure       = Pages::instance()->get_structure();
327
			$L               = new Prefix('static_pages_');
328
			$list['in'][]    = $L->root_category;
329
			$list['value'][] = 0;
330
		} else {
331
			if ($structure['id'] == $current) {
332
				return $list;
333
			}
334
			$list['in'][]    = str_repeat('&nbsp;', $level).$structure['title'];
335
			$list['value'][] = $structure['id'];
336
		}
337
		if (!empty($structure['categories'])) {
338
			foreach ($structure['categories'] as $category) {
339
				$tmp           = static::get_categories_list($current, $category, $level + 1);
340
				$list['in']    = array_merge($list['in'], $tmp['in']);
341
				$list['value'] = array_merge($list['value'], $tmp['value']);
342
			}
343
		}
344
		return $list;
345
	}
346
}
347