abook_util.php 11 KB

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