PageRenderTime 39ms CodeModel.GetById 15ms RepoModel.GetById 1ms app.codeStats 0ms

/s3db3.5.10/pearlib/Pager/Sliding.php

https://github.com/drobbins/s3db
PHP | 315 lines | 197 code | 30 blank | 88 comment | 49 complexity | eaa2b9aecb5e1638dcf5ff73676c6a6a MD5 | raw file
  1. <?php
  2. // +----------------------------------------------------------------------+
  3. // | PEAR :: Pager_Sliding |
  4. // +----------------------------------------------------------------------+
  5. // | Copyright (c) 1997-2003 The PHP Group |
  6. // +----------------------------------------------------------------------+
  7. // | This source file is subject to version 2.0 of the PHP license, |
  8. // | that is bundled with this package in the file LICENSE, and is |
  9. // | available at through the world-wide-web at |
  10. // | http://www.php.net/license/2_02.txt. |
  11. // | If you did not receive a copy of the PHP license and are unable to |
  12. // | obtain it through the world-wide-web, please send a note to |
  13. // | license@php.net so we can mail you a copy immediately. |
  14. // +----------------------------------------------------------------------+
  15. // | Author: Lorenzo Alberton <l.alberton at quipo.it> |
  16. // +----------------------------------------------------------------------+
  17. //
  18. // $Id: Sliding.php,v 1.5 2004/01/16 10:29:57 quipo Exp $
  19. require_once 'Pager/Common.php';
  20. /**
  21. * File Sliding.php
  22. *
  23. * @package Pager
  24. */
  25. /**
  26. * Pager_Sliding - Generic data paging class ("sliding window" style)
  27. *
  28. * Usage examples can be found in the doc provided
  29. *
  30. * @author Lorenzo Alberton <l.alberton at quipo.it>
  31. * @version $Id: Sliding.php,v 1.5 2004/01/16 10:29:57 quipo Exp $
  32. * @package Pager
  33. */
  34. class Pager_Sliding extends Pager_Common
  35. {
  36. // {{{ Pager_Sliding()
  37. /**
  38. * Constructor
  39. *
  40. * @param mixed $options An associative array of option names
  41. * and their values
  42. * @access public
  43. */
  44. function Pager_Sliding($options = array())
  45. {
  46. //set default Pager_Sliding options
  47. $this->_delta = 2;
  48. $this->_prevImg = '&laquo;';
  49. $this->_nextImg = '&raquo;';
  50. $this->_separator = '|';
  51. $this->_spacesBeforeSeparator = 3;
  52. $this->_spacesAfterSeparator = 3;
  53. $this->_curPageSpanPre = '<b><u>';
  54. $this->_curPageSpanPost = '</u></b>';
  55. //set custom options
  56. $err = $this->_setOptions($options);
  57. if ($err !== PAGER_OK) {
  58. return $this->raiseError($this->errorMessage($err), $err);
  59. }
  60. $this->_generatePageData();
  61. $this->_setFirstLastText();
  62. if ($this->_totalPages > (2 * $this->_delta + 1)) {
  63. $this->links .= $this->_printFirstPage();
  64. }
  65. $this->links .= $this->_getBackLink();
  66. $this->links .= $this->_getPageLinks();
  67. $this->links .= $this->_getNextLink();
  68. $this->linkTags .= $this->_getFirstLinkTag();
  69. $this->linkTags .= $this->_getPrevLinkTag();
  70. $this->linkTags .= $this->_getNextLinkTag();
  71. $this->linkTags .= $this->_getLastLinkTag();
  72. if ($this->_totalPages > (2 * $this->_delta + 1)) {
  73. $this->links .= $this->_printLastPage();
  74. }
  75. }
  76. // }}}
  77. // {{{ getPageIdByOffset()
  78. /**
  79. * "Overload" PEAR::Pager method. VOID. Not needed here...
  80. * @param integer $index Offset to get pageID for
  81. * @deprecated
  82. * @access public
  83. */
  84. function getPageIdByOffset($index=null) { }
  85. // }}}
  86. // {{{ getPageRangeByPageId()
  87. /**
  88. * Given a PageId, it returns the limits of the range of pages displayed.
  89. * While getOffsetByPageId() returns the offset of the data within the
  90. * current page, this method returns the offsets of the page numbers interval.
  91. * E.g., if you have pageId=5 and delta=2, it will return (3, 7).
  92. * PageID of 9 would give you (4, 8).
  93. * If the method is called without parameter, pageID is set to currentPage#.
  94. *
  95. * @param integer PageID to get offsets for
  96. * @return array First and last offsets
  97. * @access public
  98. */
  99. function getPageRangeByPageId($pageid = null)
  100. {
  101. $pageid = isset($pageid) ? (int)$pageid : $this->_currentPage;
  102. if (!isset($this->_pageData)) {
  103. $this->_generatePageData();
  104. }
  105. if (isset($this->_pageData[$pageid]) || is_null($this->_itemData)) {
  106. if ($this->_expanded) {
  107. $min_surplus = ($pageid <= $this->_delta) ? ($this->_delta - $pageid + 1) : 0;
  108. $max_surplus = ($pageid >= ($this->_totalPages - $this->_delta)) ?
  109. ($pageid - ($this->_totalPages - $this->_delta)) : 0;
  110. } else {
  111. $min_surplus = 0;
  112. $max_surplus = 0;
  113. }
  114. return array( max($pageid - $this->_delta - $max_surplus, 1),
  115. min($pageid + $this->_delta + $min_surplus, $this->_totalPages));
  116. } else {
  117. return array(0, 0);
  118. }
  119. }
  120. // }}}
  121. // {{{ getLinks()
  122. /**
  123. * Returns back/next/first/last and page links,
  124. * both as ordered and associative array.
  125. *
  126. * @param integer $pageID Optional pageID. If specified, links
  127. * for that page are provided instead of current one.
  128. * @return array back/pages/next/first/last/all links
  129. * @access public
  130. */
  131. function getLinks($pageID = null)
  132. {
  133. if ($pageID != null) {
  134. $_sav = $this->_currentPage;
  135. $this->_currentPage = $pageID;
  136. $this->links = '';
  137. if ($this->_totalPages > (2 * $this->_delta + 1)) {
  138. $this->links .= $this->_printFirstPage();
  139. }
  140. $this->links .= $this->_getBackLink();
  141. $this->links .= $this->_getPageLinks();
  142. $this->links .= $this->_getNextLink();
  143. if ($this->_totalPages > (2 * $this->_delta + 1)) {
  144. $this->links .= $this->_printLastPage();
  145. }
  146. }
  147. $back = str_replace('&nbsp;', '', $this->_getBackLink());
  148. $next = str_replace('&nbsp;', '', $this->_getNextLink());
  149. $pages = $this->_getPageLinks();
  150. $first = $this->_printFirstPage();
  151. $last = $this->_printLastPage();
  152. $all = $this->links;
  153. $linkTags = $this->linkTags;
  154. if ($pageID != null) {
  155. $this->_currentPage = $_sav;
  156. }
  157. return array(
  158. $back,
  159. $pages,
  160. trim($next),
  161. $first,
  162. $last,
  163. $all,
  164. $linkTags,
  165. 'back' => $back,
  166. 'pages' => $pages,
  167. 'next' => $next,
  168. 'first' => $first,
  169. 'last' => $last,
  170. 'all' => $all,
  171. 'linktags' => $linkTags
  172. );
  173. }
  174. // }}}
  175. // {{{ _getPageLinks()
  176. /**
  177. * Returns pages link
  178. *
  179. * @return string Links
  180. * @access private
  181. */
  182. function _getPageLinks()
  183. {
  184. //legacy setting... the preferred way to set an option now
  185. //is adding it to the constuctor
  186. if (!empty($url)) {
  187. $this->_path = $url;
  188. }
  189. $links = '';
  190. if ($this->_totalPages > (2 * $this->_delta + 1)) {
  191. if ($this->_expanded) {
  192. if (($this->_totalPages - $this->_delta) <= $this->_currentPage) {
  193. $_expansion_before = $this->_currentPage - ($this->_totalPages - $this->_delta);
  194. } else {
  195. $_expansion_before = 0;
  196. }
  197. for ($i = $this->_currentPage - $this->_delta - $_expansion_before; $_expansion_before; $_expansion_before--, $i++) {
  198. if (($i != $this->_currentPage + $this->_delta)){ // && ($i != $this->_totalPages - 1)) {
  199. $_print_separator_flag = true;
  200. } else {
  201. $_print_separator_flag = false;
  202. }
  203. $this->range[$i] = false;
  204. $links .= sprintf('<a href="%s" %s title="%s">%d</a>',
  205. ( $this->_append ? $this->_url.$i : $this->_url.sprintf($this->_fileName, $i) ),
  206. $this->_classString,
  207. $this->_altPage.' '.$i,
  208. $i)
  209. . $this->_spacesBefore
  210. . ($_print_separator_flag ? $this->_separator.$this->_spacesAfter : '');
  211. }
  212. }
  213. $_expansion_after = 0;
  214. for ($i = $this->_currentPage - $this->_delta; ($i <= $this->_currentPage + $this->_delta) && ($i <= $this->_totalPages); $i++) {
  215. if ($i<1) {
  216. $_expansion_after++;
  217. continue;
  218. }
  219. // check when to print separator
  220. if (($i != $this->_currentPage + $this->_delta) && ($i != $this->_totalPages )) {
  221. $_print_separator_flag = true;
  222. } else {
  223. $_print_separator_flag = false;
  224. }
  225. if ($i == $this->_currentPage) {
  226. $this->range[$i] = true;
  227. $links .= $this->_curPageSpanPre . $i . $this->_curPageSpanPost
  228. . $this->_spacesBefore
  229. . ($_print_separator_flag ? $this->_separator.$this->_spacesAfter : '');
  230. } else {
  231. $this->range[$i] = false;
  232. $links .= sprintf('<a href="%s" %s title="%s">%d</a>',
  233. ( $this->_append ? $this->_url.$i : $this->_url.sprintf($this->_fileName, $i) ),
  234. $this->_classString,
  235. $this->_altPage.' '.$i,
  236. $i)
  237. . $this->_spacesBefore
  238. . ($_print_separator_flag ? $this->_separator.$this->_spacesAfter : '');
  239. }
  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. if (($_expansion_after != 1)) {
  245. $_print_separator_flag = true;
  246. } else {
  247. $_print_separator_flag = false;
  248. }
  249. $this->range[$i] = false;
  250. $links .= sprintf('<a href="%s" %s title="%s">%d</a>',
  251. ( $this->_append ? $this->_url.$i : $this->_url.sprintf($this->_fileName, $i) ),
  252. $this->_classString,
  253. $this->_altPage.' '.$i,
  254. $i)
  255. . $this->_spacesBefore
  256. . ($_print_separator_flag ? $this->_separator.$this->_spacesAfter : '');
  257. }
  258. }
  259. } else {
  260. //if $this->_totalPages <= (2*Delta+1) show them all
  261. for ($i=1; $i<=$this->_totalPages; $i++) {
  262. if ($i != $this->_currentPage) {
  263. $this->range[$i] = false;
  264. $links .= sprintf('<a href="%s" %s title="%s">%d</a>',
  265. ( $this->_append ? $this->_url.$i : $this->_url.sprintf($this->_fileName, $i) ),
  266. $this->_classString,
  267. $this->_altPage.' '.$i,
  268. $i);
  269. } else {
  270. $this->range[$i] = true;
  271. $links .= $this->_curPageSpanPre . $i . $this->_curPageSpanPost;
  272. }
  273. $links .= $this->_spacesBefore
  274. . (($i != $this->_totalPages) ? $this->_separator.$this->_spacesAfter : '');
  275. }
  276. }
  277. if ($this->_clearIfVoid) {
  278. //If there's only one page, don't display links
  279. if ($this->_totalPages < 2) $links = '';
  280. }
  281. return $links;
  282. }
  283. // }}}
  284. }
  285. ?>