BxBaseMenu.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  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. {
  17. protected $_oTemplate;
  18. protected $_aOptionalParams = array('target' => '', 'onclick' => '');
  19. public function __construct ($aObject, $oTemplate)
  20. {
  21. parent::__construct ($aObject);
  22. if ($oTemplate)
  23. $this->_oTemplate = $oTemplate;
  24. else
  25. $this->_oTemplate = BxDolTemplate::getInstance();
  26. }
  27. /**
  28. * Get menu code.
  29. * @return string
  30. */
  31. public function getCode ()
  32. {
  33. $sMenuTitle = isset($this->_aObject['title']) ? _t($this->_aObject['title']) : 'Menu-' . rand(0, PHP_INT_MAX);
  34. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->beginMenu($sMenuTitle);
  35. $s = '';
  36. $aVars = $this->_getTemplateVars ();
  37. if (!empty($aVars['bx_repeat:menu_items'])) {
  38. $this->_addJsCss();
  39. $s = $this->_oTemplate->parseHtmlByName($this->_aObject['template'], $aVars);
  40. }
  41. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->endMenu($sMenuTitle);
  42. return $s;
  43. }
  44. /**
  45. * Get template variables array
  46. * @return array
  47. */
  48. protected function _getTemplateVars ()
  49. {
  50. return array (
  51. 'object' => $this->_sObject,
  52. 'bx_repeat:menu_items' => $this->getMenuItems (),
  53. );
  54. }
  55. /**
  56. * Get menu items array, which are ready to pass to template.
  57. * @return array
  58. */
  59. public function getMenuItems ()
  60. {
  61. $aRet = array();
  62. if (!isset($this->_aObject['menu_items']))
  63. $this->_aObject['menu_items'] = $this->_oQuery->getMenuItems();
  64. foreach ($this->_aObject['menu_items'] as $a) {
  65. if (isset($a['active']) && !$a['active'])
  66. continue;
  67. if (isset($a['visible_for_levels']) && !$this->_isVisible($a))
  68. continue;
  69. $a['title'] = _t($a['title']);
  70. $a = $this->_replaceMarkers($a);
  71. $mixedAddon = $this->_getMenuAddon($a);
  72. $this->addMarkers(array('addon' => $mixedAddon));
  73. $a = $this->_replaceMarkers($a);
  74. list ($sIcon, $sIconUrl) = $this->_getMenuIcon($a);
  75. $a['class_add'] = $this->_isSelected($a) ? 'bx-menu-tab-active' : '';
  76. $a['link'] = isset($a['link']) ? $this->_oPermalinks->permalink($a['link']) : 'javascript:void(0);';
  77. $a['title_attr'] = bx_html_attribute($a['title']);
  78. $a['bx_if:image'] = array (
  79. 'condition' => (bool)$sIconUrl,
  80. 'content' => array('icon_url' => $sIconUrl),
  81. );
  82. $a['bx_if:icon'] = array (
  83. 'condition' => (bool)$sIcon,
  84. 'content' => array('icon' => $sIcon),
  85. );
  86. $a['bx_if:title'] = array (
  87. 'condition' => (bool)$a['title'],
  88. 'content' => array('title' => $a['title']),
  89. );
  90. $a['bx_if:addon'] = array (
  91. 'condition' => (bool)$mixedAddon,
  92. 'content' => array('addon' => $mixedAddon),
  93. );
  94. foreach ($this->_aOptionalParams as $sName => $sDefaultValue)
  95. if (!isset($a[$sName]))
  96. $a[$sName] = $sDefaultValue;
  97. $aRet[] = $a;
  98. }
  99. return $aRet;
  100. }
  101. protected function _getMenuIcon ($a)
  102. {
  103. $sIcon = false;
  104. $sIconUrl = false;
  105. if (!empty($a['icon'])) {
  106. if ((int)$a['icon'] > 0 ) {
  107. bx_import('BxDolStorage');
  108. $oStorage = BxDolStorage::getObjectInstance(BX_DOL_STORAGE_OBJ_IMAGES);
  109. $sIconUrl = $oStorage ? $oStorage->getFileUrlById((int)$a['icon']) : false;
  110. } else {
  111. if (false === strpos($a['icon'], '.')) // font icons
  112. $sIcon = $a['icon'];
  113. else
  114. $sIconUrl = $this->_oTemplate->getIconUrl($a['icon']);
  115. }
  116. }
  117. return array ($sIcon, $sIconUrl);
  118. }
  119. protected function _getMenuAddon ($aMenuItem)
  120. {
  121. if (empty($aMenuItem['addon']))
  122. return '';
  123. return BxDolService::callSerialized($aMenuItem['addon'], $this->_aMarkers);
  124. }
  125. /**
  126. * Add css/js files which are needed for menu display and functionality.
  127. */
  128. protected function _addJsCss()
  129. {
  130. $this->_oTemplate->addCss('menu.css');
  131. }
  132. }
  133. /** @} */