1 <?php
defined('IN_CMS') or die('No direct access allowed.');
5 public static function extend($page) {
9 foreach($page as $itm) {
10 $pages[] = static::extend($itm);
16 if(is_object($page)) {
17 $uri = Request
::uri();
18 $page->url
= Url
::make($page->slug
);
20 $page->active
= false;
22 if($current = IoC
::resolve('page')) {
23 if($current->id
== $page->id
) {
34 public static function list_all($params = array()) {
35 $sql = "select * from pages where 1 = 1";
38 if(isset($params['status'])) {
39 $sql .= " and status = ?";
40 $args[] = $params['status'];
43 if(isset($params['sortby'])) {
44 $sql .= " order by " . $params['sortby'];
46 if(isset($params['sortmode'])) {
47 $sql .= " " . $params['sortmode'];
52 $pages = static::extend(Db
::results($sql, $args));
54 // create iterable object
55 return new Items($pages);
58 public static function count($params = array()) {
59 $sql = "select count(*) from pages where 1 = 1";
62 if(isset($params['status'])) {
63 $sql .= " and pages.status = ?";
64 $args[] = $params['status'];
68 return Db
::query($sql, $args)->fetchColumn();
71 public static function find($where = array()) {
72 $sql = "select * from pages";
77 foreach($where as $key => $value) {
78 $clause[] = '`' . $key . '` = ?';
81 $sql .= " where " . implode(' and ', $clause);
85 return static::extend(Db
::row($sql, $args));
88 public static function delete($id) {
89 Db
::delete('pages', array('id' => $id));
91 Notifications
::set('success', 'Your page has been deleted');
96 public static function update($id) {
97 $post = Input
::post(array('slug', 'name', 'title', 'content', 'status', '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);
106 Notifications
::set('error', 'Sorry, you can not delete you home page or posts page.');
110 // remove it frm array
111 unset($post['delete']);
114 if(empty($post['name'])) {
115 $errors[] = 'Please enter a name';
118 if(empty($post['title'])) {
119 $errors[] = 'Please enter a title';
123 Notifications
::set('error', $errors);
127 if(empty($post['slug'])) {
128 $post['slug'] = preg_replace('/\W+/', '-', trim(strtolower($post['name'])));
131 Db
::update('pages', $post, array('id' => $id));
133 Notifications
::set('success', 'Your page has been updated');
138 public static function add() {
139 $post = Input
::post(array('slug', 'name', 'title', 'content', 'status'));
142 if(empty($post['name'])) {
143 $errors[] = 'Please enter a name';
146 if(empty($post['title'])) {
147 $errors[] = 'Please enter a title';
151 Notifications
::set('error', $errors);
155 if(empty($post['slug'])) {
156 $post['slug'] = preg_replace('/\W+/', '-', trim(strtolower($post['name'])));
159 Db
::insert('pages', $post);
161 Notifications
::set('success', 'Your new page has been added');