BxBaseMenu.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  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. $sMenuTitle = isset($this->_aObject['title']) ? _t($this->_aObject['title']) : 'Menu-' . rand(0, PHP_INT_MAX);
  32. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->beginMenu($sMenuTitle);
  33. $s = '';
  34. $aVars = $this->_getTemplateVars ();
  35. if (!empty($aVars['bx_repeat:menu_items'])) {
  36. $this->_addJsCss();
  37. $s = $this->_oTemplate->parseHtmlByName($this->_aObject['template'], $aVars);
  38. }
  39. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->endMenu($sMenuTitle);
  40. return $s;
  41. }
  42. /**
  43. * Get template variables array
  44. * @return array
  45. */
  46. protected function _getTemplateVars ()
  47. {
  48. return array (
  49. 'object' => $this->_sObject,
  50. 'bx_repeat:menu_items' => $this->getMenuItems (),
  51. );
  52. }
  53. /**
  54. * Get menu items array, which are ready to pass to template.
  55. * @return array
  56. */
  57. public function getMenuItems ()
  58. {
  59. if (!isset($this->_aObject['menu_items']))
  60. $this->_aObject['menu_items'] = $this->getMenuItemsRaw ();
  61. $aItems = array();
  62. foreach ($this->_aObject['menu_items'] as $aItem) {
  63. $aItem = $this->_getMenuItem ($aItem);
  64. if($aItem !== false)
  65. $aItems[] = $aItem;
  66. }
  67. return $aItems;
  68. }
  69. /**
  70. * Get menu items array, this is just a wrapper for DB function for make it easier to override.
  71. * It is used in @see BxBaseMenu::getMenuItems
  72. * @return array
  73. */
  74. protected function getMenuItemsRaw ()
  75. {
  76. return $this->_oQuery->getMenuItems();
  77. }
  78. protected function _getMenuItem ($a)
  79. {
  80. $aHiddenOn = array(
  81. pow(2, BX_DB_HIDDEN_PHONE - 1) => 'bx-def-media-phone-hide',
  82. pow(2, BX_DB_HIDDEN_TABLET - 1) => 'bx-def-media-tablet-hide',
  83. pow(2, BX_DB_HIDDEN_DESKTOP - 1) => 'bx-def-media-desktop-hide'
  84. );
  85. if (isset($a['active']) && !$a['active'])
  86. return false;
  87. if (isset($a['visible_for_levels']) && !$this->_isVisible($a))
  88. return false;
  89. $a['title'] = _t($a['title']);
  90. $a['bx_if:title'] = array(
  91. 'condition' => !empty($a['title']),
  92. 'content' => array(
  93. 'title' => $a['title']
  94. )
  95. );
  96. $this->removeMarker('addon');
  97. $a = $this->_replaceMarkers($a);
  98. $mixedAddon = $this->_getMenuAddon($a);
  99. $this->addMarkers(array('addon' => $mixedAddon));
  100. $a = $this->_replaceMarkers($a);
  101. list ($sIcon, $sIconUrl, $sIconA) = $this->_getMenuIcon($a);
  102. $a['class_add'] = $this->_isSelected($a) ? 'bx-menu-tab-active' : '';
  103. $a['class_add'] .= 'hza';
  104. $sHiddenOnCssClasses = '';
  105. if(!empty($a['hidden_on']))
  106. foreach($aHiddenOn as $iHiddenOn => $sClass)
  107. if((int)$a['hidden_on'] & $iHiddenOn)
  108. $sHiddenOnCssClasses .= ' ' . $sClass;
  109. $a['class_add'] .= $sHiddenOnCssClasses;
  110. $a['link'] = isset($a['link']) ? $this->_oPermalinks->permalink($a['link']) : 'javascript:void(0);';
  111. $a['title_attr'] = bx_html_attribute(strip_tags($a['title']));
  112. $a['bx_if:image'] = array (
  113. 'condition' => (bool)$sIconUrl,
  114. 'content' => array('icon_url' => $sIconUrl),
  115. );
  116. $a['bx_if:icon'] = array (
  117. 'condition' => (bool)$sIcon,
  118. 'content' => array('icon' => $sIcon),
  119. );
  120. $a['bx_if:icon-a'] = array (
  121. 'condition' => (bool)$sIconA,
  122. 'content' => array('icon-a' => $sIconA),
  123. );
  124. $a['bx_if:title'] = array (
  125. 'condition' => (bool)$a['title'],
  126. 'content' => array(
  127. 'title' => $a['title'],
  128. 'title_attr' => $a['title_attr']
  129. ),
  130. );
  131. $a['bx_if:addon'] = array (
  132. 'condition' => (bool)$mixedAddon,
  133. 'content' => $this->_getTmplVarsAddon($mixedAddon, $a)
  134. );
  135. foreach ($this->_aOptionalParams as $sName => $sDefaultValue)
  136. if (!isset($a[$sName]))
  137. $a[$sName] = $sDefaultValue;
  138. return $a;
  139. }
  140. protected function _getMenuIcon ($a)
  141. {
  142. $sIcon = false;
  143. $sIconA = false;
  144. $sIconUrl = false;
  145. if (!empty($a['icon'])) {
  146. if ((int)$a['icon'] > 0 ) {
  147. $oStorage = BxDolStorage::getObjectInstance(BX_DOL_STORAGE_OBJ_IMAGES);
  148. $sIconUrl = $oStorage ? $oStorage->getFileUrlById((int)$a['icon']) : false;
  149. } else {
  150. if (false === strpos($a['icon'], '.')) {
  151. if (0 === strncmp($a['icon'], 'a:', 2))
  152. $sIconA = substr($a['icon'], 2); // animated icon
  153. else
  154. $sIcon = $a['icon']; // font icons
  155. } else {
  156. $sIconUrl = $this->_oTemplate->getIconUrl($a['icon']);
  157. }
  158. }
  159. }
  160. return array ($sIcon, $sIconUrl, $sIconA);
  161. }
  162. protected function _getMenuAddon ($aMenuItem)
  163. {
  164. if (empty($aMenuItem['addon']))
  165. return '';
  166. return BxDolService::callSerialized($aMenuItem['addon'], $this->_aMarkers);
  167. }
  168. /**
  169. * Add css/js files which are needed for menu display and functionality.
  170. */
  171. protected function _addJsCss()
  172. {
  173. $this->_oTemplate->addCss('menu.css');
  174. }
  175. protected function _getTmplVarsAddon($mixedAddon, $aMenuItem)
  176. {
  177. return array(
  178. 'addon' => $mixedAddon,
  179. 'addonf' => ''
  180. );
  181. }
  182. }
  183. /** @} */