BxBaseMenu.php 10 KB

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