PageRenderTime 48ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/controllers/pages_controller.php

https://bitbucket.org/wgolden/page-plugin
PHP | 176 lines | 140 code | 26 blank | 10 comment | 23 complexity | d71ed979e1a431e58300412948af5a0c MD5 | raw file
  1. <?php
  2. class PagesController extends PageAppController {
  3. var $name = 'Pages';
  4. var $helpers = array(
  5. 'Time',
  6. 'Page.Fck',
  7. // 'Core.Indent',
  8. //'Core.Tree',
  9. 'Field.CustomField',
  10. // 'Core.Tab',
  11. // 'Core.Cms'
  12. );
  13. var $components = array('RequestHandler', 'Form.Builder');
  14. var $menuItems = array('admin_index' => 'List Pages', 'admin_add' => 'New Page');
  15. function beforeFilter(){
  16. parent::beforeFilter();
  17. $this->Auth->allow(array('index'));
  18. }
  19. function index() {
  20. $slug = $this->params['slug'];
  21. if (!$slug) {
  22. $this->Session->setFlash(__('Invalid Page.', true));
  23. $this->redirect(array('action'=>'index'));
  24. }
  25. $page = $this->Page->find('first', array(
  26. 'conditions' => array(
  27. 'Page.slug' => $slug
  28. )));
  29. if(empty($page['Page']['content'])){
  30. //$page['Page']['content'] = '<p>No content yet</p>';
  31. }
  32. $this->set('slug', $slug);
  33. $this->set(compact('page'));
  34. if($page['Page']['parent_id']){
  35. $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']))));
  36. } else {
  37. $this->set('sidenav', $this->Page->find('list', array('fields' => array('slug', 'title'), 'order' => 'Page.order ASC' ,'conditions' => array('Page.parent_id' => $page['Page']['id']))));
  38. }
  39. if($page['Page']['parent_id'] == null){
  40. $this->set('section_node_id', $page['Page']['slug']);
  41. } else {
  42. $this->set('section_node_id', $page['ParentPage']['slug']);
  43. }
  44. $viewTemplate = substr($page['Page']['view'], 0, strrpos($page['Page']['view'], '.'));
  45. $layoutTemplate = substr($page['Page']['layout'], 0, strrpos($page['Page']['layout'], '.'));
  46. $this->layout = $layoutTemplate;
  47. if($page['Page']['page_title']){
  48. $this->set('title_for_layout', $page['Page']['page_title']);
  49. } else {
  50. $this->set('title_for_layout', $page['Page']['title']);
  51. }
  52. $this->set('meta_description', $page['Page']['meta_description']);
  53. $this->set('meta_keyword', $page['Page']['meta_keywords']);
  54. $this->render('templates/'. $viewTemplate);
  55. }
  56. function findSubPages($id = null){
  57. if(!$id){
  58. } else {
  59. return $this->Page->find('first', array('conditions' => array('Page.id' => $id)));
  60. }
  61. }
  62. function admin_index() {
  63. $this->Page->recursive = 0;
  64. $this->set('pageslist', $this->paginate());
  65. //$this->set('pageslist', $this->Page->find('threaded'));
  66. }
  67. function admin_view($id = null) {
  68. if (!$id) {
  69. $this->Session->setFlash(__('Invalid page', true));
  70. $this->redirect(array('action' => 'index'));
  71. }
  72. $this->set('page', $this->Page->read(null, $id));
  73. }
  74. function admin_add() {
  75. if (!empty($this->data)) {
  76. $this->Page->create();
  77. $this->data['Page']['slug'] = $this->Page->createSlug($this->data['Page']['title']);
  78. if ($this->Page->save($this->data)) {
  79. Cache::delete('page_slugs');
  80. Cache::delete('navigation');
  81. $this->Session->setFlash(__('The page has been saved', true));
  82. $this->redirect(array('action' => 'edit', $this->Page->getLastInsertId()));
  83. } else {
  84. $this->Session->setFlash(__('The page could not be saved. Please, try again.', true));
  85. }
  86. }
  87. $parentPages = $this->Page->find('list', array('conditions' => array('Page.parent_id' => null)));
  88. $this->set(compact('parentPages'));
  89. $template_dir = APP . 'views/pages/templates';
  90. $layout_dir = APP . 'views/layouts';
  91. $templates = $this->_get_files($template_dir);
  92. $layouts = $this->_get_files($layout_dir);
  93. $this->set(compact('layouts', 'templates'));
  94. }
  95. function admin_edit($id = null) {
  96. if (!$id && empty($this->data)) {
  97. $this->Session->setFlash(__('Invalid page', true));
  98. $this->redirect(array('action' => 'index'));
  99. }
  100. if (!empty($this->data)) {
  101. //$this->data['Page']['slug'] = $this->Page->createSlug($this->data['Page']['title'], $id);
  102. $this->data = $this->Page->formatSaveAll($id, $this->data);
  103. if ($this->Page->saveAll($this->data)) {
  104. //$this->loadModel('MenuItem');
  105. //$this->MenuItem->updateLinkByPage($this->data['Page']['slug'], $id);
  106. Cache::delete('page_slugs');
  107. //Cache::delete('navigation');
  108. $this->Session->setFlash(__('The page has been saved', true));
  109. $this->redirect(array('action' => 'edit', $id));
  110. } else {
  111. $this->Session->setFlash(__('The page could not be saved. Please, try again.', true));
  112. }
  113. }
  114. if (empty($this->data)) {
  115. $this->Page->Behaviors->attach('Containable');
  116. $this->data = $this->Page->find('first', array('conditions' => array('Page.id' => $id), 'contain' => array('Image', 'Field')));
  117. }
  118. $parentPages = $this->Page->find('list', array('conditions' => array('Page.parent_id' => null)));
  119. $this->set(compact('parentPages'));
  120. $fields = $this->Page->Field->find('list', array('fields' => array('Field.name', 'Field.name')));
  121. $this->set(compact('fields'));
  122. $template_dir = APP . 'views/pages/templates';
  123. $layout_dir = APP . 'views/layouts';
  124. $templates = $this->_get_files($template_dir);
  125. $layouts = $this->_get_files($layout_dir);
  126. $this->set(compact('layouts', 'templates'));
  127. }
  128. function admin_delete($id = null) {
  129. if (!$id) {
  130. $this->Session->setFlash(__('Invalid id for page', true));
  131. $this->redirect(array('action'=>'index'));
  132. }
  133. if ($this->Page->delete($id)) {
  134. $this->Session->setFlash(__('Page deleted', true));
  135. $this->redirect(array('action'=>'index'));
  136. }
  137. $this->Session->setFlash(__('Page was not deleted', true));
  138. $this->redirect(array('action' => 'index'));
  139. }
  140. function api_index(){
  141. $this->Page->recursive = -1;
  142. $pages = $this->Page->find('all', array('fields' => array('id', 'slug', 'title', 'parent_id')));
  143. $this->set('pages', $pages);
  144. }
  145. function api_view($id = null){
  146. $page = $this->Page->find('first', array('conditions' => array('Page.id' => $id)));
  147. $this->set('page', $page);
  148. }
  149. }
  150. ?>