BxDolPaginate.php 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaCore UNA Core
  7. * @{
  8. */
  9. define('BX_DOL_PAGINATE_PER_PAGE_DEFAULT', 10);
  10. /**
  11. * Paginage for any content.
  12. * It is used to create paginate, configuring it via input parameters.
  13. * Paginate don't support total number of pages, moreover it is not recommended to count all records - it slows down the site.
  14. * To correctly determine last page we need to pass number of available records on the current page plus one - so we always know if next page is available.
  15. *
  16. *
  17. * Two types of paginate presentation is supported:
  18. * - getPaginate() - to get default paginate, it is better to use it on the whole page.
  19. * - getSimplePaginate() - to get limited paginate, it is better to use in some boxes, where availabel space is limited or for ajax paginate.
  20. *
  21. *
  22. * The list of available input parameters:
  23. *
  24. *
  25. * Parameters:
  26. * - start - position of the first item.
  27. * - num - number of available items on the page, it should be number of items per page + 1 (+1 is needed to correctly determine last page). It is possible to set this value automatically @see setNumFromDataArray.
  28. * - per_page - number of items displayed on the page.
  29. * - page_url - page URL to go through pages, special markers are automatically replaced.
  30. * - on_change_page - JavaScript code to be called on change page.
  31. * - info - display info.
  32. * - view_all_url - URL for 'view all' page. This url is not showed by default. It is convinient to use with @see getSimplePaginate.
  33. * - view_all_caption - optional caption for 'view all' link.
  34. *
  35. *
  36. * Available markers to replace in 'page_url' and 'on_change_page' parameters:
  37. * - {per_page} - current number of items to display per page.
  38. * - {start} - the number to display items starting from.
  39. *
  40. *
  41. * Example of usage:
  42. * @code
  43. * $oPaginate = new BxDolPaginate(array(
  44. * 'start' => 0,
  45. * 'num' => 11,
  46. * 'per_page' => 10,
  47. * 'on_change_page' => 'changePage({start}, {per_page})'
  48. * ));
  49. * $oPaginate->getPaginate();
  50. * @endcode
  51. *
  52. *
  53. * Memberships/ACL:
  54. * Doesn't depend on user's membership.
  55. *
  56. *
  57. * Alerts:
  58. * no alerts available
  59. *
  60. */
  61. abstract class BxDolPaginate extends BxDol
  62. {
  63. protected $_aParams; ///< an array with initially provided params
  64. protected $_iStart; ///< start display items from this number
  65. protected $_iPerPage; ///< results per page
  66. protected $_iNum; ///< available results, you need to query per_page + 1 results, so paginate can determine last page
  67. protected $_iTotal; ///< total number of results. (Optional)
  68. protected $_bTotal; ///< show displayed items info with total value
  69. protected $_sPageUrl; ///< page url of next/prev page, special markers will be replaced here automatically
  70. protected $_sOnChangePage; ///< on click of next/prev page, special markers will be replaced here automatically
  71. protected $_bInfo; ///< show displayed items info
  72. protected $_sViewAllUrl; ///< view "all results" url, for "simple" paginate
  73. protected $_sViewAllCaption; ///< 'view all' link caption
  74. /**
  75. * Constructor
  76. */
  77. public function __construct($aParams)
  78. {
  79. parent::__construct();
  80. if (isset($aParams['count']))
  81. trigger_error ('Paginate "count" is deprecated - use "num" instead: ' . get_class($this), E_USER_ERROR);
  82. //--- Main settings ---//
  83. $this->_aParams = $aParams;
  84. $this->_iStart = isset($aParams['start']) && (int)$aParams['start'] > 0 ? (int)$aParams['start'] : 0;
  85. $this->_iPerPage = isset($aParams['per_page']) && (int)$aParams['per_page'] > 0 ? (int)$aParams['per_page'] : BX_DOL_PAGINATE_PER_PAGE_DEFAULT;
  86. $this->_iNum = isset($aParams['num']) ? (int)$aParams['num'] : 0;
  87. $this->_iTotal = isset($aParams['total']) ? (int)$aParams['total'] : 0;
  88. $this->_bTotal = $this->_iTotal > 0;
  89. $this->_bInfo = isset($aParams['info']) ? (bool)$aParams['info'] : true;
  90. $this->_sViewAllUrl = isset($aParams['view_all_url']) ? $aParams['view_all_url'] : false;
  91. $this->_sViewAllCaption = isset($aParams['view_all_caption']) ? $aParams['view_all_caption'] : _t('_sys_paginate_view_all');
  92. // page url
  93. $this->_sPageUrl = isset($aParams['page_url']) ? $aParams['page_url'] : BX_DOL_URL_ROOT;
  94. // on click (js mode)
  95. $this->_sOnChangePage = isset($aParams['on_change_page']) ? $aParams['on_change_page'] : '';
  96. }
  97. /**
  98. * Get number of available results per page. If it is not last page,
  99. * then this number is number of result per page plus one.
  100. * @return integer
  101. */
  102. public function getNum()
  103. {
  104. return $this->_iNum;
  105. }
  106. /**
  107. * Set number of available items on the page directly from data array.
  108. * Since data array should contain additional record - we will pop last item from array automatically.
  109. * @return nothing.
  110. */
  111. public function setNumFromDataArray(&$a, $isAutoPopLastElement = true)
  112. {
  113. if ($a && is_array($a))
  114. $this->_iNum = count($a);
  115. else
  116. $this->_iNum = 0;
  117. if ($this->_iNum > $this->_iPerPage && $isAutoPopLastElement)
  118. array_pop($a);
  119. }
  120. /**
  121. * Set number of available items on the page,
  122. * it should be number of items per page + 1 (+1 is needed to correctly determine last page).
  123. * It is possible to set this value automatically @see setNumFromDataArray.
  124. * @param $i positive integer.
  125. * @return true on success or false if $i param isn't correct.
  126. */
  127. public function setNum($i)
  128. {
  129. if ($i >= 0) {
  130. $this->_iNum = (int)$i;
  131. return true;
  132. }
  133. return false;
  134. }
  135. /**
  136. * Position, the data is showing from.
  137. * @return integer.
  138. */
  139. public function getStart()
  140. {
  141. return $this->_iStart;
  142. }
  143. /**
  144. * Set the starting position, the data is showing from.
  145. * @param $i positive integer.
  146. * @return true on siccess or false on error.
  147. */
  148. public function setStart($i)
  149. {
  150. if ($i >= 0) {
  151. $this->_iStart = (int)$i;
  152. return true;
  153. }
  154. return false;
  155. }
  156. /**
  157. * Get number of records per page.
  158. * @return integer.
  159. */
  160. public function getPerPage()
  161. {
  162. return $this->_iPerPage;
  163. }
  164. /**
  165. * Set number of records per page.
  166. * @param $i positive integer.
  167. * @return integer.
  168. */
  169. public function setPerPage($i)
  170. {
  171. if ((int)$i > 0) {
  172. $this->_iPerPage = $i;
  173. return true;
  174. }
  175. return false;
  176. }
  177. /**
  178. * Set string to pass to 'onclick' for change page button.
  179. * The following markers are replaced automatically:
  180. * - {per_page}
  181. * - {start}
  182. *
  183. * @param $i positive integer.
  184. * @return integer.
  185. */
  186. public function setOnChangePage($s)
  187. {
  188. $this->_sOnChangePage = $s;
  189. }
  190. /**
  191. * @return true if previous page is available, or false if not
  192. */
  193. public function isPrevAvail ()
  194. {
  195. return $this->_iStart > 0 ? true : false;
  196. }
  197. /**
  198. * @return true if next page is available, or false if not
  199. */
  200. public function isNextAvail ()
  201. {
  202. return $this->_iNum > $this->_iPerPage ? true : false;
  203. }
  204. public function isLoadMoreAvail ()
  205. {
  206. return $this->isNextAvail();
  207. }
  208. protected function _getReplacement()
  209. {
  210. return [
  211. 'start' => $this->_iStart,
  212. 'per_page' => $this->_iPerPage,
  213. ];
  214. }
  215. protected function _getReplacementPrev()
  216. {
  217. return array_merge($this->_getReplacement(), [
  218. 'start' => ($iStart = $this->_iStart - $this->_iPerPage) > 0 ? $iStart : 0
  219. ]);
  220. }
  221. protected function _getReplacementNext()
  222. {
  223. return array_merge($this->_getReplacement(), [
  224. 'start' => $this->_iStart + $this->_iPerPage
  225. ]);
  226. }
  227. protected function _getReplacementLoadMore()
  228. {
  229. return $this->_getReplacementNext();
  230. }
  231. protected function _getPageChangeUrl($aMarkers)
  232. {
  233. return bx_replace_markers($this->_sPageUrl, $aMarkers);
  234. }
  235. protected function _getPageChangeOnClick($aMarkers)
  236. {
  237. return !empty($this->_sOnChangePage) ? 'onclick="javascript:' . bx_replace_markers($this->_sOnChangePage, $aMarkers) . '; return false;"' : '';
  238. }
  239. }
  240. /** @} */