Jumping.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * Contains the Pager_Jumping 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. * @author Richard Heyes <richard@phpguru.org>
  33. * @copyright 2003-2008 Lorenzo Alberton, Richard Heyes
  34. * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
  35. * @version CVS: $Id: Jumping.php,v 1.20 2008/03/05 13:57:45 quipo Exp $
  36. * @link http://pear.php.net/package/Pager
  37. */
  38. /**
  39. * require PEAR::Pager_Common base class
  40. */
  41. require_once 'Pager/Common.php';
  42. /**
  43. * Pager_Jumping - Generic data paging class ("jumping window" style)
  44. * Handles paging a set of data. For usage see the example.php provided.
  45. *
  46. * @category HTML
  47. * @package Pager
  48. * @author Lorenzo Alberton <l.alberton@quipo.it>
  49. * @author Richard Heyes <richard@phpguru.org>
  50. * @copyright 2003-2008 Lorenzo Alberton, Richard Heyes
  51. * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
  52. * @link http://pear.php.net/package/Pager
  53. */
  54. class Pager_Jumping extends Pager_Common
  55. {
  56. // {{{ Pager_Jumping()
  57. /**
  58. * Constructor
  59. *
  60. * @param array $options Associative array of option names and their values
  61. *
  62. * @access public
  63. */
  64. function Pager_Jumping($options = array())
  65. {
  66. $err = $this->setOptions($options);
  67. if ($err !== PAGER_OK) {
  68. return $this->raiseError($this->errorMessage($err), $err);
  69. }
  70. $this->build();
  71. }
  72. // }}}
  73. // {{{ getPageIdByOffset()
  74. /**
  75. * Returns pageID for given offset
  76. *
  77. * @param integer $index Offset to get pageID for
  78. *
  79. * @return int PageID for given offset
  80. */
  81. function getPageIdByOffset($index)
  82. {
  83. if (!isset($this->_pageData)) {
  84. $this->_generatePageData();
  85. }
  86. if (($index % $this->_perPage) > 0) {
  87. $pageID = ceil((float)$index / (float)$this->_perPage);
  88. } else {
  89. $pageID = $index / $this->_perPage;
  90. }
  91. return $pageID;
  92. }
  93. // }}}
  94. // {{{ getPageRangeByPageId()
  95. /**
  96. * Given a PageId, it returns the limits of the range of pages displayed.
  97. * While getOffsetByPageId() returns the offset of the data within the
  98. * current page, this method returns the offsets of the page numbers interval.
  99. * E.g., if you have pageId=3 and delta=10, it will return (1, 10).
  100. * PageID of 8 would give you (1, 10) as well, because 1 <= 8 <= 10.
  101. * PageID of 11 would give you (11, 20).
  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[$pageid]) || is_null($this->_itemData)) {
  113. // I'm sure I'm missing something here, but this formula works
  114. // so I'm using it until I find something simpler.
  115. $start = ((($pageid + (($this->_delta - ($pageid % $this->_delta))) % $this->_delta) / $this->_delta) - 1) * $this->_delta +1;
  116. return array(
  117. max($start, 1),
  118. min($start+$this->_delta-1, $this->_totalPages)
  119. );
  120. } else {
  121. return array(0, 0);
  122. }
  123. }
  124. // }}}
  125. // {{{ getLinks()
  126. /**
  127. * Returns back/next/first/last and page links,
  128. * both as ordered and associative array.
  129. *
  130. * NB: in original PEAR::Pager this method accepted two parameters,
  131. * $back_html and $next_html. Now the only parameter accepted is
  132. * an integer ($pageID), since the html text for prev/next links can
  133. * be set in the constructor. If a second parameter is provided, then
  134. * the method act as it previously did. This hack's only purpose is to
  135. * mantain backward compatibility.
  136. *
  137. * @param integer $pageID Optional pageID. If specified, links for that
  138. * page are provided instead of current one.
  139. * [ADDED IN NEW PAGER VERSION]
  140. * @param string $next_html HTML to put inside the next link
  141. * [deprecated: use the factory instead]
  142. *
  143. * @return array Back/pages/next links
  144. */
  145. function getLinks($pageID=null, $next_html='')
  146. {
  147. //BC hack
  148. if (!empty($next_html)) {
  149. $back_html = $pageID;
  150. $pageID = null;
  151. } else {
  152. $back_html = '';
  153. }
  154. if (!is_null($pageID)) {
  155. $this->links = '';
  156. if ($this->_totalPages > $this->_delta) {
  157. $this->links .= $this->_printFirstPage();
  158. }
  159. $_sav = $this->_currentPage;
  160. $this->_currentPage = $pageID;
  161. $this->links .= $this->_getBackLink('', $back_html);
  162. $this->links .= $this->_getPageLinks();
  163. $this->links .= $this->_getNextLink('', $next_html);
  164. if ($this->_totalPages > $this->_delta) {
  165. $this->links .= $this->_printLastPage();
  166. }
  167. }
  168. $back = str_replace('&nbsp;', '', $this->_getBackLink());
  169. $next = str_replace('&nbsp;', '', $this->_getNextLink());
  170. $pages = $this->_getPageLinks();
  171. $first = $this->_printFirstPage();
  172. $last = $this->_printLastPage();
  173. $all = $this->links;
  174. $linkTags = $this->linkTags;
  175. $linkTagsRaw = $this->linkTagsRaw;
  176. if (!is_null($pageID)) {
  177. $this->_currentPage = $_sav;
  178. }
  179. return array(
  180. $back,
  181. $pages,
  182. trim($next),
  183. $first,
  184. $last,
  185. $all,
  186. $linkTags,
  187. 'back' => $back,
  188. 'pages' => $pages,
  189. 'next' => $next,
  190. 'first' => $first,
  191. 'last' => $last,
  192. 'all' => $all,
  193. 'linktags' => $linkTags,
  194. 'linkTagsRaw' => $linkTagsRaw,
  195. );
  196. }
  197. // }}}
  198. // {{{ _getPageLinks()
  199. /**
  200. * Returns pages link
  201. *
  202. * @param string $url URL to use in the link
  203. * [deprecated: use the constructor instead]
  204. *
  205. * @return string Links
  206. * @access private
  207. */
  208. function _getPageLinks($url = '')
  209. {
  210. //legacy setting... the preferred way to set an option now
  211. //is adding it to the constuctor
  212. if (!empty($url)) {
  213. $this->_path = $url;
  214. }
  215. //If there's only one page, don't display links
  216. if ($this->_clearIfVoid && ($this->_totalPages < 2)) {
  217. return '';
  218. }
  219. $links = '';
  220. $limits = $this->getPageRangeByPageId($this->_currentPage);
  221. for ($i=$limits[0]; $i<=min($limits[1], $this->_totalPages); $i++) {
  222. if ($i != $this->_currentPage) {
  223. $this->range[$i] = false;
  224. $this->_linkData[$this->_urlVar] = $i;
  225. $links .= $this->_renderLink(str_replace('%d', $i, $this->_altPage), $i);
  226. } else {
  227. $this->range[$i] = true;
  228. $links .= $this->_curPageSpanPre . $i . $this->_curPageSpanPost;
  229. }
  230. $links .= $this->_spacesBefore
  231. . (($i != $this->_totalPages) ? $this->_separator.$this->_spacesAfter : '');
  232. }
  233. return $links;
  234. }
  235. // }}}
  236. }
  237. ?>