BxBaseMenu.php 8.5 KB

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