BxBasePaginate.php 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 UnaBaseView UNA Base Representation Classes
  7. * @{
  8. */
  9. /**
  10. * @see BxDolPaginate
  11. */
  12. class BxBasePaginate extends BxDolPaginate
  13. {
  14. protected static $_isCssAdded = false;
  15. protected $_oTemplate;
  16. protected $_sPaginateClass; ///< add this class to whole paginate container div
  17. protected $_sButtonsClass; ///< add this class to buttons class attribute
  18. protected $_aButtonsConf; ///< buttons configuration
  19. public function __construct($aParams, $oTemplate = null)
  20. {
  21. parent::__construct($aParams);
  22. if ($oTemplate)
  23. $this->_oTemplate = $oTemplate;
  24. else
  25. $this->_oTemplate = BxDolTemplate::getInstance();
  26. $this->_sPaginateClass = $aParams['paginate_class'] ?? '';
  27. $this->_sButtonsClass = $aParams['buttons_class'] ?? '';
  28. $this->_aButtonsConf = [
  29. 'btn_1' => [
  30. 'name' => 'prev',
  31. 'active' => $aParams['button_prev'] ?? true
  32. ],
  33. 'btn_2' => [
  34. 'name' => 'next',
  35. 'active' => $aParams['button_next'] ?? true
  36. ]
  37. ];
  38. }
  39. /**
  40. * Get default paginate, it is better to use it on the whole page.
  41. * @param $iStart - @see setStart.
  42. * @param $iNum - @see setNum and @see setNumFromDataArray.
  43. * @param $iPerPage - @see setPerPage.
  44. * @return HTML string.
  45. */
  46. public function getPaginate($iStart = -1, $iNum = -1, $iPerPage = -1)
  47. {
  48. $this->setNum($iNum);
  49. if (!$this->_iNum)
  50. return '';
  51. $this->setStart($iStart);
  52. $this->setPerPage($iPerPage);
  53. if (0 == $this->getStart() && !$this->isNextAvail ())
  54. return '';
  55. $sClassAdd = ($this->_sButtonsClass ? ' ' . $this->_sButtonsClass : '');
  56. $aTmplVarsButtons = [];
  57. foreach($this->_aButtonsConf as $sTmplVar => $aConf) {
  58. if(!$aConf['active'])
  59. continue;
  60. $sName = $aConf['name'];
  61. $sClassAdd = ' bx-btn-disabled';
  62. $sLinkUrl = 'javascript:void(0);';
  63. $sLinkClick = '';
  64. if(($sMethod = bx_gen_method_name($sName)) && $this->{'is' . $sMethod . 'Avail'}()) {
  65. $aReplacementLink = $this->{'_getReplacement' . $sMethod}();
  66. $sClassAdd = '';
  67. $sLinkUrl = $this->_getPageChangeUrl($aReplacementLink);
  68. $sLinkClick = $this->_getPageChangeOnClick($aReplacementLink);
  69. }
  70. $aTmplVarsButtons[$sTmplVar] = $this->_getButton($sName, [
  71. 'class' => $sClassAdd . $sClassAdd,
  72. 'href' => $sLinkUrl,
  73. 'onclick' => $sLinkClick,
  74. ]);
  75. }
  76. $this->addCssJs();
  77. return $this->_oTemplate->parseHtmlByName('paginate.html', array_merge([
  78. 'bx_if:info' => [
  79. 'condition' => $this->_bInfo && !$this->_bTotal,
  80. 'content' => [
  81. 'text' => _t('_sys_paginate_info', $this->_iStart + 1, $this->_iStart + ($this->isNextAvail () ? $this->_iPerPage : $this->_iNum)),
  82. ],
  83. ],
  84. 'bx_if:total' => [
  85. 'condition' => $this->_bTotal,
  86. 'content' => [
  87. 'text' => _t('_sys_paginate_total', $this->_iStart + 1, $this->_iStart + ($this->isNextAvail() ? $this->_iPerPage : $this->_iNum), $this->_iTotal),
  88. ],
  89. ],
  90. 'bx_if:view_all' => [
  91. 'condition' => (bool)$this->_sViewAllUrl,
  92. 'content' => [
  93. 'lnk_url' => $this->_sViewAllUrl,
  94. 'lnk_title' => $this->_sViewAllCaption,
  95. 'lnk_content' => $this->_sViewAllCaption,
  96. ],
  97. ],
  98. 'btn_1' => '',
  99. 'btn_2' => '',
  100. 'paginate_class' => $this->_sPaginateClass,
  101. 'bx_repeat:attrs' => [
  102. ['key' => 'bx-data-start', 'value' => $this->_iStart],
  103. ['key' => 'bx-data-perpage', 'value' => $this->_iPerPage]
  104. ]
  105. ], $aTmplVarsButtons));
  106. }
  107. /**
  108. * Get limited paginate, it is better to use in some boxes, where availabel space is limited or for ajax paginate.
  109. * @param $sViewAllUrl - url to page for 'view all' link.
  110. * @param $iStart - @see setStart.
  111. * @param $iNum - @see setNum and @see setNumFromDataArray.
  112. * @param $iPerPage - @see setPerPage.
  113. * @return HTML string.
  114. */
  115. public function getSimplePaginate($sViewAllUrl = '', $iStart = -1, $iNum = -1, $iPerPage = -1)
  116. {
  117. if($sViewAllUrl)
  118. $this->_sViewAllUrl = $sViewAllUrl;
  119. if(!isset($this->_aParams['info']))
  120. $this->_bInfo = false;
  121. $this->_sButtonsClass .= ($this->_sButtonsClass ? ' ' : '') . 'bx-btn-small bx-btn-symbol-small';
  122. $this->_sPaginateClass = 'bx-paginate-simple';
  123. return $this->getPaginate($iStart, $iNum, $iPerPage);
  124. }
  125. public function getLoadMorePaginate($sViewAllUrl = '', $iStart = -1, $iNum = -1, $iPerPage = -1)
  126. {
  127. if($sViewAllUrl)
  128. $this->_sViewAllUrl = $sViewAllUrl;
  129. if(!isset($this->_aParams['info']))
  130. $this->_bInfo = false;
  131. $this->_sPaginateClass = 'bx-paginate-simple-load-more';
  132. $this->_sButtonsClass .= ($this->_sButtonsClass ? ' ' : '') . 'bx-btn-small bx-btn-symbol-small';
  133. $this->_aButtonsConf['btn_1']['active'] = false;
  134. $this->_aButtonsConf['btn_2']['name'] = 'load_more';
  135. return $this->getPaginate($iStart, $iNum, $iPerPage);
  136. }
  137. public function addCssJs ()
  138. {
  139. if(self::$_isCssAdded)
  140. return false;
  141. $this->_oTemplate->addCss('paginate.css');
  142. self::$_isCssAdded = true;
  143. return true;
  144. }
  145. protected function _getButton($sName, $aParams)
  146. {
  147. $aTmplVarsIcon = [];
  148. if(($sMethodIc = '_getButtonIcon' . bx_gen_method_name($sName)) && method_exists($this, $sMethodIc))
  149. $aTmplVarsIcon = [
  150. 'icon' => $this->$sMethodIc()
  151. ];
  152. $aTmplVarsTitle = [];
  153. if(($sMethodTtl = '_getButtonTitle' . bx_gen_method_name($sName)) && method_exists($this, $sMethodTtl))
  154. $aTmplVarsTitle = [
  155. 'title' => $this->$sMethodTtl()
  156. ];
  157. return $this->_oTemplate->parseHtmlByName('paginate_btn.html', [
  158. 'class' => 'bx-paginate-btn bx-paginate-btn-' . $sName . (!empty($aParams['class']) ? $aParams['class'] : ''),
  159. 'href' => !empty($aParams['href']) ? $aParams['href'] : 'javascript:void(0)',
  160. 'onclick' => !empty($aParams['onclick']) ? $aParams['onclick'] : '',
  161. 'bx_if:icon' => [
  162. 'condition' => $aTmplVarsIcon && is_array($aTmplVarsIcon),
  163. 'content' => $aTmplVarsIcon
  164. ],
  165. 'bx_if:title' => [
  166. 'condition' => $aTmplVarsTitle && is_array($aTmplVarsTitle),
  167. 'content' => $aTmplVarsTitle
  168. ],
  169. ]);
  170. }
  171. protected function _getButtonIconPrev()
  172. {
  173. return 'angle-double-left';
  174. }
  175. protected function _getButtonIconNext()
  176. {
  177. return 'angle-double-right';
  178. }
  179. protected function _getButtonTitleLoadMore()
  180. {
  181. return 'Load More...';
  182. }
  183. }
  184. /** @} */