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

admin::admin_sections_get()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 3
eloc 9
nc 3
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\api\Controller;
10
use
11
	cs\Config,
12
	cs\ExitException,
13
	cs\modules\Blogs\Sections;
14
15
trait admin {
16
	static function admin___get_settings () {
17
		$module_data = Config::instance()->module('Blogs');
18
		return [
19
			'posts_per_page'                => $module_data->posts_per_page,
20
			'max_sections'                  => $module_data->max_sections,
21
			'enable_comments'               => $module_data->enable_comments,
22
			'new_posts_only_from_admins'    => $module_data->new_posts_only_from_admins,
23
			'allow_iframes_without_content' => $module_data->allow_iframes_without_content
24
		];
25
	}
26
	/**
27
	 * @param \cs\Request $Request
28
	 *
29
	 * @throws ExitException
30
	 */
31
	static function admin___save_settings ($Request) {
32
		$data = $Request->data('posts_per_page', 'max_sections', 'enable_comments', 'new_posts_only_from_admins', 'allow_iframes_without_content');
33
		if (!$data) {
34
			throw new ExitException(400);
35
		}
36
		if (!Config::instance()->module('Blogs')->set($data)) {
37
			throw new ExitException(500);
38
		}
39
	}
40
	/**
41
	 * @param \cs\Request $Request
42
	 *
43
	 * @return array
44
	 *
45
	 * @throws ExitException
46
	 */
47
	static function admin_sections_get ($Request) {
48
		$id       = $Request->route_ids(0);
49
		$Sections = Sections::instance();
50
		if ($id) {
51
			$data = $Sections->get($id);
52
			if (!$data) {
53
				throw new ExitException(404);
54
			}
55
			return $data;
56
		}
57
		return $Sections->get_all();
58
	}
59
}
60