/_sohy/classes/posts.class.php
PHP | 493 lines | 256 code | 129 blank | 108 comment | 56 complexity | 693d398c933826948ffd64e523135ffa MD5 | raw file
- <?php
- /*
- ======================
- == Sohy Posts Class ==
- ======================
- */
- if(!defined('INDEX')) die();
- class posts {
- public function get_posts($post_limit = NULL) {
- global $sohy;
- if(!is_null($post_limit)) {
- $this->limit = $post_limit;
- }
- if($this->limit > 0) {
- $this->list = $this->get_start($sohy->base->posts);
- }
- $output = array();
- for($i=0; $i < $this->limit; $i++) {
- if(!isset($this->list[$i])) continue;
- if($this->get_index() == $this->list[$i]) {
- unset($this->list[$i]);
- continue;
- }
- $output[$i] = $this->list[$i];
- }
- return $output;
- }
- /*
- collect_posts()
- ===============
- */
- public function collect_posts($directory = 'content') {
- global $sohy;
- $files = glob($directory.'/*.{'.$sohy->file_types.'}', GLOB_BRACE);
- $dirs = glob($directory.'/*', GLOB_ONLYDIR);
- sort($files);
- $posts = array();
- if(isset($dirs[0])) {
- foreach($dirs as $key => $dir) {
- $posts[$key] = $this->collect_posts($dir);
- }
- }
- $posts = array_merge($posts, $files);
- $posts = array_reverse($posts);
- return $posts;
- }
- public function get_start($posts) {
- global $sohy;
- if(isset($sohy->page)) {
- $page = $sohy->page-1;
- } else {
- $page = 0;
- }
- sort($posts);
- $this->all = array_reverse($posts);
- if(count($this->all) == 0) return array();
- if($this->get_index() == $this->all[0]) {
- unset($this->all[0]);
- array_merge($this->all);
- }
- for($i=0; $i <= count($this->all); $i++) {
- if(!isset($this->all[$i])) continue;
- $this->all[$i] = $this->process_data($this->all[$i], $i);
- if(!isset($this->all[$i]->date)) {
- unset($this->all[$i]);
- }
- }
- if($this->limit > 0) {
- $this->all = array_chunk($this->all, $this->limit);
- }
- $sohy->page_count = count($this->all);
- if($this->limit > 0 && !empty($this->all)) {
- return $this->all[$page];
- }
- return $this->all;
- }
- /*
- process_data()
- ============
- Processing point before content is returned.
- Parses and reformats any date, url and content
- to the desired formats.
- Arguments:
- $file - File to be parsed and reformatted.
- */
- private function process_data($file, $key = 0) {
- global $sohy;
- if(empty($file)) return;
- $the_file = explode('/', $file);
- $the_file = end($the_file);
- $post = $sohy->load_file($file);
- if(!isset($post['date'])) $post['date'] = '';
- if($this->is_future_post($post['date'])) return false;
- $post['title'] = $this->create_post_title(@$post['title'], $file);
- $post['file'] = $file;
- $post['url'] = $this->create_url($file);
- $post['key'] = $key;
- if(!empty($post['content'])) {
- $post['rawcontent'] = $post['content'];
- $post['content'] = $post['content'];
- } else {
- $post['rawcontent'] = NULL;
- $post['content'] = NULL;
- }
- $post['date'] = $this->create_post_date($post['date']);
- if(isset($post['meta'])) {
- if($sohy->is_post() || $sohy->is_cat()) {
- $sohy->get_meta($post['meta']);
- }
- }
- $post['position']['first'] = NULL;
- $post['position']['last'] = NULL;
- $post = arrToObj($post);
- return $post;
- }
- /*
- create_post_date()
- ============
- Formats the date.
- Arguments:
- $date - Data to be processed.
- */
- private function create_post_date($date = NULL) {
- global $sohy;
- if(!is_null($date)) {
- return the_date($date,$sohy->date_format);
- } else {
- return false;
- }
- }
- /*
- is_future_post()
- ================
- Checks if the post date is for a
- future post.
- Arguments:
- $date - Date to be checked.
- */
- public function is_future_post($date) {
- if(time() < strtotime($date)) return true;
- return false;
- }
- /*
- create_post_title()
- ===================
- */
- public function create_post_title($title, $file) {
- global $sohy;
- if(!isset($title)) {
- $slug = $sohy->file_slug($file);
- $slug = str_replace('-', ' ', $slug);
- $title = ucwords($slug);
- }
- return $title;
- }
- /*
- post()
- ======
- Find and return a single post.
- Arguments:
- $file - A specific file to return.
- */
- public function post($file = NULL) {
- global $sohy;
- if(is_null($file)) {
- return $this->process_data($sohy->check_file());
- } else {
- return $this->process_data($file);
- }
- }
- /*
- CATEGORY FUNCTIONS
- */
- /*
- has_categories()
- ================
- */
- public function has_categories($base_cat = NULL) {
- global $sohy;
- $categories = array();
- if(is_null($base_cat)) {
- $base_cat = $sohy->true_base->categories;
- } elseif($base_cat == 'current') {
- $base_cat = glob($sohy->base->dir.$sohy->check_file().'/*', GLOB_ONLYDIR);
- } elseif($base_cat == 'parent') {
- $category = $sohy->base->dir.$sohy->check_file();
- $category = strrev($category);
- $dirs = explode('/', $category, 2);
- $base_cat = strrev($dirs[1]);
- $base_cat = glob($base_cat.'/*', GLOB_ONLYDIR);
- }
- if(count($base_cat) > 0) {
- return $base_cat;
- }
- return array();
- }
- /*
- categories()
- ============
- */
- public function categories($base_cat = NULL) {
- global $sohy;
- $categories = array();
- $base_cat = $this->has_categories($base_cat);
- foreach($base_cat as $category) {
- $file = glob($category.'/index.{'.$sohy->file_types.'}', GLOB_BRACE);
- if(!isset($file[0])) continue;
- $index = $sohy->load_file($file[0]);
- $index['url'] = $this->create_url($file[0]);
- $categories[] = $index;
- }
- return arrToObj($categories);
- }
- /*
- category()
- ==========
- */
- public function category() {
- global $sohy;
- $index = $this->get_index();
- if(!$index) {
- $file['title'] = ucwords($sohy->url->params[1]);
- $file['content'] = NULL;
- } else {
- $file = $sohy->load_file($index);
- $file['url'] = $this->create_url($index);
- $file['slug'] = $this->create_slug($index);
- $file['file'] = $index;
- $file['parent'] = $this->get_category_parent();
- }
- return arrToObj($file);
- }
- /*
- get_category_parent()
- =====================
- */
- public function get_category_parent() {
- global $sohy;
- $category = $sohy->base->dir.$sohy->check_file();
- $category = strrev($category);
- $dirs = explode('/', $category, 2);
- $parent_file = strrev($dirs[1]);
- $parent_file = glob($parent_file.'/index.{'.$sohy->file_types.'}', GLOB_BRACE);
- if(isset($parent_file[0])) {
- $parent = $sohy->load_file($parent_file[0]);
- $parent['url'] = $this->create_url($parent_file[0]);
- return $parent;
- }
- }
- /*
- GENERAL POST/CATEGORY FUNCTIONS
- */
- /*
- create_url()
- ============
- Creates the URL for the post.
- Arguments:
- $file - string - file path to create URL from.
- */
- public function create_url($file = NULL) {
- global $sohy;
- if(is_null($file)) {
- return;
- }
- $file_parts = $this->file_parts($file);
- $url = $sohy->url->home.'';
- if(!$sohy->prettylinks) {
- $url .= '/index.php?post=';
- }
- foreach($file_parts as $file) {
- $string = explode('.', $file, 2);
- $url .= '/'.$string[1];
- }
- return $url;
- }
- /*
- create_slug()
- ============
- Creates the slug for the post.
- Arguments:
- $file - string - file path to create URL from.
- */
- public function create_slug($file = NULL) {
- global $sohy;
- if(is_null($file)) {
- return;
- }
- $file_parts = $this->file_parts($file);
- $slug = end($file_parts);
- $slug = explode('.', $slug, 2);
- $slug = str_ireplace(array('_','.'), '-', $slug);
- return $slug[1];
- }
- /*
- file_parts()
- ============
- Arguments:
- $file - string - splits up file string to allow
- for use with create_url() and
- create_slug()
- */
- private function file_parts($file = NULL) {
- $file = str_replace('content/', '', $file);
- if(count(explode('/', $file)) > 0) {
- $file = strrev($file);
- $file = explode('.', $file, 2);
- $file = strrev($file[1]);
- }
- $file = str_replace('/index', '', $file);
- $file_parts = explode('/', $file);
- return $file_parts;
- }
- /*
- get_index()
- ===========
- Checks if index exists and returns file if true.
- Arguments:
- $dir - string - directory of file.
- */
- public function get_index($dir = NULL) {
- global $sohy;
- if(is_null($dir)) {
- $file = glob($sohy->check_file().'/index.{'.$sohy->file_types.'}', GLOB_BRACE);
- if(isset($file[0])) return $file[0];
- }
- if(empty($file)) {
- return;
- }
- if($sohy->check_file() == $file) {
- $file = glob($dir.'/index.{'.$sohy->file_types.'}', GLOB_BRACE);
- if(isset($file[0])) return $file[0];
- }
- return;
- }
- /*
- post_stylesheet();
- ==================
- Checks if current post has a stylesheet.
- */
- public function post_stylesheet() {
- global $sohy;
- if(!$sohy->is_post()) {
- return false;
- }
- if(isset($this->post()->stylesheet)) {
- return false;
- }
- $file = str_ireplace(array('.md', '.markdown', '.yml', '.yaml'), '.css', $this->post()->file);
- if(file_exists($file)) {
- return $file;
- } else {
- return false;
- }
- }
- }
- ?>