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
74: * Amount of items
75: * @param int $itemsPerPage
76: * Items displayed per page
77: * @param int $currentPage
78: * Defines the current page
79: */
80: public function __construct($items, $itemsPerPage, $currentPage) {
81: $this->_items = $items;
82: $this->_itemsPerPage = $itemsPerPage;
83: $this->_currentPage = $currentPage;
84:
85: // Default values.
86: $this->_itemPadding = 2;
87: $this->_previousItems = 2;
88: $this->_nextItems = 2;
89: }
90:
91: /**
92: * Returns the current page
93: *
94: * @return int
95: */
96: public function getCurrentPage() {
97: return $this->_currentPage;
98: }
99:
100: /**
101: * Returns if the currentPage pointer is the first page.
102: *
103: * @return bool
104: * True if we're on the first page.
105: */
106: public function isFirstPage() {
107: if ($this->_currentPage == 1) {
108: return true;
109: }
110:
111: return false;
112: }
113:
114: /**
115: * Returns if the currentPage pointer is the last page.
116: *
117: * @return bool
118: * True if we're on the last page.
119: */
120: public function isLastPage() {
121: if ($this->_currentPage == $this->getMaxPages()) {
122: return true;
123: }
124:
125: return false;
126: }
127:
128: /**
129: * Returns the amount of pages.
130: *
131: * @return int
132: * Page count
133: */
134: public function getMaxPages() {
135: if ($this->_items == 0) {
136: return 1;
137: } else if ($this->_itemsPerPage == 0) {
138: return 1;
139: } else {
140: return ceil($this->_items / $this->_itemsPerPage);
141: }
142: }
143:
144: /**
145: * Returns an array with the pager structure.
146: *
147: * Array format:
148: * Key : Page Number
149: * Value: | for "...", "current" for the current item, page number otherwise
150: *
151: * @return array
152: * Pager structure
153: */
154: public function getPagesInRange() {
155: $items = array();
156:
157: $maxPages = $this->getMaxPages();
158:
159: if (($this->_itemPadding * 3) + $this->_previousItems + $this->_nextItems > $maxPages) {
160: // Disable item padding
161: for ($i = 1; $i < $this->getMaxPages() + 1; $i++) {
162: $items[$i] = $i;
163: }
164: } else {
165: for ($i = 1; $i < $this->_previousItems + 1; $i++) {
166: if ($i <= $maxPages && $i >= 1) {
167: $items[$i] = $i;
168: }
169:
170: if ($i + 1 <= $maxPages && $i >= 2) {
171: $items[$i + 1] = "|";
172: }
173: }
174:
175: for ($i = $this->_currentPage - $this->_itemPadding; $i < $this->_currentPage + $this->_itemPadding + 1; $i++) {
176: if ($i <= $maxPages && $i >= 1) {
177: $items[$i] = $i;
178: }
179:
180: if ($i + 1 <= $maxPages && $i >= 2) {
181: $items[$i + 1] = "|";
182: }
183: }
184:
185: for ($i = ($this->getMaxPages() - $this->_nextItems) + 1; $i < $this->getMaxPages() + 1; $i++) {
186: if ($i <= $maxPages && $i >= 2) {
187: $items[$i] = $i;
188: }
189: }
190: }
191:
192: $items[$this->_currentPage] = 'current';
193:
194: return $items;
195: }
196: }