sfPager.class.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) 2004-2006 Fabien Potencier <fabien.potencier@symfony-project.com>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. /**
  10. * @package symfony
  11. * @subpackage addon
  12. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  13. * @version SVN: $Id: sfPager.class.php 18089 2009-05-09 06:36:09Z fabien $
  14. */
  15. /**
  16. *
  17. * sfPager class.
  18. *
  19. * @package symfony
  20. * @subpackage addon
  21. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  22. * @version SVN: $Id: sfPager.class.php 18089 2009-05-09 06:36:09Z fabien $
  23. */
  24. abstract class sfPager
  25. {
  26. protected
  27. $page = 1,
  28. $maxPerPage = 0,
  29. $lastPage = 1,
  30. $nbResults = 0,
  31. $class = '',
  32. $tableName = '',
  33. $objects = null,
  34. $cursor = 1,
  35. $parameters = array(),
  36. $currentMaxLink = 1,
  37. $parameterHolder = null,
  38. $maxRecordLimit = false;
  39. public function __construct($class, $maxPerPage = 10)
  40. {
  41. $this->setClass($class);
  42. $this->setMaxPerPage($maxPerPage);
  43. $this->parameterHolder = new sfParameterHolder();
  44. }
  45. // function to be called after parameters have been set
  46. abstract public function init();
  47. // main method: returns an array of result on the given page
  48. abstract public function getResults();
  49. // used internally by getCurrent()
  50. abstract protected function retrieveObject($offset);
  51. public function getCurrentMaxLink()
  52. {
  53. return $this->currentMaxLink;
  54. }
  55. public function getMaxRecordLimit()
  56. {
  57. return $this->maxRecordLimit;
  58. }
  59. public function setMaxRecordLimit($limit)
  60. {
  61. $this->maxRecordLimit = $limit;
  62. }
  63. public function getLinks($nb_links = 5)
  64. {
  65. $links = array();
  66. $tmp = $this->page - floor($nb_links / 2);
  67. $check = $this->lastPage - $nb_links + 1;
  68. $limit = ($check > 0) ? $check : 1;
  69. $begin = ($tmp > 0) ? (($tmp > $limit) ? $limit : $tmp) : 1;
  70. $i = (int) $begin;
  71. while (($i < $begin + $nb_links) && ($i <= $this->lastPage))
  72. {
  73. $links[] = $i++;
  74. }
  75. $this->currentMaxLink = count($links) ? $links[count($links) - 1] : 1;
  76. return $links;
  77. }
  78. public function haveToPaginate()
  79. {
  80. return (($this->getMaxPerPage() != 0) && ($this->getNbResults() > $this->getMaxPerPage()));
  81. }
  82. public function getCursor()
  83. {
  84. return $this->cursor;
  85. }
  86. public function setCursor($pos)
  87. {
  88. if ($pos < 1)
  89. {
  90. $this->cursor = 1;
  91. }
  92. else if ($pos > $this->nbResults)
  93. {
  94. $this->cursor = $this->nbResults;
  95. }
  96. else
  97. {
  98. $this->cursor = $pos;
  99. }
  100. }
  101. public function getObjectByCursor($pos)
  102. {
  103. $this->setCursor($pos);
  104. return $this->getCurrent();
  105. }
  106. public function getCurrent()
  107. {
  108. return $this->retrieveObject($this->cursor);
  109. }
  110. public function getNext()
  111. {
  112. if (($this->cursor + 1) > $this->nbResults)
  113. {
  114. return null;
  115. }
  116. else
  117. {
  118. return $this->retrieveObject($this->cursor + 1);
  119. }
  120. }
  121. public function getPrevious()
  122. {
  123. if (($this->cursor - 1) < 1)
  124. {
  125. return null;
  126. }
  127. else
  128. {
  129. return $this->retrieveObject($this->cursor - 1);
  130. }
  131. }
  132. public function getFirstIndice()
  133. {
  134. if ($this->page == 0)
  135. {
  136. return 1;
  137. }
  138. else
  139. {
  140. return ($this->page - 1) * $this->maxPerPage + 1;
  141. }
  142. }
  143. public function getLastIndice()
  144. {
  145. if ($this->page == 0)
  146. {
  147. return $this->nbResults;
  148. }
  149. else
  150. {
  151. if (($this->page * $this->maxPerPage) >= $this->nbResults)
  152. {
  153. return $this->nbResults;
  154. }
  155. else
  156. {
  157. return ($this->page * $this->maxPerPage);
  158. }
  159. }
  160. }
  161. public function getClass()
  162. {
  163. return $this->class;
  164. }
  165. public function setClass($class)
  166. {
  167. $this->class = $class;
  168. }
  169. public function getNbResults()
  170. {
  171. return $this->nbResults;
  172. }
  173. protected function setNbResults($nb)
  174. {
  175. $this->nbResults = $nb;
  176. }
  177. public function getFirstPage()
  178. {
  179. return 1;
  180. }
  181. public function getLastPage()
  182. {
  183. return $this->lastPage;
  184. }
  185. protected function setLastPage($page)
  186. {
  187. $this->lastPage = $page;
  188. if ($this->getPage() > $page)
  189. {
  190. $this->setPage($page);
  191. }
  192. }
  193. public function getPage()
  194. {
  195. return $this->page;
  196. }
  197. public function getNextPage()
  198. {
  199. return min($this->getPage() + 1, $this->getLastPage());
  200. }
  201. public function getPreviousPage()
  202. {
  203. return max($this->getPage() - 1, $this->getFirstPage());
  204. }
  205. public function setPage($page)
  206. {
  207. $this->page = intval($page);
  208. if ($this->page <= 0)
  209. {
  210. //set first page, which depends on a maximum set
  211. $this->page = $this->getMaxPerPage() ? 1 : 0;
  212. }
  213. }
  214. public function getMaxPerPage()
  215. {
  216. return $this->maxPerPage;
  217. }
  218. public function setMaxPerPage($max)
  219. {
  220. if ($max > 0)
  221. {
  222. $this->maxPerPage = $max;
  223. if ($this->page == 0)
  224. {
  225. $this->page = 1;
  226. }
  227. }
  228. else if ($max == 0)
  229. {
  230. $this->maxPerPage = 0;
  231. $this->page = 0;
  232. }
  233. else
  234. {
  235. $this->maxPerPage = 1;
  236. if ($this->page == 0)
  237. {
  238. $this->page = 1;
  239. }
  240. }
  241. }
  242. public function getParameterHolder()
  243. {
  244. return $this->parameterHolder;
  245. }
  246. public function getParameter($name, $default = null)
  247. {
  248. return $this->parameterHolder->get($name, $default);
  249. }
  250. public function hasParameter($name)
  251. {
  252. return $this->parameterHolder->has($name);
  253. }
  254. public function setParameter($name, $value)
  255. {
  256. return $this->parameterHolder->set($name, $value);
  257. }
  258. }