1: <?php
2: /**
3: * This file contains the the pager class.
4: *
5: * @package Core
6: * @subpackage GUI
7: * @version SVN Revision $Rev:$
8: *
9: * @author Timo Hummel
10: * @copyright four for business AG <www.4fb.de>
11: * @license http://www.contenido.org/license/LIZENZ.txt
12: * @link http://www.4fb.de
13: * @link http://www.contenido.org
14: */
15:
16: defined('CON_FRAMEWORK') || die('Illegal call: Missing framework initialization - request aborted.');
17:
18: /**
19: * cPager
20: * Basic pager class without presentation logic
21: *
22: * @package Core
23: * @subpackage GUI
24: */
25: class cPager {
26:
27: /**
28: * Amount of items
29: * @var integer
30: */
31: private $_items;
32:
33: /**
34: * Item padding (before and after the current item)
35: * @var integer
36: */
37: private $_itemPadding;
38:
39: /**
40: * Items on the left side
41: * @var integer
42: */
43: private $_previousItems;
44:
45: /**
46: * Items on the right side
47: * @var integer
48: */
49: private $_nextItems;
50:
51: /**
52: * Current page
53: * @var integer
54: */
55: private $_currentPage;
56:
57: /**
58: * Items per page
59: * @var integer
60: */
61: private $_itemsPerPage;
62:
63: /**
64: * Constructor Function
65: * Initializes the pager
66: *
67: * @param $items int Amount of items
68: * @param $itemsPerPage int Items displayed per page
69: * @param $currentPage int Defines the current page
70: */
71: public function __construct($items, $itemsPerPage, $currentPage) {
72: $this->_items = $items;
73: $this->_itemsPerPage = $itemsPerPage;
74: $this->_currentPage = $currentPage;
75:
76: // Default values.
77: $this->_itemPadding = 2;
78: $this->_previousItems = 2;
79: $this->_nextItems = 2;
80: }
81:
82: /**
83: * Returns the current page
84: */
85: public function getCurrentPage() {
86: return $this->_currentPage;
87: }
88:
89: /**
90: * Returns if the currentPage pointer is the first page.
91: *
92: * @return boolean True if we're on the first page.
93: */
94: public function isFirstPage() {
95: if ($this->_currentPage == 1) {
96: return true;
97: }
98:
99: return false;
100: }
101:
102: /**
103: * Returns if the currentPage pointer is the last page.
104: *
105: * @return boolean True if we're on the last page.
106: */
107: public function isLastPage() {
108: if ($this->_currentPage == $this->getMaxPages()) {
109: return true;
110: }
111:
112: return false;
113: }
114:
115: /**
116: * Returns the amount of pages.
117: *
118: * @return int Page count
119: */
120: public function getMaxPages() {
121: if ($this->_items == 0) {
122: return 1;
123: } else if ($this->_itemsPerPage == 0) {
124: return 1;
125: } else {
126: return (ceil($this->_items / $this->_itemsPerPage));
127: }
128: }
129:
130: /**
131: * Returns an array with the pager structure.
132: *
133: * Array format:
134: * Key : Page Number
135: * Value: | for "...", "current" for the current item, page number otherwise
136: *
137: * @return array Pager structure
138: */
139: public function getPagesInRange() {
140: $items = array();
141:
142: $maxPages = $this->getMaxPages();
143:
144: if (($this->_itemPadding * 3) + $this->_previousItems + $this->_nextItems > $maxPages) {
145: // Disable item padding
146: for ($i = 1; $i < $this->getMaxPages() + 1; $i++) {
147: $items[$i] = $i;
148: }
149: } else {
150: for ($i = 1; $i < $this->_previousItems + 1; $i++) {
151: if ($i <= $maxPages && $i >= 1) {
152: $items[$i] = $i;
153: }
154:
155: if ($i + 1 <= $maxPages && $i >= 2) {
156: $items[$i + 1] = "|";
157: }
158: }
159:
160: for ($i = $this->_currentPage - $this->_itemPadding; $i < $this->_currentPage + $this->_itemPadding + 1; $i++) {
161: if ($i <= $maxPages && $i >= 1) {
162: $items[$i] = $i;
163: }
164:
165: if ($i + 1 <= $maxPages && $i >= 2) {
166: $items[$i + 1] = "|";
167: }
168: }
169:
170: for ($i = ($this->getMaxPages() - $this->_nextItems) + 1; $i < $this->getMaxPages() + 1; $i++) {
171: if ($i <= $maxPages && $i >= 2) {
172: $items[$i] = $i;
173: }
174: }
175: }
176:
177: $items[$this->_currentPage] = 'current';
178:
179: return ($items);
180: }
181:
182: }