BxBaseRecommendation.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. * Recommendation representation.
  11. * @see BxDolRecommendation
  12. */
  13. class BxBaseRecommendation extends BxDolRecommendation
  14. {
  15. protected $_oTemplate;
  16. protected $_sClassWrapper;
  17. public function __construct ($aOptions, $oTemplate)
  18. {
  19. parent::__construct ($aOptions);
  20. if ($oTemplate)
  21. $this->_oTemplate = $oTemplate;
  22. else
  23. $this->_oTemplate = BxDolTemplate::getInstance();
  24. $this->_sClassWrapper = '';
  25. }
  26. public function getCode($iProfileId = 0, $aParams = [])
  27. {
  28. $sResult = '';
  29. if(!$iProfileId)
  30. $iProfileId = $this->_iProfileId;
  31. $bShowcaseView = (bool)($aParams['showcase'] ?? false);
  32. $iStart = !empty($aParams['start']) ? (int)$aParams['start'] : 0;
  33. $iPerPage = !empty($aParams['per_page']) ? (int)$aParams['per_page'] : $this->{'_iPerPageDefault' . ($bShowcaseView ? 'Showcase' : '')};
  34. $aItems = $this->_oDb->get($iProfileId, $this->_aObject['id'], array_merge($aParams, ['per_page' => $iPerPage + ($bShowcaseView ? 0 : 1)]));
  35. if(empty($aItems) || !is_array($aItems))
  36. return $sResult;
  37. $sClassWrapper = $this->_sClassWrapper;
  38. $sPaginate = '';
  39. if(!$bShowcaseView) {
  40. $iItems = count($aItems);
  41. $oPaginate = new BxTemplPaginate([
  42. 'on_change_page' => "return !loadDynamicBlockAutoPaginate(this, '{start}', '{per_page}')",
  43. 'num' => $iItems,
  44. 'start' => $iStart,
  45. 'per_page' => $iPerPage,
  46. ]);
  47. $sPaginate = $oPaginate->getSimplePaginate();
  48. if($iItems > $iPerPage)
  49. array_pop($aItems);
  50. }
  51. else {
  52. $sClassWrapper = 'bx-recommendation-showcase';
  53. $this->_oTemplate->addCss([BX_DIRECTORY_PATH_PLUGINS_PUBLIC . 'flickity/|flickity.css']);
  54. $this->_oTemplate->addJs(['flickity/flickity.pkgd.min.js', 'modules/base/general/js/|showcase.js']);
  55. }
  56. foreach($aItems as $iId => $iCount)
  57. $sResult .= $this->getCodeItem($iId, $iCount);
  58. return $this->_oTemplate->parseHtmlByName('recommendation_block.html', [
  59. 'class' => $sClassWrapper,
  60. 'content' => $sResult,
  61. 'bx_if:show_paginate' => [
  62. 'condition' => !$bShowcaseView && !empty($sPaginate),
  63. 'content' => [
  64. 'paginate' => $sPaginate
  65. ]
  66. ],
  67. 'bx_if:show_showcase' => [
  68. 'condition' => $bShowcaseView,
  69. 'content' => [
  70. 'class' => $sClassWrapper
  71. ]
  72. ]
  73. ]);
  74. }
  75. public function getCodeAPI($iProfileId = 0, $aParams = [])
  76. {
  77. if(!$iProfileId)
  78. $iProfileId = $this->_iProfileId;
  79. $iStart = !empty($aParams['start']) ? (int)$aParams['start'] : 0;
  80. $iPerPage = !empty($aParams['per_page']) ? (int)$aParams['per_page'] : $this->_iPerPageDefault;
  81. $bForceGetData = false;
  82. if(($sK = 'force_get_data') && isset($aParams[$sK])) {
  83. $bForceGetData = (bool)$aParams[$sK];
  84. unset($aParams[$sK]);
  85. }
  86. $aItems = [];
  87. if(!defined('BX_API_PAGE') || $bForceGetData)
  88. $aItems = $this->_oDb->get($iProfileId, $this->_aObject['id'], $aParams);
  89. $aData = [];
  90. foreach($aItems as $iId => $iCount) {
  91. $aItem = $this->getCodeItem($iId, $iCount);
  92. $aItem['id'] = $iId;
  93. $aData[] = $aItem;
  94. }
  95. return [
  96. 'request_url' => '',
  97. 'data' => $aData,
  98. 'params' => [
  99. 'start' => $iStart,
  100. 'per_page' => $iPerPage,
  101. ],
  102. ];
  103. }
  104. }
  105. /** @} */