/controllers/pages_controller.php
PHP | 176 lines | 140 code | 26 blank | 10 comment | 23 complexity | d71ed979e1a431e58300412948af5a0c MD5 | raw file
- <?php
- class PagesController extends PageAppController {
- var $name = 'Pages';
- var $helpers = array(
- 'Time',
- 'Page.Fck',
- // 'Core.Indent',
- //'Core.Tree',
- 'Field.CustomField',
- // 'Core.Tab',
- // 'Core.Cms'
- );
- var $components = array('RequestHandler', 'Form.Builder');
- var $menuItems = array('admin_index' => 'List Pages', 'admin_add' => 'New Page');
-
- function beforeFilter(){
- parent::beforeFilter();
- $this->Auth->allow(array('index'));
- }
-
- function index() {
- $slug = $this->params['slug'];
- if (!$slug) {
- $this->Session->setFlash(__('Invalid Page.', true));
- $this->redirect(array('action'=>'index'));
- }
- $page = $this->Page->find('first', array(
- 'conditions' => array(
- 'Page.slug' => $slug
- )));
-
- if(empty($page['Page']['content'])){
- //$page['Page']['content'] = '<p>No content yet</p>';
- }
- $this->set('slug', $slug);
- $this->set(compact('page'));
- if($page['Page']['parent_id']){
- $this->set('sidenav', $this->Page->find('list', array('fields' => array('slug', 'title'),'order' => 'Page.order ASC', 'conditions' => array('Page.parent_id' => $page['Page']['parent_id']))));
- } else {
- $this->set('sidenav', $this->Page->find('list', array('fields' => array('slug', 'title'), 'order' => 'Page.order ASC' ,'conditions' => array('Page.parent_id' => $page['Page']['id']))));
- }
-
- if($page['Page']['parent_id'] == null){
- $this->set('section_node_id', $page['Page']['slug']);
- } else {
- $this->set('section_node_id', $page['ParentPage']['slug']);
- }
- $viewTemplate = substr($page['Page']['view'], 0, strrpos($page['Page']['view'], '.'));
- $layoutTemplate = substr($page['Page']['layout'], 0, strrpos($page['Page']['layout'], '.'));
- $this->layout = $layoutTemplate;
-
- if($page['Page']['page_title']){
- $this->set('title_for_layout', $page['Page']['page_title']);
- } else {
- $this->set('title_for_layout', $page['Page']['title']);
- }
-
- $this->set('meta_description', $page['Page']['meta_description']);
- $this->set('meta_keyword', $page['Page']['meta_keywords']);
- $this->render('templates/'. $viewTemplate);
- }
- function findSubPages($id = null){
- if(!$id){
- } else {
- return $this->Page->find('first', array('conditions' => array('Page.id' => $id)));
- }
- }
- function admin_index() {
- $this->Page->recursive = 0;
-
- $this->set('pageslist', $this->paginate());
- //$this->set('pageslist', $this->Page->find('threaded'));
- }
- function admin_view($id = null) {
- if (!$id) {
- $this->Session->setFlash(__('Invalid page', true));
- $this->redirect(array('action' => 'index'));
- }
- $this->set('page', $this->Page->read(null, $id));
- }
- function admin_add() {
- if (!empty($this->data)) {
- $this->Page->create();
- $this->data['Page']['slug'] = $this->Page->createSlug($this->data['Page']['title']);
- if ($this->Page->save($this->data)) {
- Cache::delete('page_slugs');
- Cache::delete('navigation');
- $this->Session->setFlash(__('The page has been saved', true));
- $this->redirect(array('action' => 'edit', $this->Page->getLastInsertId()));
- } else {
- $this->Session->setFlash(__('The page could not be saved. Please, try again.', true));
- }
- }
- $parentPages = $this->Page->find('list', array('conditions' => array('Page.parent_id' => null)));
- $this->set(compact('parentPages'));
-
- $template_dir = APP . 'views/pages/templates';
- $layout_dir = APP . 'views/layouts';
- $templates = $this->_get_files($template_dir);
- $layouts = $this->_get_files($layout_dir);
-
- $this->set(compact('layouts', 'templates'));
- }
- function admin_edit($id = null) {
- if (!$id && empty($this->data)) {
- $this->Session->setFlash(__('Invalid page', true));
- $this->redirect(array('action' => 'index'));
- }
- if (!empty($this->data)) {
-
- //$this->data['Page']['slug'] = $this->Page->createSlug($this->data['Page']['title'], $id);
- $this->data = $this->Page->formatSaveAll($id, $this->data);
- if ($this->Page->saveAll($this->data)) {
- //$this->loadModel('MenuItem');
- //$this->MenuItem->updateLinkByPage($this->data['Page']['slug'], $id);
- Cache::delete('page_slugs');
- //Cache::delete('navigation');
- $this->Session->setFlash(__('The page has been saved', true));
- $this->redirect(array('action' => 'edit', $id));
- } else {
- $this->Session->setFlash(__('The page could not be saved. Please, try again.', true));
- }
- }
- if (empty($this->data)) {
- $this->Page->Behaviors->attach('Containable');
- $this->data = $this->Page->find('first', array('conditions' => array('Page.id' => $id), 'contain' => array('Image', 'Field')));
- }
- $parentPages = $this->Page->find('list', array('conditions' => array('Page.parent_id' => null)));
- $this->set(compact('parentPages'));
-
- $fields = $this->Page->Field->find('list', array('fields' => array('Field.name', 'Field.name')));
-
- $this->set(compact('fields'));
-
- $template_dir = APP . 'views/pages/templates';
- $layout_dir = APP . 'views/layouts';
- $templates = $this->_get_files($template_dir);
- $layouts = $this->_get_files($layout_dir);
-
- $this->set(compact('layouts', 'templates'));
- }
- function admin_delete($id = null) {
- if (!$id) {
- $this->Session->setFlash(__('Invalid id for page', true));
- $this->redirect(array('action'=>'index'));
- }
- if ($this->Page->delete($id)) {
- $this->Session->setFlash(__('Page deleted', true));
- $this->redirect(array('action'=>'index'));
- }
- $this->Session->setFlash(__('Page was not deleted', true));
- $this->redirect(array('action' => 'index'));
- }
-
-
- function api_index(){
- $this->Page->recursive = -1;
- $pages = $this->Page->find('all', array('fields' => array('id', 'slug', 'title', 'parent_id')));
- $this->set('pages', $pages);
- }
-
- function api_view($id = null){
- $page = $this->Page->find('first', array('conditions' => array('Page.id' => $id)));
- $this->set('page', $page);
- }
- }
- ?>