2 <?php
defined('IN_CMS') or die('No direct access allowed.');
6 public static function extend($post) {
10 foreach($post as $itm) {
11 $posts[] = static::extend($itm);
17 if(is_object($post)) {
18 $page = IoC
::resolve('posts_page');
19 $post->url
= Url
::make($page->slug
. '/' . $post->slug
);
26 public static function list_all($params = array()) {
39 coalesce(users.real_name, posts.author) as author,
40 coalesce(comments.total, 0) as total_comments,
44 left join users on (users.id = posts.author)
47 count(comments.id) as total, comments.post
49 where status = 'published'
50 group by comments.post
51 ) as comments on (posts.id = comments.post)
56 if(isset($params['status'])) {
57 $sql .= " and posts.status = ?";
58 $args[] = $params['status'];
61 if(isset($params['sortby'])) {
62 $sql .= " order by posts." . $params['sortby'];
64 if(isset($params['sortmode'])) {
65 $sql .= " " . $params['sortmode'];
69 if(isset($params['limit'])) {
70 $sql .= " limit " . $params['limit'];
72 if(isset($params['offset'])) {
73 $sql .= " offset " . $params['offset'];
77 $results = Db
::results($sql, $args);
79 // extend result set with post url
80 $results = static::extend($results);
83 return new Items($results);
86 public static function count($params = array()) {
87 $sql = "select count(*) from posts where 1 = 1";
90 if(isset($params['status'])) {
91 $sql .= " and posts.status = ?";
92 $args[] = $params['status'];
96 return Db
::query($sql, $args)->fetchColumn();
99 public static function find($where = array()) {
112 coalesce(users.real_name, posts.author) as author,
113 coalesce(users.bio, '') as bio,
118 left join users on (users.id = posts.author)
124 foreach($where as $key => $value) {
125 $clause[] = 'posts.' . $key . ' = ?';
128 $sql .= " where " . implode(' and ', $clause);
131 return static::extend(Db
::row($sql, $args));
134 public static function search($term, $params = array()) {
147 coalesce(users.real_name, posts.author) as author,
151 left join users on (users.id = posts.author)
153 where (posts.title like :term or posts.description like :term or posts.html like :term)
155 $args = array('term' => '%' . $term . '%');
157 if(isset($params['status'])) {
158 $sql .= " and posts.status = :status";
159 $args['status'] = $params['status'];
162 if(isset($params['limit'])) {
163 $sql .= " limit " . $params['limit'];
165 if(isset($params['offset'])) {
166 $sql .= " offset " . $params['offset'];
170 $results = Db
::results($sql, $args);
172 // extend result set with post url
173 $results = static::extend($results);
176 return new Items($results);
179 public static function search_count($term, $params = array()) {
181 select count(*) from posts
182 where (posts.title like :term or posts.description like :term or posts.html like :term)
184 $args = array('term' => '%' . $term . '%');
186 if(isset($params['status'])) {
187 $sql .= " and posts.status = :status";
188 $args['status'] = $params['status'];
192 return Db
::query($sql, $args)->fetchColumn();
195 public static function delete($id) {
196 Db
::delete('posts', array('id' => $id));
197 Db
::delete('comments', array('post' => $id));
199 Notifications
::set('success', 'Your post has been deleted');
204 public static function update($id) {
205 $post = Input
::post(array('title', 'slug', 'description', 'html',
206 'css', 'js', 'status', 'delete', 'field', 'comments'));
210 if($post['delete'] !== false) {
211 return static::delete($id);
213 // remove it frm array
214 unset($post['delete']);
217 if(empty($post['title'])) {
218 $errors[] = 'Please enter a title';
221 if(empty($post['description'])) {
222 $errors[] = 'Please enter a description';
225 if(empty($post['html'])) {
226 $errors[] = 'Please enter your html';
229 if(empty($post['slug'])) {
230 $post['slug'] = preg_replace('/\W+/', '-', trim(strtolower($post['title'])));
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.';
240 Notifications
::set('error', $errors);
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);
253 // remove from update
254 unset($post['field']);
256 $post['custom_fields'] = json_encode($custom);
259 Db
::update('posts', $post, array('id' => $id));
261 Notifications
::set('success', 'Your post has been updated.');
266 public static function add() {
267 $post = Input
::post(array('title', 'slug', 'description', 'html',
268 'css', 'js', 'status', 'field', 'comments'));
271 if(empty($post['title'])) {
272 $errors[] = 'Please enter a title';
275 if(empty($post['description'])) {
276 $errors[] = 'Please enter a description';
279 if(empty($post['html'])) {
280 $errors[] = 'Please enter your html';
283 if(empty($post['slug'])) {
284 $post['slug'] = preg_replace('/\W+/', '-', trim(strtolower($post['title'])));
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.';
294 Notifications
::set('error', $errors);
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);
307 // remove from update
308 unset($post['field']);
310 $post['custom_fields'] = json_encode($custom);
313 $post['created'] = time();
316 $user = Users
::authed();
317 $post['author'] = $user->id
;
319 Db
::insert('posts', $post);
321 Notifications
::set('success', 'Your new post has been added');