Completed
Push — master ( 83b49e...84b670 )
by Nazar
04:49
created

Controller::admin_pages_get()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 3
nop 1
dl 0
loc 11
rs 9.4285
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) 2016, Nazar Mokrynskyi
7
 * @license   MIT License, see license.txt
8
 */
9
namespace cs\modules\Static_pages\api;
10
use
11
	cs\ExitException,
12
	cs\modules\Static_pages\Pages,
13
	cs\modules\Static_pages\Categories;
14
15
class Controller {
16
	/**
17
	 * @param \cs\Request $Request
18
	 *
19
	 * @return array
20
	 *
21
	 * @throws ExitException
22
	 */
23
	public static function admin_categories___get ($Request) {
24
		$id         = $Request->route_ids(0);
25
		$Categories = Categories::instance();
26
		if ($id) {
27
			$data = $Categories->get($id);
28
			if (!$data) {
29
				throw new ExitException(404);
30
			}
31
			return $data;
32
		}
33
		return $Categories->get_all();
34
	}
35
	/**
36
	 * @param \cs\Request $Request
37
	 *
38
	 * @throws ExitException
39
	 */
40
	public static function admin_categories___post ($Request) {
41
		$data = $Request->data('parent', 'title', 'path');
42
		if (!$data) {
43
			throw new ExitException(400);
44
		}
45
		if (!Categories::instance()->add($data['parent'], $data['title'], $data['path'])) {
46
			throw new ExitException(500);
47
		}
48
	}
49
	/**
50
	 * @param \cs\Request $Request
51
	 *
52
	 * @throws ExitException
53
	 */
54
	public static function admin_categories___put ($Request) {
55
		$id   = $Request->route_ids(0);
56
		$data = $Request->data('parent', 'title', 'path');
57
		if (!$id || !$data) {
58
			throw new ExitException(400);
59
		}
60
		if (!Categories::instance()->set($id, $data['parent'], $data['title'], $data['path'])) {
61
			throw new ExitException(500);
62
		}
63
	}
64
	/**
65
	 * @param \cs\Request $Request
66
	 *
67
	 * @throws ExitException
68
	 */
69
	public static function admin_categories___delete ($Request) {
70
		if (!Categories::instance()->del($Request->route_ids(0))) {
71
			throw new ExitException(500);
72
		}
73
	}
74
	/**
75
	 * @param \cs\Request $Request
76
	 *
77
	 * @return array[]
78
	 *
79
	 * @throws ExitException
80
	 */
81
	public static function admin_categories_pages_get ($Request) {
82
		$category = $Request->route_ids(0);
83
		if ($category === null) {
84
			throw new ExitException(400);
85
		}
86
		$Pages = Pages::instance();
87
		return $Pages->get($Pages->get_for_category($category));
88
	}
89
	/**
90
	 * @param \cs\Request $Request
91
	 *
92
	 * @return array
93
	 *
94
	 * @throws ExitException
95
	 */
96
	public static function admin_pages_get ($Request) {
97
		$id = $Request->route_ids(0);
98
		if ($id === null) {
99
			throw new ExitException(400);
100
		}
101
		$data = Pages::instance()->get($id);
102
		if (!$data) {
103
			throw new ExitException(404);
104
		}
105
		return $data;
106
	}
107
	/**
108
	 * @param \cs\Request $Request
109
	 *
110
	 * @throws ExitException
111
	 */
112
	public static function admin_pages_post ($Request) {
113
		$data = $Request->data('category', 'title', 'path', 'content', 'interface');
114
		if (!$data) {
115
			throw new ExitException(400);
116
		}
117
		if (!Pages::instance()->add($data['category'], $data['title'], $data['path'], $data['content'], $data['interface'])) {
118
			throw new ExitException(500);
119
		}
120
	}
121
	/**
122
	 * @param \cs\Request $Request
123
	 *
124
	 * @throws ExitException
125
	 */
126
	public static function admin_pages_put ($Request) {
127
		$id   = $Request->route_ids(0);
128
		$data = $Request->data('category', 'title', 'path', 'content', 'interface');
129
		if (!$id || !$data) {
130
			throw new ExitException(400);
131
		}
132
		if (!Pages::instance()->set($id, $data['category'], $data['title'], $data['path'], $data['content'], $data['interface'])) {
133
			throw new ExitException(500);
134
		}
135
	}
136
	/**
137
	 * @param \cs\Request $Request
138
	 *
139
	 * @throws ExitException
140
	 */
141
	public static function admin_pages_delete ($Request) {
142
		if (!Pages::instance()->del($Request->route_ids(0))) {
143
			throw new ExitException(500);
144
		}
145
	}
146
}
147