BxBaseCategory.php 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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 $_sBrowseAllTitle;
  20. protected $_sBrowseUrl;
  21. public function __construct ($aObject, $oTemplate = null)
  22. {
  23. parent::__construct ($aObject);
  24. if ($oTemplate)
  25. $this->_oTemplate = $oTemplate;
  26. else
  27. $this->_oTemplate = BxDolTemplate::getInstance();
  28. $this->_oModule = null;
  29. if(!empty($this->_aObject['module']))
  30. $this->_oModule = BxDolModule::getInstance($this->_aObject['module']);
  31. $this->_bModule = $this->_oModule !== null;
  32. $this->_sBrowseAllUrl = '';
  33. $this->_sBrowseAllTitle = '_all';
  34. $this->_sBrowseUrl = bx_append_url_params('searchKeyword.php', [
  35. 'cat' => '{category}',
  36. 'keyword' => '{keyword}'
  37. ], true, ['{category}', '{keyword}']) . '{sections}' . '{context}';
  38. if($this->_bModule) {
  39. $CNF = &$this->_oModule->_oConfig->CNF;
  40. if(!empty($CNF['URL_HOME']))
  41. $this->_sBrowseAllUrl = BxDolPermalinks::getInstance()->permalink($CNF['URL_HOME']);
  42. if(!isset($CNF['T']['txt_all_categories'])) {
  43. $sBrowseAllTitleKey = '_' . $this->_oModule->getName() . '_txt_all_categories';
  44. if(strcmp($sBrowseAllTitleKey, _t($sBrowseAllTitleKey)) != 0)
  45. $this->_sBrowseAllTitle = $sBrowseAllTitleKey;
  46. }
  47. else
  48. $this->_sBrowseAllTitle = $CNF['T']['txt_all_categories'];
  49. if(!empty($CNF['URL_CATEGORY']))
  50. $this->_sBrowseUrl = bx_append_url_params(BxDolPermalinks::getInstance()->permalink($CNF['URL_CATEGORY']), [
  51. 'category' => '{keyword}'
  52. ], true, ['{keyword}']);
  53. }
  54. }
  55. public function getCategoryIcon($sValue)
  56. {
  57. $a = BxDolForm::getDataItems($this->_aObject['list_name'], false, BX_DATA_VALUES_ALL);
  58. if(!$a || !isset($a[$sValue]))
  59. return '';
  60. $aData = $this->_getCategoryData($a[$sValue]);
  61. if(empty($aData) || !is_array($aData))
  62. return '';
  63. return $this->_getCategoryIcon($aData);
  64. }
  65. public function getCategoryTitle($sValue)
  66. {
  67. $a = BxDolForm::getDataItems($this->_aObject['list_name']);
  68. if (!$a || !isset($a[$sValue]))
  69. return '';
  70. return $a[$sValue];
  71. }
  72. public function getCategoryUrl($sValue, $aParams = [])
  73. {
  74. $s = BX_DOL_URL_ROOT . bx_replace_markers($this->_sBrowseUrl, [
  75. 'category' => rawurlencode($this->getObjectName()),
  76. 'keyword' => rawurlencode($sValue),
  77. 'sections' => $this->_aObject['search_object'] ? '&section[]=' . rawurlencode($this->_aObject['search_object']) : '',
  78. 'context' => isset($aParams['context_id']) ? '&context_id=' . $aParams['context_id'] : ''
  79. ]);
  80. if($this->_bIsApi)
  81. return bx_api_get_relative_url($s);
  82. return $s;
  83. }
  84. /**
  85. * Get link to list all items with the same category
  86. * @param $sName category title
  87. * @param $sValue category value
  88. * @return category name wrapped with A tag
  89. */
  90. public function getCategoryLink($sName, $sValue)
  91. {
  92. $sUrl = $this->getCategoryUrl($sValue);
  93. return '<a class="bx-category-link" href="' . $sUrl . '">' . $sName . '</a>';
  94. }
  95. /**
  96. * Get all categories list
  97. * @param $bDisplayEmptyCats display categories with no items, true by default
  98. * @return categories list html
  99. */
  100. public function getCategoriesList($bDisplayEmptyCats = true, $bAsArray = false)
  101. {
  102. $aContextInfo = bx_get_page_info();
  103. $mProfileContextId = false;
  104. if($aContextInfo !== false)
  105. $mProfileContextId = $aContextInfo['context_profile_id'];
  106. $a = BxDolForm::getDataItems($this->_aObject['list_name'], false, BX_DATA_VALUES_ALL);
  107. if(!$a)
  108. return $bAsArray ? [] : '';
  109. $aVars = [
  110. 'bx_repeat:cats' => [],
  111. 'bx_if:show_all' => [
  112. 'condition' => $this->_sBrowseAllUrl != '',
  113. 'content' => [
  114. 'url' => $this->_sBrowseAllUrl,
  115. 'name' => _t($this->_sBrowseAllTitle)
  116. ]
  117. ]
  118. ];
  119. foreach ($a as $sValue => $aCategory) {
  120. if(!is_numeric($sValue) && !$sValue)
  121. continue;
  122. $iNum = $this->getItemsNum($sValue, ['context_id' => $mProfileContextId]);
  123. if(!$bDisplayEmptyCats && !$iNum)
  124. continue;
  125. $aData = $this->_getCategoryData($aCategory);
  126. $sIconType = $this->_getCategoryIconType($aData);
  127. $sIcon = $this->_getCategoryIcon($aData);
  128. $bIcon = !empty($sIcon);
  129. $aVars['bx_repeat:cats'][] = array_merge([
  130. 'url' => $this->getCategoryUrl($sValue, ($mProfileContextId ? ['context_id' => $mProfileContextId] : [])),
  131. 'name' => _t($aCategory['LKey']),
  132. 'value' => $sValue,
  133. 'num' => $iNum
  134. ], !$this->_bIsApi ? [
  135. 'bx_if:show_icon' => [
  136. 'condition' => $bIcon,
  137. 'content' => [
  138. 'bx_if:show_icon_font' => [
  139. 'condition' => $bIcon && $sIconType == 'icon',
  140. 'content' => [
  141. 'icon_name' => $sIcon,
  142. ]
  143. ],
  144. 'bx_if:show_icon_html' => [
  145. 'condition' => $bIcon && in_array($sIconType, ['emoji', 'image']),
  146. 'content' => [
  147. 'icon_code' => $sIcon,
  148. ]
  149. ],
  150. ]
  151. ],
  152. 'selected_class' => $sValue == bx_get('category') ? 'bx-category-list-item-selected' : '',
  153. ] : [
  154. 'icon_type' => $sIconType,
  155. 'icon' => $sIcon
  156. ]);
  157. }
  158. if($this->_bIsApi)
  159. return [bx_api_get_block('categories_list', $aVars['bx_repeat:cats'])];
  160. if($bAsArray)
  161. return $aVars;
  162. return !empty($aVars['bx_repeat:cats']) ? $this->_oTemplate->parseHtmlByName('category_list.html', $aVars) : '';
  163. }
  164. protected function _getCategoryIconType($aData)
  165. {
  166. return !empty($aData['use']) ? $aData['use'] : '';
  167. }
  168. protected function _getCategoryIcon($aData)
  169. {
  170. return !empty($aData['use']) && !empty($aData[$aData['use']]) ? $aData[$aData['use']] : '';
  171. }
  172. protected function _getCategoryData($aCategory)
  173. {
  174. if(is_array($aCategory['Data']))
  175. return $aCategory['Data'];
  176. return !empty($aCategory['Data']) && bx_is_serialized($aCategory['Data']) ? unserialize($aCategory['Data']) : [];
  177. }
  178. }
  179. /** @} */