Completed
Push — master ( d9fcc5...35afbd )
by Nazar
04:10
created

Controller::delete_section()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 28
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 28
rs 8.8571
cc 1
eloc 17
nc 1
nop 1
1
<?php
2
/**
3
 * @package   Blogs
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\Blogs\admin;
10
use
11
	h,
12
	cs\Config,
13
	cs\Language,
14
	cs\Page,
15
	cs\modules\Blogs\Posts,
16
	cs\modules\Blogs\Sections;
17
use function
18
	cs\modules\Blogs\get_sections_select_section,
19
	cs\modules\Blogs\get_posts_rows;
20
21
class Controller {
22
	static function index () {
23
		if (!isset($_POST['mode'])) {
24
			return;
25
		}
26
		$L        = Language::prefix('blogs_');
27
		$Page     = Page::instance();
28
		$Posts    = Posts::instance();
29
		$Sections = Sections::instance();
30
		switch ($_POST['mode']) {
31
			case 'add_section':
32
				if ($Sections->add($_POST['parent'], $_POST['title'], isset($_POST['path']) ? $_POST['path'] : null)) {
33
					$Page->success($L->changes_saved);
34
				} else {
35
					$Page->warning($L->changes_save_error);
36
				}
37
				break;
38
			case 'edit_section':
39
				if ($Sections->set($_POST['id'], $_POST['parent'], $_POST['title'], isset($_POST['path']) ? $_POST['path'] : null)) {
40
					$Page->success($L->changes_saved);
41
				} else {
42
					$Page->warning($L->changes_save_error);
43
				}
44
				break;
45
			case 'delete_section':
46
				if ($Sections->del($_POST['id'])) {
47
					$Page->success($L->changes_saved);
48
				} else {
49
					$Page->warning($L->changes_save_error);
50
				}
51
				break;
52
			case 'delete_post':
53
				if ($Posts->del($_POST['id'])) {
54
					$Page->success($L->changes_saved);
55
				} else {
56
					$Page->warning($L->changes_save_error);
57
				}
58
				break;
59
		}
60
	}
61
	static function general () {
62
		$L = Language::prefix('blogs_');
63
		Page::instance()
64
			->title($L->general)
65
			->content(
66
				h::cs_blogs_admin_general()
67
			);
68
	}
69
	static function browse_sections () {
70
		$L = Language::prefix('blogs_');
71
		Page::instance()
72
			->title($L->browse_sections)
73
			->content(
74
				h::cs_blogs_admin_sections_list()
75
			);
76
	}
77
	/**
78
	 * @param \cs\Request $Request
79
	 */
80
	static function browse_posts ($Request) {
81
		$Config = Config::instance();
82
		$L      = Language::prefix('blogs_');
83
		$page   = isset($Request->route[1]) ? (int)$Request->route[1] : 1;
84
		$page   = $page > 0 ? $page : 1;
85
		$total  = Posts::instance()->get_total_count();
86
		Page::instance()
87
			->title($L->browse_posts)
88
			->content(
89
				h::{'table.cs-table[center][list]'}(
90
					h::{'tr th'}(
91
						[
92
							$L->post_title,
93
							[
94
								'style' => 'width: 30%'
95
							]
96
						],
97
						[
98
							$L->post_sections,
99
							[
100
								'style' => 'width: 25%'
101
							]
102
						],
103
						[
104
							$L->post_tags,
105
							[
106
								'style' => 'width: 20%'
107
							]
108
						],
109
						[
110
							$L->author_date,
111
							[
112
								'style' => 'width: 15%'
113
							]
114
						],
115
						$L->action
116
					).
117
					h::{'tr| td'}(
118
						get_posts_rows($page)
119
					)
120
				).
121
				(
122
				$total ? h::{'.cs-block-margin.cs-text-center.cs-margin nav[is=cs-nav-pagination]'}(
123
					pages(
124
						$page,
125
						ceil($total / $Config->module('Blogs')->posts_per_page),
126
						function ($page) {
127
							return $page == 1 ? 'admin/Blogs/browse_posts' : "admin/Blogs/browse_posts/$page";
128
						}
129
					)
130
				) : ''
131
				)
132
			);
133
	}
134
	/**
135
	 * @param \cs\Request $Request
136
	 */
