| 3 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/**
|
|
|
4 |
* Project: SmartyPaginate: Pagination for the Smarty Template Engine
|
|
|
5 |
* File: function.paginate_middle.php
|
|
|
6 |
* Author: Monte Ohrt <monte at newdigitalgroup dot com>
|
|
|
7 |
*
|
|
|
8 |
* This library is free software; you can redistribute it and/or
|
|
|
9 |
* modify it under the terms of the GNU Lesser General Public
|
|
|
10 |
* License as published by the Free Software Foundation; either
|
|
|
11 |
* version 2.1 of the License, or (at your option) any later version.
|
|
|
12 |
*
|
|
|
13 |
* This library is distributed in the hope that it will be useful,
|
|
|
14 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
15 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
16 |
* Lesser General Public License for more details.
|
|
|
17 |
*
|
|
|
18 |
* You should have received a copy of the GNU Lesser General Public
|
|
|
19 |
* License along with this library; if not, write to the Free Software
|
|
|
20 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
21 |
*
|
|
|
22 |
* @link http://www.phpinsider.com/php/code/SmartyPaginate/
|
|
|
23 |
* @copyright 2001-2005 New Digital Group, Inc.
|
|
|
24 |
* @author Monte Ohrt <monte at newdigitalgroup dot com>
|
|
|
25 |
* @package SmartyPaginate
|
|
|
26 |
* @version 1.6-dev
|
|
|
27 |
*/
|
|
|
28 |
|
|
|
29 |
function smarty_function_paginate_middle($params, &$smarty) {
|
|
|
30 |
|
|
|
31 |
$_id = 'default';
|
|
|
32 |
$_prefix = '[';
|
|
|
33 |
$_suffix = ']';
|
|
|
34 |
$_link_prefix = '';
|
|
|
35 |
$_link_suffix = '';
|
|
|
36 |
$_page_limit = null;
|
|
|
37 |
$_attrs = array();
|
|
|
38 |
|
|
|
39 |
if (!class_exists('SmartyPaginate')) {
|
|
|
40 |
$smarty->trigger_error("paginate_middle: missing SmartyPaginate class");
|
|
|
41 |
return;
|
|
|
42 |
}
|
|
|
43 |
if (!isset($_SESSION['SmartyPaginate'])) {
|
|
|
44 |
$smarty->trigger_error("paginate_middle: SmartyPaginate is not initialized, use connect() first");
|
|
|
45 |
return;
|
|
|
46 |
}
|
|
|
47 |
|
|
|
48 |
foreach($params as $_key => $_val) {
|
|
|
49 |
switch($_key) {
|
|
|
50 |
case 'id':
|
|
|
51 |
if (!SmartyPaginate::isConnected($_val)) {
|
|
|
52 |
$smarty->trigger_error("paginate_middle: unknown id '$_val'");
|
|
|
53 |
return;
|
|
|
54 |
}
|
|
|
55 |
$_id = $_val;
|
|
|
56 |
break;
|
|
|
57 |
case 'prefix':
|
|
|
58 |
$_prefix = $_val;
|
|
|
59 |
break;
|
|
|
60 |
case 'suffix':
|
|
|
61 |
$_suffix = $_val;
|
|
|
62 |
break;
|
|
|
63 |
case 'link_prefix':
|
|
|
64 |
$_link_prefix = $_val;
|
|
|
65 |
break;
|
|
|
66 |
case 'link_suffix':
|
|
|
67 |
$_link_suffix = $_val;
|
|
|
68 |
break;
|
|
|
69 |
case 'page_limit';
|
|
|
70 |
$_page_limit = $_val;
|
|
|
71 |
break;
|
|
|
72 |
case 'format':
|
|
|
73 |
break;
|
|
|
74 |
default:
|
|
|
75 |
$_attrs[] = $_key . '="' . $_val . '"';
|
|
|
76 |
break;
|
|
|
77 |
}
|
|
|
78 |
}
|
|
|
79 |
|
|
|
80 |
if (!isset($_SESSION['SmartyPaginate'][$_id]['item_total'])) {
|
|
|
81 |
$smarty->trigger_error("paginate_middle: total was not set");
|
|
|
82 |
return;
|
|
|
83 |
}
|
|
|
84 |
|
|
|
85 |
if(!isset($_page_limit) && isset($_SESSION['SmartyPaginate'][$_id]['page_limit'])) {
|
|
|
86 |
$_page_limit = $_SESSION['SmartyPaginate'][$_id]['page_limit'];
|
|
|
87 |
}
|
|
|
88 |
|
|
|
89 |
$_url = $_SESSION['SmartyPaginate'][$_id]['url'];
|
|
|
90 |
|
|
|
91 |
$_total = SmartyPaginate::getTotal($_id);
|
|
|
92 |
$_curr_item = SmartyPaginate::getCurrentItem($_id);
|
|
|
93 |
$_limit = SmartyPaginate::getLimit($_id);
|
|
|
94 |
|
|
|
95 |
$_item = 1;
|
|
|
96 |
$_page = 1;
|
|
|
97 |
$_display_pages = 0;
|
|
|
98 |
$_ret = '';
|
|
|
99 |
|
|
|
100 |
$_attrs = !empty($_attrs) ? ' ' . implode(' ', $_attrs) : '';
|
|
|
101 |
|
|
|
102 |
if(isset($_page_limit)) {
|
|
|
103 |
// find halfway point
|
|
|
104 |
$_page_limit_half = floor($_page_limit / 2);
|
|
|
105 |
// determine what item/page we start with
|
|
|
106 |
$_item_start = $_curr_item - $_limit * $_page_limit_half;
|
|
|
107 |
if( ($_view = ceil(($_total - $_item_start) / $_limit)) < $_page_limit) {
|
|
|
108 |
$_item_start -= ($_limit * ( $_page_limit - $_view ));
|
|
|
109 |
}
|
|
|
110 |
$_item = ($_item_start >= 1) ? $_item_start : 1;
|
|
|
111 |
$_page = ceil($_item / $_limit);
|
|
|
112 |
}
|
|
|
113 |
|
|
|
114 |
while($_item <= $_total) {
|
|
|
115 |
if(isset($params['format']) && $params['format'] == 'page') {
|
|
|
116 |
$_text = $_prefix . $_page . $_suffix;
|
|
|
117 |
} else {
|
|
|
118 |
$_text = $_prefix . $_item . '-';
|
|
|
119 |
$_text .= ($_item + $_limit - 1 <= $_total) ? $_item + $_limit - 1 : $_total;
|
|
|
120 |
$_text .= $_suffix;
|
|
|
121 |
}
|
|
|
122 |
if($_item != $_curr_item) {
|
|
|
123 |
$_this_url = $_url;
|
|
|
124 |
$_this_url .= (strpos($_url, '?') === false) ? '?' : '&';
|
|
|
125 |
$_this_url .= SmartyPaginate::getUrlVar($_id) . '=' . $_item;
|
|
|
126 |
$_ret .= $_link_prefix . '<a href="' . str_replace('&', '&', $_this_url) . '"' . $_attrs . '>' . $_text . '</a>' . $_link_suffix;
|
|
|
127 |
} else {
|
|
|
128 |
$_ret .= $_link_prefix . $_text . $_link_suffix;
|
|
|
129 |
}
|
|
|
130 |
$_item += $_limit;
|
|
|
131 |
$_page++;
|
|
|
132 |
$_display_pages++;
|
|
|
133 |
if(isset($_page_limit) && $_display_pages == $_page_limit)
|
|
|
134 |
break;
|
|
|
135 |
}
|
|
|
136 |
|
|
|
137 |
return $_ret;
|
|
|
138 |
|
|
|
139 |
}
|
|
|
140 |
|
|
|
141 |
?>
|