1: <?php defined('_JOOS_CORE') or exit();
2:
3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13: 14: 15: 16: 17: 18: 19: 20: 21: 22:
23: class joosPager
24: {
25: 26: 27: 28: 29:
30: public $components;
31: 32: 33: 34: 35:
36: public $item_per_page = 10;
37: 38: 39: 40: 41:
42: public $current_page = 1;
43: 44: 45: 46: 47:
48: public $max_length = 10;
49: 50: 51: 52: 53:
54: public $total_item;
55: 56: 57: 58: 59:
60: public $total_page;
61: 62: 63: 64: 65:
66: public $output;
67: 68: 69: 70: 71:
72: public $base_url;
73:
74: 75: 76: 77: 78:
79: public $low;
80: 81: 82: 83: 84:
85: public $high;
86: 87: 88: 89: 90:
91: public $limit;
92: public $offset;
93: 94: 95:
96: private $_output;
97:
98: public function __construct($base_url = '', $total_items = 120, $item_per_page = 10, $max_length = 11, $extra_url = '')
99: {
100: $this->base_url = $base_url;
101: $this->base_url = $extra_url ? $this->base_url . $extra_url : $this->base_url;
102: $this->total_item = $total_items;
103: $this->max_length = $max_length;
104: $this->item_per_page = $item_per_page;
105: }
106:
107: public function paginate($page, $item_per_page = 0)
108: {
109: if ($item_per_page === 0) {
110: $item_per_page = $this->item_per_page;
111: } else {
112: $this->item_per_page = $item_per_page;
113: }
114:
115: $this->total_page = ceil($this->total_item / $item_per_page);
116:
117: $this->current_page = (int) $page;
118:
119: if ($this->current_page < 1 || !is_numeric($this->current_page)) {
120: $this->current_page = 1;
121: }
122:
123: if ($this->current_page > $this->total_page) {
124: $this->current_page = $this->total_page;
125: }
126:
127: $prev_page = $this->current_page - 1;
128: $next_page = $this->current_page + 1;
129:
130: $this->_output .= '';
131:
132:
133: $_prev = "<a class=\"page_left\" href=\"{$this->base_url}/page/{$prev_page}\">←</a>";
134: $_class = ($this->current_page != 1) ? '' : ' class="disabled"';
135: $this->components['prev'] = '<li' . $_class . '>' . $_prev . '</li>';
136:
137:
138: if ($this->total_page > $this->max_length) {
139: $midRange = $this->max_length - 2;
140:
141: $start_range = $this->current_page - floor($midRange / 2);
142: $end_range = $this->current_page + floor($midRange / 2);
143:
144: if ($start_range <= 0) {
145: $end_range += abs($start_range) + 1;
146: $start_range = 1;
147: }
148:
149: if ($end_range > $this->total_page) {
150: $start_range -= $end_range - $this->total_page;
151: $end_range = $this->total_page;
152: }
153:
154: while ($end_range - $start_range + 1 < $this->max_length - 1) {
155: $end_range++;
156: }
157:
158: $modulus = (int) $this->max_length % 2 == 0;
159: $center = floor($this->max_length / 2);
160:
161: if ($this->current_page > $center) {
162: $end_range--;
163: }
164:
165: if ($modulus == 0 && $this->total_page - $this->current_page + 1 <= $center) {
166: while ($end_range - $start_range + 1 < $this->max_length - 1) {
167: $start_range--;
168: }
169: }
170: $range = range($start_range, $end_range);
171:
172: for ($i = 1; $i <= $this->total_page; $i++) {
173: if ($i == 1 || $i == $this->total_page || in_array($i, $range)) {
174: $lastDot = '';
175:
176: if ($modulus == 1) {
177: if ($i == $this->total_page && $this->current_page < ($this->total_page - ($this->max_length - $center - $modulus))) {
178: $lastDot = '...';
179: }
180: } else {
181: if ($i == $this->total_page && $this->current_page <= ($this->total_page - ($this->max_length - $center))) {
182: $lastDot = '...';
183: }
184: }
185:
186:
187: $_number = "<a href=\"{$this->base_url}/page/$i\">$lastDot $i";
188:
189: if ($range[0] > 2 && $i == 1) {
190: $_number .= " ...</a>";
191: } else {
192: $_number .= '</a>';
193: }
194:
195: $_class = ($i == $this->current_page) ? ' class="active"' : '';
196: $this->_output .= '<li' . $_class . '>' . $_number . '</li>';
197: }
198: }
199:
200:
201: $_next = "<a class=\"page_right\" href=\"{$this->base_url}/page/$next_page\">→</a>";
202: $_class = ($this->current_page != $this->total_page && $this->total_item >= $this->max_length) ? '' : ' class="disabled"';
203: $this->components['next'] = '<li' . $_class . '>' . $_next . '</li>';
204:
205: } else {
206:
207: for ($i = 1; $i <= $this->total_page; $i++) {
208: $_number = "<a href=\"{$this->base_url}/page/$i\">$i</a>";
209: $_class = ($i == $this->current_page) ? ' class="active"' : '';
210: $this->_output .= '<li' . $_class . '>' . $_number . '</li>';
211: }
212:
213:
214: $_next = "<a class=\"page_right\" href=\"{$this->base_url}/page/$next_page\">→</a>";
215:
216: $_class = ($this->current_page != $this->total_page) ? '' : ' class="disabled"';
217: $this->components['next'] = '<li' . $_class . '>' . $_next . '</li>';
218:
219: }
220:
221: $this->low = ($this->current_page - 1) * $this->item_per_page;
222: $this->high = ($this->current_page * $this->item_per_page) - 1;
223: $this->low = $this->low < 0 ? 0 : $this->low;
224: $this->limit = $this->item_per_page;
225: $this->offset = $this->low;
226:
227: $this->components['current_page'] = $this->current_page;
228: $this->components['total_page'] = $this->total_page;
229:
230: $this->output = '<div class="pagination"><ul>' . $this->components['prev'] . $this->_output . $this->components['next'] . '</ul></div>';
231:
232: $this->output = $this->total_item > 0 && $this->total_page > 1 ? $this->output : '';
233:
234: $this->components['pages'] = $this->output;
235:
236: return $this->components;
237: }
238:
239: }
240: