BxBaseCategory.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. return 'star';
  58. }
  59. public function getCategoryTitle($sValue)
  60. {
  61. $a = BxDolForm::getDataItems($this->_aObject['list_name']);
  62. if (!$a || !isset($a[$sValue]))
  63. return '';
  64. return $a[$sValue];
  65. }
  66. public function getCategoryUrl($sValue, $aParams = [])
  67. {
  68. $s = BX_DOL_URL_ROOT . bx_replace_markers($this->_sBrowseUrl, [
  69. 'category' => rawurlencode($this->getObjectName()),
  70. 'keyword' => rawurlencode($sValue),
  71. 'sections' => $this->_aObject['search_object'] ? '&section[]=' . rawurlencode($this->_aObject['search_object']) : '',
  72. 'context' => isset($aParams['context_id']) ? '&context_id=' . $aParams['context_id'] : ''
  73. ]);
  74. if (bx_is_api())
  75. return bx_api_get_relative_url($s);
  76. return $s;
  77. }
  78. /**
  79. * Get link to list all items with the same category
  80. * @param $sName category title
  81. * @param $sValue category value
  82. * @return category name wrapped with A tag
  83. */
  84. public function getCategoryLink($sName, $sValue)
  85. {
  86. $sUrl = $this->getCategoryUrl($sValue);
  87. return '<a class="bx-category-link" href="' . $sUrl . '">' . $sName . '</a>';
  88. }
  89. /**
  90. * Get all categories list
  91. * @param $bDisplayEmptyCats display categories with no items, true by default
  92. * @return categories list html
  93. */
  94. public function getCategoriesList($bDisplayEmptyCats = true, $bAsArray = false)
  95. {
  96. $aContextInfo = bx_get_page_info();
  97. $mProfileContextId = false;
  98. if ($aContextInfo !== false)
  99. $mProfileContextId = $aContextInfo['context_profile_id'];
  100. $a = BxDolForm::getDataItems($this->_aObject['list_name']);
  101. if (!$a)
  102. return $bAsArray ? [] : '';
  103. $aVars = [
  104. 'bx_repeat:cats' => [],
  105. 'bx_if:show_all' => [
  106. 'condition' => $this->_sBrowseAllUrl != '',
  107. 'content' => [
  108. 'url' => $this->_sBrowseAllUrl,
  109. 'name' => _t($this->_sBrowseAllTitle)
  110. ]
  111. ]
  112. ];
  113. foreach ($a as $sValue => $sName) {
  114. if (!is_numeric($sValue) && !$sValue)
  115. continue;
  116. $iNum = $this->getItemsNum($sValue, ['context_id' => $mProfileContextId]);
  117. if (!$bDisplayEmptyCats && !$iNum)
  118. continue;
  119. $aVars['bx_repeat:cats'][] = [
  120. 'url' => $this->getCategoryUrl($sValue, ($mProfileContextId ? ['context_id' => $mProfileContextId] : [])),
  121. 'name' => $sName,
  122. 'value' => $sValue,
  123. 'num' => $iNum,
  124. 'icon' => $this->getCategoryIcon($sValue),
  125. 'selected_class' => $sValue == bx_get('category') ? 'bx-category-list-item-selected' : '',
  126. ];
  127. }
  128. if(bx_is_api())
  129. return [bx_api_get_block('categories_list', $aVars['bx_repeat:cats'])];
  130. if ($bAsArray)
  131. return $aVars;
  132. if (!$aVars['bx_repeat:cats'])
  133. return '';
  134. return $this->_oTemplate->parseHtmlByName('category_list.html', $aVars);
  135. }
  136. }
  137. /** @} */