Sliding.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  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. // {{{ Pager_Sliding()
  51. /**
  52. * Constructor
  53. *
  54. * @param array $options Associative array of option names and their values
  55. *
  56. * @access public
  57. */
  58. function Pager_Sliding($options = array())
  59. {
  60. //set default Pager_Sliding options
  61. $this->_delta = 2;
  62. $this->_prevImg = '&laquo;';
  63. $this->_nextImg = '&raquo;';
  64. $this->_separator = '|';
  65. $this->_spacesBeforeSeparator = 3;
  66. $this->_spacesAfterSeparator = 3;
  67. $this->_curPageSpanPre = '<b>';
  68. $this->_curPageSpanPost = '</b>';
  69. //set custom options
  70. $err = $this->setOptions($options);
  71. if ($err !== PAGER_OK) {
  72. return $this->raiseError($this->errorMessage($err), $err);
  73. }
  74. $this->build();
  75. }
  76. // }}}
  77. // {{{ getPageIdByOffset()
  78. /**
  79. * "Overload" PEAR::Pager method. VOID. Not needed here...
  80. *
  81. * @param integer $index Offset to get pageID for
  82. *
  83. * @return void
  84. * @deprecated
  85. * @access public
  86. */
  87. function getPageIdByOffset($index)
  88. {
  89. }
  90. // }}}
  91. // {{{ getPageRangeByPageId()
  92. /**
  93. * Given a PageId, it returns the limits of the range of pages displayed.
  94. * While getOffsetByPageId() returns the offset of the data within the
  95. * current page, this method returns the offsets of the page numbers interval.
  96. * E.g., if you have pageId=5 and delta=2, it will return (3, 7).
  97. * PageID of 9 would give you (4, 8).
  98. * If the method is called without parameter, pageID is set to currentPage#.
  99. *
  100. * @param integer $pageid PageID to get offsets for
  101. *
  102. * @return array First and last offsets
  103. * @access public
  104. */
  105. function getPageRangeByPageId($pageid = null)
  106. {
  107. $pageid = isset($pageid) ? (int)$pageid : $this->_currentPage;
  108. if (!isset($this->_pageData)) {
  109. $this->_generatePageData();
  110. }
  111. if (isset($this->_pageData[$pageid]) || is_null($this->_itemData)) {
  112. if ($this->_expanded) {
  113. $min_surplus = ($pageid <= $this->_delta) ? ($this->_delta - $pageid + 1) : 0;
  114. $max_surplus = ($pageid >= ($this->_totalPages - $this->_delta)) ?
  115. ($pageid - ($this->_totalPages - $this->_delta)) : 0;
  116. } else {
  117. $min_surplus = $max_surplus = 0;
  118. }
  119. return array(
  120. max($pageid - $this->_delta - $max_surplus, 1),
  121. min($pageid + $this->_delta + $min_surplus, $this->_totalPages)
  122. );
  123. }
  124. return array(0, 0);
  125. }
  126. // }}}
  127. // {{{ getLinks()
  128. /**
  129. * Returns back/next/first/last and page links,
  130. * both as ordered and associative array.
  131. *
  132. * @param integer $pageID Optional pageID. If specified, links for that page
  133. * are provided instead of current one.
  134. * @param string $dummy used to comply with parent signature (leave empty)
  135. *
  136. * @return array back/pages/next/first/last/all links
  137. * @access public
  138. */
  139. function getLinks($pageID = null, $dummy='')
  140. {
  141. if (!is_null($pageID)) {
  142. $_sav = $this->_currentPage;
  143. $this->_currentPage = $pageID;
  144. $this->links = '';
  145. if ($this->_totalPages > (2 * $this->_delta + 1)) {
  146. $this->links .= $this->_printFirstPage();
  147. }
  148. $this->links .= $this->_getBackLink();
  149. $this->links .= $this->_getPageLinks();
  150. $this->links .= $this->_getNextLink();
  151. if ($this->_totalPages > (2 * $this->_delta + 1)) {
  152. $this->links .= $this->_printLastPage();
  153. }
  154. }
  155. $back = str_replace('&nbsp;', '', $this->_getBackLink());
  156. $next = str_replace('&nbsp;', '', $this->_getNextLink());
  157. $pages = $this->_getPageLinks();
  158. $first = $this->_printFirstPage();
  159. $last = $this->_printLastPage();
  160. $all = $this->links;
  161. $linkTags = $this->linkTags;
  162. $linkTagsRaw = $this->linkTagsRaw;
  163. if (!is_null($pageID)) {
  164. $this->_currentPage = $_sav;
  165. }
  166. return array(
  167. $back,
  168. $pages,
  169. trim($next),
  170. $first,
  171. $last,
  172. $all,
  173. $linkTags,
  174. 'back' => $back,
  175. 'pages' => $pages,
  176. 'next' => $next,
  177. 'first' => $first,
  178. 'last' => $last,
  179. 'all' => $all,
  180. 'linktags' => $linkTags,
  181. 'linkTagsRaw' => $linkTagsRaw,
  182. );
  183. }
  184. // }}}
  185. // {{{ _getPageLinks()
  186. /**
  187. * Returns pages link
  188. *
  189. * @param string $url URL string [deprecated]
  190. *
  191. * @return string Links
  192. * @access private
  193. */
  194. function _getPageLinks($url = '')
  195. {
  196. //legacy setting... the preferred way to set an option now
  197. //is adding it to the constuctor
  198. if (!empty($url)) {
  199. $this->_path = $url;
  200. }
  201. //If there's only one page, don't display links
  202. if ($this->_clearIfVoid && ($this->_totalPages < 2)) {
  203. return '';
  204. }
  205. $links = '';
  206. if ($this->_totalPages > (2 * $this->_delta + 1)) {
  207. if ($this->_expanded) {
  208. if (($this->_totalPages - $this->_delta) <= $this->_currentPage) {
  209. $expansion_before = $this->_currentPage - ($this->_totalPages - $this->_delta);
  210. } else {
  211. $expansion_before = 0;
  212. }
  213. for ($i = $this->_currentPage - $this->_delta - $expansion_before; $expansion_before; $expansion_before--, $i++) {
  214. $print_separator_flag = ($i != $this->_currentPage + $this->_delta); // && ($i != $this->_totalPages - 1)
  215. $this->range[$i] = false;
  216. $this->_linkData[$this->_urlVar] = $i;
  217. $links .= $this->_renderLink(str_replace('%d', $i, $this->_altPage), $i)
  218. . $this->_spacesBefore
  219. . ($print_separator_flag ? $this->_separator.$this->_spacesAfter : '');
  220. }
  221. }
  222. $expansion_after = 0;
  223. for ($i = $this->_currentPage - $this->_delta; ($i <= $this->_currentPage + $this->_delta) && ($i <= $this->_totalPages); $i++) {
  224. if ($i < 1) {
  225. ++$expansion_after;
  226. continue;
  227. }
  228. // check when to print separator
  229. $print_separator_flag = (($i != $this->_currentPage + $this->_delta) && ($i != $this->_totalPages));
  230. if ($i == $this->_currentPage) {
  231. $this->range[$i] = true;
  232. $links .= $this->_curPageSpanPre . $i . $this->_curPageSpanPost;
  233. } else {
  234. $this->range[$i] = false;
  235. $this->_linkData[$this->_urlVar] = $i;
  236. $links .= $this->_renderLink(str_replace('%d', $i, $this->_altPage), $i);
  237. }
  238. $links .= $this->_spacesBefore
  239. . ($print_separator_flag ? $this->_separator.$this->_spacesAfter : '');
  240. }
  241. if ($this->_expanded && $expansion_after) {
  242. $links .= $this->_separator . $this->_spacesAfter;
  243. for ($i = $this->_currentPage + $this->_delta +1; $expansion_after; $expansion_after--, $i++) {
  244. $print_separator_flag = ($expansion_after != 1);
  245. $this->range[$i] = false;
  246. $this->_linkData[$this->_urlVar] = $i;
  247. $links .= $this->_renderLink(str_replace('%d', $i, $this->_altPage), $i)
  248. . $this->_spacesBefore
  249. . ($print_separator_flag ? $this->_separator.$this->_spacesAfter : '');
  250. }
  251. }
  252. } else {
  253. //if $this->_totalPages <= (2*Delta+1) show them all
  254. for ($i=1; $i<=$this->_totalPages; $i++) {
  255. if ($i != $this->_currentPage) {
  256. $this->range[$i] = false;
  257. $this->_linkData[$this->_urlVar] = $i;
  258. $links .= $this->_renderLink(str_replace('%d', $i, $this->_altPage), $i);
  259. } else {
  260. $this->range[$i] = true;
  261. $links .= $this->_curPageSpanPre . $i . $this->_curPageSpanPost;
  262. }
  263. $links .= $this->_spacesBefore
  264. . (($i != $this->_totalPages) ? $this->_separator.$this->_spacesAfter : '');
  265. }
  266. }
  267. return $links;
  268. }
  269. // }}}
  270. }
  271. ?>