Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/site/app/views/helpers/pagination.php
blob: 3bb22432965b2b55afcfaf94804b91297fdf418e (plain)
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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
<?php
/**
 * Pagination Helper, responsible for managing the LINKS required for pagination.
 * ALL parameters are specified in the component.
 */
class PaginationHelper extends Helper 
{
/**
 * Options to be passed to ajax links if used
 *
 * @var array
 * @access public
 */
    var $ajaxLinkOptions = array();
/**
 * Placeholder for the link style - defined in/by the component
 *
 * @var boolean
 * @access private
 */
    var $style = 'html';
/**
 * Placeholder for the parameter style - defined in/by the component
 *
 * @var boolean
 * @access private
 */
    var $paramStyle = 'get';
/**
 * Placeholder for the pagination details
 *
 * @var array
 * @access private
 */
    var $_pageDetails = array();
/**
 * Helpers
 *
 * @var array
 * @access private
 */
    var $helpers = array("Html","Ajax");

/**
 * Sets the default pagination options. Fails if the value $paging is not set
 *
 * @param array $paging an array detailing the page options
 * @return boolean
 */
    function setPaging($paging)
    {
        if (empty($paging)) {return false;}
        $this->_pageDetails = $paging;
        $this->_pageDetails['previousPage'] = ($paging['page']>1) ? $this->_pageDetails['page']-1 : '';
        $this->_pageDetails['nextPage'] = ($paging['page'] < $this->_pageDetails['pageCount']) ? $this->_pageDetails['page']+1 : '';

        $this->url = $this->_pageDetails['url'];
        
        $getParams = $this->params['url'];
        unset($getParams['url']);
        $this->getParams = $getParams;
        
        $this->showLimits = $this->_pageDetails['showLimits'];
        $this->style = isset($this->_pageDetails['style'])?$this->_pageDetails['style']:$this->style;
        
        if (($this->_pageDetails['maxPages'] % 2)==0) // need odd number of page links
        {
            $this->_pageDetails['maxPages'] = $this->_pageDetails['maxPages']-1;
        }
        
        $this->maxPages = $this->_pageDetails['maxPages'];
        $this->pageSpan = ($this->_pageDetails['maxPages']-1)/2;
    
        return true;
    }
    
/**
* Displays the list of pages for the given parameters.
*
* @param string text to display before limits
* @param string display a separate between limits
* @param boolean whether to escape the title or not
* @return unknown the html string for modifying the number of results per page
**/
    function resultsPerPage($t="Results per page: ", $separator=" ",$escapeTitle=true)
    {
        if (empty($this->_pageDetails)) { return false; }
        if ( !empty($this->_pageDetails['pageCount']) )
        {
            if(is_array($this->_pageDetails['resultsPerPage']))
            {
                $OriginalValue = $this->_pageDetails['show'];
                $t .= $separator;
                foreach($this->_pageDetails['resultsPerPage'] as $value)
                {
                    if($OriginalValue == $value)
                    {
                        $t .= '<em>'.$value.'</em>'.$separator;
                    }
                    else
                    {
                        $this->_pageDetails['show'] = $value;
                        $t .= $this->_generateLink($value,1,$escapeTitle).$separator;
                    }
                }
                $this->_pageDetails['show'] = $OriginalValue;
            }
            return $t;
        }
        return false;
    }