137
	static function add_section ($Request) {
138
		$Config = Config::instance();
139
		$L      = Language::prefix('blogs_');
140
		Page::instance()
141
			->title($L->addition_of_posts_section)
142
			->content(
143
				h::{'form[is=cs-form][action=admin/Blogs/browse_sections]'}(
144
					h::{'h2.cs-text-center'}(
145
						$L->addition_of_posts_section
146
					).
147
					h::label($L->parent_section).
148
					h::{'select[is=cs-select][name=parent][size=5]'}(
149
						get_sections_select_section(),
150
						[
151
							'selected' => isset($Request->route[1]) ? (int)$Request->route[1] : 0
152
						]
153
					).
154
					h::label($L->section_title).
155
					h::{'input[is=cs-input-text][name=title]'}().
156
					($Config->core['simple_admin_mode'] ? false :
157
						h::{'label info'}('blogs_section_path').
158
						h::{'input[is=cs-input-text][name=path]'}()
159
					).
160
					h::p(
161
						h::{'button[is=cs-button][type=submit][name=mode][value=add_section]'}(
162
							$L->save,
163
							[
164
								'tooltip' => $L->save_info
165
							]
166
						).
167
						h::{'button[is=cs-button]'}(
168
							$L->cancel,
169
							[
170
								'type'    => 'button',
171
								'onclick' => 'history.go(-1);'
172
							]
173
						)
174
					)
175
				)
176
			);
177
	}
178
	/**
179
	 * @param \cs\Request $Request
180
	 */
181
	static function delete_post ($Request) {
182
		$post = Posts::instance()->get($Request->route[1]);
183
		$L    = Language::prefix('blogs_');
184
		Page::instance()
185
			->title($L->deletion_of_post($post['title']))
186
			->content(
187
				h::{'form[is=cs-form][action=admin/Blogs/browse_posts]'}(
188
					h::{'h2.cs-text-center'}(
189
						$L->sure_to_delete_post($post['title'])
190
					).
191
					h::p(
192
						h::{'button[is=cs-button][type=submit][name=mode][value=delete_post]'}($L->yes).
193
						h::{'button[is=cs-button]'}(
194
							$L->cancel,
195
							[
196
								'type'    => 'button',
197
								'onclick' => 'history.go(-1);'
198
							]
199
						)
200
					).
201
					h::{'input[type=hidden][name=id]'}(
202
						[
203
							'value' => $post['id']
204
						]
205
					)
206
				)
207
			);
208
	}
209
	/**
210
	 * @param \cs\Request $Request
211
	 */
212
	static function delete_section ($Request) {
213
		$section = Sections::instance()->get($Request->route[1]);
214
		$L       = Language::prefix('blogs_');
215
		Page::instance()
216
			->title($L->deletion_of_posts_section($section['title']))
217
			->content(
218
				h::{'form[is=cs-form][action=admin/Blogs/browse_sections]'}(
219
					h::{'h2.cs-text-center'}(
220
						$L->sure_to_delete_posts_section($section['title'])
221
					).
222
					h::p(
223
						h::{'button[is=cs-button][type=submit][name=mode][value=delete_section]'}($L->yes).
224
						h::{'button[is=cs-button]'}(
225
							$L->cancel,
226
							[
227
								'type'    => 'button',
228
								'onclick' => 'history.go(-1);'
229
							]
230
						)
231
					).
232
					h::{'input[type=hidden][name=id]'}(
233
						[
234
							'value' => $section['id']
235
						]
236
					)
237
				)
238
			);
239
	}
240
	/**
241
	 * @param \cs\Request $Request
242
	 */
243
	static function edit_section ($Request) {
244
		$section = Sections::instance()->get($Request->route[1]);
245
		$Config  = Config::instance();
246
		$L       = Language::prefix('blogs_');
247
		Page::instance()
248
			->title($L->editing_of_posts_section($section['title']))
249
			->content(
250
				h::{'form[is=cs-form][action=admin/Blogs/browse_sections]'}(
251
					h::{'h2.cs-text-center'}(
252
						$L->editing_of_posts_section($section['title'])
253
					).
254
					h::label($L->parent_section).
255
					h::{'select[is=cs-select][name=parent][size=5]'}(
256
						get_sections_select_section($section['id']),
257
						[
258
							'selected' => $section['parent']
259
						]
260
					).
261
					h::label($L->section_title).
262
					h::{'input[is=cs-input-text][name=title]'}(
263
						[
264
							'value' => $section['title']
265
						]
266
					).
267
					($Config->core['simple_admin_mode'] ? false :
268
						h::{'label info'}('blogs_section_path').
269
						h::{'input[is=cs-input-text][name=path]'}(
270
							[
271
								'value' => $section['path']
272
							]
273
						)
274
					).
275
					h::p(
276
						h::{'button[is=cs-button][type=submit][name=mode][value=edit_section]'}(
277
							$L->save
278
						).
279
						h::{'button[is=cs-button]'}(
280
							$L->cancel,
281
							[
282
								'type'    => 'button',
283
								'onclick' => 'history.go(-1);'
284
							]
285
						)
286
					).
287
					h::{'input[type=hidden][name=id]'}(
288
						[
289
							'value' => $section['id']
290
						]
291
					)
292
				)
293
			);
294
	}
295
}
296