-
Notifications
You must be signed in to change notification settings - Fork 0
/
paginator.php
367 lines (247 loc) · 9.03 KB
/
paginator.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
//jreviews_overrides/views/helpers/paginator.php
<?php
/**
* JReviews - Reviews Extension
* Copyright (C) 2010-2015 ClickFWD LLC
* This is not free software, do not distribute it.
* For licencing information visit https://www.jreviews.com
* or contact sales@jreviews.com
**/
defined( 'MVC_FRAMEWORK') or die( 'Direct Access to this location is not allowed.' );
S2App::import('Helper','form','jreviews');
class PaginatorHelper extends MyHelper {
var $base_url = null;
var $items_per_page;
var $items_total;
var $current_page;
var $num_pages;
var $mid_range = 6;
var $num_pages_threshold = 10; // After this number the previous/next buttons show up.
var $return;
var $return_module;
var $default_limit = 25;
var $pageUrl = array();
function __construct()
{
$this->current_page = 1;
$this->items_per_page = (!empty($_GET['limit'])) ? (int) $_GET['limit'] : $this->default_limit;
}
function initialize($params = array())
{
if (count($params) > 0)
{
foreach ($params as $key => $val)
{
$this->$key = $val;
}
}
$this->mid_range = Sanitize::getInt($this->Config,'paginator_mid_range',6);
# Construct new route
if(isset($this->passedArgs) && is_null($this->base_url))
{
$this->base_url = cmsFramework::constructRoute($this->passedArgs,array(S2_QVAR_PAGE,'limit','lang'));
}
}
function addPagination($page,$limit)
{
$url = '';
$order = Sanitize::getString($this->params,'order');
$default_limit = Sanitize::getInt($this->params,'default_limit');
$url_params = $this->passedArgs;
unset($url_params[S2_QVAR_PAGE],$url_params['Itemid'],$url_params['option'],$url_params['view']);
if($page == 1
&& $this->limit == $default_limit
&& (
$order == ''
||
$order == Sanitize::getString($this->params,'default_order')
)
&& empty($url_params)
) {
preg_match('/^index.php\?option=com_jreviews&Itemid=[0-9]+/i',$this->base_url,$matches);
$url = $matches[0];
}
else {
$url = $this->base_url;
$page > 1 and $url = rtrim($url,'/') . '/' . S2_QVAR_PAGE . _PARAM_CHAR . $page . '/';
if($this->limit != $default_limit) {
$url = rtrim($url,'/').'/limit'._PARAM_CHAR.$limit.'/';
}
}
// Remove menu segment from url if page 1 and it' a menu
if($page == 1 && preg_match('/^(index.php\?option=com_jreviews&Itemid=[0-9]+)(&url=menu\/)$/i',$url,$matches)) {
$url = $matches[1];
}
$url = cmsFramework::route($url);
return $url;
}
function sortArrayByArray($array,$orderArray) {
$ordered = array();
foreach($orderArray as $key) {
if(array_key_exists($key,$array)) {
$ordered[$key] = $array[$key];
unset($array[$key]);
}
}
return $ordered + $array;
}
function paginate($params)
{
$this->return = '';
$this->initialize($params);
if(!is_numeric($this->items_per_page) OR $this->items_per_page <= 0) {
$this->items_per_page = $this->default_limit;
}
$this->num_pages = ceil($this->items_total/$this->items_per_page);
if($this->current_page < 1 || !is_numeric($this->current_page)) $this->current_page = 1;
if($this->current_page > $this->num_pages) $this->current_page = $this->num_pages;
$prev_page = $this->current_page-1;
$next_page = $this->current_page+1;
# More than num_pages_threshold pages
if($this->num_pages > $this->num_pages_threshold)
{
$url = $this->addPagination($prev_page,$this->items_per_page);
//aldo
$this->return = ($this->current_page != 1 && $this->items_total >= 10) ?
'<a class="jr-pagenav-prev jrPageActive jrButton jrSmall" title="Μεταβείτε στη πρώτη σελίδα" href="./?pg=1">❮❮</a> '
:
'<a class="jrPageCurrent jrButton jrSmall jrDisabled" href="'.$url.'">❮❮</a> ';
//end aldo
//aldo //teleia apo kate prin to = shmantikh
$this->return .= ($this->current_page != 1 && $this->items_total >= 10) ?
'<a class="jr-pagenav-prev jrPageActive jrButton jrSmall" title="Μεταβείτε στη προηγούμενη σελίδα" href="'.$url.'">'.__t("❮",true).'</a> '
:
'<span class="jrPageCurrent jrButton jrSmall jrDisabled">'.__t("❮",true).'</span> ';
$this->start_range = $this->current_page - floor($this->mid_range/2);
$this->end_range = $this->current_page + floor($this->mid_range/2);
if($this->start_range <= 0)
{
$this->end_range += abs($this->start_range)+1;
$this->start_range = 1;
}
if($this->end_range > $this->num_pages)
{
$this->start_range -= $this->end_range-$this->num_pages;
$this->end_range = $this->num_pages;
}
$this->range = range($this->start_range,$this->end_range+1);
// Even mid range
if(!($this->mid_range%2)) {
if($this->current_page < 1 + $this->mid_range) {
$this->range = array_splice($this->range,0,$this->mid_range);
}
elseif($this->current_page > $this->num_pages - $this->mid_range) {
$this->range = array_splice($this->range,1,$this->mid_range+1);
}
else {
$this->range = array_splice($this->range,0,$this->mid_range);
}
}
else {
$this->range = array_splice($this->range,0,$this->mid_range);
}
// INDIVIDUAL PAGES
for($i=1;$i<=$this->num_pages;$i++)
{
if($this->range[0] > 2 && $i == $this->range[0]) {
$this->return .= " ... ";
}
// loop through all pages. if first, last, or in range, display
if($i==1 /*|| $i==$this->num_pages*/ || (in_array($i,$this->range)))
{
$url = $this->addPagination($i,$this->items_per_page);
$this->return .= ($i == $this->current_page) ?
'<span title="'.sprintf(__t("Go to page %s",true),$i,$i,$this->num_pages).'" class="jrPageCurrent jrButton jrSmall">'.$i.'</span> '
:
'<a class="jr-pagenav-page jrButton jrSmall" title="'.sprintf(__t("Go to page %s of %s",true),$i,$this->num_pages).'" href="'.$url.'">'.$i.'</a> ';
$this->pageUrl[$i] = $url;
}
if($this->range[$this->mid_range-1] < $this->num_pages-1 && $i == end($this->range)) {
$this->return .= " ... ";
}
}
// NEXT PAGE
$url = $this->addPagination($next_page,$this->items_per_page);
$this->return .= ($this->current_page != $this->num_pages && $this->items_total >= 10) ?
'<a class="jr-pagenav-next jrButton jrSmall" title="Μεταβείτε στην επόμενη σελίδα" href="'.$url.'">'.__t("❯",true).'</a>'
:
'<span class="jrPageCurrent jrButton jrSmall jrDisabled">'.__t("❯",true).'</span>';
//aldo
$this->return .= ($this->current_page != $this->num_pages && $this->items_total >= 10) ?
'<a class="jr-pagenav-next jrButton jrSmall" title="Μεταβείτε στη τελευταία σελίδα" href="./?pg='.$this->num_pages.'">❯❯</a> '
:
'<span class="jrPageCurrent jrButton jrSmall jrDisabled">❯❯</span>';
//end aldo
}
# num_pages_threshold pages or less
else {
// INDIVIDUAL PAGES
for($i=1;$i<=$this->num_pages;$i++)
{
$url = $this->addPagination($i,$this->items_per_page);
$this->return .= ($i == $this->current_page) ?
'<span class="jrPageCurrent jrButton jrSmall jrDisabled">'.$i.'</span> '
:
'<a class="jr-pagenav-page jrButton jrSmall" href="'.$url.'">'.$i.'</a> ';
$this->pageUrl[$i] = $url;
}
}
}
/**
* Generates the dropdown list for number of items per page
* @return html select list
*/
function display_items_per_page()
{
if(!$this->Config->display_list_limit) return;
$items_per_page = array(5,10,15,20,25,30,35,40,45,50);
$Form = ClassRegistry::getClass('FormHelper');
$segments = '';
$url_param = array();
$passedArgs = $this->passedArgs;
$default_limit = Sanitize::getInt($this->params,'default_limit');
foreach($items_per_page as $limit)
{
if($limit != $default_limit) {
$url = rtrim($this->base_url,'/') . '/limit' . _PARAM_CHAR . $limit . '/';
}
else {
$url = $this->base_url;
}
$url = cmsFramework::route($url);
$selectList[] = array('value'=>$url,'text'=>$limit);
}
if($this->limit != $default_limit) {
$selected = rtrim($this->base_url,'/') . '/limit' . _PARAM_CHAR . $this->limit . '/';
}
else {
$selected = $this->base_url;
}
$selected = cmsFramework::route($selected);
return __t("Results per page",true). ': ' . $Form->select('order_limit',$selectList,$selected,array('class'=>'jr-pagenav-limit','onchange'=>"window.location=this.value"));
}
function display_pages() {
return $this->return;
}
function getPageUrl($page) {
return str_replace('&','&',$this->pageUrl[$page]);
}
/**
* http://googlewebmastercentral.blogspot.com/2011/09/pagination-with-relnext-and-relprev.html
* @param type $page
*/
function addPrevNextUrls(&$page) {
if($this->num_pages > 1) {
if($this->current_page == 1) {
$page['next_url'] = $this->getPageUrl(2);
}
elseif($this->current_page == $this->num_pages) {
$page['prev_url'] = $this->getPageUrl($this->num_pages-1);
}
else {
$page['prev_url'] = $this->getPageUrl($this->current_page-1);
$page['next_url'] = $this->getPageUrl($this->current_page+1);
}
}
}
}