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