Shorten strings
[squirrelmail.git] / functions / template / abook_util.php
CommitLineData
caa596b2 1<?php
2
3/**
4 * abook_util.php
5 *
6 * The following functions are utility functions for templates. Do not
7 * echo output in these functions.
8 *
9 * @copyright &copy; 2005-2008 The SquirrelMail Project Team
10 * @license http://opensource.org/licenses/gpl-license.php GNU Public License
11 * @version $Id$
12 * @package squirrelmail
13 */
14
15
16/**
4fe67ca6 17 * Display a column header with sort buttons
18 *
19 * @param string $field Which field to display
20 * @param array $current_page_args All known query string arguments
21 * for the current page request; structured
22 * as an associative array of key/value pairs
23 *
24 * @author Steve Brown
25 * @since 1.5.2
26 */
27function addAbookSort ($field, $current_page_args) {
caa596b2 28 global $abook_sort_order, $nbsp;
29
30 switch ($field) {
31 case 'nickname':
32 $str = _("Nickname");
33 $alt = _("Sort by nickname");
34 $down = 0;
35 $up = 1;
36 $has_sort = true;
37 break;
38 case 'fullname':
39 $str = _("Name");
40 $alt = _("Sort by name");
41 $down = 2;
42 $up = 3;
43 $has_sort = true;
44 break;
45 case 'email':
46 $str = _("E-mail");
47 $alt = _("Sort by email");
48 $down = 4;
49 $up = 5;
50 $has_sort = true;
51 break;
52 case 'info':
53 $str = _("Info");
54 $alt = _("Sort by info");
55 $down = 6;
56 $up = 7;
57 $has_sort = true;
58 break;
59 default:
60 return 'BAD SORT FIELD GIVEN: "'.$field.'"';
61 }
62
63 // show_abook_sort_button() creates a hyperlink (using hyperlink.tpl) that encompases an image, using a getImage() call
4fe67ca6 64 return $str . ($has_sort ? $nbsp . show_abook_sort_button($abook_sort_order, $alt, $down, $up, $current_page_args) : '');
65}
66
67
68/**
69 * Creates an address book paginator
70 *
71 * @param boolean $abook_page_selector Whether or not to show the page selector
72 * @param int $abook_page_selector_max The maximum number of page links to show
73 * on screen
74 * @param int $page_number What page is being viewed - 0 if not used
75 * @param int $page_size Maximum number of addresses to be shown
76 * per page
77 * @param int $total_addresses The total count of addresses in the backend
78 * @param boolean $show_all Whether or not all addresses are being shown
79 * @param array $current_page_args All known query string arguments
80 * for the current page request; structured
81 * as an associative array of key/value pairs
82 * @param boolean $compact Whether or not to build a smaller,
83 * "compact" paginator
84 *
85 * @return string The paginator, ready for output
86 *
87 */
88function get_abook_paginator($abook_page_selector, $abook_page_selector_max,
89 $page_number, $page_size, $total_addresses,
90 $show_all, $current_page_args, $compact) {
91
92 // if showing all, just show pagination link
93 //
94 if ($show_all)
95 {
96 unset($current_page_args['show_all']);
97 return '[' . make_abook_paginator_link(1, _("Paginate"), $current_page_args) . ']';
98 }
99
100
101 // if we don't have enough information to build the paginator, return nothing
102 //
103 if (empty($page_number) || empty($page_size) || empty($total_addresses))
104 return '';
105
106
107 // calculate some values we need below
108 //
109 $show_elipses_before = FALSE;
110 $show_elipses_after = FALSE;
111 global $nbsp;
112 $sep = '|';
113 $paginator_string = '[';
114 $total_pages = ceil($total_addresses / $page_size);
115 if ($page_number > $total_pages) $page_number = $total_pages;
116 $spacing = ($compact ? $nbsp : $nbsp . $nbsp);
117
118
119 // only enough addresses for one page anyway? no pagination needed
120 //
121 if ($total_pages < 2) return '';
122
123
124 // build "Show All" link
125 //
126 $show_all_string = '['
127 . make_abook_paginator_link(1, _("Show All"),
128 array_merge($current_page_args, array('show_all' => 1)))
129 . ']';
130
131
132 // build next/previous links for compact paginator
133 //
134 if ($compact)
135 {
136 if ($page_number > 1)
137 $paginator_string .= make_abook_paginator_link(1,
138 _("<<"),
139 $current_page_args)
140 . ']['
141 . make_abook_paginator_link($page_number - 1,
142 _("<"),
143 $current_page_args)
144 . '][';
145 else
146 $paginator_string .= _("<<") . '][' . _("<") . '][';
147 if ($page_number < $total_pages)
148 $paginator_string .= make_abook_paginator_link($page_number + 1,
149 _(">"),
150 $current_page_args)
151 . ']['
152 . make_abook_paginator_link($total_pages,
153 _(">>"),
154 $current_page_args)
155 . ']';
156 else
157 $paginator_string .= _(">") . '][' . _(">>") . ']';
158 }
159
160
161 // build next/previous links for regular paginator
162 //
163 else
164 {
165 if ($page_number > 1)
166 $paginator_string .= make_abook_paginator_link($page_number - 1,
167 _("Previous"),
168 $current_page_args);
169 else
170 $paginator_string .= _("Previous");
171 $paginator_string .= $nbsp . $sep . $nbsp;
172 if ($page_number < $total_pages)
173 $paginator_string .= make_abook_paginator_link($page_number + 1,
174 _("Next"),
175 $current_page_args);
176 else
177 $paginator_string .= _("Next");
178 $paginator_string .= ']';
179 }
180
181
182 // paginator is turned off - just show previous/next links
183 //
184 if (!$abook_page_selector)
185 {
186 return $paginator_string . $spacing . $show_all_string;
187 }
188
189
190 $paginator_string .= $spacing;
191
192
193 if ($total_pages <= $abook_page_selector_max)
194 {
195 $start_page = 1;
196 $end_page = $total_pages;
197 }
198 else
199 {
200 $pages_to_show = ($abook_page_selector_max % 2 ? $abook_page_selector_max : $abook_page_selector_max - 1);
201 $end_page = $page_number + floor($pages_to_show / 2);
202 $start_page = $page_number - floor($pages_to_show / 2);
203 if (!($abook_page_selector_max % 2)) $start_page--;
204
205 if ($start_page < 1)
206 {
207 $end_page += 1 - $start_page;
208 $start_page = 1;
209 }
210 else if ($end_page > $total_pages)
211 {
212 $start_page -= $end_page - $total_pages;
213 $end_page = $total_pages;
214 }
215
216
217 // do we need to insert elipses?
218 //
219 if (1 < $start_page)
220 {
221 $start_page++;
222 $show_elipses_before = TRUE;
223 }
224 if ($total_pages > $end_page)
225 {
226 $end_page--;
227 $show_elipses_after = TRUE;
228 }
229 }
230
231
232 // now build the actual (compact) paginator
233 //
234 if ($compact)
235 {
236 $aValues = array();
237 for ($i = 1; $i <= $total_pages; $i++)
238 $aValues[$i] = $i . '/' . $total_pages;
239 $page_uri = sqm_baseuri() . 'src/addressbook.php';
240 $temp_page_number = $current_page_args['page_number'];
241 unset($current_page_args['page_number']);
242 $page_uri = set_uri_vars($page_uri, array_diff($current_page_args, array('page_number' => 0)), FALSE);
243 $current_page_args['page_number'] = $temp_page_number;
244 $paginator_string .= addSelect('page_number', $aValues,
245 $page_number, TRUE,
246 (checkForJavascript()
247 ? array('onchange' => 'SubmitOnSelect(this, \''
248 . $page_uri
249 . '&page_number='
250 . '\')')
251 : array()));
252
253 // need a submit button when select widget cannot submit itself
254 //
255 if (!checkForJavascript())
256 {
257 $paginator_string .= addSubmit(_("Go"), 'paginator_submit');
258 }
259 }
260
261
262 // now build the actual (regular) paginator
263 //
264 else
265 {
266 $paginator_string .= '[' . $nbsp;
267 if ($show_elipses_before)
268 $paginator_string .= make_abook_paginator_link(1, 1, $current_page_args)
269 . $nbsp . '...' . $nbsp;
270 for ($x = $start_page; $x <= $end_page; $x++)
271 {
272 if ($x == $page_number)
273 $paginator_string .= $x . $nbsp;
274 else
275 $paginator_string .= make_abook_paginator_link($x, $x, $current_page_args) . $nbsp;
276 }
277 if ($show_elipses_after)
278 $paginator_string .= '...' . $nbsp
279 . make_abook_paginator_link($total_pages, $total_pages, $current_page_args)
280 . $nbsp;
281 $paginator_string .= ']';
282 }
283 $paginator_string .= $spacing . $show_all_string;
284
285
286 return $paginator_string;
287
288}
289
290
291/**
292 * Build a page (pagination) link for use with the address book list page
293 *
294 * @param int $page_number The page number for the link
295 * @param string $text The link text
296 * @param array $current_page_args All known query string arguments
297 * for the current page request; structured
298 * as an associative array of key/value pairs
299 *
300 */
301function make_abook_paginator_link($page_number, $text, $current_page_args) {
302
303 $uri = sqm_baseuri() . 'src/addressbook.php';
304
305 $current_page_args['page_number'] = $page_number;
306 $uri = set_uri_vars($uri, $current_page_args, FALSE);
307
308 return create_hyperlink($uri, $text);
309
caa596b2 310}
311
312