From f83760a8710799f034abaad13affa8011144847a Mon Sep 17 00:00:00 2001 From: Makis Tracend Date: Thu, 4 Jul 2013 03:27:34 -0700 Subject: [PATCH] #140 adding page controller in helpers --- app/controllers/main.php | 112 +---------------------------- app/helpers/page_controller.php | 121 ++++++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+), 110 deletions(-) create mode 100644 app/helpers/page_controller.php diff --git a/app/controllers/main.php b/app/controllers/main.php index 74193dc..49ad1d1 100644 --- a/app/controllers/main.php +++ b/app/controllers/main.php @@ -1,120 +1,12 @@ getPage(); - $is_category = $this->getCategoryPages( $is_page ); - // check if this is a category page - if(!$is_page && !$is_category){ - $this->getNewPage(); - } - // FIX: add the new page link for category pages - if(!$is_page && $is_category){ - $this->data['status']="new"; - } - - // render the page - $this->render(); - } - - - // Main render method - function render() { - - // display the page - Template::output($this->data); - } - - // - Helpers - function getPage( ) { - - $data = array(); - // if there is no path, load the index - if( empty( $this->data['path'] ) ){ - $page = new Page(1); - } else { - $page = new Page(); - $page->get_page_from_path($this->data['path']); - } - - // see if we have found a page - if( $page->get('id') ){ - // store the information of the page - $data['id'] = $this->data['id'] = $page->get('id'); - $data['title'] = stripslashes( $page->get('title') ); - $data['content'] = stripslashes( $page->get('content') ); - $data['tags'] = stripslashes( $page->get('tags') ); - $data['date'] = strtotime( stripslashes( $page->get('date') ) ); - // check if the page has been classified as a category - $this->category = ( strpos( $data['tags'], "category" ) > -1) ? true : false; - - $data['path']= $this->data['path']; - $data['view'] = getPath('views/main/body.php'); - $this->data['body'][] = $data; - // FIX #38: copy data of "static" pages in a seperate array for easy access - $this->data['_page'] = $data; - $this->data['template'] = stripslashes( $page->get('template') ); - return true; - } else { - return false; - } - + parent::index( $params ); } - function getCategoryPages($is_page=false) { - // a page needs the tag "category" to jump into this behaviour - if( $is_page && !$this->category ) return false; - - $page=new Page(); - $page->tablename = "pages"; - - // add a leading slash to the path - $path = ( substr($this->data['path'], -1) == "/" ) ? $this->data['path'] : $this->data['path'].'/'; - - $pages = $page->retrieve_many("path like '". $path ."%'"); - - // FIX: reset the data (in case it has page info) - if( $is_page ) $this->data['body'] = array(); - - if( count($pages) > 0 ){ - foreach( $pages as $data ){ - $data['view'] = getPath('views/main/category.php'); - $this->data['body'][] = $data; - } - // everything checked out - ready to set the template - $this->data['template'] = "category.php"; - - return true; - } else { - return false; - } - - } - - - function getNewPage() { - - if( array_key_exists('admin', $_SESSION) && $_SESSION['admin'] ){ - // forward to create a new page - $data['status']= $this->data['status']="new"; - $data['path']= $this->data['path']; - $data['view']= getPath('views/admin/confirm_new.php'); - $this->data['body'][] = $data; - } else { - // show 404 error if not loggedin - header("HTTP/1.0 404 Not Found"); - $data['view']= getPath('views/errors/404.php'); - $this->data['body'][] = $data; - } - - } - - } diff --git a/app/helpers/page_controller.php b/app/helpers/page_controller.php new file mode 100644 index 0000000..3cbefb2 --- /dev/null +++ b/app/helpers/page_controller.php @@ -0,0 +1,121 @@ +getPage(); + $is_category = $this->getCategoryPages( $is_page ); + // check if this is a category page + if(!$is_page && !$is_category){ + $this->getNewPage(); + } + // FIX: add the new page link for category pages + if(!$is_page && $is_category){ + $this->data['status']="new"; + } + + // render the page + $this->render(); + } + + + // Main render method + function render() { + + // display the page + Template::output($this->data); + } + + // - Helpers + function getPage( ) { + + $data = array(); + // if there is no path, load the index + if( empty( $this->data['path'] ) ){ + $page = new Page(1); + } else { + $page = new Page(); + $page->get_page_from_path($this->data['path']); + } + + // see if we have found a page + if( $page->get('id') ){ + // store the information of the page + $data['id'] = $this->data['id'] = $page->get('id'); + $data['title'] = stripslashes( $page->get('title') ); + $data['content'] = stripslashes( $page->get('content') ); + $data['tags'] = stripslashes( $page->get('tags') ); + $data['date'] = strtotime( stripslashes( $page->get('date') ) ); + // check if the page has been classified as a category + $this->category = ( strpos( $data['tags'], "category" ) > -1) ? true : false; + + $data['path']= $this->data['path']; + $data['view'] = getPath('views/main/body.php'); + $this->data['body'][] = $data; + // FIX #38: copy data of "static" pages in a seperate array for easy access + $this->data['_page'] = $data; + $this->data['template'] = stripslashes( $page->get('template') ); + return true; + } else { + return false; + } + + } + + function getCategoryPages($is_page=false) { + // a page needs the tag "category" to jump into this behaviour + if( $is_page && !$this->category ) return false; + + $page=new Page(); + $page->tablename = "pages"; + + // add a leading slash to the path + $path = ( substr($this->data['path'], -1) == "/" ) ? $this->data['path'] : $this->data['path'].'/'; + + $pages = $page->retrieve_many("path like '". $path ."%'"); + + // FIX: reset the data (in case it has page info) + if( $is_page ) $this->data['body'] = array(); + + if( count($pages) > 0 ){ + foreach( $pages as $data ){ + $data['view'] = getPath('views/main/category.php'); + $this->data['body'][] = $data; + } + // everything checked out - ready to set the template + $this->data['template'] = "category.php"; + + return true; + } else { + return false; + } + + } + + + function getNewPage() { + + if( array_key_exists('admin', $_SESSION) && $_SESSION['admin'] ){ + // forward to create a new page + $data['status']= $this->data['status']="new"; + $data['path']= $this->data['path']; + $data['view']= getPath('views/admin/confirm_new.php'); + $this->data['body'][] = $data; + } else { + // show 404 error if not loggedin + header("HTTP/1.0 404 Not Found"); + $data['view']= getPath('views/errors/404.php'); + $this->data['body'][] = $data; + } + + } + + +} + + +?> \ No newline at end of file