HtmlWidgets.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 dot alberton at quipo dot it>
  32. * @copyright 2003-2006 Lorenzo Alberton
  33. * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
  34. * @version CVS: $Id: HtmlWidgets.php,v 1.1 2006/03/08 15:22:42 quipo Exp $
  35. * @link http://pear.php.net/package/Pager
  36. */
  37. /**
  38. * Two constants used to guess the path- and file-name of the page
  39. * when the user doesn't set any other value
  40. */
  41. class Pager_HtmlWidgets
  42. {
  43. var $pager = null;
  44. // {{{ constructor
  45. function Pager_HtmlWidgets(&$pager)
  46. {
  47. $this->pager =& $pager;
  48. }
  49. // }}}
  50. // {{{ getPerPageSelectBox()
  51. /**
  52. * Returns a string with a XHTML SELECT menu,
  53. * useful for letting the user choose how many items per page should be
  54. * displayed. If parameter useSessions is TRUE, this value is stored in
  55. * a session var. The string isn't echoed right now so you can use it
  56. * with template engines.
  57. *
  58. * @param integer $start
  59. * @param integer $end
  60. * @param integer $step
  61. * @param boolean $showAllData If true, perPage is set equal to totalItems.
  62. * @param array (or string $optionText for BC reasons)
  63. * - 'optionText': text to show in each option.
  64. * Use '%d' where you want to see the number of pages selected.
  65. * - 'attributes': (html attributes) Tag attributes or
  66. * HTML attributes (id="foo" pairs), will be inserted in the
  67. * <select> tag
  68. * @return string xhtml select box
  69. * @access public
  70. */
  71. function getPerPageSelectBox($start=5, $end=30, $step=5, $showAllData=false, $extraParams=array())
  72. {
  73. // FIXME: needs POST support
  74. $optionText = '%d';
  75. $attributes = '';
  76. if (is_string($extraParams)) {
  77. //old behavior, BC maintained
  78. $optionText = $extraParams;
  79. } else {
  80. if (array_key_exists('optionText', $extraParams)) {
  81. $optionText = $extraParams['optionText'];
  82. }
  83. if (array_key_exists('attributes', $extraParams)) {
  84. $attributes = $extraParams['attributes'];
  85. }
  86. }
  87. if (!strstr($optionText, '%d')) {
  88. return $this->pager->raiseError(
  89. $this->pager->errorMessage(ERROR_PAGER_INVALID_PLACEHOLDER),
  90. ERROR_PAGER_INVALID_PLACEHOLDER
  91. );
  92. }
  93. $start = (int)$start;
  94. $end = (int)$end;
  95. $step = (int)$step;
  96. if (!empty($_SESSION[$this->pager->_sessionVar])) {
  97. $selected = (int)$_SESSION[$this->pager->_sessionVar];
  98. } else {
  99. $selected = $this->pager->_perPage;
  100. }
  101. $tmp = '<select name="'.$this->pager->_sessionVar.'"';
  102. if (!empty($attributes)) {
  103. $tmp .= ' '.$attributes;
  104. }
  105. $tmp .= '>';
  106. for ($i=$start; $i<=$end; $i+=$step) {
  107. $tmp .= '<option value="'.$i.'"';
  108. if ($i == $selected) {
  109. $tmp .= ' selected="selected"';
  110. }
  111. $tmp .= '>'.sprintf($optionText, $i).'</option>';
  112. }
  113. if ($showAllData && $end < $this->pager->_totalItems) {
  114. $tmp .= '<option value="'.$this->pager->_totalItems.'"';
  115. if ($this->pager->_totalItems == $selected) {
  116. $tmp .= ' selected="selected"';
  117. }
  118. $tmp .= '>';
  119. if (empty($this->pager->_showAllText)) {
  120. $tmp .= str_replace('%d', $this->pager->_totalItems, $optionText);
  121. } else {
  122. $tmp .= $this->pager->_showAllText;
  123. }
  124. $tmp .= '</option>';
  125. }
  126. $tmp .= '</select>';
  127. return $tmp;
  128. }
  129. // }}}
  130. // {{{ getPageSelectBox()
  131. /**
  132. * Returns a string with a XHTML SELECT menu with the page numbers,
  133. * useful as an alternative to the links
  134. *
  135. * @param array - 'optionText': text to show in each option.
  136. * Use '%d' where you want to see the number of pages selected.
  137. * - 'autoSubmit': if TRUE, add some js code to submit the
  138. * form on the onChange event
  139. * @param string $extraAttributes (html attributes) Tag attributes or
  140. * HTML attributes (id="foo" pairs), will be inserted in the
  141. * <select> tag
  142. * @return string xhtml select box
  143. * @access public
  144. */
  145. function getPageSelectBox($params = array(), $extraAttributes = '')
  146. {
  147. $optionText = '%d';
  148. if (array_key_exists('optionText', $params)) {
  149. $optionText = $params['optionText'];
  150. }
  151. if (!strstr($optionText, '%d')) {
  152. return $this->pager->raiseError(
  153. $this->pager->errorMessage(ERROR_PAGER_INVALID_PLACEHOLDER),
  154. ERROR_PAGER_INVALID_PLACEHOLDER
  155. );
  156. }
  157. $tmp = '<select name="'.$this->pager->_urlVar.'"';
  158. if (!empty($extraAttributes)) {
  159. $tmp .= ' '.$extraAttributes;
  160. }
  161. if (!empty($params['autoSubmit'])) {
  162. if ($this->pager->_httpMethod == 'GET') {
  163. $selector = '\' + '.'this.options[this.selectedIndex].value + \'';
  164. if ($this->pager->_append) {
  165. $href = '?' . $this->pager->_http_build_query_wrapper($this->pager->_linkData);
  166. $href = htmlentities($this->pager->_url). preg_replace(
  167. '/(&|&amp;|\?)('.$this->pager->_urlVar.'=)(\d+)/',
  168. '\\1\\2'.$selector,
  169. htmlentities($href)
  170. );
  171. } else {
  172. $href = htmlentities($this->pager->_url . str_replace('%d', $selector, $this->pager->_fileName));
  173. }
  174. $tmp .= ' onchange="document.location.href=\''
  175. . $href .'\''
  176. . '"';
  177. } elseif ($this->pager->_httpMethod == 'POST') {
  178. $tmp .= " onchange='"
  179. . $this->pager->_generateFormOnClick($this->pager->_url, $this->pager->_linkData)
  180. . "'";
  181. $tmp = preg_replace(
  182. '/(input\.name = \"'.$this->pager->_urlVar.'\"; input\.value =) \"(\d+)\";/',
  183. '\\1 this.options[this.selectedIndex].value;',
  184. $tmp
  185. );
  186. }
  187. }
  188. $tmp .= '>';
  189. $start = 1;
  190. $end = $this->pager->numPages();
  191. $selected = $this->pager->getCurrentPageID();
  192. for ($i=$start; $i<=$end; $i++) {
  193. $tmp .= '<option value="'.$i.'"';
  194. if ($i == $selected) {
  195. $tmp .= ' selected="selected"';
  196. }
  197. $tmp .= '>'.sprintf($optionText, $i).'</option>';
  198. }
  199. $tmp .= '</select>';
  200. return $tmp;
  201. }
  202. // }}}
  203. }
  204. ?>