add cms, add todo
[ssproject1617.git] / testcms-final-anon / system / admin / controllers / posts.php
1 <?php defined('IN_CMS') or die('No direct access allowed.');
2
3 class Posts_controller {
4
5 public function __construct() {
6 $this->admin_url = Config::get('application.admin_folder');
7 }
8
9 public function index() {
10 $data['posts'] = Posts::list_all(array('sortby' => 'id', 'sortmode' => 'desc'));
11 Template::render('posts/index', $data);
12 }
13
14 public function add() {
15 if(Input::method() == 'POST') {
16 if(Posts::add()) {
17 return Response::redirect($this->admin_url . '/posts/edit/' . Db::insert_id());
18 }
19 }
20
21 Template::render('posts/add');
22 }
23
24 public function edit($id) {
25 // find article
26 if(($article = Posts::find(array('id' => $id))) === false) {
27 return Response::redirect($this->admin_url . '/posts');
28 }
29
30 // process post request
31 if(Input::method() == 'POST') {
32 if(Posts::update($id)) {
33 // redirect path
34 return Response::redirect($this->admin_url . '/posts/edit/' . $id);
35 }
36 }
37
38 // get comments
39 $comments = Comments::list_all(array('post' => $id));
40 $pending = array();
41
42 foreach($comments as $comment) {
43 if($comment->status == 'pending') {
44 $pending[] = $comment->id;
45 }
46 }
47
48 $pending = count($pending);
49
50 // get posts page
51 $page = Pages::find(array('id' => Config::get('metadata.posts_page')));
52
53 Template::render('posts/edit', array('article' => $article, 'comments' => $comments, 'page' => $page, 'pending' => $pending));
54 }
55
56 }