Pager.class.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. <?php
  2. /**
  3. * @author Ironpilot
  4. * @copyright Copywrite (c) 2011, STAPLE CODE
  5. *
  6. * This file is part of the STAPLE Framework.
  7. *
  8. * The STAPLE Framework is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published by the
  10. * Free Software Foundation, either version 3 of the License, or (at your option)
  11. * any later version.
  12. *
  13. * The STAPLE Framework is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  15. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for
  16. * more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public License
  19. * along with the STAPLE Framework. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. class Staple_Pager
  23. {
  24. /**
  25. * Total number of items in the paged set.
  26. * @var int
  27. */
  28. protected $total;
  29. /**
  30. * The current page of the item set.
  31. * @var int
  32. */
  33. protected $page = 1;
  34. /**
  35. * Number of items listed on each page.
  36. * @var int
  37. */
  38. protected $itemsPerPage = 10;
  39. /**
  40. * Number of pages to list before the current page.
  41. * @var int
  42. */
  43. protected $pagesBeforeCurrent;
  44. /**
  45. * Number of pages to list after the current page.
  46. * @var int
  47. */
  48. protected $pagesAfterCurrent;
  49. /**
  50. * An array that contains additional variables for the paging function.
  51. * @var array[string]
  52. */
  53. protected $pageVariables = array();
  54. /**
  55. * A switch to disable the item amount selector field.
  56. * @var bool
  57. */
  58. protected $displayItemAmountSelector = true;
  59. /**
  60. * The default item amount selections.
  61. * @var array[int]
  62. */
  63. protected $itemAmountSelections = array(5,10,20,50,100);
  64. /**
  65. * The constructor loads values into the pager.
  66. * @param int $itemsPerPage
  67. * @param int $total
  68. * @param int $currentPage
  69. * @param int $pageBuffer
  70. */
  71. function __construct($itemsPerPage = NULL, $total = NULL, $currentPage = NULL, $pageBuffer = NULL)
  72. {
  73. if(isset($itemsPerPage))
  74. {
  75. $this->setItemsPerPage($itemsPerPage);
  76. }
  77. if(isset($total))
  78. {
  79. $this->setTotal($total);
  80. }
  81. if(isset($currentPage))
  82. {
  83. $this->setPage($currentPage);
  84. }
  85. if(isset($pageBuffer))
  86. {
  87. $this->setPageBuffer($pageBuffer);
  88. }
  89. }
  90. /**
  91. * Returns the displayPaging() function.
  92. */
  93. public function __toString()
  94. {
  95. return $this->displayPaging();
  96. }
  97. /**
  98. * Returns the item number for the starting item in the result set. Useful for filling in the LIMIT clause in MySQL.
  99. */
  100. public function getStartingItem()
  101. {
  102. $start = (($this->page - 1) * $this->itemsPerPage);
  103. if($start > 0)
  104. {
  105. return $start;
  106. }
  107. else
  108. {
  109. return 0;
  110. }
  111. }
  112. /**
  113. * An Alias of self::getLastPage()
  114. */
  115. public function getNumberOfPages()
  116. {
  117. return $this->getLastPage();
  118. }
  119. /**
  120. * Returns the ending page of the set.
  121. * @return int
  122. */
  123. public function getLastPage()
  124. {
  125. if(($this->total % $this->itemsPerPage) == 0)
  126. {
  127. return (int)($this->total / $this->itemsPerPage);
  128. }
  129. else
  130. {
  131. return (int)ceil($this->total / $this->itemsPerPage);
  132. }
  133. }
  134. /**
  135. * Returns an array with the page numbers.
  136. */
  137. public function getPages()
  138. {
  139. $start = 1;
  140. if(isset($this->pagesBeforeCurrent))
  141. {
  142. if(($this->page - $this->pagesBeforeCurrent) > 0)
  143. {
  144. $start = $this->page - $this->pagesBeforeCurrent;
  145. }
  146. }
  147. $end = $this->getNumberOfPages();
  148. if(isset($this->pagesAfterCurrent))
  149. {
  150. if(($this->page + $this->pagesAfterCurrent) < $end)
  151. {
  152. $end = $this->page + $this->pagesAfterCurrent;
  153. }
  154. }
  155. $pages = array();
  156. for($i = $start; $i <= $end; $i++)
  157. {
  158. $pages[] = $i;
  159. }
  160. return $pages;
  161. }
  162. //---------------------------------------------Getters and Setters---------------------------------------------
  163. /**
  164. * @return the $total
  165. */
  166. public function getTotal()
  167. {
  168. return $this->total;
  169. }
  170. /**
  171. * @param int $total
  172. */
  173. public function setTotal($total)
  174. {
  175. $this->total = $total;
  176. if($this->getStartingItem() > $total)
  177. {
  178. $this->setPage($this->getLastPage());
  179. }
  180. return $this;
  181. }
  182. /**
  183. * Alias of getPage()
  184. * @see Staple_Pager::getPage()
  185. */
  186. public function getCurrentPage()
  187. {
  188. return $this->getPage();
  189. }
  190. /**
  191. * @return the $page
  192. */
  193. public function getPage()
  194. {
  195. return $this->page;
  196. }
  197. /**
  198. * @param int $page
  199. */
  200. public function setPage($page)
  201. {
  202. if(isset($this->total) && ($page*$this->itemsPerPage) > $this->total)
  203. {
  204. $this->page = $this->getLastPage();
  205. }
  206. elseif ($page <= 0)
  207. {
  208. $this->page = 1;
  209. }
  210. else
  211. {
  212. $this->page = $page;
  213. }
  214. return $this;
  215. }
  216. /**
  217. * @return the $itemsPerPage
  218. */
  219. public function getItemsPerPage()
  220. {
  221. return (int)$this->itemsPerPage;
  222. }
  223. /**
  224. * @param int $itemsPerPage
  225. */
  226. public function setItemsPerPage($itemsPerPage)
  227. {
  228. //@todo recalculate the current page.
  229. $this->itemsPerPage = $itemsPerPage;
  230. return $this;
  231. }
  232. /**
  233. * @return bool $displayItemAmountSelector
  234. */
  235. public function getDisplayItemAmountSelector()
  236. {
  237. return $this->displayItemAmountSelector;
  238. }
  239. /**
  240. * @return array[int] $itemAmountSelections
  241. */
  242. public function getItemAmountSelections()
  243. {
  244. return $this->itemAmountSelections;
  245. }
  246. /**
  247. * @param boolean $displayItemAmountSelector
  248. */
  249. public function setDisplayItemAmountSelector($displayItemAmountSelector)
  250. {
  251. $this->displayItemAmountSelector = (bool)$displayItemAmountSelector;
  252. return $this;
  253. }
  254. /**
  255. * @param array[int] $itemAmountSelections
  256. */
  257. public function setItemAmountSelections(array $itemAmountSelections)
  258. {
  259. $this->itemAmountSelections = array();
  260. foreach ($itemAmountSelections as $value)
  261. {
  262. if(is_int($value))
  263. {
  264. $this->itemAmountSelections[] = $value;
  265. }
  266. }
  267. return $this;
  268. }
  269. /**
  270. * Add a single entry to the selection list.
  271. * @param int $amount
  272. * @return Staple_Pager
  273. */
  274. public function addItemAmountSelection($amount)
  275. {
  276. $this->itemAmountSelections[] = (int)$amount;
  277. sort($this->itemAmountSelections);
  278. return $this;
  279. }
  280. /**
  281. * @return the $pagesBeforeCurrent
  282. */
  283. public function getPagesBeforeCurrent()
  284. {
  285. return $this->pagesBeforeCurrent;
  286. }
  287. /**
  288. * @return the $pagesAfterCurrent
  289. */
  290. public function getPagesAfterCurrent()
  291. {
  292. return $this->pagesAfterCurrent;
  293. }
  294. /**
  295. * @param int $pagesBeforeCurrent
  296. */
  297. public function setPagesBeforeCurrent($pagesBeforeCurrent)
  298. {
  299. $this->pagesBeforeCurrent = $pagesBeforeCurrent;
  300. return $this;
  301. }
  302. /**
  303. * @param int $pagesAfterCurrent
  304. */
  305. public function setPagesAfterCurrent($pagesAfterCurrent)
  306. {
  307. $this->pagesAfterCurrent = $pagesAfterCurrent;
  308. return $this;
  309. }
  310. /**
  311. * Sets the $pagesBeforeCurrent and $pagesAfterCurrent to the same value.
  312. * @param int $pages
  313. */
  314. public function setPageBuffer($pages)
  315. {
  316. $this->pagesAfterCurrent = $pages;
  317. $this->pagesBeforeCurrent = $pages;
  318. return $this;
  319. }
  320. /**
  321. * This function allows the user to add additional variables to the GET string of the page links.
  322. * The array should be associative with it formated such that variabelName=>value.
  323. * @param array $vars
  324. * @return Staple_Pager
  325. */
  326. public function setVariables(array $vars)
  327. {
  328. if(array_key_exists('items', $vars))
  329. {
  330. unset($vars['items']);
  331. }
  332. $this->pageVariables = $vars;
  333. return $this;
  334. }
  335. /**
  336. * Add a single variable to the page GET string
  337. * @param string $varname
  338. * @param string $value
  339. */
  340. public function addVariable($varname,$value)
  341. {
  342. $this->pageVariables[$varname] = $value;
  343. return $this;
  344. }
  345. /**
  346. * Remove a single variable from the GET string.
  347. * @param string $varname
  348. */
  349. public function removeVariable($varname)
  350. {
  351. if(array_key_exists($varname, $this->pageVariables))
  352. {
  353. unset($this->pageVariables[$varname]);
  354. return true;
  355. }
  356. else
  357. {
  358. return false;
  359. }
  360. }
  361. /**
  362. * This function is a temporary fix until the changes are completed to Staple_Route.
  363. * @param string $action
  364. * @deprecated
  365. */
  366. public function displayPaging($action = NULL)
  367. {
  368. //Setup Link Variables
  369. $linkVars = array_merge($_GET,$this->pageVariables);
  370. //These two variables would be overridden anyway.
  371. if(array_key_exists('items', $linkVars)) unset($linkVars['items']);
  372. if(array_key_exists('page', $linkVars)) unset($linkVars['page']);
  373. //Set a default link action location if none is submitted.
  374. if($action == NULL)
  375. {
  376. $action = Staple_Main::getRoute();
  377. }
  378. $buffer = "<div class=\"staple_pager\">\n<div class=\"staple_pager_pages\">\nPage: ";
  379. $pages = $this->getPages();
  380. if(count($pages) > 1)
  381. {
  382. if($this->getCurrentPage() == 1)
  383. {
  384. $buffer .= ' &lt;&lt; - &lt; ';
  385. }
  386. elseif($this->getCurrentPage() > 1)
  387. {
  388. $buffer .= '<a href="'.Staple_Link::get($action,array_merge($linkVars,array('page'=>1))).'">&lt;&lt;</a> - ';
  389. $buffer .= '<a href="'.Staple_Link::get($action,array_merge($linkVars,array('page'=>($this->getCurrentPage()-1)))).'">&lt;</a> ';
  390. }
  391. if($pages[0] != 1)
  392. {
  393. $buffer .= '... ';
  394. }
  395. foreach($pages as $page)
  396. {
  397. if($this->getCurrentPage() == $page)
  398. {
  399. $buffer .= '<span class="currentpage">'.((int)$page).'</span> ';
  400. }
  401. else
  402. {
  403. $buffer .= '<a href="'.Staple_Link::get($action,array_merge($linkVars,array('page'=>(int)$page))).'">'.((int)$page).'</a> ';
  404. }
  405. }
  406. if($pages[count($pages)-1] != $this->getNumberOfPages())
  407. {
  408. $buffer .= '... ';
  409. }
  410. if($this->getCurrentPage() == $this->getNumberOfPages())
  411. {
  412. $buffer .= ' &gt; - &gt;&gt; ';
  413. }
  414. else
  415. {
  416. $buffer .= '<a href="'.Staple_Link::get($action,array_merge($linkVars,array('page'=>($this->getCurrentPage()+1)))).'">&gt;</a> - ';
  417. $buffer .= '<a href="'.Staple_Link::get($action,array_merge($linkVars,array('page'=>$this->getNumberOfPages()))).'">&gt;&gt;</a> ';
  418. }
  419. }
  420. else
  421. {
  422. $buffer .= '<< - < 1 > - >>';
  423. }
  424. $buffer .= "</div>\n";
  425. if($this->getDisplayItemAmountSelector() === true)
  426. {
  427. $buffer .= "<div class=\"staple_pager_items\">\n";
  428. $buffer .= 'Items Per Page: <select onChange="window.location=\''.Staple_Link::get($action,array_merge($linkVars,array('page'=>1)))."&items='+this.value\">\n";
  429. foreach($this->getItemAmountSelections() as $value)
  430. {
  431. $selected = '';
  432. if($this->getItemsPerPage() == $value)
  433. {
  434. $selected = ' selected';
  435. }
  436. $buffer .= '<option value="'.$value.'"'.$selected.'>'.$value."</option>\n";
  437. }
  438. $buffer .= "</select>\n";
  439. $buffer .= "</div>\n";
  440. }
  441. $buffer .= "</div>\n";
  442. return $buffer;
  443. }
  444. }
  445. ?>