Pager.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * Contains the Pager 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. * @author Richard Heyes <richard@phpguru.org>
  33. * @copyright 2003-2008 Lorenzo Alberton, Richard Heyes
  34. * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
  35. * @version CVS: $Id: Pager.php,v 1.26 2008/02/02 16:55:04 quipo Exp $
  36. * @link http://pear.php.net/package/Pager
  37. */
  38. /**
  39. * Pager - Wrapper class for [Sliding|Jumping]-window Pager
  40. * Usage examples can be found in the PEAR manual
  41. *
  42. * @category HTML
  43. * @package Pager
  44. * @author Lorenzo Alberton <l.alberton@quipo.it>
  45. * @author Richard Heyes <richard@phpguru.org>
  46. * @copyright 2003-2008 Lorenzo Alberton, Richard Heyes
  47. * @license http://www.debian.org/misc/bsd.license BSD License (3 Clause)
  48. * @link http://pear.php.net/package/Pager
  49. */
  50. class Pager
  51. {
  52. // {{{ Pager()
  53. /**
  54. * Constructor
  55. *
  56. * -------------------------------------------------------------------------
  57. * VALID options are (default values are set some lines before):
  58. * - mode (string): "Jumping" or "Sliding" -window - It determines
  59. * pager behaviour. See the manual for more details
  60. * - totalItems (int): # of items to page.
  61. * - perPage (int): # of items per page.
  62. * - delta (int): # of page #s to show before and after the current
  63. * one
  64. * - linkClass (string): name of CSS class used for link styling.
  65. * - append (bool): if true pageID is appended as GET value to the
  66. * URL - if false it is embedded in the URL
  67. * according to "fileName" specs
  68. * - httpMethod (string): Specifies the HTTP method to use. Valid values
  69. * are 'GET' or 'POST'
  70. * according to "fileName" specs
  71. * - importQuery (bool): if true (default behaviour), variables and
  72. * values are imported from the submitted data
  73. * (query string) and used in the generated links
  74. * otherwise they're ignored completely
  75. * - path (string): complete path to the page (without the page name)
  76. * - fileName (string): name of the page, with a %d if append=true
  77. * - urlVar (string): name of pageNumber URL var, for example "pageID"
  78. * - altPrev (string): alt text to display for prev page, on prev link.
  79. * - altNext (string): alt text to display for next page, on next link.
  80. * - altPage (string): alt text to display before the page number.
  81. * - prevImg (string): sth (it can be text such as "<< PREV" or an
  82. * <img/> as well...) to display instead of "<<".
  83. * - nextImg (string): same as prevImg, used for NEXT link, instead of
  84. * the default value, which is ">>".
  85. * - separator (string): what to use to separate numbers (can be an
  86. * <img/>, a comma, an hyphen, or whatever.
  87. * - spacesBeforeSeparator
  88. * (int): number of spaces before the separator.
  89. * - firstPagePre (string):
  90. * string used before first page number (can be an
  91. * <img/>, a "{", an empty string, or whatever.
  92. * - firstPageText (string):
  93. * string used in place of first page number
  94. * - firstPagePost (string):
  95. * string used after first page number (can be an
  96. * <img/>, a "}", an empty string, or whatever.
  97. * - lastPagePre (string):
  98. * similar to firstPagePre.
  99. * - lastPageText (string):
  100. * similar to firstPageText.
  101. * - lastPagePost (string):
  102. * similar to firstPagePost.
  103. * - spacesAfterSeparator
  104. * (int): number of spaces after the separator.
  105. * - firstLinkTitle (string):
  106. * string used as title in <link rel="first"> tag
  107. * - lastLinkTitle (string):
  108. * string used as title in <link rel="last"> tag
  109. * - prevLinkTitle (string):
  110. * string used as title in <link rel="prev"> tag
  111. * - nextLinkTitle (string):
  112. * string used as title in <link rel="next"> tag
  113. * - curPageLinkClassName
  114. * (string): name of CSS class used for current page link.
  115. * - clearIfVoid(bool): if there's only one page, don't display pager.
  116. * - extraVars (array): additional URL vars to be added to the querystring
  117. * - excludeVars (array): URL vars to be excluded in the querystring
  118. * - itemData (array): array of items to page.
  119. * - useSessions (bool): if true, number of items to display per page is
  120. * stored in the $_SESSION[$_sessionVar] var.
  121. * - closeSession (bool): if true, the session is closed just after R/W.
  122. * - sessionVar (string): name of the session var for perPage value.
  123. * A value != from default can be useful when
  124. * using more than one Pager istance in the page.
  125. * - pearErrorMode (constant):
  126. * PEAR_ERROR mode for raiseError().
  127. * Default is PEAR_ERROR_RETURN.
  128. * -------------------------------------------------------------------------
  129. * REQUIRED options are:
  130. * - fileName IF append==false (default is true)
  131. * - itemData OR totalItems (if itemData is set, totalItems is overwritten)
  132. * -------------------------------------------------------------------------
  133. *
  134. * @param mixed $options Associative array of option names and their values
  135. *
  136. * @access public
  137. */
  138. function Pager($options = array())
  139. {
  140. //this check evaluates to true on 5.0.0RC-dev,
  141. //so i'm using another one, for now...
  142. //if (version_compare(phpversion(), '5.0.0') == -1) {
  143. if (get_class($this) == 'pager') { //php4 lowers class names
  144. // assign factoried method to this for PHP 4
  145. eval('$this = Pager::factory($options);');
  146. } else { //php5 is case sensitive
  147. $msg = 'Pager constructor is deprecated.'
  148. .' You must use the "Pager::factory($params)" method'
  149. .' instead of "new Pager($params)"';
  150. trigger_error($msg, E_USER_ERROR);
  151. }
  152. }
  153. // }}}
  154. // {{{ factory()
  155. /**
  156. * Return a pager based on $mode and $options
  157. *
  158. * @param array $options Optional parameters for the storage class
  159. *
  160. * @return object Storage object
  161. * @static
  162. * @access public
  163. */
  164. static function &factory($options = array())
  165. {
  166. $mode = (isset($options['mode']) ? ucfirst($options['mode']) : 'Jumping');
  167. $classname = 'Pager_' . $mode;
  168. $classfile = 'Pager' . DIRECTORY_SEPARATOR . $mode . '.php';
  169. // Attempt to include a custom version of the named class, but don't treat
  170. // a failure as fatal. The caller may have already included their own
  171. // version of the named class.
  172. if (!class_exists($classname)) {
  173. include_once $classfile;
  174. }
  175. // If the class exists, return a new instance of it.
  176. if (class_exists($classname)) {
  177. $pager = new $classname($options);
  178. return $pager;
  179. }
  180. $null = null;
  181. return $null;
  182. }
  183. // }}}
  184. }
  185. ?>