add cms, add todo
[ssproject1617.git] / testcms-final-anon / system / admin / controllers / posts.php
diff --git a/testcms-final-anon/system/admin/controllers/posts.php b/testcms-final-anon/system/admin/controllers/posts.php
new file mode 100644 (file)
index 0000000..3e173e3
--- /dev/null
@@ -0,0 +1,56 @@
+<?php defined('IN_CMS') or die('No direct access allowed.');\r
+\r
+class Posts_controller {\r
+\r
+       public function __construct() {\r
+               $this->admin_url = Config::get('application.admin_folder');\r
+       }\r
+\r
+       public function index() {\r
+               $data['posts'] = Posts::list_all(array('sortby' => 'id', 'sortmode' => 'desc'));\r
+               Template::render('posts/index', $data);\r
+       }\r
+       \r
+       public function add() {\r
+               if(Input::method() == 'POST') {\r
+                       if(Posts::add()) {\r
+                               return Response::redirect($this->admin_url . '/posts/edit/' . Db::insert_id());\r
+                       }\r
+               }\r
+\r
+               Template::render('posts/add');\r
+       }\r
+       \r
+       public function edit($id) {\r
+               // find article\r
+               if(($article = Posts::find(array('id' => $id))) === false) {\r
+                       return Response::redirect($this->admin_url . '/posts');\r
+               }\r
+\r
+               // process post request\r
+               if(Input::method() == 'POST') {\r
+                       if(Posts::update($id)) {\r
+                               // redirect path\r
+                               return Response::redirect($this->admin_url . '/posts/edit/' . $id);\r
+                       }\r
+               }\r
+               \r
+               // get comments\r
+               $comments = Comments::list_all(array('post' => $id));\r
+               $pending = array();\r
+               \r
+               foreach($comments as $comment) {\r
+                   if($comment->status == 'pending') {\r
+                       $pending[] = $comment->id;\r
+                   }\r
+               }\r
+               \r
+               $pending = count($pending);\r
+\r
+               // get posts page\r
+               $page = Pages::find(array('id' => Config::get('metadata.posts_page')));\r
+\r
+               Template::render('posts/edit', array('article' => $article, 'comments' => $comments, 'page' => $page, 'pending' => $pending));\r
+       }\r
+       \r
+}\r