Completed
Push — master ( ec315b...376b23 )
by Nazar
04:18
created

Controller::posts_post()   B

Complexity

Conditions 6
Paths 5

Size

Total Lines 26
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
c 1
b 1
f 0
dl 0
loc 26
rs 8.439
cc 6
eloc 20
nc 5
nop 2
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\ExitException,
13
	cs\Language\Prefix,
14
	cs\User,
15
	cs\modules\Blogs\Posts,
16
	cs\modules\Blogs\Sections;
17
18
class Controller {
19
	static function __get_settings () {
20
		$User        = User::instance();
21
		$module_data = Config::instance()->module('Blogs');
22
		return [
23
			'inline_editor'              => functionality('inline_editor'),
24
			'max_sections'               => $module_data->max_sections,
25
			'new_posts_only_from_admins' => $module_data->new_posts_only_from_admins, //TODO use this on frontend
26
			'can_delete_posts'           => //TODO use this on frontend
27
				$User->admin() &&
28
				$User->get_permission('admin/Blogs', 'index') &&
29
				$User->get_permission('admin/Blogs', 'edit_post')
30
		];
31
	}
32
	/**
33
	 * @param \cs\Request $Request
34
	 *
35
	 * @return array
36
	 *
37
	 * @throws ExitException
38
	 */
39
	static function posts_get ($Request) {
40
		$id = $Request->route_ids(0);
41
		if ($id) {
42
			$post = Posts::instance()->get($id);
43
			if (!$post) {
44
				throw new ExitException(404);
45
			}
46
			return $post;
47
		} 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...
48
			// TODO: implement latest posts
49
		}
50
	}
51
	/**
52
	 * @param \cs\Request  $Request
53
	 * @param \cs\Response $Response
54
	 *
55
	 * @return array
56
	 *
57
	 * @throws ExitException
58
	 */
59
	static function posts_post ($Request, $Response) {
60
		$Config      = Config::instance();
61
		$module_data = $Config->module('Blogs');
62
		$L           = new Prefix('blogs_');
63
		$User        = User::instance();
64
		if (!$User->admin() && $module_data->new_posts_only_from_admins) {
65
			throw new ExitException(403);
66
		}
67
		if (!$User->user()) {
68
			throw new ExitException($L->for_registered_users_only, 403);
69
		}
70
		$data = static::check_request_data($Request, $L);
71
		if (!$data) {
72
			throw new ExitException(400);
73
		}
74
		$Posts = Posts::instance();
75
		$id    = $Posts->add($data['title'], $data['path'], $data['content'], $data['sections'], $data['tags'], $data['mode'] == 'draft');
76
		if (!$id) {
77
			throw new ExitException($L->post_adding_error, 500);
78
		}
79
		$Response->code = 201;
80
		return [
81
			'id'  => $id,
82
			'url' => $Config->base_url().'/'.path($L->Blogs).'/'.$Posts->get($id)['path'].":$id"
83
		];
84
	}
85
	/**
86
	 * @param \cs\Request $Request
87
	 *
88
	 * @return array
89
	 *
90
	 * @throws ExitException
91
	 */
92
	static function posts_put ($Request) {
93
		$Config = Config::instance();
94
		$L      = new Prefix('blogs_');
95
		$User   = User::instance();
96
		$id     = $Request->route(1);
97
		$data   = static::check_request_data($Request, $L);
98
		if (!$id || !$data) {
99
			throw new ExitException(400);
100
		}
101
		$Posts = Posts::instance();
102
		$post  = $Posts->get($id);
103
		if (!$post) {
104
			throw new ExitException(404);
105
		}
106
		if (
107
			!$User->admin() ||
108
			!$User->get_permission('admin/Blogs', 'index') ||
109
			!$User->get_permission('admin/Blogs', 'edit_post')
110
		) {
111
			throw new ExitException(403);
112
		}
113
		if (!$Posts->set($id, $data['title'], $data['path'], $data['content'], $data['sections'], $data['tags'], $data['mode'] == 'draft')) {
114
			throw new ExitException($L->post_saving_error, 500);
115
		}
116
		return [
117
			'id'  => $id,
118
			'url' => $Config->base_url().'/'.path($L->Blogs).'/'.$Posts->get($id)['path'].":$id"
119
		];
120
	}
121
	/**
122
	 * @param \cs\Request $Request
123
	 *
124
	 * @throws ExitException
125
	 */
126
	static function posts_delete ($Request) {
127
		$L    = new Prefix('blogs_');
128
		$User = User::instance();
129
		$id   = $Request->route(1);
130
		if (!$id) {
131
			throw new ExitException(400);
132
		}
133
		$Posts = Posts::instance();
134
		$post  = $Posts->get($id);
135
		if (!$post) {
136
			throw new ExitException(404);
137
		}
138
		if (
139
			$post['user'] != $User->id &&
140
			!(
141
				$User->admin() &&
142
				$User->get_permission('admin/Blogs', 'index') &&
143
				$User->get_permission('admin/Blogs', 'edit_post')
144
			)
145
		) {
146
			throw new ExitException(403);
147
		}
148
		if (!$Posts->del($id)) {
149
			throw new ExitException($L->post_deleting_error, 500);
150
		}
151
	}
152
	/**
153
	 * @param \cs\Request $Request
154
	 * @param Prefix      $L
155
	 *
156
	 * @return array
157
	 *
158
	 * @throws ExitException
159
	 */
160
	protected static function check_request_data ($Request, $L) {
161
		$data = $Request->data('title', 'sections', 'content', 'tags', 'mode');
162
		if (!$data) {
163
			throw new ExitException(400);
164
		}
165
		$data['path'] = $Request->data('path');
166
		if (empty($data['title'])) {
167
			throw new ExitException($L->post_title_empty, 400);
168
		}
169
		if (empty($data['sections']) || !is_array($data['sections'])) {
170
			throw new ExitException($L->no_post_sections_specified, 400);
171
		}
172
		if (empty($data['content'])) {
173
			throw new ExitException($L->post_content_empty, 400);
174
		}
175
		if (empty($data['tags']) || !is_array($data['tags'])) {
176
			throw new ExitException($L->no_post_tags_specified, 400);
177
		}
178
		return $data;
179
	}
180
	/**
181
	 * @param \cs\Request $Request
182
	 *
183
	 * @return array
1 ignored issue
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<string,array>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
184
	 *
185
	 * @throws ExitException
186
	 */
187
	static function posts_preview ($Request) {
188
		$User = User::instance();
189
		if (!$User->user()) {
190
			throw new ExitException(403);
191
		}
192
		$data  = $Request->data('title', 'sections', 'content', 'tags');
193
		$Posts = Posts::instance();
194
		return $Posts->post_to_jsonld($data);
195
	}
196
	static function sections_get () {
197
		return Sections::instance()->get_all() ?: [];
198
	}
199
}
200