HtmlWidgets.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * Contains the Pager_HtmlWidgets 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. * @copyright 2003-2007 Lorenzo Alberton
  33. * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
  34. * @version CVS: $Id: HtmlWidgets.php,v 1.7 2009/03/13 16:51:37 quipo Exp $
  35. * @link http://pear.php.net/package/Pager
  36. */
  37. /**
  38. * Pager_HtmlWidgets
  39. *
  40. * @category HTML
  41. * @package Pager
  42. * @author Lorenzo Alberton <l.alberton@quipo.it>
  43. * @copyright 2003-2007 Lorenzo Alberton
  44. * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
  45. * @link http://pear.php.net/package/Pager
  46. */
  47. class Pager_HtmlWidgets
  48. {
  49. var $pager = null;
  50. // {{{ constructor
  51. /**
  52. * Constructor
  53. *
  54. * @param object &$pager Pager instance
  55. */
  56. function Pager_HtmlWidgets(&$pager)
  57. {
  58. $this->pager =& $pager;
  59. }
  60. // }}}
  61. // {{{ getPerPageSelectBox()
  62. /**
  63. * Returns a string with a XHTML SELECT menu,
  64. * useful for letting the user choose how many items per page should be
  65. * displayed. If parameter useSessions is TRUE, this value is stored in
  66. * a session var. The string isn't echoed right now so you can use it
  67. * with template engines.
  68. *
  69. * @param integer $start starting value for the select menu
  70. * @param integer $end ending value for the select menu
  71. * @param integer $step step between values in the select menu
  72. * @param boolean $showAllData If true, perPage is set equal to totalItems.
  73. * @param array $extraParams (or string $optionText for BC reasons)
  74. * - 'optionText': text to show in each option.
  75. * Use '%d' where you want to see the number of pages selected.
  76. * - 'attributes': (html attributes) Tag attributes or
  77. * HTML attributes (id="foo" pairs), will be inserted in the
  78. * <select> tag
  79. * - 'checkMaxLimit': if true, Pager checks if $end is bigger
  80. * than $totalItems, and doesn't show the extra select options
  81. * - 'autoSubmit': if TRUE, add some js code
  82. * to submit the form on the onChange event
  83. *
  84. * @return string xhtml select box
  85. * @access public
  86. */
  87. function getPerPageSelectBox($start=5, $end=30, $step=5, $showAllData=false, $extraParams=array())
  88. {
  89. // FIXME: needs POST support
  90. $optionText = '%d';
  91. $attributes = '';
  92. $checkMaxLimit = false;
  93. if (is_string($extraParams)) {
  94. //old behavior, BC maintained
  95. $optionText = $extraParams;
  96. } else {
  97. if (array_key_exists('optionText', $extraParams)) {
  98. $optionText = $extraParams['optionText'];
  99. }
  100. if (array_key_exists('attributes', $extraParams)) {
  101. $attributes = $extraParams['attributes'];
  102. }
  103. if (array_key_exists('checkMaxLimit', $extraParams)) {
  104. $checkMaxLimit = $extraParams['checkMaxLimit'];
  105. }
  106. }
  107. if (!strstr($optionText, '%d')) {
  108. return $this->pager->raiseError(
  109. $this->pager->errorMessage(ERROR_PAGER_INVALID_PLACEHOLDER),
  110. ERROR_PAGER_INVALID_PLACEHOLDER
  111. );
  112. }
  113. $start = (int)$start;
  114. $end = (int)$end;
  115. $step = (int)$step;
  116. if (!empty($_SESSION[$this->pager->_sessionVar])) {
  117. $selected = (int)$_SESSION[$this->pager->_sessionVar];
  118. } else {
  119. $selected = $this->pager->_perPage;
  120. }
  121. if ($checkMaxLimit && $this->pager->_totalItems >= 0 && $this->pager->_totalItems < $end) {
  122. $end = $this->pager->_totalItems;
  123. }
  124. $tmp = '<select name="'.$this->pager->_sessionVar.'"';
  125. if (!empty($attributes)) {
  126. $tmp .= ' '.$attributes;
  127. }
  128. if (!empty($extraParams['autoSubmit'])) {
  129. if ('GET' == $this->pager->_httpMethod) {
  130. $selector = '\' + '.'this.options[this.selectedIndex].value + \'';
  131. if ($this->pager->_append) {
  132. $tmpLinkData = $this->pager->_linkData;
  133. if (isset($tmpLinkData[$this->pager->_urlVar])) {
  134. $tmpLinkData[$this->pager->_urlVar] = $this->pager->getCurrentPageID();
  135. }
  136. $tmpLinkData[$this->pager->_sessionVar] = '1';
  137. $href = '?' . $this->pager->_http_build_query_wrapper($tmpLinkData);
  138. $href = htmlentities($this->pager->_url, ENT_COMPAT, 'UTF-8'). preg_replace(
  139. '/(&|&amp;|\?)('.$this->pager->_sessionVar.'=)(\d+)/',
  140. '\\1\\2'.$selector,
  141. htmlentities($href, ENT_COMPAT, 'UTF-8')
  142. );
  143. } else {
  144. $href = htmlentities($this->pager->_url . str_replace('%d', $selector, $this->pager->_fileName), ENT_COMPAT, 'UTF-8');
  145. }
  146. $tmp .= ' onchange="document.location.href=\''
  147. . $href .'\''
  148. . '"';
  149. } elseif ($this->pager->_httpMethod == 'POST') {
  150. $tmp .= " onchange='"
  151. . $this->pager->_generateFormOnClick($this->pager->_url, $this->pager->_linkData)
  152. . "'";
  153. $tmp = preg_replace(
  154. '/(input\.name = \"'.$this->pager->_sessionVar.'\"; input\.value =) \"(\d+)\";/',
  155. '\\1 this.options[this.selectedIndex].value;',
  156. $tmp
  157. );
  158. }
  159. }
  160. $tmp .= '>';
  161. $last = $start;
  162. for ($i=$start; $i<=$end; $i+=$step) {
  163. $last = $i;
  164. $tmp .= '<option value="'.$i.'"';
  165. if ($i == $selected) {
  166. $tmp .= ' selected="selected"';
  167. }
  168. $tmp .= '>'.sprintf($optionText, $i).'</option>';
  169. }
  170. if ($showAllData && $last != $this->pager->_totalItems) {
  171. $tmp .= '<option value="'.$this->pager->_totalItems.'"';
  172. if ($this->pager->_totalItems == $selected) {
  173. $tmp .= ' selected="selected"';
  174. }
  175. $tmp .= '>';
  176. if (empty($this->pager->_showAllText)) {
  177. $tmp .= str_replace('%d', $this->pager->_totalItems, $optionText);
  178. } else {
  179. $tmp .= $this->pager->_showAllText;
  180. }
  181. $tmp .= '</option>';
  182. }
  183. if (substr($tmp, -9, 9) !== '</option>') {
  184. //empty select
  185. $tmp .= '<option />';
  186. }
  187. $tmp .= '</select>';
  188. return $tmp;
  189. }
  190. // }}}
  191. // {{{ getPageSelectBox()
  192. /**
  193. * Returns a string with a XHTML SELECT menu with the page numbers,
  194. * useful as an alternative to the links
  195. *
  196. * @param array $params - 'optionText': text to show in each option.
  197. * Use '%d' where you want to see the number
  198. * of pages selected.
  199. * - 'autoSubmit': if TRUE, add some js code
  200. * to submit the form on the onChange event
  201. * @param string $extraAttributes (html attributes) Tag attributes or
  202. * HTML attributes (id="foo" pairs), will be
  203. * inserted in the <select> tag
  204. *
  205. * @return string xhtml select box
  206. * @access public
  207. */
  208. function getPageSelectBox($params = array(), $extraAttributes = '')
  209. {
  210. $optionText = '%d';
  211. if (array_key_exists('optionText', $params)) {
  212. $optionText = $params['optionText'];
  213. }
  214. if (!strstr($optionText, '%d')) {
  215. return $this->pager->raiseError(
  216. $this->pager->errorMessage(ERROR_PAGER_INVALID_PLACEHOLDER),
  217. ERROR_PAGER_INVALID_PLACEHOLDER
  218. );
  219. }
  220. $tmp = '<select name="'.$this->pager->_urlVar.'"';
  221. if (!empty($extraAttributes)) {
  222. $tmp .= ' '.$extraAttributes;
  223. }
  224. if (!empty($params['autoSubmit'])) {
  225. if ($this->pager->_httpMethod == 'GET') {
  226. $selector = '\' + '.'this.options[this.selectedIndex].value + \'';
  227. if ($this->pager->_append) {
  228. $href = '?' . $this->pager->_http_build_query_wrapper($this->pager->_linkData);
  229. // Modified by Ivan Tcholakov, 17-OCT-2008.
  230. //$href = htmlentities($this->pager->_url, ENT_COMPAT, 'UTF-8'). preg_replace(
  231. // '/(&|&amp;|\?)('.$this->pager->_urlVar.'=)(\d+)/',
  232. // '\\1\\2'.$selector,
  233. // htmlentities($href, ENT_COMPAT, 'UTF-8')
  234. $href = api_htmlentities($this->pager->_url). preg_replace(
  235. '/(&|&amp;|\?)('.$this->pager->_urlVar.'=)(\d+)/',
  236. '\\1\\2'.$selector,
  237. api_htmlentities($href)
  238. );
  239. //
  240. } else {
  241. // Modified by Ivan Tcholakov, 17-OCT-2008.
  242. //$href = htmlentities($this->pager->_url . str_replace('%d', $selector, $this->pager->_fileName), ENT_COMPAT, 'UTF-8');
  243. $href = api_htmlentities($this->pager->_url . str_replace('%d', $selector, $this->pager->_fileName));
  244. //
  245. }
  246. $tmp .= ' onchange="javascript: document.location.href=\''
  247. . $href .'\''
  248. . '"';
  249. } elseif ($this->pager->_httpMethod == 'POST') {
  250. $tmp .= " onchange='"
  251. . $this->pager->_generateFormOnClick($this->pager->_url, $this->pager->_linkData)
  252. . "'";
  253. $tmp = preg_replace(
  254. '/(input\.name = \"'.$this->pager->_urlVar.'\"; input\.value =) \"(\d+)\";/',
  255. '\\1 this.options[this.selectedIndex].value;',
  256. $tmp
  257. );
  258. }
  259. }
  260. $tmp .= '>';
  261. $start = 1;
  262. $end = $this->pager->numPages();
  263. $selected = $this->pager->getCurrentPageID();
  264. for ($i=$start; $i<=$end; $i++) {
  265. $tmp .= '<option value="'.$i.'"';
  266. if ($i == $selected) {
  267. $tmp .= ' selected="selected"';
  268. }
  269. $tmp .= '>'.sprintf($optionText, $i).'</option>';
  270. }
  271. $tmp .= '</select>';
  272. return $tmp;
  273. }
  274. // }}}
  275. }
  276. ?>