add cms, add todo
[ssproject1617.git] / testcms-final-anon / system / classes / pages.php
1 <?php defined('IN_CMS') or die('No direct access allowed.');
2
3 class Pages {
4
5 public static function extend($page) {
6 if(is_array($page)) {
7 $pages = array();
8
9 foreach($page as $itm) {
10 $pages[] = static::extend($itm);
11 }
12
13 return $pages;
14 }
15
16 if(is_object($page)) {
17 $uri = Request::uri();
18 $page->url = Url::make($page->slug);
19
20 $page->active = false;
21
22 if($current = IoC::resolve('page')) {
23 if($current->id == $page->id) {
24 $page->active = true;
25 }
26 }
27
28 return $page;
29 }
30
31 return false;
32 }
33
34 public static function list_all($params = array()) {
35 $sql = "select * from pages where 1 = 1";
36 $args = array();
37
38 if(isset($params['status'])) {
39 $sql .= " and status = ?";
40 $args[] = $params['status'];
41 }
42
43 if(isset($params['sortby'])) {
44 $sql .= " order by " . $params['sortby'];
45
46 if(isset($params['sortmode'])) {
47 $sql .= " " . $params['sortmode'];
48 }
49 }
50
51 // extend data set
52 $pages = static::extend(Db::results($sql, $args));
53
54 // create iterable object
55 return new Items($pages);
56 }
57
58 public static function count($params = array()) {
59 $sql = "select count(*) from pages where 1 = 1";
60 $args = array();
61
62 if(isset($params['status'])) {
63 $sql .= " and pages.status = ?";
64 $args[] = $params['status'];
65 }
66
67 // get total
68 return Db::query($sql, $args)->fetchColumn();
69 }
70
71 public static function find($where = array()) {
72 $sql = "select * from pages";
73 $args = array();
74
75 if(count($where)) {
76 $clause = array();
77 foreach($where as $key => $value) {
78 $clause[] = '`' . $key . '` = ?';
79 $args[] = $value;
80 }
81 $sql .= " where " . implode(' and ', $clause);
82 }
83
84 // extend page object
85 return static::extend(Db::row($sql, $args));
86 }
87
88 public static function delete($id) {
89 Db::delete('pages', array('id' => $id));
90
91 Notifications::set('success', 'Your page has been deleted');
92
93 return true;
94 }
95
96 public static function update($id) {
97 $post = Input::post(array('slug', 'name', 'title', 'content', 'status', 'delete'));
98 $errors = array();
99
100 // delete
101 if($post['delete'] !== false) {
102 // prevent the deletion of the posts page and home page
103 if(in_array($id, array(Config::get('metadata.home_page'), Config::get('metadata.posts_page'))) === false) {
104 return static::delete($id);
105 } else {
106 Notifications::set('error', 'Sorry, you can not delete you home page or posts page.');
107 return false;
108 }
109 } else {
110 // remove it frm array
111 unset($post['delete']);
112 }
113
114 if(empty($post['name'])) {
115 $errors[] = 'Please enter a name';
116 }
117
118 if(empty($post['title'])) {
119 $errors[] = 'Please enter a title';
120 }
121
122 if(count($errors)) {
123 Notifications::set('error', $errors);
124 return false;
125 }
126
127 if(empty($post['slug'])) {
128 $post['slug'] = preg_replace('/\W+/', '-', trim(strtolower($post['name'])));
129 }
130
131 Db::update('pages', $post, array('id' => $id));
132
133 Notifications::set('success', 'Your page has been updated');
134
135 return true;
136 }
137
138 public static function add() {
139 $post = Input::post(array('slug', 'name', 'title', 'content', 'status'));
140 $errors = array();
141
142 if(empty($post['name'])) {
143 $errors[] = 'Please enter a name';
144 }
145
146 if(empty($post['title'])) {
147 $errors[] = 'Please enter a title';
148 }
149
150 if(count($errors)) {
151 Notifications::set('error', $errors);
152 return false;
153 }
154
155 if(empty($post['slug'])) {
156 $post['slug'] = preg_replace('/\W+/', '-', trim(strtolower($post['name'])));
157 }
158
159 Db::insert('pages', $post);
160
161 Notifications::set('success', 'Your new page has been added');
162
163 return true;
164 }
165
166 }