PHP File Manager
Editing File: Pages.php
<?php /* * Copyright (C) Wayne Purton-Smith - All Rights Reserved * Unauthorized copying of this file or removing this paragraph, via any medium is strictly prohibited * Proprietary and confidential * Written by Wayne Purton-Smith <waynepurtonsmith@hotmail.co.uk> February 2014 */ class Pages extends CI_Model { public function get($page_id = 0, $page_url = NULL, $parent_id = 0, $filters = array(), $skip = 0, $limit = 30) { $filters = array_merge(array ( 'only_visible' => true, 'only_top_level' => false, 'in_menu' => NULL ), (array) $filters); $visible_pages_only = ($filters['only_visible'] === true); $sql = "SELECT `p`.*, IF(`p2`.`page_id` IS NOT NULL, 1, 0) AS `has_children` FROM `pages` `p` LEFT OUTER JOIN `pages` `p2` ON `p2`.`parent_id` = `p`.`page_id` AND `p2`.`is_deleted` = 0 " . (($visible_pages_only) ? ' AND `p2`.`is_visible` = 1' : '') . ((is_bool($filters['in_menu'])) ? ' AND `p2`.`in_menu` = ' . (int) $filters['in_menu'] : '') . " WHERE `p`.`is_deleted` = 0"; if(($page_id = (int) $page_id) > 0) { $sql .= " AND `p`.`page_id` = $page_id "; $limit = 1; } if($page_url !== NULL) { $page_url = $this->trimUri($page_url); $page_url = (in_array($page_url, array('', 'home', 'index'))) ? 'home' : $page_url; $sql .= " AND `p`.`url` = " . $this->db->escape($page_url) . " "; $limit = 1; } if(($parent_id = (int) $parent_id) > 0) { $sql .= " AND `p`.`parent_id` = $parent_id "; } if($visible_pages_only) { $sql .= " AND `p`.`is_visible` = 1 "; } if($filters['only_top_level'] === true) { $sql .= " AND `p`.`parent_id` IS NULL "; } if(is_bool($filters['in_menu'])) { $sql .= " AND `p`.`in_menu` = " . (int) $filters['in_menu'] . " "; } $sql .= " GROUP BY `p`.`page_id` ORDER BY `p`.`page_order`"; $sql .= $this->common->returnOffset($skip, $limit); $result = $this->db->query($sql); //preprint($this->db->last_query()); if($result->num_rows()) { if($limit === 1) return $result->row(); $rows = $result->result(); $result->free_result(); return $rows; } return array(); } public function is($check_uri = '', $strict = true, $against_uri = NULL) { $check_uri = $this->trimUri($check_uri, '/'); $against_uri = $this->trimUri((is_string($against_uri)) ? $against_uri : uri_string()); $against_uri = '/' . ((in_array($against_uri, array('', 'home', 'index'))) ? 'home' : $against_uri) . '/'; $check_regex = '/^\/' . preg_quote($check_uri, '/') . '\/' . (($strict) ? '$' : '') . '/i'; $check_regex = str_replace(array('\(\:any\)', '\(\:num\)'), array('[^\/]+', '[0-9]+'), $check_regex); if(preg_match($check_regex, $against_uri)) { return true; } return false; } public function basePageExists($against_uri = NULL) { $against_uri = $this->trimUri((is_string($against_uri)) ? $against_uri : uri_string()); $base_page = $this->extractBase($against_uri); $base_page = (in_array($base_page, array('', 'home', 'index'))) ? 'home' : $base_page; return (bool) $this->get(NULL, $base_page, NULL, array('only_top_level' => null)); } private function extractBase($uri_str = '') { $uri_str = $this->trimUri($uri_str); if(preg_match('/^([^\/]+)?/', $uri_str, $matches) && isset($matches[1])) { return $matches[1]; } return ''; } private function trimUri($uri_str = '') { return trim($uri_str, '/'); } public function save($data = array(), $page_id = 0) { $page_id = (($page_id = (int) $page_id) > 0) ? $page_id : NULL; $is_update = ($page_id > 0); $parent_id = (isset($data['parent_id']) && ($parent_id = (int) $data['parent_id']) > 0 && ($parent_id != $page_id) && $this->get($parent_id, NULL, NULL, array('only_visible' => false))) ? $parent_id : NULL; $page_title = (isset($data['page_title'])) ? format_whitespace($data['page_title']) : NULL; $menu_title = (isset($data['menu_title']) && $data['menu_title']) ? format_whitespace($data['menu_title']) : $page_title; $url_title = (isset($data['page_url']) && ($url_title = format_whitespace($data['page_url'])) !== '') ? strtolower(preg_replace('/\s+/', '-', format_whitespace($url_title))) : url_title($page_title, '-', true); if($this->pageUrlExists($url_title, $page_id)) { return $this->common->setResponseMessage('The custom page URL is already used by another page'); } $page_content = (isset($data['page_content'])) ? trim($data['page_content']) : ''; $is_visible = (isset($data['is_visible'])) ? (bool) $data['is_visible'] : false; $in_menu = (isset($data['in_menu'])) ? (bool) $data['in_menu'] : false; $sql_data = array ( 'parent_id' => $parent_id, 'title' => $page_title, 'menu_title' => $menu_title, 'url' => $url_title, 'content' => $page_content, 'date_created' => NULL, 'last_updated' => NULL, 'page_order' => NULL, 'in_menu' => $in_menu, 'is_visible' => $is_visible ); if($is_update) { unset($sql_data['date_created'], $sql_data['page_order']); $sql_data['last_updated'] = time(); $sql = $this->db->update_string('pages', $sql_data, "`page_id` = $page_id"); } else { unset($sql_data['last_updated']); $sql_data['date_created'] = time(); $sql_data['page_order'] = $this->getLatestPageOrder($parent_id); $sql = $this->db->insert_string('pages', $sql_data); } $result = $this->db->query($sql); if(($is_update && $result) || (!$is_update && $this->db->affected_rows() === 1)) { return $this->common->setResponseMessage('The page was successfully saved', true); } return $this->common->setResponseMessage('The page could not be saved at this time - please try again'); } public function getContentBlocks($block_id = 0, $block_url = NULL, $page_id = 0, $skip = 0, $limit = 30) { $sql = "SELECT `cb`.*, `p`.`title` AS `parent_page` FROM `page_content_blocks` `cb` LEFT OUTER JOIN `pages` `p` ON `p`.`page_id` = `cb`.`page_id` WHERE `cb`.`is_deleted` = 0"; if(($block_id = (int) $block_id) > 0) { $sql .= " AND `cb`.`block_id` = $block_id "; $limit = 1; } if(($block_url = format_whitespace($block_url)) != '') { $sql .= " AND `cb`.`url` = " . $this->db->escape($block_url) . " "; $limit = 1; } if(($page_id = (int) $page_id) > 0) { $sql .= " AND `cb`.`page_id` = $page_id "; } $sql .= " GROUP BY `cb`.`block_id` ORDER BY CASE WHEN `title` IS NOT NULL THEN 0 ELSE 1 END"; $sql .= $this->common->returnOffset($skip, $limit); $result = $this->db->query($sql); if($result->num_rows()) { if($limit === 1) return $result->row(); $rows = $result->result(); $result->free_result(); return $rows; } return array(); } public function outputBlock($block_identifier = NULL) { $block_id = 0; $block_url = NULL; if(is_numeric($block_identifier) && ($_block_id = (int) $block_identifier) > 0) { $block_id = $_block_id; } elseif(is_string($block_identifier) && ($_block_url = remove_whitespace($block_identifier)) != '') { $block_url = $_block_url; } else { return ''; } if(($block_info = $this->getContentBlocks($block_id, $block_url)) && isset($block_info->content)) { return $block_info->content; } return ''; } public function saveContentBlock($data = array(), $block_id = 0) { $block_id = (($block_id = (int) $block_id) > 0) ? $block_id : 0; $is_update = ($block_id > 0); $page_id = (isset($data['page']) && ($page_id = (int) $data['page']) > 0) ? $page_id : NULL; $name = (isset($data['name']) && ($name = format_whitespace($data['name'])) != '') ? $name : NULL; if($name === NULL) { return $this->common->setResponseMessage('The content block name is required'); } $content = (isset($data['content'])) ? trim($data['content']) : ''; $sql_data = array ( 'page_id' => $page_id, 'name' => $name, 'url' => NULL, 'content' => $content ); if($is_update) { unset($sql_data['url']); $sql = $this->db->update_string('page_content_blocks', $sql_data, "`block_id` = $block_id"); } else { $sql_data['url'] = url_title($name, '-', true); $sql = $this->db->insert_string('page_content_blocks', $sql_data); } $result = $this->db->query($sql); if(($is_update && $result) || (!$is_update && $this->db->affected_rows() === 1)) { return $this->common->setResponseMessage('The content block was successfully saved', true); } return $this->common->setResponseMessage('The content block could not be saved at this time - please try again'); } public function getMenu($parent_id = 0, $only_visible = true, $only_top_level = true) { $pages = $this->get(NULL, NULL, $parent_id, array('only_visible' => $only_visible, 'only_top_level' => $only_top_level, 'in_menu' => true), -1); // $this->load->model('news'); // $categories = $this->news->getCategories(); // return array_merge($pages, $categories); return $pages; } public function buildMenuItems($menu_data = NULL) { $menu_html = ''; $menu_data = ($menu_data === NULL) ? $this->getMenu() : $menu_data; foreach($menu_data as $menu_item) { $menu_html .= '<li' . ((isset($menu_item->has_children) && $menu_item->has_children) ? ' class="dropdown"' : '') . '>'; if (isset($menu_item->category_id)) { $menu_html .= '<a href="/' . $menu_item->category_id . '">' . input_value($menu_item->name) . '</a>'; } else { if($menu_item->has_children) { $menu_html .= '<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">' . input_value($menu_item->menu_title) . '</a>'; } else { $menu_html .= '<a href="/' . $menu_item->url . '">' . input_value($menu_item->menu_title) . '</a>'; } if($menu_item->has_children) { if(($sub_items = $this->buildMenuItems($this->getMenu($menu_item->page_id, true, false)))) { $menu_html .= '<ul class="dropdown-menu" role="menu">'; $menu_html .= '<li><a href="/' . $menu_item->url . '">Overview</a></li>'; $menu_html .= $sub_items; $menu_html .= '</ul>'; } } } $menu_html .= '</li>'; } return $menu_html; } public function getLatestPageOrder($parent_id = 0) { $sql = "SELECT `page_order` FROM `pages` WHERE `is_deleted` = 0"; if(($parent_id = (int) $parent_id) > 0) { $sql .= " AND `parent_id` = $parent_id "; } $sql .= " ORDER BY `page_order` DESC LIMIT 1"; return (($result = $this->db->query($sql)) && $result->num_rows() === 1) ? ($result->row('page_order') + 1) : 1; } public function pageUrlExists($page_url = NULL, $existing_page_id = 0) { return (($existing_page = $this->get(NULL, $page_url))) ? ((($existing_page_id = (int) $existing_page_id) > 0) ? ($existing_page->page_id != $existing_page_id) : true) : false; } public function changePageOrder($page_id = 0, $direction = NULL) { if(($page_id = (int) $page_id) > 0 && in_array($direction, array('up', 'down')) && ($page_info = $this->get($page_id))) { $parent_id = ($page_info->parent_id) ? $page_info->parent_id : NULL; if(($sibling_pages = $this->get(NULL, NULL, $parent_id, array('only_visible' => false, 'only_top_level' => !$parent_id)))) { foreach($sibling_pages as $i => $page_data) { if($page_id == $page_data->page_id) { if($direction == 'up' && isset($sibling_pages[$i - 1])) { $page_swap = $sibling_pages[$i - 1]; } elseif($direction == 'down' && isset($sibling_pages[$i + 1])) { $page_swap = $sibling_pages[$i + 1]; } break; } } if(isset($page_swap)) { $this->db->query($this->db->update_string('pages', array('page_order' => $page_swap->page_order), "`page_id` = " . $page_id)); $this->db->query($this->db->update_string('pages', array('page_order' => $page_info->page_order), "`page_id` = " . $page_swap->page_id)); return true; } } } return false; } public function isReservedPage($page_url = '') { $reserved = array ( 'login', 'logout', 'account', 'products', 'checkout', 'place', 'complete', 'error' ); return (($page_url = trim($page_url)) !== '') ? in_array($page_url, $reserved) : false; } public function pagesComboBox($name = '', $selected = array(), $attributes = '') { $options = $this->buildPagesTree($this->get(NULL, NULL, NULL, array('only_visible' => false, 'only_top_level' => true), -1)); $options = array('' => '---') + $options; return form_dropdown($name, $options, $selected, $attributes); } private function buildPagesTree($pages = array()) { $options = array(); foreach($pages as $page_info) { $options[$page_info->page_id] = ((($depth_level = $this->getPageDepth($page_info->page_id))) ? str_repeat('---', $depth_level) . ' ' : '') . $page_info->title; if($page_info->has_children) { $options = $options + $this->buildPagesTree($this->get(NULL, NULL, $page_info->page_id, array('only_visible' => false), -1)); } } return $options; } public function getPageDepth($page_id = 0) { $level = 0; if(($page_id = (int) $page_id) > 0) { while(true) { $result = $this->db->query("SELECT `parent_id` FROM `pages` WHERE `page_id` = $page_id LIMIT 1"); if($result->num_rows() === 0 || !$result->row('parent_id')) { return $level; break; } $page_id = $result->row('parent_id'); $level++; } } return 0; } public function delete($page_id = 0) { if(($page_id = (int) $page_id) > 0) { $this->db->query($this->db->update_string('pages', array('is_deleted' => 1), "`page_id` = $page_id")); return ($this->db->affected_rows() === 1); } return false; } }
Cancel