BxBaseMenu.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499
  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. protected $_aSelected; // Selected menu item.
  21. public function __construct ($aObject, $oTemplate)
  22. {
  23. parent::__construct ($aObject);
  24. if ($oTemplate)
  25. $this->_oTemplate = $oTemplate;
  26. else
  27. $this->_oTemplate = BxDolTemplate::getInstance();
  28. $this->_iPageType = false;
  29. $this->_aSelected = [];
  30. }
  31. public function getDisplayAddons()
  32. {
  33. return $this->_bDisplayAddons;
  34. }
  35. public function setDisplayAddons($b)
  36. {
  37. $bRet = $this->_bDisplayAddons;
  38. $this->_bDisplayAddons = $b;
  39. return $bRet;
  40. }
  41. /**
  42. * Get menu code.
  43. * @return string
  44. */
  45. public function getCode ()
  46. {
  47. $sMenuTitle = isset($this->_aObject['title']) ? _t($this->_aObject['title']) : 'Menu-' . rand(0, PHP_INT_MAX);
  48. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->beginMenu($sMenuTitle);
  49. if(!$this->_iPageType)
  50. $this->_iPageType = BxDolTemplate::getInstance()->getPageType();
  51. $s = '';
  52. $aVars = $this->_getTemplateVars ();
  53. if (!empty($aVars['bx_repeat:menu_items'])) {
  54. $this->_addJsCss();
  55. $s = $this->_getCode($this->_aObject['template'], $aVars);
  56. }
  57. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->endMenu($sMenuTitle);
  58. return $s;
  59. }
  60. /**
  61. * Get menu code API.
  62. * @return array
  63. */
  64. public function getCodeAPI ()
  65. {
  66. $aItems = [];
  67. $aVars = $this->_getTemplateVars();
  68. if(!empty($aVars['bx_repeat:menu_items']))
  69. $aItems = $aVars['bx_repeat:menu_items'];
  70. return [
  71. 'object' => $this->_sObject,
  72. 'config' => $this->_aObject['config_api'],
  73. 'persistent' => $this->_aObject['persistent'],
  74. 'params' => $this->getContentParams(),
  75. 'items' => $aItems
  76. ];
  77. }
  78. /**
  79. * Get menu short code API.
  80. * @return array
  81. */
  82. public function getShortCodeAPI ()
  83. {
  84. return [
  85. 'object' => $this->_sObject,
  86. 'params' => $this->getContentParams()
  87. ];
  88. }
  89. protected function _getCode($sTmplName, $aTmplVars)
  90. {
  91. return $this->_oTemplate->parseHtmlByName($this->getTemplateName($sTmplName), $aTmplVars);
  92. }
  93. public function getCodeItem ($sName)
  94. {
  95. if(empty($sName))
  96. return '';
  97. $sCode = $this->_oTemplate->getHtml(str_replace('.html', '_item.html', $this->_aObject['template']));
  98. if(empty($sCode))
  99. return '';
  100. $mixedTmplVars = $this->getMenuItem($sName);
  101. if($mixedTmplVars === false)
  102. return '';
  103. return $this->_oTemplate->parseHtmlByContent($sCode, $mixedTmplVars);
  104. }
  105. /**
  106. * Get template variables array
  107. * @return array
  108. */
  109. protected function _getTemplateVars ()
  110. {
  111. $aMenuItems = $this->getMenuItems();
  112. $sMenuItemSelected = _t($aMenuItems && $this->_aSelected && !empty($this->_aSelected['title']) ? $this->_aSelected['title'] : '_Contents');
  113. return [
  114. 'object' => $this->_sObject,
  115. 'menu_item_selected' => $sMenuItemSelected,
  116. 'bx_repeat:menu_items' => $aMenuItems,
  117. ];
  118. }
  119. /**
  120. * Get menu items array, which is ready to pass to menu template.
  121. * @return array or false
  122. */
  123. public function getMenuItems ()
  124. {
  125. if (!isset($this->_aObject['menu_items']))
  126. $this->_aObject['menu_items'] = $this->getMenuItemsRaw ();
  127. $aItems = array();
  128. foreach ($this->_aObject['menu_items'] as $aItem) {
  129. $aItem = $this->_getMenuItem ($aItem);
  130. if($aItem !== false)
  131. $aItems[] = $aItem;
  132. }
  133. return $aItems;
  134. }
  135. /**
  136. * Get menu item array, which is ready to pass to whole menu or
  137. * single menu item template. May return false if single menu item
  138. * is requested but cannot be shown by circumstances.
  139. * @return array or false
  140. */
  141. public function getMenuItem ($sName)
  142. {
  143. if (!isset($this->_aObject['menu_items']))
  144. $this->_aObject['menu_items'] = $this->getMenuItemsRaw ();
  145. if(!empty($sName))
  146. return $this->_getMenuItem($this->_aObject['menu_items'][$sName]);
  147. }
  148. /**
  149. * Get menu items array, this is just a wrapper for DB function for make it easier to override.
  150. * It is used in @see BxBaseMenu::getMenuItems
  151. * @return array
  152. */
  153. protected function getMenuItemsRaw ()
  154. {
  155. if($this->_bMultilevel)
  156. return $this->_oQuery->getMenuItemsHierarchy();
  157. else
  158. return $this->_oQuery->getMenuItems();
  159. }
  160. protected function _getMenuItem ($a)
  161. {
  162. if (!$this->_isActive($a) || !$this->_isVisible($a))
  163. return false;
  164. if ($this->_bIsApi) {
  165. list ($sIcon, $sIconUrl, $sIconA, $sIconHtml) = $this->_getMenuIcon($a);
  166. $aResult = [
  167. 'id' => $a['id'],
  168. 'name' => $a['name'],
  169. 'title' => _t($a['title']),
  170. 'info' => isset($a['info']) ? _t($a['info']) : '',
  171. 'link' => isset($a['link']) ? $a['link'] : '',
  172. 'icon' => $sIcon ? $sIcon : ($sIconHtml ? $sIconHtml : ''),
  173. 'image' => $sIconUrl ? $sIconUrl : '',
  174. 'submenu' => !empty($a['submenu_object']) ? $a['submenu_object'] : '',
  175. 'addon' => $this->_bDisplayAddons ? $this->_getMenuAddon($a) : '',
  176. 'config' => isset($a['config_api']) ? $a['config_api'] : '',
  177. 'primary' => isset($a['primary']) ? $a['primary'] : 0,
  178. 'persistent' => isset($a['persistent']) ? $a['persistent'] : 0,
  179. ];
  180. if(($aMarkers = $this->_getMenuMarkers($a)) && is_array($aMarkers))
  181. $this->addMarkers($aMarkers);
  182. $aResult = $this->_replaceMarkers($aResult);
  183. if(!empty($aResult['link']))
  184. $aResult['link'] = $this->_oPermalinks->permalink($aResult['link']);
  185. return $aResult;
  186. }
  187. $bIsSelected = $this->_isSelected($a);
  188. if($bIsSelected)
  189. $this->_aSelected = $a;
  190. $a['object'] = $this->_sObject;
  191. $a['title'] = _t($a['title']);
  192. $a['title_attr'] = $this->_getMenuTitle($a);
  193. $a['info'] = isset($a['info']) ? _t($a['info']) : '';
  194. $this->removeMarker('addon');
  195. $a = $this->_replaceMarkers($a);
  196. if ($this->_bDisplayAddons) {
  197. $mixedAddon = $this->_getMenuAddon($a);
  198. if (!is_array($mixedAddon)) {
  199. $this->addMarkers(array('addon' => $mixedAddon));
  200. $a = $this->_replaceMarkers($a);
  201. }
  202. }
  203. $aMarkers = $this->_getMenuMarkers($a);
  204. if ($aMarkers && is_array($aMarkers)) {
  205. $this->addMarkers($aMarkers);
  206. $a = $this->_replaceMarkers($a);
  207. }
  208. list ($sIcon, $sIconUrl, $sIconA, $sIconHtml) = $this->_getMenuIcon($a);
  209. if(!isset($a['class_add']))
  210. $a['class_add'] = '';
  211. if($bIsSelected)
  212. $a['class_add'] .= ' bx-menu-tab-active';
  213. $a['class_add'] .= $this->_getVisibilityClass($a);
  214. $a['class_link'] = '';
  215. $a['link'] = isset($a['link']) ? $this->_oPermalinks->permalink($a['link']) : 'javascript:void(0);';
  216. $a['attrs'] = $this->_getMenuAttrs($a);
  217. $a['attrs_wrp'] = '';
  218. if($this->_bHx && !empty($a['link']) && strpos($a['link'], 'javascript:') === false) {
  219. $this->_aHx['get'] = $a['link'];
  220. $a['attrs'] .= bx_get_htmx_attrs($this->_aHx, $this->_mHxPreload);
  221. if(!bx_is_htmx_request() && !$bIsSelected)
  222. $a['attrs_wrp'] .= bx_get_htmx_attrs([
  223. 'get' => $a['link'],
  224. 'trigger' => 'load',
  225. 'target' => '#bx-content-preload',
  226. 'swap' => 'beforeend',
  227. ]);
  228. }
  229. $a['bx_if:image'] = [
  230. 'condition' => (bool)$sIconUrl,
  231. 'content' => [
  232. 'icon_url' => $sIconUrl,
  233. 'attrs' => $this->_getMenuIconAttrs('image', $a)
  234. ],
  235. ];
  236. $a['bx_if:image_inline'] = array (
  237. 'condition' => false,
  238. 'content' => array('image' => ''),
  239. );
  240. $a['bx_if:icon'] = array (
  241. 'condition' => (bool)$sIcon,
  242. 'content' => array('icon' => $sIcon),
  243. );
  244. $a['bx_if:icon-html'] = array (
  245. 'condition' => (bool)$sIconHtml,
  246. 'content' => array('icon' => $sIconHtml),
  247. );
  248. $a['bx_if:icon-a'] = array (
  249. 'condition' => (bool)$sIconA,
  250. 'content' => array('icon-a' => $sIconA),
  251. );
  252. $a['bx_if:title'] = [
  253. 'condition' => (bool)$a['title'] && (!isset($a['icon_only']) || (int)$a['icon_only'] == 0),
  254. 'content' => [
  255. 'title' => $a['title'],
  256. 'title_attr' => $a['title_attr']
  257. ],
  258. ];
  259. $a['bx_if:info'] = [
  260. 'condition' => (bool)$a['info'],
  261. 'content' => [
  262. 'info' => $a['info'],
  263. ],
  264. ];
  265. $bOnClick = !empty($a['onclick']);
  266. $aOnClick = $bOnClick ? [
  267. 'onclick' => $a['onclick'],
  268. ] : [];
  269. $a['bx_if:onclick'] = [
  270. 'condition' => $bOnClick,
  271. 'content' => $aOnClick
  272. ];
  273. $aTmplVarsAddon = $this->_bDisplayAddons ? $this->_getTmplVarsAddon($mixedAddon, $a) : array('addon' => '', 'addonf' => '');
  274. $a['bx_if:addon'] = array (
  275. 'condition' => $this->_bDisplayAddons && !empty($aTmplVarsAddon['addon']),
  276. 'content' => $aTmplVarsAddon
  277. );
  278. $aTmplVarsSubitems = array('subitems' => '');
  279. $bTmplVarsSubitems = $this->_bMultilevel && !empty($a['subitems']);
  280. if($bTmplVarsSubitems) {
  281. $sClassCollpsed = 'bx-mi-collapsed';
  282. if(($iCollapsed = $this->getUserChoiceCollapsedSubmenu($a)) !== false)
  283. $a['class_add'] .= $iCollapsed ? ' ' . $sClassCollpsed : '';
  284. else if(isset($a['collapsed']) && $a['collapsed'])
  285. $a['class_add'] .= ' ' . $sClassCollpsed;
  286. $aSubitems = array();
  287. foreach($a['subitems'] as $aSubitem) {
  288. $aSubitem = $this->_getMenuItem($aSubitem);
  289. if($aSubitem !== false)
  290. $aSubitems[] = $aSubitem;
  291. }
  292. if(!$bOnClick)
  293. $a['bx_if:onclick'] = [
  294. 'condition' => true,
  295. 'content' => [
  296. 'onclick' => "javascript:return bx_menu_toggle(this, '" . $this->_sObject . "', '" . $a['name'] . "')"
  297. ]
  298. ];
  299. $aTmplVarsSubitems['subitems'] = $this->_oTemplate->parseHtmlByName(str_replace('.html', '_subitems.html', $this->getTemplateName()), array(
  300. 'bx_repeat:menu_items' => $aSubitems,
  301. ));
  302. }
  303. $a['bx_if:show_toggler'] = [
  304. 'condition' => $bTmplVarsSubitems,
  305. 'content' => []
  306. ];
  307. $a['bx_if:show_subitems'] = array (
  308. 'condition' => $bTmplVarsSubitems,
  309. 'content' => $aTmplVarsSubitems
  310. );
  311. unset($a['subitems']);
  312. foreach ($this->_aOptionalParams as $sName => $sDefaultValue)
  313. if (!isset($a[$sName]))
  314. $a[$sName] = $sDefaultValue;
  315. return $a;
  316. }
  317. protected function _getMenuIcon ($a)
  318. {
  319. return BxTemplFunctions::getInstanceWithTemplate($this->_oTemplate)->getIcon(!empty($a['icon']) ? $a['icon'] : '');
  320. }
  321. protected function _getMenuIconAttrs ($sType, $a)
  322. {
  323. $sAttrs = '';
  324. if($sType == 'image' && ($sTitleAttr = $this->_getMenuTitle($a)))
  325. $sAttrs .= ' alt="' . $sTitleAttr . '"';
  326. return $sAttrs;
  327. }
  328. protected function _getMenuTitle($a)
  329. {
  330. return bx_html_attribute(strip_tags(!empty($a['title_attr']) && ($sTitleAttr = _t($a['title_attr'])) ? $sTitleAttr : $a['title']));
  331. }
  332. public function getMenuIconHtml($sIcon)
  333. {
  334. list ($sIcon, $sIconUrl, $sIconA, $sIconHtml) = BxTemplFunctions::getInstanceWithTemplate($this->_oTemplate)->getIcon($sIcon);
  335. $a['bx_if:image'] = [
  336. 'condition' => (bool)$sIconUrl,
  337. 'content' => [
  338. 'icon_url' => $sIconUrl,
  339. 'attrs' => ''
  340. ],
  341. ];
  342. $a['bx_if:icon'] = array (
  343. 'condition' => (bool)$sIcon,
  344. 'content' => array('icon' => $sIcon),
  345. );
  346. $a['bx_if:icon-a'] = array (
  347. 'condition' => (bool)$sIconA,
  348. 'content' => array('icon-a' => $sIconA),
  349. );
  350. $a['bx_if:icon-html'] = array (
  351. 'condition' => (bool)$sIconHtml,
  352. 'content' => array('icon' => $sIconHtml),
  353. );
  354. return $this->_oTemplate->parseHtmlByName('menu_icon.html', $a);
  355. }
  356. protected function _getMenuAddon ($aMenuItem)
  357. {
  358. if (empty($aMenuItem['addon']))
  359. return '';
  360. if (isset($aMenuItem['addon_cache']) && $aMenuItem['addon_cache']) {
  361. $oCache = BxDolDb::getInstance()->getDbCacheObject();
  362. $sKey = 'menu_' . $this->_sObject . '_' . $aMenuItem['name'] . '_' . bx_get_logged_profile_id() . '_' . bx_site_hash() . '.php';
  363. $s = $oCache->getData($sKey);
  364. if ($s !== null) {
  365. return $s;
  366. }
  367. else {
  368. $s = BxDolService::callSerialized($aMenuItem['addon'], $this->_aMarkers);
  369. $oCache->setData($sKey, $s);
  370. }
  371. return $s;
  372. }
  373. else {
  374. return BxDolService::callSerialized($aMenuItem['addon'], $this->_aMarkers);
  375. }
  376. }
  377. protected function _getMenuMarkers ($aMenuItem)
  378. {
  379. if (empty($aMenuItem['markers']))
  380. return '';
  381. return BxDolService::callSerialized($aMenuItem['markers'], $this->_aMarkers);
  382. }
  383. protected function _getMenuAttrs ($aMenuItem)
  384. {
  385. $sAttrs = '';
  386. if(($sTitleAttr = $this->_getMenuTitle($aMenuItem)))
  387. $sAttrs .= ' title="' . $sTitleAttr . '"';
  388. if(!empty($aMenuItem['target']))
  389. $sAttrs .= ' target="' . $aMenuItem['target'] . '"';
  390. if($this->_bAddNoFollow && !empty($aMenuItem['link']) && preg_match('@^https?://@', $aMenuItem['link']) && strncmp($aMenuItem['link'], BX_DOL_URL_ROOT, strlen(BX_DOL_URL_ROOT)) !== 0)
  391. $sAttrs .= ' rel="noreferrer"';
  392. if(!empty($aMenuItem['area_label']))
  393. $sAttrs .= ' area-label="' . bx_html_attribute(_t($aMenuItem['area_label'])) . '"';
  394. return $sAttrs;
  395. }
  396. /**
  397. * Add css/js files which are needed for menu display and functionality.
  398. */
  399. protected function _addJsCss()
  400. {
  401. $this->_oTemplate->addCss('menu.css');
  402. }
  403. protected function _getTmplVarsAddon($mixedAddon, $aMenuItem)
  404. {
  405. $sAddon = '';
  406. if(!is_array($mixedAddon))
  407. $sAddon = $mixedAddon;
  408. else if(!empty($mixedAddon['addon']))
  409. $sAddon = $mixedAddon['addon'];
  410. return array(
  411. 'addon' => $sAddon,
  412. 'addonf' => ''
  413. );
  414. }
  415. }
  416. /** @} */