1
0

BxBaseMenu.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. * Menu representation.
  11. * @see BxDolMenu
  12. */
  13. class BxBaseMenu extends BxDolMenu
  14. {
  15. protected $_oTemplate;
  16. protected $_aOptionalParams = array('target' => '', 'onclick' => '');
  17. public function __construct ($aObject, $oTemplate)
  18. {
  19. parent::__construct ($aObject);
  20. if ($oTemplate)
  21. $this->_oTemplate = $oTemplate;
  22. else
  23. $this->_oTemplate = BxDolTemplate::getInstance();
  24. }
  25. /**
  26. * Get menu code.
  27. * @return string
  28. */
  29. public function getCode ()
  30. {
  31. if(!$this->isVisible())
  32. return '';
  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->getMenuItemsRaw ();
  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. /**
  72. * Get menu items array, this is just a wrapper for DB function for make it easier to override.
  73. * It is used in @see BxBaseMenu::getMenuItems
  74. * @return array
  75. */
  76. protected function getMenuItemsRaw ()
  77. {
  78. return $this->_oQuery->getMenuItems();
  79. }
  80. protected function _getMenuItem ($a)
  81. {
  82. if (isset($a['active']) && !$a['active'])
  83. return false;
  84. if (isset($a['visible_for_levels']) && !$this->_isVisible($a))
  85. return false;
  86. $a['title'] = _t($a['title']);
  87. $a['bx_if:title'] = array(
  88. 'condition' => !empty($a['title']),
  89. 'content' => array(
  90. 'title' => $a['title']
  91. )
  92. );
  93. $this->removeMarker('addon');
  94. $a = $this->_replaceMarkers($a);
  95. $mixedAddon = $this->_getMenuAddon($a);
  96. $this->addMarkers(array('addon' => $mixedAddon));
  97. $a = $this->_replaceMarkers($a);
  98. list ($sIcon, $sIconUrl, $sIconA) = $this->_getMenuIcon($a);
  99. $a['class_add'] = $this->_isSelected($a) ? 'bx-menu-tab-active' : '';
  100. $a['link'] = isset($a['link']) ? $this->_oPermalinks->permalink($a['link']) : 'javascript:void(0);';
  101. $a['title_attr'] = bx_html_attribute(strip_tags($a['title']));
  102. $a['bx_if:image'] = array (
  103. 'condition' => (bool)$sIconUrl,
  104. 'content' => array('icon_url' => $sIconUrl),
  105. );
  106. $a['bx_if:icon'] = array (
  107. 'condition' => (bool)$sIcon,
  108. 'content' => array('icon' => $sIcon),
  109. );
  110. $a['bx_if:icon-a'] = array (
  111. 'condition' => (bool)$sIconA,
  112. 'content' => array('icon-a' => $sIconA),
  113. );
  114. $a['bx_if:title'] = array (
  115. 'condition' => (bool)$a['title'],
  116. 'content' => array(
  117. 'title' => $a['title'],
  118. 'title_attr' => $a['title_attr']
  119. ),
  120. );
  121. $a['bx_if:addon'] = array (
  122. 'condition' => (bool)$mixedAddon,
  123. 'content' => $this->_getTmplVarsAddon($mixedAddon, $a)
  124. );
  125. foreach ($this->_aOptionalParams as $sName => $sDefaultValue)
  126. if (!isset($a[$sName]))
  127. $a[$sName] = $sDefaultValue;
  128. return $a;
  129. }
  130. protected function _getMenuIcon ($a)
  131. {
  132. $sIcon = false;
  133. $sIconA = false;
  134. $sIconUrl = false;
  135. if (!empty($a['icon'])) {
  136. if ((int)$a['icon'] > 0 ) {
  137. $oStorage = BxDolStorage::getObjectInstance(BX_DOL_STORAGE_OBJ_IMAGES);
  138. $sIconUrl = $oStorage ? $oStorage->getFileUrlById((int)$a['icon']) : false;
  139. } else {
  140. if (false === strpos($a['icon'], '.')) {
  141. if (0 === strncmp($a['icon'], 'a:', 2))
  142. $sIconA = substr($a['icon'], 2); // animated icon
  143. else
  144. $sIcon = $a['icon']; // font icons
  145. } else {
  146. $sIconUrl = $this->_oTemplate->getIconUrl($a['icon']);
  147. }
  148. }
  149. }
  150. return array ($sIcon, $sIconUrl, $sIconA);
  151. }
  152. protected function _getMenuAddon ($aMenuItem)
  153. {
  154. if (empty($aMenuItem['addon']))
  155. return '';
  156. return BxDolService::callSerialized($aMenuItem['addon'], $this->_aMarkers);
  157. }
  158. /**
  159. * Add css/js files which are needed for menu display and functionality.
  160. */
  161. protected function _addJsCss()
  162. {
  163. $this->_oTemplate->addCss('menu.css');
  164. }
  165. protected function _getTmplVarsAddon($mixedAddon, $aMenuItem)
  166. {
  167. return array(
  168. 'addon' => $mixedAddon,
  169. 'addonf' => ''
  170. );
  171. }
  172. }
  173. /** @} */