1 <?php
2
3 4 5 6 7 8 9 10
11
12 namespace Brickrouge;
13
14 class extends Element
15 {
16 const T_COUNT = '#pager-count';
17 const T_GAP = '#pager-gap';
18 const T_LIMIT = '#pager-limit';
19 const T_NO_ARROWS = '#pager-no-arrows';
20 const T_POSITION = '#pager-position';
21 const T_SEPARATOR = '#pager-separator';
22 const T_URLBASE = '#pager-urlbase';
23 const T_USING = '#pager-using';
24 const T_WITH = '#pager-with';
25 const BROWSE_PREVIOUS_LABEL = '#pagination-browse-previous-label';
26 const BROWSE_NEXT_LABEL = '#pagination-browse-next-label';
27
28 public function __construct($type, $tags)
29 {
30 parent::__construct
31 (
32 $type, $tags + array
33 (
34 self::T_LIMIT => 5,
35
36 self::T_GAP => '<span class="gap"> … </span>',
37 self::T_USING => 'page',
38
39 'class' => 'pagination'
40 )
41 );
42 }
43
44 protected $urlbase;
45
46 public function render_inner_html()
47 {
48 $limit = $this[self::T_LIMIT];
49 $count = $this[self::T_COUNT];
50
51 $pages = ceil($count / $limit);
52
53 $this->urlbase = $this->getURLBase();
54
55 $gap = $this[self::T_GAP];
56 $separator = $this[self::T_SEPARATOR];
57
58
59
60
61
62
63
64 $on_page = $this[self::T_POSITION] + 1;
65
66 $rc = '';
67
68 if ($pages > 10)
69 {
70 $init_page_max = min($pages, 3);
71
72 for ($i = 1 ; $i < $init_page_max + 1 ; $i++)
73 {
74 if ($i == $on_page)
75 {
76 $rc .= $this->getPosition($i);
77 }
78 else
79 {
80 $rc .= $this->getLink($i - 1);
81 }
82
83 if ($i < $init_page_max)
84 {
85 $rc .= $separator;
86 }
87 }
88
89 if ($pages > 3)
90 {
91 if (($on_page > 1) && ($on_page < $pages))
92 {
93 $rc .= ($on_page > 5) ? $gap : $separator;
94
95 $init_page_min = ($on_page > 4) ? $on_page : 5;
96 $init_page_max = ($on_page < $pages - 4) ? $on_page : $pages - 4;
97
98 for ($i = $init_page_min - 1; $i < $init_page_max + 2; $i++)
99 {
100 $rc .= ($i == $on_page) ? $this->getPosition($i) : $this->getLink($i - 1);
101
102 if ($i < $init_page_max + 1)
103 {
104 $rc .= $separator;
105 }
106 }
107
108 $rc .= ($on_page < $pages - 4) ? $gap : $separator;
109 }
110 else
111 {
112 $rc .= $gap;
113 }
114
115 for ($i = $pages - 2 ; $i < $pages + 1 ; $i++)
116 {
117 $rc .= ($i == $on_page) ? $this->getPosition($i) : $this->getLink($i - 1);
118
119 if ($i < $pages)
120 {
121 $rc .= $separator;
122 }
123 }
124 }
125 }
126 else
127 {
128 for ($i = 1 ; $i < $pages + 1 ; $i++)
129 {
130 $rc .= ($i == $on_page) ? $this->getPosition($i) : $this->getLink($i - 1);
131
132 if ($i < $pages)
133 {
134 $rc .= $separator;
135 }
136 }
137 }
138
139 if (!$this[self::T_NO_ARROWS])
140 {
141
142
143
144
145 $next_text = $this[self::BROWSE_NEXT_LABEL];
146 $previous_text = $this[self::BROWSE_PREVIOUS_LABEL];
147
148 if (!$next_text)
149 {
150 $next_text = t('Next', array(), array('scope' => 'pagination.label', 'default' => 'Next →'));
151 }
152
153 if (!$previous_text)
154 {
155 $previous_text = t('Previous', array(), array('scope' => 'pagination.label', 'default' => '← Previous'));
156 }
157
158
159 if ($on_page < $pages)
160 {
161 $rc .= $this->getLink($on_page, $next_text, 'next');
162
163 }
164 else
165 {
166 $rc .= '<li class="next disabled"><a href="#">' . $next_text . '</a></li>';
167 }
168
169
170
171
172
173
174
175 if ($on_page > 1)
176 {
177 $rc = $this->getLink($on_page - 2, $previous_text, 'previous') . $rc;
178
179 }
180 else
181 {
182 $rc = '<li class="previous disabled"><a href="#">' . $previous_text . '</a></li>' . $rc;
183 }
184 }
185
186 return '<ul>' . $rc . '</ul>';
187 }
188
189 public function __toString()
190 {
191 $limit = $this[self::T_LIMIT];
192
193 if (!$limit)
194 {
195 return '';
196 }
197
198 $count = $this[self::T_COUNT];
199
200 $pages = ceil($count / $limit);
201
202 if ($pages < 2)
203 {
204 return '';
205 }
206
207 return parent::__toString();
208 }
209 210 211 212 213 214 215
216
217 protected function getURLBase()
218 {
219 $rc = $this[self::T_URLBASE];
220
221 $with = $this[self::T_WITH];
222
223 if ($with)
224 {
225 if (is_string($with))
226 {
227 $parts = explode(',', $with);
228 $parts = array_map('trim', $parts);
229 $parts = array_flip($parts);
230
231 foreach ($parts as $name => &$part)
232 {
233 $part = isset($_REQUEST[$name]) ? $_REQUEST[$name] : null;
234 }
235 }
236 else
237 {
238 $parts = (array) $with;
239 }
240 }
241 else
242 {
243 $parts = array();
244 }
245
246
247
248
249
250 $using = $this[self::T_USING] ?: 'page';
251
252 unset($parts[$using]);
253
254 $parts[$using] = '';
255
256
257
258
259
260 $rc .= '?' . http_build_query
261 (
262 $parts, '', '&'
263 );
264
265 return $rc;
266 }
267
268 protected function getURL($n)
269 {
270 return $this->urlbase . $n;
271 }
272
273 protected function getLink($n, $label=null, $class='page')
274 {
275 $rc = '<li' . ($class ? ' class="' . $class . '"' : '') . '><a href="' . $this->getURL($n) . '">';
276 $rc .= $label ? $label : ($n + 1);
277 $rc .= '</a></li>';
278
279 return $rc;
280 }
281
282 protected function getPosition($n)
283 {
284 return '<li class="page active"><a href="#">' . $n . '</a></li>';
285 }
286 }