Ticket #261: function.pagination.php

File function.pagination.php, 5.0 kB (added by bballizlife, 1 year ago)

pagination plugin for jTpl

Line 
1 <?php
2 /**
3 * @package    jelix
4 * @subpackage jtpl_plugin
5 * @author     Loic Mathaud <loic@mathaud.net>
6 * @copyright  Loic Mathaud 2007
7 * @contributor Christian Tritten (christian.tritten@laposte.net)
8 * @copyright  2007 Christian Tritten
9 * @link        http://www.jelix.org
10 * @licence    GNU Lesser General Public Licence see LICENCE file or http://www.gnu.org/licenses/lgpl.html
11 */
12
13 /**
14 * displays page links
15 *
16 * @param jTpl $tpl template engine
17 * @param string $action selector of the action
18 * @param array $actionParams parameters for the action
19 * @param object $pageCounter jPageCounter object
20 * @param string $paramName name of the parameter in the actionParams which will content the page number
21 * @param array $displayProperties properties for the links display
22 */
23 function jtpl_function_html_pagination($tpl, $action, $actionParams,
24                 $pageCounter, $paramName = 'page', $displayProperties = array()) {
25
26
27     // If there are at least two pages of results
28     if ($pageCounter->getTotalItems() > $pageCounter->getItemsPerPage()) {
29         $jUrlEngine = jUrl::getEngine();
30
31         $urlaction = jUrl::get($action, $actionParams, jUrl::JURLACTION);
32
33         $defaultDisplayProperties = array('start-label' => '|&lt;',
34                                           'prev-label'  => '&lt;',
35                                           'next-label'  => '&gt;',
36                                           'end-label'   => '&gt;|',
37                                           'area-size'   => 0);
38
39         if (is_array($displayProperties) && count($displayProperties) > 0) {
40             $displayProperties = array_merge($defaultDisplayProperties, $displayProperties);
41         } else {
42             $displayProperties = $defaultDisplayProperties;
43         }
44         
45         $current_page = $pageCounter->getCurrentPage();
46         $total_pages = $pageCounter->getTotalPages(); 
47
48         // Links display
49         echo '<ul class="pagelinks">';
50
51         // Start and Prev Links
52         if ($current_page > 1) {
53             // Calculate start page url
54             $urlaction->params[$paramName] = 1;
55             $urlStartPage = $jUrlEngine->create($urlaction);
56
57             // Calculate previous page url
58             $urlaction->params[$paramName] = $current_page - 1;
59             $urlPrevPage = $jUrlEngine->create($urlaction);
60         
61             // Start link
62             if (!empty($displayProperties['start-label'])) {
63                 echo '<li class="pagelinks-start">';
64                 echo '<a href="', $urlStartPage->toString(true), '">', $displayProperties['start-label'], '</a>';
65                 echo '</li>', "\n";
66             }
67
68             // Previous link
69             if (!empty($displayProperties['prev-label'])) {
70                 echo '<li class="pagelinks-prev">';
71                 echo '<a href="', $urlPrevPage->toString(true), '">', $displayProperties['prev-label'], '</a>';
72                 echo '</li>', "\n";
73             }
74             
75             // Prev Area Links
76             $area_start = $current_page - $displayProperties['area-size'];
77             if ($displayProperties['area-size'] == 0 || $area_start <= 0) {
78                 $area_start = 1;
79             }
80             for ($i = $area_start; $i < $current_page; $i++) {
81                 $urlaction->params[$paramName] = $i;
82                 $url = $jUrlEngine->create($urlaction);
83                 echo '<li><a href="', $url->toString(true), '">', $i, '</a></li>', "\n";
84             }
85         }
86
87         // Current page
88         echo '<li class="pagelinks-current">', $current_page, '</li>',"\n";
89
90         // Next and End Links
91         if ($current_page < $total_pages) {
92             // Next Area Links
93             $area_end = $current_page + $displayProperties['area-size'];
94             if ($displayProperties['area-size'] == 0 || $area_end > $total_pages) {
95                 $area_end = $total_pages;
96             }
97             
98             for ($i = $current_page + 1; $i <= $area_end; $i++) {
99                 $urlaction->params[$paramName] = $i;
100                 $url = $jUrlEngine->create($urlaction);
101                 echo '<li><a href="', $url->toString(true), '">', $i, '</a></li>', "\n";
102             }
103         
104             // Calculate next page url
105             $urlaction->params[$paramName] = $current_page + 1;
106             $urlNextPage = $jUrlEngine->create($urlaction);
107
108             // Calculate end page url
109             $urlaction->params[$paramName] = $total_pages;
110             $urlEndPage = $jUrlEngine->create($urlaction);
111
112         
113             // Next link
114             if (!empty($displayProperties['next-label'])) {
115                 echo '<li class="pagelinks-next">';
116                 echo '<a href="', $urlNextPage->toString(true), '">', $displayProperties['next-label'], '</a>';
117                    echo '</li>', "\n";
118             }
119
120             // End link
121             if (!empty($displayProperties['end-label'])) {
122                 echo '<li class="pagelinks-end">';
123                 echo '<a href="', $urlEndPage->toString(true), '">', $displayProperties['end-label'], '</a>';
124                    echo '</li>', "\n";
125             }
126         }
127
128         echo '</ul>';
129     }
130 }
131
132 ?>
133
Download in other formats: Original Format