Completed
Push — master ( 1c7fde...850e60 )
by Nazar
04:11
created

Controller::index()   C

Complexity

Conditions 12
Paths 10

Size

Total Lines 39
Code Lines 32

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 39
rs 5.1612
cc 12
eloc 32
nc 10
nop 0

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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