1
0

BxBaseMenu.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. if (!isset($this->_aObject['menu_items']))
  62. $this->_aObject['menu_items'] = $this->_oQuery->getMenuItems();
  63. $aItems = array();
  64. foreach ($this->_aObject['menu_items'] as $aItem) {
  65. $aItem = $this->_getMenuItem ($aItem);
  66. if($aItem !== false)
  67. $aItems[] = $aItem;
  68. }
  69. return $aItems;
  70. }
  71. protected function _getMenuItem ($a)
  72. {
  73. if (isset($a['active']) && !$a['active'])
  74. return false;
  75. if (isset($a['visible_for_levels']) && !$this->_isVisible($a))
  76. return false;
  77. $a['title'] = _t($a['title']);
  78. $this->removeMarker('addon');
  79. $a = $this->_replaceMarkers($a);
  80. $mixedAddon = $this->_getMenuAddon($a);
  81. $this->addMarkers(array('addon' => $mixedAddon));
  82. $a = $this->_replaceMarkers($a);
  83. list ($sIcon, $sIconUrl, $sIconA) = $this->_getMenuIcon($a);
  84. $a['class_add'] = $this->_isSelected($a) ? 'bx-menu-tab-active' : '';
  85. $a['link'] = isset($a['link']) ? $this->_oPermalinks->permalink($a['link']) : 'javascript:void(0);';
  86. $a['title_attr'] = bx_html_attribute($a['title']);
  87. $a['bx_if:image'] = array (
  88. 'condition' => (bool)$sIconUrl,
  89. 'content' => array('icon_url' => $sIconUrl),
  90. );
  91. $a['bx_if:icon'] = array (
  92. 'condition' => (bool)$sIcon,
  93. 'content' => array('icon' => $sIcon),
  94. );
  95. $a['bx_if:icon-a'] = array (
  96. 'condition' => (bool)$sIconA,
  97. 'content' => array('icon-a' => $sIconA),
  98. );
  99. $a['bx_if:title'] = array (
  100. 'condition' => (bool)$a['title'],
  101. 'content' => array('title' => $a['title']),
  102. );
  103. $a['bx_if:addon'] = array (
  104. 'condition' => (bool)$mixedAddon,
  105. 'content' => array('addon' => $mixedAddon),
  106. );
  107. foreach ($this->_aOptionalParams as $sName => $sDefaultValue)
  108. if (!isset($a[$sName]))
  109. $a[$sName] = $sDefaultValue;
  110. return $a;
  111. }
  112. protected function _getMenuIcon ($a)
  113. {
  114. $sIcon = false;
  115. $sIconA = false;
  116. $sIconUrl = false;
  117. if (!empty($a['icon'])) {
  118. if ((int)$a['icon'] > 0 ) {
  119. bx_import('BxDolStorage');
  120. $oStorage = BxDolStorage::getObjectInstance(BX_DOL_STORAGE_OBJ_IMAGES);
  121. $sIconUrl = $oStorage ? $oStorage->getFileUrlById((int)$a['icon']) : false;
  122. } else {
  123. if (false === strpos($a['icon'], '.')) {
  124. if (0 === strncmp($a['icon'], 'a:', 2))
  125. $sIconA = substr($a['icon'], 2); // animated icon
  126. else
  127. $sIcon = $a['icon']; // font icons
  128. } else {
  129. $sIconUrl = $this->_oTemplate->getIconUrl($a['icon']);
  130. }
  131. }
  132. }
  133. return array ($sIcon, $sIconUrl, $sIconA);
  134. }
  135. protected function _getMenuAddon ($aMenuItem)
  136. {
  137. if (empty($aMenuItem['addon']))
  138. return '';
  139. return BxDolService::callSerialized($aMenuItem['addon'], $this->_aMarkers);
  140. }
  141. /**
  142. * Add css/js files which are needed for menu display and functionality.
  143. */
  144. protected function _addJsCss()
  145. {
  146. $this->_oTemplate->addCss('menu.css');
  147. }
  148. }
  149. /** @} */