BxBaseRecommendation.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. }
  25. public function getCode($iProfileId = 0, $iStart = 0, $iPerPage = 0)
  26. {
  27. $sResult = '';
  28. if(!$iProfileId)
  29. $iProfileId = $this->_iProfileId;
  30. if(!$iPerPage)
  31. $iPerPage = $this->_iPerPageDefault;
  32. $aItems = $this->_oDb->get($iProfileId, $this->_aObject['id'], $iStart, $iPerPage + 1);
  33. if(empty($aItems) || !is_array($aItems))
  34. return '';
  35. $iItems = count($aItems);
  36. $oPaginate = new BxTemplPaginate([
  37. 'on_change_page' => "return !loadDynamicBlockAutoPaginate(this, '{start}', '{per_page}')",
  38. 'num' => $iItems,
  39. 'start' => $iStart,
  40. 'per_page' => $iPerPage,
  41. ]);
  42. $sPaginate = $oPaginate->getSimplePaginate();
  43. if($iItems > $iPerPage)
  44. array_pop($aItems);
  45. foreach($aItems as $iId => $iCount)
  46. $sResult .= $this->getCodeItem($iId, $iCount);
  47. return $this->_oTemplate->parseHtmlByName('recommendation_block.html', [
  48. 'class' => $this->_sClassWrapper,
  49. 'content' => $sResult,
  50. 'bx_if:show_paginate' => [
  51. 'condition' => !empty($sPaginate),
  52. 'content' => [
  53. 'paginate' => $sPaginate
  54. ]
  55. ]
  56. ]);
  57. }
  58. public function getCodeAPI($iProfileId = 0, $iStart = 0, $iPerPage = 0)
  59. {
  60. if(!$iProfileId)
  61. $iProfileId = $this->_iProfileId;
  62. if(!$iPerPage)
  63. $iPerPage = $this->_iPerPageDefault;
  64. $aItems = $this->_oDb->get($iProfileId, $this->_aObject['id'], $iStart, $iPerPage);
  65. $aData = [];
  66. foreach($aItems as $iId => $iCount)
  67. $aData[] = $this->getCodeItem($iId, $iCount);
  68. return [
  69. 'request_url' => '',
  70. 'data' => $aData,
  71. 'params' => [
  72. 'per_page' => $iStart,
  73. 'start' => $iPerPage,
  74. ],
  75. ];
  76. }
  77. }
  78. /** @} */