add cms, add todo
[ssproject1617.git] / testcms-final-anon / system / classes / routes.php
1 <?php defined('IN_CMS') or die('No direct access allowed.');
2
3 /*
4 This class contains all routing details
5 */
6 class Routes {
7
8 public function article($slug = '') {
9 // find article
10 $params = array('slug' => $slug);
11
12 // allow admin to view unpublished posts
13 if(Users::authed() === false) {
14 $params['status'] = 'published';
15 }
16
17 if(($article = Posts::find($params)) === false) {
18 Log::warning('Article connot be found: ' . $slug);
19 return Response::error(404);
20 }
21
22 // add comment
23 if(Input::method() == 'POST') {
24 if(Comments::add($article->id)) {
25 $page = IoC::resolve('posts_page');
26 return Response::redirect($page->slug . '/' . $article->slug);
27 }
28 }
29
30 // register single item for templating functions
31 IoC::instance('article', $article, true);
32
33 Template::render('article');
34 }
35
36 public function page($slug = '') {
37 // allow admin to view unpublished posts
38 if(Users::authed() === false) {
39 $params['status'] = 'published';
40 }
41
42 // if no slug is set we will use our default page
43 if(empty($slug)) {
44 $params['id'] = Config::get('metadata.home_page');
45 } else {
46 $params['slug'] = $slug;
47 }
48
49 // if we cant find either it looks like we're barney rubble (in trouble)
50 if(($page = Pages::find($params)) === false) {
51 Log::warning('Page connot be found: ' . $slug);
52 return Response::error(404);
53 }
54
55 // store our page for template functions
56 IoC::instance('page', $page, true);
57
58 // does the current page host our posts?
59 if($page->id == Config::get('metadata.posts_page')) {
60 // render our posts template
61 return Template::render('posts');
62 }
63
64 // render our page template
65 Template::render('page');
66 }
67
68 public function rss() {
69 // set headers
70 Rss::headers();
71
72 // set content
73 Rss::generate();
74 }
75
76 public function search($term = '') {
77 if(Input::method() == 'POST') {
78 if(Input::post('term') !== false) {
79 return Response::redirect('search/' . rawurlencode(Input::post('term')));
80 }
81 }
82
83 $search = Posts::search($term, array(
84 'status' => 'published',
85 'limit' => Config::get('metadata.posts_per_page', 10),
86 'offset' => Input::get('offset', 0)
87 ));
88 IoC::instance('search', $search, true);
89
90 $total = Posts::search_count($term, array(
91 'status' => 'published'
92 ));
93 IoC::instance('total_search', $total, true);
94
95 $page = new StdClass;
96 $page->id = -1;
97 $page->title = 'Search';
98 IoC::instance('page', $page, true);
99 Template::render('search');
100 }
101
102 }