| 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 2005-2022 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 | /** |
| 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 | */ |
| 27 | function addAbookSort ($field, $current_page_args) { |
| 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 |
| 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 | */ |
| 88 | function 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, _("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 | // i18n: "<<" is for the first page in the paginator. "<" is for the previous page. |
| 147 | $paginator_string .= _("<<") . '][' . _("<") . ']['; |
| 148 | if ($page_number < $total_pages) |
| 149 | $paginator_string .= make_abook_paginator_link($page_number + 1, |
| 150 | _(">"), |
| 151 | $current_page_args) |
| 152 | . '][' |
| 153 | . make_abook_paginator_link($total_pages, |
| 154 | _(">>"), |
| 155 | $current_page_args) |
| 156 | . ']'; |
| 157 | else |
| 158 | // i18n: ">>" is for the last page in the paginator. ">" is for the next page. |
| 159 | $paginator_string .= _(">") . '][' . _(">>") . ']'; |
| 160 | } |
| 161 | |
| 162 | |
| 163 | // build next/previous links for regular paginator |
| 164 | // |
| 165 | else |
| 166 | { |
| 167 | if ($page_number > 1) |
| 168 | $paginator_string .= make_abook_paginator_link($page_number - 1, |
| 169 | _("Previous"), |
| 170 | $current_page_args); |
| 171 | else |
| 172 | $paginator_string .= _("Previous"); |
| 173 | $paginator_string .= $nbsp . $sep . $nbsp; |
| 174 | if ($page_number < $total_pages) |
| 175 | $paginator_string .= make_abook_paginator_link($page_number + 1, |
| 176 | _("Next"), |
| 177 | $current_page_args); |
| 178 | else |
| 179 | $paginator_string .= _("Next"); |
| 180 | $paginator_string .= ']'; |
| 181 | } |
| 182 | |
| 183 | |
| 184 | // paginator is turned off - just show previous/next links |
| 185 | // |
| 186 | if (!$abook_page_selector) |
| 187 | { |
| 188 | return $paginator_string . $spacing . $show_all_string; |
| 189 | } |
| 190 | |
| 191 | |
| 192 | $paginator_string .= $spacing; |
| 193 | |
| 194 | |
| 195 | if ($total_pages <= $abook_page_selector_max) |
| 196 | { |
| 197 | $start_page = 1; |
| 198 | $end_page = $total_pages; |
| 199 | } |
| 200 | else |
| 201 | { |
| 202 | $pages_to_show = ($abook_page_selector_max % 2 ? $abook_page_selector_max : $abook_page_selector_max - 1); |
| 203 | $end_page = $page_number + floor($pages_to_show / 2); |
| 204 | $start_page = $page_number - floor($pages_to_show / 2); |
| 205 | if (!($abook_page_selector_max % 2)) $start_page--; |
| 206 | |
| 207 | if ($start_page < 1) |
| 208 | { |
| 209 | $end_page += 1 - $start_page; |
| 210 | $start_page = 1; |
| 211 | } |
| 212 | else if ($end_page > $total_pages) |
| 213 | { |
| 214 | $start_page -= $end_page - $total_pages; |
| 215 | $end_page = $total_pages; |
| 216 | } |
| 217 | |
| 218 | |
| 219 | // do we need to insert elipses? |
| 220 | // |
| 221 | if (1 < $start_page) |
| 222 | { |
| 223 | $start_page++; |
| 224 | $show_elipses_before = TRUE; |
| 225 | } |
| 226 | if ($total_pages > $end_page) |
| 227 | { |
| 228 | $end_page--; |
| 229 | $show_elipses_after = TRUE; |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | |
| 234 | // now build the actual (compact) paginator |
| 235 | // |
| 236 | if ($compact) |
| 237 | { |
| 238 | $aValues = array(); |
| 239 | for ($i = 1; $i <= $total_pages; $i++) |
| 240 | $aValues[$i] = $i . '/' . $total_pages; |
| 241 | $page_uri = sqm_baseuri() . 'src/addressbook.php'; |
| 242 | $temp_page_number = $current_page_args['page_number']; |
| 243 | unset($current_page_args['page_number']); |
| 244 | $page_uri = set_uri_vars($page_uri, array_diff($current_page_args, array('page_number' => 0)), FALSE); |
| 245 | $current_page_args['page_number'] = $temp_page_number; |
| 246 | $paginator_string .= addSelect('page_number', $aValues, |
| 247 | $page_number, TRUE, |
| 248 | (checkForJavascript() |
| 249 | ? array('onchange' => 'SubmitOnSelect(this, \'' |
| 250 | . $page_uri |
| 251 | . '&page_number=' |
| 252 | . '\')') |
| 253 | : array())); |
| 254 | |
| 255 | // need a submit button when select widget cannot submit itself |
| 256 | // |
| 257 | if (!checkForJavascript()) |
| 258 | { |
| 259 | $paginator_string .= addSubmit(_("Go"), 'paginator_submit'); |
| 260 | } |
| 261 | } |
| 262 | |
| 263 | |
| 264 | // now build the actual (regular) paginator |
| 265 | // |
| 266 | else |
| 267 | { |
| 268 | $paginator_string .= '[' . $nbsp; |
| 269 | if ($show_elipses_before) |
| 270 | $paginator_string .= make_abook_paginator_link(1, 1, $current_page_args) |
| 271 | . $nbsp . '...' . $nbsp; |
| 272 | for ($x = $start_page; $x <= $end_page; $x++) |
| 273 | { |
| 274 | if ($x == $page_number) |
| 275 | $paginator_string .= $x . $nbsp; |
| 276 | else |
| 277 | $paginator_string .= make_abook_paginator_link($x, $x, $current_page_args) . $nbsp; |
| 278 | } |
| 279 | if ($show_elipses_after) |
| 280 | $paginator_string .= '...' . $nbsp |
| 281 | . make_abook_paginator_link($total_pages, $total_pages, $current_page_args) |
| 282 | . $nbsp; |
| 283 | $paginator_string .= ']'; |
| 284 | } |
| 285 | $paginator_string .= $spacing . $show_all_string; |
| 286 | |
| 287 | |
| 288 | return $paginator_string; |
| 289 | |
| 290 | } |
| 291 | |
| 292 | |
| 293 | /** |
| 294 | * Build a page (pagination) link for use with the address book list page |
| 295 | * |
| 296 | * @param int $page_number The page number for the link |
| 297 | * @param string $text The link text |
| 298 | * @param array $current_page_args All known query string arguments |
| 299 | * for the current page request; structured |
| 300 | * as an associative array of key/value pairs |
| 301 | * |
| 302 | */ |
| 303 | function make_abook_paginator_link($page_number, $text, $current_page_args) { |
| 304 | |
| 305 | $uri = sqm_baseuri() . 'src/addressbook.php'; |
| 306 | |
| 307 | $current_page_args['page_number'] = $page_number; |
| 308 | $uri = set_uri_vars($uri, $current_page_args, FALSE); |
| 309 | |
| 310 | return create_hyperlink($uri, $text); |
| 311 | |
| 312 | } |
| 313 | |
| 314 | |