e4e5fb7b5e46d61066fcb9db498c896bdcd7ebc2
[ssproject1617.git] / testcms-final-anon / system / functions / posts.php
1 <?php defined('IN_CMS') or die('No direct access allowed.');
2
3 /**
4 Theme functions for posts
5 */
6 function has_posts() {
7 if(($posts = IoC::resolve('posts')) === false) {
8 $params = array(
9 'status' => 'published',
10 'sortby' => 'id',
11 'sortmode' => 'desc',
12 'limit' => Config::get('metadata.posts_per_page', 10),
13 'offset' => Input::get('offset', 0)
14 );
15 $posts = Posts::list_all($params);
16 IoC::instance('posts', $posts, true);
17
18 $total_posts = Posts::count(array('status' => 'published'));
19 IoC::instance('total_posts', $total_posts, true);
20 }
21
22 return $posts->length() > 0;
23 }
24
25 function posts() {
26 if(has_posts() === false) {
27 return false;
28 }
29
30 $posts = IoC::resolve('posts');
31
32 if($result = $posts->valid()) {
33 // register single post
34 IoC::instance('article', $posts->current(), true);
35
36 // move to next
37 $posts->next();
38 }
39
40 return $result;
41 }
42
43 function posts_next($text = 'Next', $default = '') {
44 $per_page = Config::get('metadata.posts_per_page');
45 $offset = Input::get('offset', 0);
46 $total = IoC::resolve('total_posts');
47
48 $pages = floor($total / $per_page);
49 $page = $offset / $per_page;
50
51 if($page < $pages) {
52 return '<a href="' . current_url() . '?offset=' . ($offset + $per_page) . '">' . $text . '</a>';
53 }
54
55 return $default;
56 }
57
58 function posts_prev($text = 'Previous', $default = '') {
59 $per_page = Config::get('metadata.posts_per_page');
60 $offset = Input::get('offset', 0);
61 $total = IoC::resolve('total_posts');
62
63 $pages = ceil($total / $per_page);
64 $page = $offset / $per_page;
65
66 if($offset > 0) {
67 return '<a href="' . current_url() . '?offset=' . ($offset - $per_page) . '">' . $text . '</a>';
68 }
69
70 return $default;
71 }