2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
5 * Contains the Pager_Jumping class
9 * LICENSE: Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote products
17 * derived from this software without specific prior written permission.
19 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR "AS IS" AND ANY EXPRESS OR IMPLIED
20 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
22 * IN NO EVENT SHALL THE FREEBSD PROJECT OR CONTRIBUTORS BE LIABLE FOR ANY
23 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
24 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
25 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
26 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
28 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * @author Lorenzo Alberton <l.alberton@quipo.it>
33 * @author Richard Heyes <richard@phpguru.org>
34 * @copyright 2003-2008 Lorenzo Alberton, Richard Heyes
35 * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
36 * @version CVS: $Id: Jumping.php,v 1.20 2008/03/05 13:57:45 quipo Exp $
37 * @link http://pear.php.net/package/Pager
41 * require PEAR::Pager_Common base class
43 require_once 'Pager/Common.php';
46 * Pager_Jumping - Generic data paging class ("jumping window" style)
47 * Handles paging a set of data. For usage see the example.php provided.
51 * @author Lorenzo Alberton <l.alberton@quipo.it>
52 * @author Richard Heyes <richard@phpguru.org>
53 * @copyright 2003-2008 Lorenzo Alberton, Richard Heyes
54 * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
55 * @link http://pear.php.net/package/Pager
57 class Pager_Jumping
extends Pager_Common
59 // {{{ Pager_Jumping()
64 * @param array $options Associative array of option names and their values
68 function Pager_Jumping($options = array())
70 $err = $this->setOptions($options);
71 if ($err !== PAGER_OK
) {
72 return $this->raiseError($this->errorMessage($err), $err);
78 // {{{ getPageIdByOffset()
81 * Returns pageID for given offset
83 * @param integer $index Offset to get pageID for
85 * @return int PageID for given offset
87 function getPageIdByOffset($index)
89 if (!isset($this->_pageData
)) {
90 $this->_generatePageData();
93 if (($index %
$this->_perPage
) > 0) {
94 $pageID = ceil((float)$index / (float)$this->_perPage
);
96 $pageID = $index / $this->_perPage
;
102 // {{{ getPageRangeByPageId()
105 * Given a PageId, it returns the limits of the range of pages displayed.
106 * While getOffsetByPageId() returns the offset of the data within the
107 * current page, this method returns the offsets of the page numbers interval.
108 * E.g., if you have pageId=3 and delta=10, it will return (1, 10).
109 * PageID of 8 would give you (1, 10) as well, because 1 <= 8 <= 10.
110 * PageID of 11 would give you (11, 20).
111 * If the method is called without parameter, pageID is set to currentPage#.
113 * @param integer $pageid PageID to get offsets for
115 * @return array First and last offsets
118 function getPageRangeByPageId($pageid = null)
120 $pageid = isset($pageid) ?
(int)$pageid : $this->_currentPage
;
121 if (isset($this->_pageData
[$pageid]) ||
is_null($this->_itemData
)) {
122 // I'm sure I'm missing something here, but this formula works
123 // so I'm using it until I find something simpler.
124 $start = ((($pageid +
(($this->_delta
- ($pageid %
$this->_delta
))) %
$this->_delta
) / $this->_delta
) - 1) * $this->_delta +
1;
127 min($start+
$this->_delta
-1, $this->_totalPages
)
138 * Returns back/next/first/last and page links,
139 * both as ordered and associative array.
141 * NB: in original PEAR::Pager this method accepted two parameters,
142 * $back_html and $next_html. Now the only parameter accepted is
143 * an integer ($pageID), since the html text for prev/next links can
144 * be set in the constructor. If a second parameter is provided, then
145 * the method act as it previously did. This hack's only purpose is to
146 * mantain backward compatibility.
148 * @param integer $pageID Optional pageID. If specified, links for that
149 * page are provided instead of current one.
150 * [ADDED IN NEW PAGER VERSION]
151 * @param string $next_html HTML to put inside the next link
152 * [deprecated: use the factory instead]
154 * @return array Back/pages/next links
156 function getLinks($pageID=null, $next_html='')
159 if (!empty($next_html)) {
160 $back_html = $pageID;
166 if (!is_null($pageID)) {
168 if ($this->_totalPages
> $this->_delta
) {
169 $this->links
.= $this->_printFirstPage();
172 $_sav = $this->_currentPage
;
173 $this->_currentPage
= $pageID;
175 $this->links
.= $this->_getBackLink('', $back_html);
176 $this->links
.= $this->_getPageLinks();
177 $this->links
.= $this->_getNextLink('', $next_html);
178 if ($this->_totalPages
> $this->_delta
) {
179 $this->links
.= $this->_printLastPage();
183 $back = str_replace(' ', '', $this->_getBackLink());
184 $next = str_replace(' ', '', $this->_getNextLink());
185 $pages = $this->_getPageLinks();
186 $first = $this->_printFirstPage();
187 $last = $this->_printLastPage();
189 $linkTags = $this->linkTags
;
190 $linkTagsRaw = $this->linkTagsRaw
;
192 if (!is_null($pageID)) {
193 $this->_currentPage
= $_sav;
210 'linktags' => $linkTags,
211 'linkTagsRaw' => $linkTagsRaw,
216 // {{{ _getPageLinks()
221 * @param string $url URL to use in the link
222 * [deprecated: use the constructor instead]
224 * @return string Links
227 function _getPageLinks($url = '')
229 //legacy setting... the preferred way to set an option now
230 //is adding it to the constuctor
235 //If there's only one page, don't display links
236 if ($this->_clearIfVoid
&& ($this->_totalPages
< 2)) {
241 $limits = $this->getPageRangeByPageId($this->_currentPage
);
243 for ($i=$limits[0]; $i<=min($limits[1], $this->_totalPages
); $i++
) {
244 if ($i != $this->_currentPage
) {
245 $this->range
[$i] = false;
246 $this->_linkData
[$this->_urlVar
] = $i;
247 $links .= $this->_renderLink(str_replace('%d', $i, $this->_altPage
), $i);
249 $this->range
[$i] = true;
250 $links .= $this->_curPageSpanPre
. $i . $this->_curPageSpanPost
;
252 $links .= $this->_spacesBefore
253 . (($i != $this->_totalPages
) ?
$this->_separator
.$this->_spacesAfter
: '');