BxBaseCategory.php 2.8 KB

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