BxBaseCategory.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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. )) . '{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. return BX_DOL_URL_ROOT . bx_replace_markers($this->_sBrowseUrl, array(
  39. 'category' => rawurlencode($this->getObjectName()),
  40. 'keyword' => rawurlencode($sValue),
  41. 'sections' => $this->_aObject['search_object'] ? '&section[]=' . rawurlencode($this->_aObject['search_object']) : '',
  42. 'context' => isset($aParams['context_id']) ? '&context_id=' . $aParams['context_id'] : ''
  43. ));
  44. }
  45. /**
  46. * Get link to list all items with the same category
  47. * @param $sName category title
  48. * @param $sValue category value
  49. * @return category name wrapped with A tag
  50. */
  51. public function getCategoryLink($sName, $sValue)
  52. {
  53. $sUrl = $this->getCategoryUrl($sValue);
  54. return '<a class="bx-category-link" href="' . $sUrl . '">' . $sName . '</a>';
  55. }
  56. /**
  57. * Get all categories list
  58. * @param $bDisplayEmptyCats display categories with no items, true by default
  59. * @return categories list html
  60. */
  61. public function getCategoriesList($bDisplayEmptyCats = true, $bAsArray = false)
  62. {
  63. $aContextInfo = bx_get_page_info();
  64. $mProfileContextId = false;
  65. if (isset($aContextInfo['context_module']) && isset($aContextInfo['profile_context_id'])){
  66. $mProfileContextId = $aContextInfo['profile_context_id'];
  67. }
  68. $a = BxDolForm::getDataItems($this->_aObject['list_name']);
  69. if (!$a)
  70. return $bAsArray ? array() : '';
  71. $aVars = array('bx_repeat:cats' => array());
  72. foreach ($a as $sValue => $sName) {
  73. if (!is_numeric($sValue) && !$sValue)
  74. continue;
  75. $iNum = $this->getItemsNum($sValue, ['context_id' => $mProfileContextId]);
  76. if (!$bDisplayEmptyCats && !$iNum)
  77. continue;
  78. $aVars['bx_repeat:cats'][] = array(
  79. 'url' => $mProfileContextId? $this->getCategoryUrl($sValue, ['context_id' => $mProfileContextId]) : $this->getCategoryUrl($sValue),
  80. 'name' => $sName,
  81. 'value' => $sValue,
  82. 'num' => $iNum,
  83. );
  84. }
  85. if ($bAsArray)
  86. return $aVars;
  87. if (!$aVars['bx_repeat:cats'])
  88. return '';
  89. return $this->_oTemplate->parseHtmlByName('category_list.html', $aVars);
  90. }
  91. }
  92. /** @} */