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: *
30: * @var int
31: */
32: private $_items;
33:
34: /**
35: * Item padding (before and after the current item)
36: *
37: * @var int
38: */
39: private $_itemPadding;
40:
41: /**
42: * Items on the left side
43: *
44: * @var int
45: */
46: private $_previousItems;
47:
48: /**
49: * Items on the right side
50: *
51: * @var int
52: */
53: private $_nextItems;
54:
55: /**
56: * Current page
57: *
58: * @var int
59: */
60: private $_currentPage;
61:
62: /**
63: * Items per page
64: *
65: * @var int
66: */
67: private $_itemsPerPage;
68:
69: /**
70: * Constructor Function
71: * Initializes the pager
72: *
73: * @param int $items Amount of items
74: * @param int $itemsPerPage Items displayed per page
75: * @param int $currentPage Defines the current page
76: */
77: public function __construct($items, $itemsPerPage, $currentPage) {
78: $this->_items = $items;
79: $this->_itemsPerPage = $itemsPerPage;
80: $this->_currentPage = $currentPage;
81:
82: // Default values.
83: $this->_itemPadding = 2;
84: $this->_previousItems = 2;
85: $this->_nextItems = 2;
86: }
87:
88: /**
89: * Returns the current page
90: *
91: * @return int
92: */
93: public function getCurrentPage() {
94: return $this->_currentPage;
95: }
96:
97: /**
98: * Returns if the currentPage pointer is the first page.
99: *
100: * @return boolean True if we're on the first page.
101: */
102: public function isFirstPage() {
103: if ($this->_currentPage == 1) {
104: return true;
105: }
106:
107: return false;
108: }
109:
110: /**
111: * Returns if the currentPage pointer is the last page.
112: *
113: * @return boolean True if we're on the last page.
114: */
115: public function isLastPage() {
116: if ($this->_currentPage == $this->getMaxPages()) {
117: return true;
118: }
119:
120: return false;
121: }
122:
123: /**
124: * Returns the amount of pages.
125: *
126: * @return int Page count
127: */
128: public function getMaxPages() {
129: if ($this->_items == 0) {
130: return 1;
131: } else if ($this->_itemsPerPage == 0) {
132: return 1;
133: } else {
134: return (ceil($this->_items / $this->_itemsPerPage));
135: }
136: }
137:
138: /**
139: * Returns an array with the pager structure.
140: *
141: * Array format:
142: * Key : Page Number
143: * Value: | for "...", "current" for the current item, page number otherwise
144: *
145: * @return array Pager structure
146: */
147: public function getPagesInRange() {
148: $items = array();
149:
150: $maxPages = $this->getMaxPages();
151:
152: if (($this->_itemPadding * 3) + $this->_previousItems + $this->_nextItems > $maxPages) {
153: // Disable item padding
154: for ($i = 1; $i < $this->getMaxPages() + 1; $i++) {
155: $items[$i] = $i;
156: }
157: } else {
158: for ($i = 1; $i < $this->_previousItems + 1; $i++) {
159: if ($i <= $maxPages && $i >= 1) {
160: $items[$i] = $i;
161: }
162:
163: if ($i + 1 <= $maxPages && $i >= 2) {
164: $items[$i + 1] = "|";
165: }
166: }
167:
168: for ($i = $this->_currentPage - $this->_itemPadding; $i < $this->_currentPage + $this->_itemPadding + 1; $i++) {
169: if ($i <= $maxPages && $i >= 1) {
170: $items[$i] = $i;
171: }
172:
173: if ($i + 1 <= $maxPages && $i >= 2) {
174: $items[$i + 1] = "|";
175: }
176: }
177:
178: for ($i = ($this->getMaxPages() - $this->_nextItems) + 1; $i < $this->getMaxPages() + 1; $i++) {
179: if ($i <= $maxPages && $i >= 2) {
180: $items[$i] = $i;
181: }
182: }
183: }
184:
185: $items[$this->_currentPage] = 'current';
186:
187: return ($items);
188: }
189: }