BxBaseCategory.php 4.7 KB

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