BxBaseRecommendation.php 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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, $aParams = [])
  26. {
  27. $sResult = '';
  28. if(!$iProfileId)
  29. $iProfileId = $this->_iProfileId;
  30. $iStart = !empty($aParams['start']) ? (int)$aParams['start'] : 0;
  31. $iPerPage = !empty($aParams['per_page']) ? (int)$aParams['per_page'] : $this->_iPerPageDefault;
  32. $aItems = $this->_oDb->get($iProfileId, $this->_aObject['id'], array_merge($aParams, ['per_page' => $iPerPage + 1]));
  33. if(empty($aItems) || !is_array($aItems))
  34. return $sResult;
  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, $aParams = [])
  59. {
  60. if(!$iProfileId)
  61. $iProfileId = $this->_iProfileId;
  62. $iStart = !empty($aParams['start']) ? (int)$aParams['start'] : 0;
  63. $iPerPage = !empty($aParams['per_page']) ? (int)$aParams['per_page'] : $this->_iPerPageDefault;
  64. $aItems = [];
  65. if (!defined('BX_API_PAGE'))
  66. $aItems = $this->_oDb->get($iProfileId, $this->_aObject['id'], $aParams);
  67. $aData = [];
  68. foreach($aItems as $iId => $iCount)
  69. $aData[] = $this->getCodeItem($iId, $iCount);
  70. return [
  71. 'request_url' => '',
  72. 'data' => $aData,
  73. 'params' => [
  74. 'start' => $iStart,
  75. 'per_page' => $iPerPage,
  76. ],
  77. ];
  78. }
  79. }
  80. /** @} */