2 /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
5 * Contains the Pager_HtmlWidgets 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 * @copyright 2003-2007 Lorenzo Alberton
34 * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
35 * @version CVS: $Id: HtmlWidgets.php,v 1.7 2009/03/13 16:51:37 quipo Exp $
36 * @link http://pear.php.net/package/Pager
44 * @author Lorenzo Alberton <l.alberton@quipo.it>
45 * @copyright 2003-2007 Lorenzo Alberton
46 * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
47 * @link http://pear.php.net/package/Pager
49 class Pager_HtmlWidgets
58 * @param object &$pager Pager instance
60 function Pager_HtmlWidgets(&$pager)
62 $this->pager
=& $pager;
66 // {{{ getPerPageSelectBox()
69 * Returns a string with a XHTML SELECT menu,
70 * useful for letting the user choose how many items per page should be
71 * displayed. If parameter useSessions is TRUE, this value is stored in
72 * a session var. The string isn't echoed right now so you can use it
73 * with template engines.
75 * @param integer $start starting value for the select menu
76 * @param integer $end ending value for the select menu
77 * @param integer $step step between values in the select menu
78 * @param boolean $showAllData If true, perPage is set equal to totalItems.
79 * @param array $extraParams (or string $optionText for BC reasons)
80 * - 'optionText': text to show in each option.
81 * Use '%d' where you want to see the number of pages selected.
82 * - 'attributes': (html attributes) Tag attributes or
83 * HTML attributes (id="foo" pairs), will be inserted in the
85 * - 'checkMaxLimit': if true, Pager checks if $end is bigger
86 * than $totalItems, and doesn't show the extra select options
87 * - 'autoSubmit': if TRUE, add some js code
88 * to submit the form on the onChange event
90 * @return string xhtml select box
93 function getPerPageSelectBox($start=5, $end=30, $step=5, $showAllData=false, $extraParams=array())
95 // FIXME: needs POST support
98 $checkMaxLimit = false;
99 if (is_string($extraParams)) {
100 //old behavior, BC maintained
101 $optionText = $extraParams;
103 if (array_key_exists('optionText', $extraParams)) {
104 $optionText = $extraParams['optionText'];
106 if (array_key_exists('attributes', $extraParams)) {
107 $attributes = $extraParams['attributes'];
109 if (array_key_exists('checkMaxLimit', $extraParams)) {
110 $checkMaxLimit = $extraParams['checkMaxLimit'];
114 if (!strstr($optionText, '%d')) {
115 return $this->pager
->raiseError(
116 $this->pager
->errorMessage(ERROR_PAGER_INVALID_PLACEHOLDER
),
117 ERROR_PAGER_INVALID_PLACEHOLDER
120 $start = (int)$start;
123 if (!empty($_SESSION[$this->pager
->_sessionVar
])) {
124 $selected = (int)$_SESSION[$this->pager
->_sessionVar
];
126 $selected = $this->pager
->_perPage
;
129 if ($checkMaxLimit && $this->pager
->_totalItems
>= 0 && $this->pager
->_totalItems
< $end) {
130 $end = $this->pager
->_totalItems
;
133 $tmp = '<select name="'.$this->pager
->_sessionVar
.'"';
134 if (!empty($attributes)) {
135 $tmp .= ' '.$attributes;
137 if (!empty($extraParams['autoSubmit'])) {
138 if ('GET' == $this->pager
->_httpMethod
) {
139 $selector = '\' + '.'this.options[this.selectedIndex].value + \'';
140 if ($this->pager
->_append
) {
141 $tmpLinkData = $this->pager
->_linkData
;
142 if (isset($tmpLinkData[$this->pager
->_urlVar
])) {
143 $tmpLinkData[$this->pager
->_urlVar
] = $this->pager
->getCurrentPageID();
145 $tmpLinkData[$this->pager
->_sessionVar
] = '1';
146 $href = '?' . $this->pager
->_http_build_query_wrapper($tmpLinkData);
147 $href = htmlentities($this->pager
->_url
, ENT_COMPAT
, 'UTF-8'). preg_replace(
148 '/(&|&|\?)('.$this->pager
->_sessionVar
.'=)(\d+)/',
150 htmlentities($href, ENT_COMPAT
, 'UTF-8')
153 $href = htmlentities($this->pager
->_url
. str_replace('%d', $selector, $this->pager
->_fileName
), ENT_COMPAT
, 'UTF-8');
155 $tmp .= ' onchange="document.location.href=\''
158 } elseif ($this->pager
->_httpMethod
== 'POST') {
159 $tmp .= " onchange='"
160 . $this->pager
->_generateFormOnClick($this->pager
->_url
, $this->pager
->_linkData
)
163 '/(input\.name = \"'.$this->pager
->_sessionVar
.'\"; input\.value =) \"(\d+)\";/',
164 '\\1 this.options[this.selectedIndex].value;',
173 for ($i=$start; $i<=$end; $i+
=$step) {
175 $tmp .= '<option value="'.$i.'"';
176 if ($i == $selected) {
177 $tmp .= ' selected="selected"';
179 $tmp .= '>'.sprintf($optionText, $i).'</option>';
181 if ($showAllData && $last != $this->pager
->_totalItems
) {
182 $tmp .= '<option value="'.$this->pager
->_totalItems
.'"';
183 if ($this->pager
->_totalItems
== $selected) {
184 $tmp .= ' selected="selected"';
187 if (empty($this->pager
->_showAllText
)) {
188 $tmp .= str_replace('%d', $this->pager
->_totalItems
, $optionText);
190 $tmp .= $this->pager
->_showAllText
;
194 if (substr($tmp, -9, 9) !== '</option>') {
196 $tmp .= '<option />';
203 // {{{ getPageSelectBox()
206 * Returns a string with a XHTML SELECT menu with the page numbers,
207 * useful as an alternative to the links
209 * @param array $params - 'optionText': text to show in each option.
210 * Use '%d' where you want to see the number
212 * - 'autoSubmit': if TRUE, add some js code
213 * to submit the form on the onChange event
214 * @param string $extraAttributes (html attributes) Tag attributes or
215 * HTML attributes (id="foo" pairs), will be
216 * inserted in the <select> tag
218 * @return string xhtml select box
221 function getPageSelectBox($params = array(), $extraAttributes = '')
224 if (array_key_exists('optionText', $params)) {
225 $optionText = $params['optionText'];
228 if (!strstr($optionText, '%d')) {
229 return $this->pager
->raiseError(
230 $this->pager
->errorMessage(ERROR_PAGER_INVALID_PLACEHOLDER
),
231 ERROR_PAGER_INVALID_PLACEHOLDER
235 $tmp = '<select name="'.$this->pager
->_urlVar
.'"';
236 if (!empty($extraAttributes)) {
237 $tmp .= ' '.$extraAttributes;
239 if (!empty($params['autoSubmit'])) {
240 if ($this->pager
->_httpMethod
== 'GET') {
241 $selector = '\' + '.'this.options[this.selectedIndex].value + \'';
242 if ($this->pager
->_append
) {
243 $href = '?' . $this->pager
->_http_build_query_wrapper($this->pager
->_linkData
);
244 $href = htmlentities($this->pager
->_url
, ENT_COMPAT
, 'UTF-8'). preg_replace(
245 '/(&|&|\?)('.$this->pager
->_urlVar
.'=)(\d+)/',
247 htmlentities($href, ENT_COMPAT
, 'UTF-8')
250 $href = htmlentities($this->pager
->_url
. str_replace('%d', $selector, $this->pager
->_fileName
), ENT_COMPAT
, 'UTF-8');
252 $tmp .= ' onchange="document.location.href=\''
255 } elseif ($this->pager
->_httpMethod
== 'POST') {
256 $tmp .= " onchange='"
257 . $this->pager
->_generateFormOnClick($this->pager
->_url
, $this->pager
->_linkData
)
260 '/(input\.name = \"'.$this->pager
->_urlVar
.'\"; input\.value =) \"(\d+)\";/',
261 '\\1 this.options[this.selectedIndex].value;',
268 $end = $this->pager
->numPages();
269 $selected = $this->pager
->getCurrentPageID();
270 for ($i=$start; $i<=$end; $i++
) {
271 $tmp .= '<option value="'.$i.'"';
272 if ($i == $selected) {
273 $tmp .= ' selected="selected"';
275 $tmp .= '>'.sprintf($optionText, $i).'</option>';