1
0

BxBaseMenu.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup DolphinCore Dolphin Core
  7. * @{
  8. */
  9. bx_import('BxDolMenu');
  10. bx_import('BxDolPaginate');
  11. /**
  12. * Menu representation.
  13. * @see BxDolMenu
  14. */
  15. class BxBaseMenu extends BxDolMenu {
  16. protected $_oTemplate;
  17. public function __construct ($aObject, $oTemplate) {
  18. parent::__construct ($aObject);
  19. if ($oTemplate)
  20. $this->_oTemplate = $oTemplate;
  21. else
  22. $this->_oTemplate = BxDolTemplate::getInstance();
  23. }
  24. /**
  25. * Get menu code.
  26. * @return string
  27. */
  28. public function getCode () {
  29. $aVars = $this->_getTemplateVars ();
  30. if (empty($aVars['bx_repeat:menu_items']))
  31. return false;
  32. $this->_addJsCss();
  33. return $this->_oTemplate->parseHtmlByName($this->_aObject['template'], $aVars);
  34. }
  35. /**
  36. * Get template variables array
  37. * @return array
  38. */
  39. protected function _getTemplateVars () {
  40. return array (
  41. 'object' => $this->_sObject,
  42. 'bx_repeat:menu_items' => $this->_getMenuItems (),
  43. );
  44. }
  45. /**
  46. * Get menu items array, which are ready to pass to template.
  47. * @return array
  48. */
  49. protected function _getMenuItems () {
  50. $aRet = array();
  51. if (!isset($this->_aObject['menu_items']))
  52. $this->_aObject['menu_items'] = $this->_oQuery->getMenuItems();
  53. foreach ($this->_aObject['menu_items'] as $a) {
  54. if (isset($a['active']) && !$a['active'])
  55. continue;
  56. if (isset($a['visible_for_levels']) && !$this->_isVisible($a))
  57. continue;
  58. $a['title'] = _t($a['title']);
  59. $a = $this->_replaceMarkers($a);
  60. list ($sIcon, $sIconUrl) = $this->_getMenuIcon($a);
  61. $a['class_add'] = $this->_isSelected($a) ? 'bx-menu-tab-active' : '';
  62. $a['link'] = isset($a['link']) ? $this->_oPermalinks->permalink($a['link']) : 'javascript:void(0);';
  63. $a['title_attr'] = bx_html_attribute($a['title']);
  64. $a['bx_if:image'] = array (
  65. 'condition' => (bool)$sIconUrl,
  66. 'content' => array('icon_url' => $sIconUrl),
  67. );
  68. $a['bx_if:icon'] = array (
  69. 'condition' => (bool)$sIcon,
  70. 'content' => array('icon' => $sIcon),
  71. );
  72. $a['bx_if:title'] = array (
  73. 'condition' => (bool)$a['title'],
  74. 'content' => array('title' => $a['title']),
  75. );
  76. $aRet[] = $a;
  77. }
  78. return $aRet;
  79. }
  80. protected function _getMenuIcon ($a) {
  81. $sIcon = false;
  82. $sIconUrl = false;
  83. if (!empty($a['icon'])) {
  84. if ((int)$a['icon'] > 0 ) {
  85. bx_import('BxDolStorage');
  86. $oStorage = BxDolStorage::getObjectInstance(BX_DOL_STORAGE_OBJ_IMAGES);
  87. $sIconUrl = $oStorage ? $oStorage->getFileUrlById((int)$a['icon']) : false;
  88. } else {
  89. if (false === strpos($a['icon'], '.')) // font icons
  90. $sIcon = $a['icon'];
  91. else
  92. $sIconUrl = $this->_oTemplate->getIconUrl($a['icon']); // TODO: it is used in studio only, so move this line of code to studio or maybe all studio icons will use font icons too ?!
  93. }
  94. }
  95. return array ($sIcon, $sIconUrl);
  96. }
  97. /**
  98. * Add css/js files which are needed for menu display and functionality.
  99. */
  100. protected function _addJsCss() {
  101. $this->_oTemplate->addCss('menu.css');
  102. }
  103. }
  104. /** @} */