Sliding.php 11 KB

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