BxBaseCategory.php 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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)
  61. {
  62. $a = BxDolForm::getDataItems($this->_aObject['list_name']);
  63. if (!$a)
  64. return '';
  65. $aVars = array('bx_repeat:cats' => array());
  66. foreach ($a as $sValue => $sName) {
  67. if (!$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 (!$aVars['bx_repeat:cats'])
  80. return '';
  81. return $this->_oTemplate->parseHtmlByName('category_list.html', $aVars);
  82. }
  83. }
  84. /** @} */