Sliding.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * Contains the Pager_Sliding class
  5. *
  6. * PHP versions 4 and 5
  7. *
  8. * LICENSE: Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. The name of the author may not be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
  19. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  20. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
  21. * IN NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
  27. * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @category HTML
  30. * @package Pager
  31. * @author Lorenzo Alberton <l.alberton@quipo.it>
  32. * @copyright 2003-2008 Lorenzo Alberton
  33. * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
  34. * @version CVS: $Id: Sliding.php,v 1.18 2008/01/06 13:36:22 quipo Exp $
  35. * @link http://pear.php.net/package/Pager
  36. */
  37. /**
  38. * Pager_Sliding - Generic data paging class ("sliding window" style)
  39. * Usage examples can be found in the PEAR manual
  40. *
  41. * @category HTML
  42. * @package Pager
  43. * @author Lorenzo Alberton <l.alberton@quipo.it>
  44. * @copyright 2003-2008 Lorenzo Alberton
  45. * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
  46. * @link http://pear.php.net/package/Pager
  47. */
  48. class Pager_Sliding extends Pager_Common
  49. {
  50. /**
  51. * Constructor
  52. *
  53. * @param array $options Associative array of option names and their values
  54. *
  55. * @access public
  56. */
  57. public function __construct($options = array())
  58. {
  59. //set default Pager_Sliding options
  60. $this->_delta = 2;
  61. $this->_prevImg = '&laquo;';
  62. $this->_nextImg = '&raquo;';
  63. $this->_separator = '|';
  64. $this->_spacesBeforeSeparator = 3;
  65. $this->_spacesAfterSeparator = 3;
  66. $this->_curPageSpanPre = '<b>';
  67. $this->_curPageSpanPost = '</b>';
  68. //set custom options
  69. $err = $this->setOptions($options);
  70. if ($err !== PAGER_OK) {
  71. return $this->raiseError($this->errorMessage($err), $err);
  72. }
  73. $this->build();
  74. }
  75. // }}}
  76. // {{{ getPageIdByOffset()
  77. /**
  78. * "Overload" PEAR::Pager method. VOID. Not needed here...
  79. *
  80. * @param integer $index Offset to get pageID for
  81. *
  82. * @return void
  83. * @deprecated
  84. * @access public
  85. */
  86. function getPageIdByOffset($index)
  87. {
  88. }
  89. // }}}
  90. // {{{ getPageRangeByPageId()
  91. /**
  92. * Given a PageId, it returns the limits of the range of pages displayed.
  93. * While getOffsetByPageId() returns the offset of the data within the
  94. * current page, this method returns the offsets of the page numbers interval.
  95. * E.g., if you have pageId=5 and delta=2, it will return (3, 7).
  96. * PageID of 9 would give you (4, 8).
  97. * If the method is called without parameter, pageID is set to currentPage#.
  98. *
  99. * @param integer $pageid PageID to get offsets for
  100. *
  101. * @return array First and last offsets
  102. * @access public
  103. */
  104. function getPageRangeByPageId($pageid = null)
  105. {
  106. $pageid = isset($pageid) ? (int)$pageid : $this->_currentPage;
  107. if (!isset($this->_pageData)) {
  108. $this->_generatePageData();
  109. }
  110. if (isset($this->_pageData[$pageid]) || is_null($this->_itemData)) {
  111. if ($this->_expanded) {
  112. $min_surplus = ($pageid <= $this->_delta) ? ($this->_delta - $pageid + 1) : 0;
  113. $max_surplus = ($pageid >= ($this->_totalPages - $this->_delta)) ?
  114. ($pageid - ($this->_totalPages - $this->_delta)) : 0;
  115. } else {
  116. $min_surplus = $max_surplus = 0;
  117. }
  118. return array(
  119. max($pageid - $this->_delta - $max_surplus, 1),
  120. min($pageid + $this->_delta + $min_surplus, $this->_totalPages)
  121. );
  122. }
  123. return array(0, 0);
  124. }
  125. // }}}
  126. // {{{ getLinks()
  127. /**
  128. * Returns back/next/first/last and page links,
  129. * both as ordered and associative array.
  130. *
  131. * @param integer $pageID Optional pageID. If specified, links for that page
  132. * are provided instead of current one.
  133. * @param string $dummy used to comply with parent signature (leave empty)
  134. *
  135. * @return array back/pages/next/first/last/all links
  136. * @access public
  137. */
  138. function getLinks($pageID = null, $dummy='')
  139. {
  140. if (!is_null($pageID)) {
  141. $_sav = $this->_currentPage;
  142. $this->_currentPage = $pageID;
  143. $this->links = '';
  144. if ($this->_totalPages > (2 * $this->_delta + 1)) {
  145. $this->links .= $this->_printFirstPage();
  146. }
  147. $this->links .= $this->_getBackLink();
  148. $this->links .= $this->_getPageLinks();
  149. $this->links .= $this->_getNextLink();
  150. if ($this->_totalPages > (2 * $this->_delta + 1)) {
  151. $this->links .= $this->_printLastPage();
  152. }
  153. }
  154. $back = str_replace('&nbsp;', '', $this->_getBackLink());
  155. $next = str_replace('&nbsp;', '', $this->_getNextLink());
  156. $pages = $this->_getPageLinks();
  157. $first = $this->_printFirstPage();
  158. $last = $this->_printLastPage();
  159. $all = $this->links;
  160. $linkTags = $this->linkTags;
  161. $linkTagsRaw = $this->linkTagsRaw;
  162. if (!is_null($pageID)) {
  163. $this->_currentPage = $_sav;
  164. }
  165. return array(
  166. $back,
  167. $pages,
  168. trim($next),
  169. $first,
  170. $last,
  171. $all,
  172. $linkTags,
  173. 'back' => $back,
  174. 'pages' => $pages,
  175. 'next' => $next,
  176. 'first' => $first,
  177. 'last' => $last,
  178. 'all' => $all,
  179. 'linktags' => $linkTags,
  180. 'linkTagsRaw' => $linkTagsRaw,
  181. );
  182. }
  183. // }}}
  184. // {{{ _getPageLinks()
  185. /**
  186. * Returns pages link
  187. *
  188. * @param string $url URL string [deprecated]
  189. *
  190. * @return string Links
  191. * @access private
  192. */
  193. function _getPageLinks($url = '')
  194. {
  195. //legacy setting... the preferred way to set an option now
  196. //is adding it to the constuctor
  197. if (!empty($url)) {
  198. $this->_path = $url;
  199. }
  200. //If there's only one page, don't display links
  201. if ($this->_clearIfVoid && ($this->_totalPages < 2)) {
  202. return '';
  203. }
  204. $links = '';
  205. if ($this->_totalPages > (2 * $this->_delta + 1)) {
  206. if ($this->_expanded) {
  207. if (($this->_totalPages - $this->_delta) <= $this->_currentPage) {
  208. $expansion_before = $this->_currentPage - ($this->_totalPages - $this->_delta);
  209. } else {
  210. $expansion_before = 0;
  211. }
  212. for ($i = $this->_currentPage - $this->_delta - $expansion_before; $expansion_before; $expansion_before--, $i++) {
  213. $print_separator_flag = ($i != $this->_currentPage + $this->_delta); // && ($i != $this->_totalPages - 1)
  214. $this->range[$i] = false;
  215. $this->_linkData[$this->_urlVar] = $i;
  216. $links .= $this->_renderLink(str_replace('%d', $i, $this->_altPage), $i)
  217. . $this->_spacesBefore
  218. . ($print_separator_flag ? $this->_separator.$this->_spacesAfter : '');
  219. }
  220. }
  221. $expansion_after = 0;
  222. for ($i = $this->_currentPage - $this->_delta; ($i <= $this->_currentPage + $this->_delta) && ($i <= $this->_totalPages); $i++) {
  223. if ($i < 1) {
  224. ++$expansion_after;
  225. continue;
  226. }
  227. // check when to print separator
  228. $print_separator_flag = (($i != $this->_currentPage + $this->_delta) && ($i != $this->_totalPages));
  229. if ($i == $this->_currentPage) {
  230. $this->range[$i] = true;
  231. $links .= $this->_curPageSpanPre . $i . $this->_curPageSpanPost;
  232. } else {
  233. $this->range[$i] = false;
  234. $this->_linkData[$this->_urlVar] = $i;
  235. $links .= $this->_renderLink(str_replace('%d', $i, $this->_altPage), $i);
  236. }
  237. $links .= $this->_spacesBefore
  238. . ($print_separator_flag ? $this->_separator.$this->_spacesAfter : '');
  239. }
  240. if ($this->_expanded && $expansion_after) {
  241. $links .= $this->_separator . $this->_spacesAfter;
  242. for ($i = $this->_currentPage + $this->_delta +1; $expansion_after; $expansion_after--, $i++) {
  243. $print_separator_flag = ($expansion_after != 1);
  244. $this->range[$i] = false;
  245. $this->_linkData[$this->_urlVar] = $i;
  246. $links .= $this->_renderLink(str_replace('%d', $i, $this->_altPage), $i)
  247. . $this->_spacesBefore
  248. . ($print_separator_flag ? $this->_separator.$this->_spacesAfter : '');
  249. }
  250. }
  251. } else {
  252. //if $this->_totalPages <= (2*Delta+1) show them all
  253. for ($i=1; $i<=$this->_totalPages; $i++) {
  254. if ($i != $this->_currentPage) {
  255. $this->range[$i] = false;
  256. $this->_linkData[$this->_urlVar] = $i;
  257. $links .= $this->_renderLink(str_replace('%d', $i, $this->_altPage), $i);
  258. } else {
  259. $this->range[$i] = true;
  260. $links .= $this->_curPageSpanPre . $i . $this->_curPageSpanPost;
  261. }
  262. $links .= $this->_spacesBefore
  263. . (($i != $this->_totalPages) ? $this->_separator.$this->_spacesAfter : '');
  264. }
  265. }
  266. return $links;
  267. }
  268. // }}}
  269. }
  270. ?>