    function resultsPerPageSelect($t="Results per page: ")
    {
        if (empty($this->_pageDetails)) { return false; }
        if ( !empty($this->_pageDetails['pageCount']) )
        {
            $Options = array();
            if(is_array($this->_pageDetails['resultsPerPage']))
            {
                foreach($this->_pageDetails['resultsPerPage'] as $value)
                {
                    $Options[$value] = $value;
                }
            }
            return $t.$this->Html->selectTag("pagination/show", $Options, $this->_pageDetails['show'], NULL, NULL,FALSE);
        }
        return false;
    }

/**
* Displays info of the current result set
*
* @param string
* @param string
* @param string
* @param string
* @return unknown the html string for the current result set.
**/
    function result($t = "%s items") {
        if (empty($this->_pageDetails))
            return false;
        
        if (!empty($this->_pageDetails['pageCount'])) {
            $t = sprintf($t, $this->_pageDetails['total']);
            return $t;
        }
        return false;
    }
/**
* Returns a list of page numbers separated by $separator
*
* @param boolean
* @return string html for the list of page numbers
**/
    function pageNumbers($escapeTitle=true)
    {
        if (empty($this->_pageDetails) || $this->_pageDetails['pageCount'] == 1) {
            // single page result
            return '';
        }
        $total = $this->_pageDetails['pageCount'];
        $max = $this->maxPages;
        $span = $this->pageSpan;
        $spacer = '<li class="skip">&#8230;</li>';
        
        if ($total<$max) {
            $upperLimit = min($total,($span*2+1));
            $lowerLimit = 1;
        } elseif ($this->_pageDetails['page']<($span+1)) {
            $lowerLimit = 1;
            $upperLimit = min($total,($span*2+1));
        } elseif ($this->_pageDetails['page']>($total-$span)) {
            $upperLimit = $total;
            $lowerLimit = max(1,$total-$span*2);
        } else {
            $upperLimit = min ($total,$this->_pageDetails['page']+$span);
            $lowerLimit = max (1,($this->_pageDetails['page']-$span));
        }
        
        $t = array();
        if (($lowerLimit<>1) && ($this->showLimits)) {
            $lowerLimit = $lowerLimit+1;
            $t[] = '<li>'.$this->_generateLink(1,1,$escapeTitle).'</li>';
            $t[] = $spacer;
        }
        
        if (($upperLimit<>$total) && ($this->showLimits))
            $dottedUpperLimit = true;
        else
            $dottedUpperLimit = false;
        
        if (($upperLimit<>$total) && ($this->showLimits))
            $upperLimit = $upperLimit-1;

        for ($i = $lowerLimit; $i <= $upperLimit; $i++) {
             $class = ($i == $this->_pageDetails['page']) ? 'class="selected"' : '';
             $text = "<li {$class}>".$this->_generateLink($i,$i,$escapeTitle).'</li>';
             $t[] = $text;
        }
        if ($dottedUpperLimit) {
            $t[] = $spacer;
            $t[] = '<li>'.$this->_generateLink($this->_pageDetails['pageCount'],
                $this->_pageDetails['pageCount'],$escapeTitle).'</li>';
        }
        
        $t = implode("\n", $t);
        return $t;
    }
    
/**
* Displays a link to the previous page, where the page doesn't exist then
* display the $text
*
* @param string $text - text display: defaults to next
* @return string html for link/text for previous item
**/
    function prevPage($text='prev',$escapeTitle=true)
    {
        if (empty($this->_pageDetails)) { return false; }
        if ( !empty($this->_pageDetails['previousPage']) )
        {
            return $this->_generateLink($text,$this->_pageDetails['previousPage'],$escapeTitle,'prev');
        }
        return '';
    }
    
/**
* Displays a link to the next page, where the page doesn't exist then
* display the $text
*
* @param string $text - text to display: defaults to next
* @return string html for link/text for next item
**/
    function nextPage($text='next',$escapeTitle=true)
    {
        if (empty($this->_pageDetails)) { return false; }
        if (!empty($this->_pageDetails['nextPage']))
        {
            return $this->_generateLink($text,$this->_pageDetails['nextPage'],$escapeTitle,'next');
        }
        return '';
    }

/**
* Displays a link to the first page
* display the $text
*
* @param string $text - text to display: defaults to next
* @return string html for link/text for next item
**/
    function firstPage($text='first',$escapeTitle=true)
    {
        if (empty($this->_pageDetails)) { return false; }
        if ($this->_pageDetails['page']<>1)
        {
            return $this->_generateLink($text,1,$escapeTitle);
        }
        else
        {
            return false;
        }
    }

/**
* Displays a link to the last page
* display the $text
*
* @param string $text - text to display: defaults to next
* @return string html for link/text for next item
**/
    function lastPage($text='last',$escapeTitle=true)
    {
        if (empty($this->_pageDetails)) { return false; }
        if ($this->_pageDetails['page']<>$this->_pageDetails['pageCount'])
        {
            return $this->_generateLink($text,$this->_pageDetails['pageCount'],$escapeTitle);
        }
        else
        {
            return false;
        }
    }


/**
* Generate link to sort the results by the given value
*
* @param string field to sort by
* @param string title for link defaults to $value
* @param string model to sort by - uses the default model class if absent
* @param boolean escape title
* @param string text to append to links to indicate sorted ASC
* @param string text to append to links to indicate sorted DESC
* @return string html for link to modify sort order
**/
    function sortBy ($value, $title=NULL, $Model=NULL,$escapeTitle=true,$upText=" ^",$downText=" v") 
    {
        if (empty($this->_pageDetails)) { return false; }
        $title = $title?$title:ucfirst($value);
        $value = strtolower($value);
        $Model = $Model?$Model:$this->_pageDetails['Defaults']['sortByClass'];

        $OriginalSort = $this->_pageDetails['sortBy'];
        $OriginalModel = $this->_pageDetails['sortByClass'];
        $OriginalDirection = $this->_pageDetails['direction'];
        //echo "does $value = $OriginalSort<br>";
        //echo "does '$Model' = '$OriginalModel'<br>";

        if (($value==$OriginalSort)&&($Model==$OriginalModel)) 
        {
            if (up($OriginalDirection)=="DESC") 
            {
                $this->_pageDetails['direction'] = "ASC";
                $title .= $upText;
            } 
            else 
            {
                $this->_pageDetails['direction'] = "DESC";
                $title .= $downText;
            }
        }
        else
        {
            if ($Model) 
            {
                $this->_pageDetails['sortByClass'] = $Model;
                //echo "page details model class set to ".$this->_pageDetails['sortByClass']."<br>";
            }
            else
            {
                $this->_pageDetails['sortByClass'] = NULL;
            }
            $this->_pageDetails['sortBy'] = $value;
        }
        $link = $this->_generateLink ($title,1,$escapeTitle);
        $this->_pageDetails['sortBy'] = $OriginalSort;
        $this->_pageDetails['sortByClass'] = $OriginalModel;
        $this->_pageDetails['direction'] = $OriginalDirection;
        return $link;
    }

/**
* Generate a select box for options to sort results
*
* @param array array of text strings, formatted as "Field::Direction::Class".
* @param string prefix text
* @param string text to append to links to indicate sorted ASC
* @param string text to append to links to indicate sorted DESC
* @return unknown the html string for the select box for selecting sort order
**/
    function sortBySelect($sortFields, $t="Sort By: ",$upText=" ^",$downText=" v", $selectAttr = array(), $optionAttr = array())
    {
        if (empty($this->_pageDetails)) { return false; }
        if ( !empty($this->_pageDetails['pageCount']) )
        {
            $OriginalValue = $this->_pageDetails['sortBy']."::".$this->_pageDetails['direction']."::".$this->_pageDetails['sortByClass'];
            if(is_array($sortFields))
            {
                foreach($sortFields as $value)
                {
                    $Vals = Array();
                    $Vals = explode("::",$value);
                    if (isset($Vals[2]))
                    {
                        $DisplayVal = $Vals[2]." ";
                    }
                    else
                    {
                        $DisplayVal = "";
                    }
                    //$DisplayVal .= $Vals[0];
                    if (up($Vals[1])=="ASC")
                    {
                        $DisplayVal .= $downText;
                    }
                    else
                    {
                        $DisplayVal .= $upText;                        
                    }
                    $Options[$value] = $DisplayVal;
                }
                return $t.$this->Html->selectTag("pagination/sortByComposite", $Options, $OriginalValue, $selectAttr, $optionAttr, false);
            }
        }
        return false;
    }
    
    
/**
* Internal method to generate links based upon passed parameters.
*
* @param string title for link
* @param string page the page number
* @param boolean escape title
* @param string the div to be updated by AJAX updates
* @return string html for link
**/
    function _generateLink ($title,$page=NULL,$escapeTitle,$rel=null)
    {
        $url = $this->_generateUrl($page);
        $AjaxDivUpdate = $this->_pageDetails['ajaxDivUpdate'];
        if ($this->style=="ajax")
        {
            $options = am($this->ajaxLinkOptions,
                            array(
                                "update" => $this->_pageDetails['ajaxDivUpdate']
                                )
                            );
            if (isset($this->_pageDetails['ajaxFormId']))
            {
                $id = 'link' . intval(rand());
                $return = $this->Html->link(
                                $title,
                                $url,
                                array('id' => $id, 'onclick'=>" return false;"),
                                NULL,
                                $escapeTitle
                                    );
                $options['with'] = "Form.serialize('{$this->_pageDetails['ajaxFormId']}')";
                $options['url'] = $url;
                $return .= $this->Ajax->Javascript->event("'$id'", "click", $this->Ajax->remoteFunction($options));
                return $return;
            }
            else
            {
                // @TODO make ajax helper locale-aware and change this part accordingly if needed
                return $this->Ajax->link(
                                $title,
                                $url,
                                $options,
                                NULL,
                                NULL,
                                $escapeTitle
                                    );
            }
        }
        else
        {
            return $this->Html->linkNoLocaleNoApp(
                            $title,
                            $url,
                            isset($rel) ? array('rel' => $rel) : NULL,
                            NULL,
                            $escapeTitle
                                );
        }
    }

