Completed
Push — master ( 8da695...83b49e )
by Nazar
05:06
created

Controller::add_category()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 32
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 21
nc 1
nop 0
dl 0
loc 32
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
		switch ($Request->data('mode')) {
26
			case 'add_page':
27
				if ($Pages->add(
28
					$Request->data['category'],
29
					$Request->data['title'],
30
					$Request->data['path'],
31
					$Request->data['content'],
32
					$Request->data['interface']
33
				)
34
				) {
35
					$Page->success($L->changes_saved);
36
				} else {
37
					$Page->warning($L->changes_save_error);
38
				}
39
				break;
40
			case 'edit_page':
41
				if ($Pages->set(
42
					$Request->data['id'],
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
		}
56
	}
57
	/**
58
	 * @return string
59
	 */
60
	public static function browse_categories () {
61
		return h::cs_static_pages_admin_categories_list();
62
	}
63
	/**
64
	 * @param \cs\Request $Request
65
	 *
66
	 * @return string
67
	 */
68
	public static function browse_pages ($Request) {
69
		$L        = new Prefix('static_pages_');
70
		$category = $Request->route_ids(0);
71
		Page::instance()->title(
72
			$category ? Categories::instance()->get($category)['title'] : $L->root_category
73
		);
74
		return h::cs_static_pages_admin_pages_list(
75
			[
76
				'category' => $category
77
			]
78
		);
79
	}
80
	/**
81
	 * @param \cs\Request $Request
82
	 */
83
	public static function add_page ($Request) {
84
		$L = new Prefix('static_pages_');
85
		Page::instance()->title($L->adding_of_page)
86
			->content(
87
				h::{'form[is=cs-form][action=admin/Static_pages]'}(
88
					h::h2(
89
						$L->adding_of_page
90
					).
91
					h::{'table.cs-table[center] tr'}(
92
						h::th(
93
							$L->category,
94
							$L->page_title,
95
							h::info('static_pages_page_path'),
96
							h::info('static_pages_page_interface')
97
						),
98
						h::td(
99
							h::{'select[is=cs-select][full-width][name=category][size=5]'}(
100
								static::get_categories_list(),
101
								[
102
									'selected' => isset($Request->route[1]) ? (int)$Request->route[1] : 0
103
								]
104
							),
105
							h::{'input[is=cs-input-text][full-width][name=title]'}(),
106
							h::{'input[is=cs-input-text][full-width][name=path]'}(),
107
							h::{'div radio[name=interface]'}(
108
								[
109
									'checked' => 1,
110
									'value'   => [0, 1],
111
									'in'      => [$L->off, $L->on]
112
								]
113
							)
114
						)
115
					).
116
					h::{'table.cs-table[center] tr'}(
117
						h::th($L->content),
118
						h::{'td cs-editor textarea[cs-textarea][autosize][name=content]'}()
119
					).
120
					h::p(
121
						h::{'button[is=cs-button][type=submit][name=mode][value=add_page]'}(
122
							$L->save,
123
							[
124
								'tooltip' => $L->save_info
125
							]
126
						).
127
						h::{'button[is=cs-button][type=button]'}(
128
							$L->cancel,
129
							[
130
								'onclick' => 'history.go(-1);'
131
							]
132
						)
133
					)
134
				)
135
			);
136
	}
137
	/**
138
	 * @param \cs\Request $Request
139
	 */
140
	public static function edit_page ($Request) {
141
		$L        = new Prefix('static_pages_');
142
		$id       = (int)$Request->route[1];
143
		$data     = Pages::instance()->get($id);
144
		$textarea = h::{'textarea[is=cs-textarea][autosize][name=content]'}($data['content']);
145
		Page::instance()
146
			->title($L->editing_of_page($data['title']))
147
			->content(
148
				h::{'form[is=cs-form][action=admin/Static_pages]'}(
149
					h::h2(
150
						$L->editing_of_page($data['title'])
151
					).
152
					h::{'table.cs-table[center] tr'}(
153
						h::th(
154
							$L->category,
155
							$L->page_title,
156
							h::info('static_pages_page_path'),
157
							h::info('static_pages_page_interface')
158
						),
159
						h::td(
160
							h::{'select[is=cs-select][full-width][name=category][size=5]'}(
161
								static::get_categories_list(),
162
								[
163
									'selected' => $data['category']
164
								]
165
							),
166
							h::{'input[is=cs-input-text][full-width][name=title]'}(
167
								[
168
									'value' => $data['title']
169
								]
170
							),
171
							h::{'input[is=cs-input-text][full-width][name=path]'}(
172
								[
173
									'value' => $data['path']
174
								]
175
							),
176
							h::{'div radio[name=interface]'}(
177
								[
178
									'checked' => $data['interface'],
179
									'value'   => [0, 1],
180
									'in'      => [$L->off, $L->on]
181
								]
182
							)
183
						)
184
					).
185
					h::{'table.cs-table[center] tr'}(
186
						h::th($L->content),
187
						h::td(
188
							$data['interface'] ? h::cs_editor($textarea) : $textarea
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_page]'}(
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 int|null   $current
215
	 * @param array|null $structure
216
	 * @param int        $level
217
	 *
218
	 * @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...
219
	 */
220
	protected static function get_categories_list ($current = null, $structure = null, $level = 0) {
221
		$list = [
222
			'in'    => [],
223
			'value' => []
224
		];
225
		if ($structure === null) {
226
			$structure       = Pages::instance()->get_structure();
227
			$L               = new Prefix('static_pages_');
228
			$list['in'][]    = $L->root_category;
229
			$list['value'][] = 0;
230
		} else {
231
			if ($structure['id'] == $current) {
232
				return $list;
233
			}
234
			$list['in'][]    = str_repeat('&nbsp;', $level).$structure['title'];
235
			$list['value'][] = $structure['id'];
236
		}
237
		if (!empty($structure['categories'])) {
238
			foreach ($structure['categories'] as $category) {
239
				$tmp           = static::get_categories_list($current, $category, $level + 1);
240
				$list['in']    = array_merge($list['in'], $tmp['in']);
241
				$list['value'] = array_merge($list['value'], $tmp['value']);
242
			}
243
		}
244
		return $list;
245
	}
246
}
247