BxBaseCategory.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. * Category objects representation.
  11. * @see BxDolCategory
  12. */
  13. class BxBaseCategory extends BxDolCategory
  14. {
  15. protected $_oTemplate;
  16. protected $_oModule;
  17. protected $_bModule;
  18. protected $_sBrowseAllUrl;
  19. protected $_sBrowseUrl;
  20. public function __construct ($aObject, $oTemplate = null)
  21. {
  22. parent::__construct ($aObject);
  23. if ($oTemplate)
  24. $this->_oTemplate = $oTemplate;
  25. else
  26. $this->_oTemplate = BxDolTemplate::getInstance();
  27. $this->_oModule = null;
  28. if(!empty($this->_aObject['module']))
  29. $this->_oModule = BxDolModule::getInstance($this->_aObject['module']);
  30. $this->_bModule = $this->_oModule !== null;
  31. $this->_sBrowseAllUrl = '';
  32. $this->_sBrowseUrl = bx_append_url_params('searchKeyword.php', [
  33. 'cat' => '{category}',
  34. 'keyword' => '{keyword}'
  35. ], true, ['{category}', '{keyword}']) . '{sections}' . '{context}';
  36. if($this->_bModule) {
  37. $CNF = &$this->_oModule->_oConfig->CNF;
  38. if(!empty($CNF['URL_HOME']))
  39. $this->_sBrowseAllUrl = BxDolPermalinks::getInstance()->permalink($CNF['URL_HOME']);
  40. if(!empty($CNF['URL_CATEGORY']))
  41. $this->_sBrowseUrl = bx_append_url_params(BxDolPermalinks::getInstance()->permalink($CNF['URL_CATEGORY']), [
  42. 'category' => '{keyword}'
  43. ], true, ['{keyword}']);
  44. }
  45. }
  46. public function getCategoryIcon($sValue)
  47. {
  48. return 'star';
  49. }
  50. public function getCategoryTitle($sValue)
  51. {
  52. $a = BxDolForm::getDataItems($this->_aObject['list_name']);
  53. if (!$a || !isset($a[$sValue]))
  54. return '';
  55. return $a[$sValue];
  56. }
  57. public function getCategoryUrl($sValue, $aParams = [])
  58. {
  59. $s = BX_DOL_URL_ROOT . bx_replace_markers($this->_sBrowseUrl, [
  60. 'category' => rawurlencode($this->getObjectName()),
  61. 'keyword' => rawurlencode($sValue),
  62. 'sections' => $this->_aObject['search_object'] ? '&section[]=' . rawurlencode($this->_aObject['search_object']) : '',
  63. 'context' => isset($aParams['context_id']) ? '&context_id=' . $aParams['context_id'] : ''
  64. ]);
  65. if (bx_is_api())
  66. return bx_api_get_relative_url($s);
  67. return $s;
  68. }
  69. /**
  70. * Get link to list all items with the same category
  71. * @param $sName category title
  72. * @param $sValue category value
  73. * @return category name wrapped with A tag
  74. */
  75. public function getCategoryLink($sName, $sValue)
  76. {
  77. $sUrl = $this->getCategoryUrl($sValue);
  78. return '<a class="bx-category-link" href="' . $sUrl . '">' . $sName . '</a>';
  79. }
  80. /**
  81. * Get all categories list
  82. * @param $bDisplayEmptyCats display categories with no items, true by default
  83. * @return categories list html
  84. */
  85. public function getCategoriesList($bDisplayEmptyCats = true, $bAsArray = false)
  86. {
  87. $aContextInfo = bx_get_page_info();
  88. $mProfileContextId = false;
  89. if ($aContextInfo !== false)
  90. $mProfileContextId = $aContextInfo['context_profile_id'];
  91. $a = BxDolForm::getDataItems($this->_aObject['list_name']);
  92. if (!$a)
  93. return $bAsArray ? [] : '';
  94. $aVars = [
  95. 'bx_repeat:cats' => [],
  96. 'bx_if:show_all' => [
  97. 'condition' => $this->_sBrowseAllUrl != '',
  98. 'content' => [
  99. 'url' => $this->_sBrowseAllUrl,
  100. 'name' => _t('all')
  101. ]
  102. ]
  103. ];
  104. foreach ($a as $sValue => $sName) {
  105. if (!is_numeric($sValue) && !$sValue)
  106. continue;
  107. $iNum = $this->getItemsNum($sValue, ['context_id' => $mProfileContextId]);
  108. if (!$bDisplayEmptyCats && !$iNum)
  109. continue;
  110. $aVars['bx_repeat:cats'][] = [
  111. 'url' => $this->getCategoryUrl($sValue, ($mProfileContextId ? ['context_id' => $mProfileContextId] : [])),
  112. 'name' => $sName,
  113. 'value' => $sValue,
  114. 'num' => $iNum,
  115. 'icon' => $this->getCategoryIcon($sValue),
  116. 'selected_class' => $sValue == bx_get('category') ? 'bx-category-list-item-selected' : '',
  117. ];
  118. }
  119. if(bx_is_api())
  120. return [bx_api_get_block('categories_list', $aVars['bx_repeat:cats'])];
  121. if ($bAsArray)
  122. return $aVars;
  123. if (!$aVars['bx_repeat:cats'])
  124. return '';
  125. return $this->_oTemplate->parseHtmlByName('category_list.html', $aVars);
  126. }
  127. }
  128. /** @} */