    function _generateUrl ($page=NULL) 
    {
        $getParams = $this->getParams; // Import any other pre-existing get parameters
        if ($this->_pageDetails['paramStyle']=="pretty")
        {
            $pageParams=$this->_pageDetails['importParams'];
        }
        $pageParams['show'] = $this->_pageDetails['show'];
        $pageParams['sortBy'] = $this->_pageDetails['sortBy'];
        $pageParams['direction'] = $this->_pageDetails['direction'];
        $pageParams['page'] = $page?$page:$this->_pageDetails['page'];
        if (isset($this->_pageDetails['sortByClass']))
        {
            $pageParams['sortByClass'] = $this->_pageDetails['sortByClass'];
        }
        $getString = array();
        $prettyString = array();
        if ($this->_pageDetails['paramStyle']=="get")
        {
            $getParams = am($getParams,$pageParams);
        }
        else
        {
            foreach($pageParams as $key => $value)
            {
                if (isset($this->_pageDetails['Defaults'][$key]))
                {
                    if (up($this->_pageDetails['Defaults'][$key])<>up($value))
                    {
                        $prettyString[] = "$key{$this->_pageDetails['paramSeperator']}$value";
                    }
                }
                else
                {
                    $prettyString[] = "$key{$this->_pageDetails['paramSeperator']}$value";
                }            
            }
        }
        foreach($getParams as $key => $value)
        {
            if ($this->_pageDetails['paramStyle']=="get")
            {
                if (isset($this->_pageDetails['Defaults'][$key]))
                {
                    if ($key == 'show' || (up($this->_pageDetails['Defaults'][$key])<>up($value)))
                    {
                        $getString[] = "$key=$value";
                    }
                }
                else
                {
                    $getString[] = "$key=$value";
                }
            }
            else
            {
                $getString[] = "$key=$value";
            }            
        }
        $url = $this->url;
        if ($prettyString)
        {
            $prettyString = implode ("/", $prettyString);
            $url .= $prettyString;
        }
        if ($getString)
        {
            $getString = implode ("&", $getString);
            $url .= "?".$getString;
        }
                
		// Escape url to prevent XSS
		$this->view->controller->_sanitizeArray($url);
                
        return $url;
    }
}
?>