commiting uncommited changes on live site
[weblabels.fsf.org.git] / crm.fsf.org / 20131203 / files / sites / all / modules-new / civicrm / packages / Pager / Jumping.php
1 <?php
2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
3
4 /**
5 * Contains the Pager_Jumping class
6 *
7 * PHP versions 4 and 5
8 *
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.
18 *
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.
29 *
30 * @category HTML
31 * @package Pager
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
38 */
39
40 /**
41 * require PEAR::Pager_Common base class
42 */
43 require_once 'Pager/Common.php';
44
45 /**
46 * Pager_Jumping - Generic data paging class ("jumping window" style)
47 * Handles paging a set of data. For usage see the example.php provided.
48 *
49 * @category HTML
50 * @package Pager
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
56 */
57 class Pager_Jumping extends Pager_Common
58 {
59 // {{{ Pager_Jumping()
60
61 /**
62 * Constructor
63 *
64 * @param array $options Associative array of option names and their values
65 *
66 * @access public
67 */
68 function Pager_Jumping($options = array())
69 {
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 // }}}
78 // {{{ getPageIdByOffset()
79
80 /**
81 * Returns pageID for given offset
82 *
83 * @param integer $index Offset to get pageID for
84 *
85 * @return int PageID for given offset
86 */
87 function getPageIdByOffset($index)
88 {
89 if (!isset($this->_pageData)) {
90 $this->_generatePageData();
91 }
92
93 if (($index % $this->_perPage) > 0) {
94 $pageID = ceil((float)$index / (float)$this->_perPage);
95 } else {
96 $pageID = $index / $this->_perPage;
97 }
98 return $pageID;
99 }
100
101 // }}}
102 // {{{ getPageRangeByPageId()
103
104 /**
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#.
112 *
113 * @param integer $pageid PageID to get offsets for
114 *
115 * @return array First and last offsets
116 * @access public
117 */
118 function getPageRangeByPageId($pageid = null)
119 {
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;
125 return array(
126 max($start, 1),
127 min($start+$this->_delta-1, $this->_totalPages)
128 );
129 } else {
130 return array(0, 0);
131 }
132 }
133
134 // }}}
135 // {{{ getLinks()
136
137 /**
138 * Returns back/next/first/last and page links,
139 * both as ordered and associative array.
140 *
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.
147 *
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]
153 *
154 * @return array Back/pages/next links
155 */
156 function getLinks($pageID=null, $next_html='')
157 {
158 //BC hack
159 if (!empty($next_html)) {
160 $back_html = $pageID;
161 $pageID = null;
162 } else {
163 $back_html = '';
164 }
165
166 if (!is_null($pageID)) {
167 $this->links = '';
168 if ($this->_totalPages > $this->_delta) {
169 $this->links .= $this->_printFirstPage();
170 }
171
172 $_sav = $this->_currentPage;
173 $this->_currentPage = $pageID;
174
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();
180 }
181 }
182
183 $back = str_replace('&nbsp;', '', $this->_getBackLink());
184 $next = str_replace('&nbsp;', '', $this->_getNextLink());
185 $pages = $this->_getPageLinks();
186 $first = $this->_printFirstPage();
187 $last = $this->_printLastPage();
188 $all = $this->links;
189 $linkTags = $this->linkTags;
190 $linkTagsRaw = $this->linkTagsRaw;
191
192 if (!is_null($pageID)) {
193 $this->_currentPage = $_sav;
194 }
195
196 return array(
197 $back,
198 $pages,
199 trim($next),
200 $first,
201 $last,
202 $all,
203 $linkTags,
204 'back' => $back,
205 'pages' => $pages,
206 'next' => $next,
207 'first' => $first,
208 'last' => $last,
209 'all' => $all,
210 'linktags' => $linkTags,
211 'linkTagsRaw' => $linkTagsRaw,
212 );
213 }
214
215 // }}}
216 // {{{ _getPageLinks()
217
218 /**
219 * Returns pages link
220 *
221 * @param string $url URL to use in the link
222 * [deprecated: use the constructor instead]
223 *
224 * @return string Links
225 * @access private
226 */
227 function _getPageLinks($url = '')
228 {
229 //legacy setting... the preferred way to set an option now
230 //is adding it to the constuctor
231 if (!empty($url)) {
232 $this->_path = $url;
233 }
234
235 //If there's only one page, don't display links
236 if ($this->_clearIfVoid && ($this->_totalPages < 2)) {
237 return '';
238 }
239
240 $links = '';
241 $limits = $this->getPageRangeByPageId($this->_currentPage);
242
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);
248 } else {
249 $this->range[$i] = true;
250 $links .= $this->_curPageSpanPre . $i . $this->_curPageSpanPost;
251 }
252 $links .= $this->_spacesBefore
253 . (($i != $this->_totalPages) ? $this->_separator.$this->_spacesAfter : '');
254 }
255 return $links;
256 }
257
258 // }}}
259 }
260 ?>