Daan is sweet to Kelley and delegates himself TestCMS code reflection
[ssproject1617.git] / testcms-final-anon / system / classes / posts.php
1
2 <?php defined('IN_CMS') or die('No direct access allowed.');
3
4 class Posts {
5
6 public static function extend($post) {
7 if(is_array($post)) {
8 $posts = array();
9
10 foreach($post as $itm) {
11 $posts[] = static::extend($itm);
12 }
13
14 return $posts;
15 }
16
17 if(is_object($post)) {
18 $page = IoC::resolve('posts_page');
19 $post->url = Url::make($page->slug . '/' . $post->slug);
20 return $post;
21 }
22
23 return false;
24 }
25
26 public static function list_all($params = array()) {
27 $sql = "
28 select
29
30 posts.id,
31 posts.title,
32 posts.slug,
33 posts.description,
34 posts.html,
35 posts.css,
36 posts.js,
37 posts.created,
38 posts.custom_fields,
39 coalesce(users.real_name, posts.author) as author,
40 coalesce(comments.total, 0) as total_comments,
41 posts.status
42
43 from posts
44 left join users on (users.id = posts.author)
45 left join (
46 select
47 count(comments.id) as total, comments.post
48 from comments
49 where status = 'published'
50 group by comments.post
51 ) as comments on (posts.id = comments.post)
52 where 1 = 1
53 ";
54 $args = array();
55
56 if(isset($params['status'])) {
57 $sql .= " and posts.status = ?";
58 $args[] = $params['status'];
59 }
60
61 if(isset($params['sortby'])) {
62 $sql .= " order by posts." . $params['sortby'];
63
64 if(isset($params['sortmode'])) {
65 $sql .= " " . $params['sortmode'];
66 }
67 }
68
69 if(isset($params['limit'])) {
70 $sql .= " limit " . $params['limit'];
71
72 if(isset($params['offset'])) {
73 $sql .= " offset " . $params['offset'];
74 }
75 }
76
77 $results = Db::results($sql, $args);
78
79 // extend result set with post url
80 $results = static::extend($results);
81
82 // return items obj
83 return new Items($results);
84 }
85
86 public static function count($params = array()) {
87 $sql = "select count(*) from posts where 1 = 1";
88 $args = array();
89
90 if(isset($params['status'])) {
91 $sql .= " and posts.status = ?";
92 $args[] = $params['status'];
93 }
94
95 // return total
96 return Db::query($sql, $args)->fetchColumn();
97 }
98
99 public static function find($where = array()) {
100 $sql = "
101 select
102
103 posts.id,
104 posts.title,
105 posts.slug,
106 posts.description,
107 posts.html,
108 posts.css,
109 posts.js,
110 posts.created,
111 posts.custom_fields,
112 coalesce(users.real_name, posts.author) as author,
113 coalesce(users.bio, '') as bio,
114 posts.status,
115 posts.comments
116
117 from posts
118 left join users on (users.id = posts.author)
119 ";
120 $args = array();
121
122 if(count($where)) {
123 $clause = array();
124 foreach($where as $key => $value) {
125 $clause[] = 'posts.' . $key . ' = ?';
126 $args[] = $value;
127 }
128 $sql .= " where " . implode(' and ', $clause);
129 }
130
131 return static::extend(Db::row($sql, $args));
132 }
133
134 public static function search($term, $params = array()) {
135 $sql = "
136 select
137
138 posts.id,
139 posts.title,
140 posts.slug,
141 posts.description,
142 posts.html,
143 posts.css,
144 posts.js,
145 posts.created,
146 posts.custom_fields,
147 coalesce(users.real_name, posts.author) as author,
148 posts.status
149
150 from posts
151 left join users on (users.id = posts.author)
152
153 where (posts.title like :term or posts.description like :term or posts.html like :term)
154 ";
155 $args = array('term' => '%' . $term . '%');
156
157 if(isset($params['status'])) {
158 $sql .= " and posts.status = :status";
159 $args['status'] = $params['status'];
160 }
161
162 if(isset($params['limit'])) {
163 $sql .= " limit " . $params['limit'];
164
165 if(isset($params['offset'])) {
166 $sql .= " offset " . $params['offset'];
167 }
168 }
169
170 $results = Db::results($sql, $args);
171
172 // extend result set with post url
173 $results = static::extend($results);
174
175 // return items obj
176 return new Items($results);
177 }
178
179 public static function search_count($term, $params = array()) {
180 $sql = "
181 select count(*) from posts
182 where (posts.title like :term or posts.description like :term or posts.html like :term)
183 ";
184 $args = array('term' => '%' . $term . '%');
185
186 if(isset($params['status'])) {
187 $sql .= " and posts.status = :status";
188 $args['status'] = $params['status'];
189 }
190
191 // return total
192 return Db::query($sql, $args)->fetchColumn();
193 }
194
195 public static function delete($id) {
196 Db::delete('posts', array('id' => $id));
197 Db::delete('comments', array('post' => $id));
198
199 Notifications::set('success', 'Your post has been deleted');
200
201 return true;
202 }
203
204 public static function update($id) {
205 $post = Input::post(array('title', 'slug', 'description', 'html',
206 'css', 'js', 'status', 'delete', 'field', 'comments'));
207 $errors = array();
208
209 // delete
210 if($post['delete'] !== false) {
211 return static::delete($id);
212 } else {
213 // remove it frm array
214 unset($post['delete']);
215 }
216
217 if(empty($post['title'])) {
218 $errors[] = 'Please enter a title';
219 }
220
221 if(empty($post['description'])) {
222 $errors[] = 'Please enter a description';
223 }
224
225 if(empty($post['html'])) {
226 $errors[] = 'Please enter your html';
227 }
228
229 if(empty($post['slug'])) {
230 $post['slug'] = preg_replace('/\W+/', '-', trim(strtolower($post['title'])));
231 }
232
233 // check for duplicate slug
234 $sql = "select id from posts where slug = ? and id <> ?";
235 if(Db::row($sql, array($post['slug'], $id))) {
236 $errors[] = 'A post with the same slug already exists, please change your post slug.';
237 }
238
239 if(count($errors)) {
240 Notifications::set('error', $errors);
241 return false;
242 }
243
244 $custom = array();
245
246 if(is_array($post['field'])) {
247 foreach($post['field'] as $keylabel => $value) {
248 list($key, $label) = explode(':', $keylabel);
249 $custom[$key] = array('label' => $label, 'value' => $value);
250 }
251 }
252
253 // remove from update
254 unset($post['field']);
255
256 $post['custom_fields'] = json_encode($custom);
257
258 // update row
259 Db::update('posts', $post, array('id' => $id));
260
261 Notifications::set('success', 'Your post has been updated.');
262
263 return true;
264 }
265
266 public static function add() {
267 $post = Input::post(array('title', 'slug', 'description', 'html',
268 'css', 'js', 'status', 'field', 'comments'));
269 $errors = array();
270
271 if(empty($post['title'])) {
272 $errors[] = 'Please enter a title';
273 }
274
275 if(empty($post['description'])) {
276 $errors[] = 'Please enter a description';
277 }
278
279 if(empty($post['html'])) {
280 $errors[] = 'Please enter your html';
281 }
282
283 if(empty($post['slug'])) {
284 $post['slug'] = preg_replace('/\W+/', '-', trim(strtolower($post['title'])));
285 }
286
287 // check for duplicate slug
288 $sql = "select id from posts where slug = ?";
289 if(Db::row($sql, array($post['slug']))) {
290 $errors[] = 'A post with the same slug already exists, please change your post slug.';
291 }
292
293 if(count($errors)) {
294 Notifications::set('error', $errors);
295 return false;
296 }
297
298 $custom = array();
299
300 if(is_array($post['field'])) {
301 foreach($post['field'] as $keylabel => $value) {
302 list($key, $label) = explode(':', $keylabel);
303 $custom[$key] = array('label' => $label, 'value' => $value);
304 }
305 }
306
307 // remove from update
308 unset($post['field']);
309
310 $post['custom_fields'] = json_encode($custom);
311
312 // set creation date
313 $post['created'] = time();
314
315 // set author
316 $user = Users::authed();
317 $post['author'] = $user->id;
318
319 Db::insert('posts', $post);
320
321 Notifications::set('success', 'Your new post has been added');
322
323 return true;
324 }
325
326 }