BxBaseRecommendation.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. if(empty($aParams['validate']) || !is_array($aParams['validate'])) {
  65. $aItems = [];
  66. if(!defined('BX_API_PAGE'))
  67. $aItems = $this->_oDb->get($iProfileId, $this->_aObject['id'], $aParams);
  68. $aData = [];
  69. foreach($aItems as $iId => $iCount)
  70. $aData[] = $this->getCodeItem($iId, $iCount);
  71. }
  72. else
  73. $aData = $this->validate($iProfileId, $aParams);
  74. return [
  75. 'request_url' => '',
  76. 'data' => $aData,
  77. 'params' => [
  78. 'start' => $iStart,
  79. 'per_page' => $iPerPage,
  80. ],
  81. ];
  82. }
  83. public function validate($iProfileId = 0, $aParams = [])
  84. {
  85. $aItems = $this->_oDb->get($iProfileId, $this->_aObject['id'], array_merge($aParams, [
  86. 'start' => 0,
  87. 'per_page' => count($aParams['validate'])
  88. ]));
  89. return array_keys($aItems) == $aParams['validate'] ? 'valid' : 'invalid';
  90. }
  91. }
  92. /** @} */