Completed
Push — master ( 0bab36...9a2ec2 )
by Nazar
04:08
created

Controller::__get_settings()   B

Complexity

Conditions 4
Paths 6

Size

Total Lines 23
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
c 2
b 1
f 0
dl 0
loc 23
rs 8.7972
cc 4
eloc 15
nc 6
nop 0
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;
10
use
11
	cs\Config,
12
	cs\Event,
13
	cs\ExitException,
14
	cs\Language\Prefix,
15
	cs\User,
16
	cs\modules\Blogs\Posts,
17
	cs\modules\Blogs\Sections,
18
	cs\modules\Blogs\api\Controller\admin;
19
20
class Controller {
21
	use
22
		admin;
23
24
	static function __get_settings () {
25
		$User        = User::instance();
26
		$module_data = Config::instance()->module('Blogs');
27
		$Comments    = null;
28
		Event::instance()->fire(
29
			'Comments/instance',
30
			[
31
				'Comments' => &$Comments
32
			]
33
		);
34
		/**
35
		 * @var \cs\modules\Comments\Comments $Comments
36
		 */
37
		return [
38
			'inline_editor'    => functionality('inline_editor'),
39
			'max_sections'     => $module_data->max_sections,
40
			'comments_enabled' => $module_data->enable_comments && $Comments,
41
			'can_delete_posts' =>
42
				$User->admin() &&
43
				$User->get_permission('admin/Blogs', 'index') &&
44
				$User->get_permission('admin/Blogs', 'edit_post')
45
		];
46
	}
47
	/**
48
	 * @param \cs\Request $Request
49
	 *
50
	 * @return array
51
	 *
52
	 * @throws ExitException
53
	 */
54
	static function posts_get ($Request) {
55
		$id = $Request->route_ids(0);
56
		if ($id) {
57
			$post = Posts::instance()->get($id);
58
			if (!$post) {
59
				throw new ExitException(404);
60
			}
61
			return $post;
62
		} else {
1 ignored issue
show
Unused Code introduced by
This else statement is empty and can be removed.

This check looks for the else branches of if statements that have no statements or where all statements have been commented out. This may be the result of changes for debugging or the code may simply be obsolete.

These else branches can be removed.

if (rand(1, 6) > 3) {
print "Check failed";
} else {
    //print "Check succeeded";
}

could be turned into

if (rand(1, 6) > 3) {
    print "Check failed";
}

This is much more concise to read.

Loading history...
63
			// TODO: implement latest posts
64
		}
65
	}
66
	/**
67
	 * @param \cs\Request  $Request
68
	 * @param \cs\Response $Response
69
	 *
70
	 * @return array
71
	 *
72
	 * @throws ExitException
73
	 */
74
	static function posts_post ($Request, $Response) {
75
		$Config      = Config::instance();
76
		$module_data = $Config->module('Blogs');
77
		$L           = new Prefix('blogs_');
78
		$User        = User::instance();
79
		if (!$User->admin() && $module_data->new_posts_only_from_admins) {
80
			throw new ExitException(403);
81
		}
82
		if (!$User->user()) {
83
			throw new ExitException($L->for_registered_users_only, 403);
84
		}
85
		$data = static::check_request_data($Request, $L);
86
		if (!$data) {
87
			throw new ExitException(400);
88
		}
89
		$Posts = Posts::instance();
90
		$id    = $Posts->add($data['title'], $data['path'], $data['content'], $data['sections'], $data['tags'], $data['mode'] == 'draft');
91
		if (!$id) {
92
			throw new ExitException($L->post_adding_error, 500);
93
		}
94
		$Response->code = 201;
95
		return [
96
			'id'  => $id,
97
			'url' => $Config->base_url().'/'.path($L->Blogs).'/'.$Posts->get($id)['path'].":$id"
98
		];
99
	}
100
	/**
101
	 * @param \cs\Request $Request
102
	 *
103
	 * @return array
104
	 *
105
	 * @throws ExitException
106
	 */
107
	static function posts_put ($Request) {
108
		$Config = Config::instance();
109
		$L      = new Prefix('blogs_');
110
		$User   = User::instance();
111
		$id     = $Request->route(1);
112
		$data   = static::check_request_data($Request, $L);
113
		if (!$id || !$data) {
114
			throw new ExitException(400);
115
		}
116
		$Posts = Posts::instance();
117
		$post  = $Posts->get($id);
118
		if (!$post) {
119
			throw new ExitException(404);
120
		}
121
		if (
122
			!$User->admin() ||
123
			!$User->get_permission('admin/Blogs', 'index') ||
124
			!$User->get_permission('admin/Blogs', 'edit_post')
125
		) {
126
			throw new ExitException(403);
127
		}
128
		if (!$Posts->set($id, $data['title'], $data['path'], $data['content'], $data['sections'], $data['tags'], $data['mode'] == 'draft')) {
129
			throw new ExitException($L->post_saving_error, 500);
130
		}
131
		return [
132
			'id'  => $id,
133
			'url' => $Config->base_url().'/'.path($L->Blogs).'/'.$Posts->get($id)['path'].":$id"
134
		];
135
	}
136
	/**
137
	 * @param \cs\Request $Request
138
	 *
139
	 * @throws ExitException
140
	 */
141
	static function posts_delete ($Request) {
142
		$L    = new Prefix('blogs_');
143
		$User = User::instance();
144
		$id   = $Request->route(1);
145
		if (!$id) {
146
			throw new ExitException(400);
147
		}
148
		$Posts = Posts::instance();
149
		$post  = $Posts->get($id);
150
		if (!$post) {
151
			throw new ExitException(404);
152
		}
153
		if (
154
			$post['user'] != $User->id &&
155
			!(
156
				$User->admin() &&
157
				$User->get_permission('admin/Blogs', 'index') &&
158
				$User->get_permission('admin/Blogs', 'edit_post')
159
			)
160
		) {
161
			throw new ExitException(403);
162
		}
163
		if (!$Posts->del($id)) {
164
			throw new ExitException($L->post_deleting_error, 500);
165
		}
166
	}
167
	/**
168
	 * @param \cs\Request $Request
169
	 * @param Prefix      $L
170
	 *
171
	 * @return array
172
	 *
173
	 * @throws ExitException
174
	 */
175
	protected static function check_request_data ($Request, $L) {
176
		$data = $Request->data('title', 'sections', 'content', 'tags', 'mode');
177
		if (!$data) {
178
			throw new ExitException(400);
179
		}
180
		$data['path'] = $Request->data('path');
181
		if (empty($data['title'])) {
182
			throw new ExitException($L->post_title_empty, 400);
183
		}
184
		if (empty($data['sections']) || !is_array($data['sections'])) {
185
			throw new ExitException($L->no_post_sections_specified, 400);
186
		}
187
		if (empty($data['content'])) {
188
			throw new ExitException($L->post_content_empty, 400);
189
		}
190
		if (empty($data['tags']) || !is_array($data['tags'])) {
191
			throw new ExitException($L->no_post_tags_specified, 400);
192
		}
193
		return $data;
194
	}
195
	/**
196
	 * @param \cs\Request $Request
197
	 *
198
	 * @return array
199
	 *
200
	 * @throws ExitException
201
	 */
202
	static function posts_preview ($Request) {
203
		$User = User::instance();
204
		if (!$User->user()) {
205
			throw new ExitException(403);
206
		}
207
		$data = $Request->data('title', 'sections', 'content', 'tags');
208
		$data += [
209
			'id'   => 0,
210
			'path' => path($Request->data('path') ?: $data['title']),
211
			'user' => $User->id,
212
			'date' => 0
213
		];
214
		$Posts = Posts::instance();
215
		return $Posts->post_to_jsonld($data);
216
	}
217
	/**
218
	 * @param \cs\Request $Request
219
	 *
220
	 * @return array
221
	 *
222
	 * @throws ExitException
223
	 */
224
	static function sections_get ($Request) {
225
		$id       = $Request->route_ids(0);
226
		$Sections = Sections::instance();
227
		if ($id) {
228
			$data = $Sections->get($id);
229
			if (!$data) {
230
				throw new ExitException(404);
231
			}
232
			return $data;
233
		}
234
		return $Sections->get_all();
235
	}
236
}
237