BxBaseRecommendation.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. if(empty($aParams['validate']) || !is_array($aParams['validate'])) {
  82. $aItems = [];
  83. if(!defined('BX_API_PAGE'))
  84. $aItems = $this->_oDb->get($iProfileId, $this->_aObject['id'], $aParams);
  85. $aData = [];
  86. foreach($aItems as $iId => $iCount) {
  87. $aItem = $this->getCodeItem($iId, $iCount);
  88. $aItem['id'] = $iId;
  89. $aData[] = $aItem;
  90. }
  91. }
  92. else
  93. $aData = $this->validate($iProfileId, $aParams);
  94. return [
  95. 'request_url' => '',
  96. 'data' => $aData,
  97. 'params' => [
  98. 'start' => $iStart,
  99. 'per_page' => $iPerPage,
  100. ],
  101. ];
  102. }
  103. public function validate($iProfileId = 0, $aParams = [])
  104. {
  105. $aItems = $this->_oDb->get($iProfileId, $this->_aObject['id'], array_merge($aParams, [
  106. 'start' => 0,
  107. 'per_page' => count($aParams['validate'])
  108. ]));
  109. return sort(array_keys($aItems)) == sort($aParams['validate']) ? 'valid' : 'invalid';
  110. }
  111. }
  112. /** @} */