BxBaseMenu.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  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. public function getDisplayAddons()
  28. {
  29. return $this->_bDisplayAddons;
  30. }
  31. public function setDisplayAddons($b)
  32. {
  33. $bRet = $this->_bDisplayAddons;
  34. $this->_bDisplayAddons = $b;
  35. return $bRet;
  36. }
  37. /**
  38. * Get menu code.
  39. * @return string
  40. */
  41. public function getCode ()
  42. {
  43. $sMenuTitle = isset($this->_aObject['title']) ? _t($this->_aObject['title']) : 'Menu-' . rand(0, PHP_INT_MAX);
  44. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->beginMenu($sMenuTitle);
  45. $s = '';
  46. $aVars = $this->_getTemplateVars ();
  47. if (!empty($aVars['bx_repeat:menu_items'])) {
  48. $this->_addJsCss();
  49. $s = $this->_getCode($this->_aObject['template'], $aVars);
  50. }
  51. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->endMenu($sMenuTitle);
  52. return $s;
  53. }
  54. protected function _getCode($sTmplName, $aTmplVars)
  55. {
  56. return $this->_oTemplate->parseHtmlByName($this->getTemplateName($sTmplName), $aTmplVars);
  57. }
  58. public function getCodeItem ($sName)
  59. {
  60. if(empty($sName))
  61. return '';
  62. $sCode = $this->_oTemplate->getHtml(str_replace('.html', '_item.html', $this->_aObject['template']));
  63. if(empty($sCode))
  64. return '';
  65. $mixedTmplVars = $this->getMenuItem($sName);
  66. if($mixedTmplVars === false)
  67. return '';
  68. return $this->_oTemplate->parseHtmlByContent($sCode, $mixedTmplVars);
  69. }
  70. /**
  71. * Get template variables array
  72. * @return array
  73. */
  74. protected function _getTemplateVars ()
  75. {
  76. return array (
  77. 'object' => $this->_sObject,
  78. 'bx_repeat:menu_items' => $this->getMenuItems (),
  79. );
  80. }
  81. /**
  82. * Get menu items array, which is ready to pass to menu template.
  83. * @return array or false
  84. */
  85. public function getMenuItems ()
  86. {
  87. if (!isset($this->_aObject['menu_items']))
  88. $this->_aObject['menu_items'] = $this->getMenuItemsRaw ();
  89. $aItems = array();
  90. foreach ($this->_aObject['menu_items'] as $aItem) {
  91. $aItem = $this->_getMenuItem ($aItem);
  92. if($aItem !== false)
  93. $aItems[] = $aItem;
  94. }
  95. return $aItems;
  96. }
  97. /**
  98. * Get menu item array, which is ready to pass to whole menu or
  99. * single menu item template. May return false if single menu item
  100. * is requested but cannot be shown by circumstances.
  101. * @return array or false
  102. */
  103. public function getMenuItem ($sName)
  104. {
  105. if (!isset($this->_aObject['menu_items']))
  106. $this->_aObject['menu_items'] = $this->getMenuItemsRaw ();
  107. if(!empty($sName))
  108. return $this->_getMenuItem($this->_aObject['menu_items'][$sName]);
  109. }
  110. /**
  111. * Get menu items array, this is just a wrapper for DB function for make it easier to override.
  112. * It is used in @see BxBaseMenu::getMenuItems
  113. * @return array
  114. */
  115. protected function getMenuItemsRaw ()
  116. {
  117. if($this->_bMultilevel)
  118. return $this->_oQuery->getMenuItemsHierarchy();
  119. else
  120. return $this->_oQuery->getMenuItems();
  121. }
  122. protected function _getMenuItem ($a)
  123. {
  124. $aHiddenOn = array(
  125. pow(2, BX_DB_HIDDEN_PHONE - 1) => 'bx-def-media-phone-hide',
  126. pow(2, BX_DB_HIDDEN_TABLET - 1) => 'bx-def-media-tablet-hide',
  127. pow(2, BX_DB_HIDDEN_DESKTOP - 1) => 'bx-def-media-desktop-hide',
  128. pow(2, BX_DB_HIDDEN_MOBILE - 1) => 'bx-def-mobile-app-hide'
  129. );
  130. if (isset($a['active']) && !$a['active'])
  131. return false;
  132. if (isset($a['visible_for_levels']) && !$this->_isVisible($a))
  133. return false;
  134. $a['title'] = _t($a['title']);
  135. $a['bx_if:title'] = array(
  136. 'condition' => !empty($a['title']),
  137. 'content' => array(
  138. 'title' => $a['title']
  139. )
  140. );
  141. $this->removeMarker('addon');
  142. $a = $this->_replaceMarkers($a);
  143. if ($this->_bDisplayAddons) {
  144. $mixedAddon = $this->_getMenuAddon($a);
  145. if (!is_array($mixedAddon)) {
  146. $this->addMarkers(array('addon' => $mixedAddon));
  147. $a = $this->_replaceMarkers($a);
  148. }
  149. }
  150. $aMarkers = $this->_getMenuMarkers($a);
  151. if ($aMarkers && is_array($aMarkers)) {
  152. $this->addMarkers($aMarkers);
  153. $a = $this->_replaceMarkers($a);
  154. }
  155. list ($sIcon, $sIconUrl, $sIconA, $sIconHtml) = $this->_getMenuIcon($a);
  156. $a['class_add'] = $this->_isSelected($a) ? 'bx-menu-tab-active' : '';
  157. $sHiddenOnCssClasses = '';
  158. if(!empty($a['hidden_on']))
  159. foreach($aHiddenOn as $iHiddenOn => $sClass)
  160. if((int)$a['hidden_on'] & $iHiddenOn)
  161. $sHiddenOnCssClasses .= ' ' . $sClass;
  162. $a['class_add'] .= $sHiddenOnCssClasses;
  163. $a['link'] = isset($a['link']) ? $this->_oPermalinks->permalink($a['link']) : 'javascript:void(0);';
  164. $a['title_attr'] = bx_html_attribute(strip_tags($a['title']));
  165. $a['attrs'] = $this->_getMenuAttrs($a);
  166. $a['bx_if:image'] = array (
  167. 'condition' => (bool)$sIconUrl,
  168. 'content' => array('icon_url' => $sIconUrl),
  169. );
  170. $a['bx_if:image_inline'] = array (
  171. 'condition' => false,
  172. 'content' => array('image' => ''),
  173. );
  174. $a['bx_if:icon'] = array (
  175. 'condition' => (bool)$sIcon,
  176. 'content' => array('icon' => $sIcon),
  177. );
  178. $a['bx_if:icon-html'] = array (
  179. 'condition' => (bool)$sIconHtml,
  180. 'content' => array('icon' => $sIconHtml),
  181. );
  182. $a['bx_if:icon-a'] = array (
  183. 'condition' => (bool)$sIconA,
  184. 'content' => array('icon-a' => $sIconA),
  185. );
  186. $a['bx_if:title'] = array (
  187. 'condition' => (bool)$a['title'],
  188. 'content' => array(
  189. 'title' => $a['title'],
  190. 'title_attr' => $a['title_attr']
  191. ),
  192. );
  193. $aTmplVarsAddon = $this->_bDisplayAddons ? $this->_getTmplVarsAddon($mixedAddon, $a) : array('addon' => '', 'addonf' => '');
  194. $a['bx_if:addon'] = array (
  195. 'condition' => $this->_bDisplayAddons && !empty($aTmplVarsAddon['addon']),
  196. 'content' => $aTmplVarsAddon
  197. );
  198. $aTmplVarsSubitems = array('subitems' => '');
  199. $bTmplVarsSubitems = $this->_bMultilevel && !empty($a['subitems']);
  200. if($bTmplVarsSubitems) {
  201. $aSubitems = array();
  202. foreach($a['subitems'] as $aSubitem) {
  203. $aSubitem = $this->_getMenuItem($aSubitem);
  204. if($aSubitem !== false)
  205. $aSubitems[] = $aSubitem;
  206. }
  207. $aTmplVarsSubitems['subitems'] = $this->_oTemplate->parseHtmlByName(str_replace('.html', '_subitems.html', $this->_aObject['template']), array(
  208. 'bx_repeat:menu_items' => $aSubitems,
  209. ));
  210. }
  211. $a['bx_if:show_subitems'] = array (
  212. 'condition' => $bTmplVarsSubitems,
  213. 'content' => $aTmplVarsSubitems
  214. );
  215. unset($a['subitems']);
  216. foreach ($this->_aOptionalParams as $sName => $sDefaultValue)
  217. if (!isset($a[$sName]))
  218. $a[$sName] = $sDefaultValue;
  219. return $a;
  220. }
  221. protected function _getMenuIcon ($a)
  222. {
  223. return BxTemplFunctions::getInstanceWithTemplate($this->_oTemplate)->getIcon(!empty($a['icon']) ? $a['icon'] : '');
  224. }
  225. protected function _getMenuAddon ($aMenuItem)
  226. {
  227. if (empty($aMenuItem['addon']))
  228. return '';
  229. return BxDolService::callSerialized($aMenuItem['addon'], $this->_aMarkers);
  230. }
  231. protected function _getMenuMarkers ($aMenuItem)
  232. {
  233. if (empty($aMenuItem['markers']))
  234. return '';
  235. return BxDolService::callSerialized($aMenuItem['markers'], $this->_aMarkers);
  236. }
  237. protected function _getMenuAttrs ($aMenuItem)
  238. {
  239. $sAttrs = '';
  240. if(!empty($aMenuItem['target']))
  241. $sAttrs .= ' target="' . $aMenuItem['target'] . '"';
  242. if($this->_bAddNoFollow && !empty($aMenuItem['link']) && preg_match('@^https?://@', $aMenuItem['link']) && strncmp($aMenuItem['link'], BX_DOL_URL_ROOT, strlen(BX_DOL_URL_ROOT)) !== 0)
  243. $sAttrs .= ' rel="noreferrer"';
  244. return $sAttrs;
  245. }
  246. /**
  247. * Add css/js files which are needed for menu display and functionality.
  248. */
  249. protected function _addJsCss()
  250. {
  251. $this->_oTemplate->addCss('menu.css');
  252. }
  253. protected function _getTmplVarsAddon($mixedAddon, $aMenuItem)
  254. {
  255. $sAddon = '';
  256. if(!is_array($mixedAddon))
  257. $sAddon = $mixedAddon;
  258. else if(!empty($mixedAddon['addon']))
  259. $sAddon = $mixedAddon['addon'];
  260. return array(
  261. 'addon' => $sAddon,
  262. 'addonf' => ''
  263. );
  264. }
  265. }
  266. /** @} */