BxBaseMenu.php 6.9 KB

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