BxBaseMenu.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835
  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. /**
  22. * Menus drawn as tabs, by template or by a menu's own flag: picking one of their items plays Cuelume's select
  23. * (interface sounds, see bx_sound).
  24. */
  25. protected $_aTemplatesCueSelect = ['menu_main_submenu.html', 'menu_main_submenu_more_auto.html', 'menu_block_submenu_hor.html', 'menu_block_submenu_ver.html', 'menu_feeds.html'];
  26. protected $_bCueSelect = false;
  27. public function __construct ($aObject, $oTemplate)
  28. {
  29. parent::__construct ($aObject);
  30. if ($oTemplate)
  31. $this->_oTemplate = $oTemplate;
  32. else
  33. $this->_oTemplate = BxDolTemplate::getInstance();
  34. $this->_iPageType = false;
  35. $this->_aSelected = [];
  36. }
  37. public function getDisplayAddons()
  38. {
  39. return $this->_bDisplayAddons;
  40. }
  41. public function setDisplayAddons($b)
  42. {
  43. $bRet = $this->_bDisplayAddons;
  44. $this->_bDisplayAddons = $b;
  45. return $bRet;
  46. }
  47. /**
  48. * Get menu code.
  49. * @return string
  50. */
  51. public function getCode ()
  52. {
  53. $sMenuTitle = isset($this->_aObject['object']) ? $this->_aObject['object'] : 'Menu-' . rand(0, PHP_INT_MAX);
  54. if (isset($GLOBALS['bx_profiler']) && $GLOBALS['bx_profiler']->isMenu($sMenuTitle))
  55. $sMenuTitle .= rand(0, PHP_INT_MAX);
  56. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->beginMenu($sMenuTitle);
  57. $sRet = '';
  58. $bCacheUsed = false;
  59. $sCacheKey = '';
  60. $mixedCache = null;
  61. // Use cache only if:
  62. // - cache mode is set, AND
  63. // - it's not disabled (`off`), AND
  64. // - it's not "guest cache" while a user is logged in
  65. if (isset($this->_aObject['cache']) && !($this->_aObject['cache'] == 'off' || ($this->_aObject['cache'] == 'guest' && isLogged()))) {
  66. switch ($this->_aObject['cache']) {
  67. case 'global': // one cache for everyone
  68. case 'guest': // one cache for guests only
  69. $sCacheKey = "menu_{$this->_sObject}";
  70. break;
  71. case 'per_user': // separate cache for each user
  72. $sCacheKey = "menu_{$this->_sObject}_p" . bx_get_logged_profile_id();
  73. break;
  74. case 'per_acl': // separate cache for each ACL
  75. $aAcl = BxDolAcl::getInstance()->getMemberMembershipInfo(bx_get_logged_profile_id());
  76. $sCacheKey = "menu_{$this->_sObject}" . "_acl" . $aAcl['id'] . '_a' . (isAdmin() ? 1 : 0);
  77. break;
  78. default:
  79. trigger_error("Unknown cache mode for menu ({$this->_sObject}): {$this->_aObject['cache']}", E_USER_ERROR);
  80. break;
  81. }
  82. // The output marks the selected item (bx-menu-tab-active), so each selected item gets its own entry.
  83. $sCacheKey .= $this->_getCacheKeySelected($sCacheKey);
  84. $mixedCache = bx_content_cache_get($sCacheKey);
  85. }
  86. if ($mixedCache !== null) {
  87. if ($mixedCache)
  88. $this->_addJsCss();
  89. $bCacheUsed = true;
  90. $sRet = $mixedCache;
  91. }
  92. else {
  93. /**
  94. * @hooks
  95. * @hookdef hook-menu-get_code_before 'menu', 'get_code_before' - hook before menu output
  96. * - $unit_name - equals `menu`
  97. * - $action - equals `get_code_before`
  98. * - $object_id - not used
  99. * - $sender_id - not used
  100. * - $extra_params - array of additional params with the following array keys:
  101. * - `object_name` - menu object name
  102. * - `object_array` - menu object array
  103. * - `object` - menu object
  104. * - `override_result` - menu code
  105. * @hook @ref hook-menu-get_code_before
  106. */
  107. $mixedRes = null;
  108. bx_alert('menu', 'get_code_before', 0, 0, [
  109. 'object_name' => $this->_sObject,
  110. 'object_array' => $this->_aObject,
  111. 'object' => $this,
  112. 'override_result' => &$mixedRes,
  113. ]);
  114. if ($mixedRes !== null) {
  115. $sRet = $mixedRes;
  116. }
  117. else {
  118. if(!$this->_iPageType)
  119. $this->_iPageType = BxDolTemplate::getInstance()->getPageType();
  120. $s = '';
  121. $aVars = $this->_getTemplateVars ();
  122. if (!empty($aVars['bx_repeat:menu_items'])) {
  123. $this->_addJsCss();
  124. $s = $this->_getCode($this->_aObject['template'], $aVars);
  125. }
  126. /**
  127. * @hooks
  128. * @hookdef hook-menu-get_code_after 'menu', 'get_code_after' - hook after menu output
  129. * - $unit_name - equals `menu`
  130. * - $action - equals `get_code_after`
  131. * - $object_id - not used
  132. * - $sender_id - not used
  133. * - $extra_params - array of additional params with the following array keys:
  134. * - `object_name` - menu object name
  135. * - `object_array` - menu object array
  136. * - `object` - menu object
  137. * - `override_result` - menu code
  138. * @hook @ref hook-menu-get_code_after
  139. */
  140. $mixedRes = null;
  141. bx_alert('menu', 'get_code_after', 0, 0, [
  142. 'object_name' => $this->_sObject,
  143. 'object_array' => $this->_aObject,
  144. 'object' => $this,
  145. 'vars' => $aVars,
  146. 'override_result' => &$s,
  147. ]);
  148. $sRet = $s;
  149. }
  150. if ($sCacheKey)
  151. bx_content_cache_set($sCacheKey, $sRet);
  152. }
  153. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->endMenu($sMenuTitle, $this->_aObject['cache'] ?? 'undefined', $bCacheUsed);
  154. return $sRet;
  155. }
  156. /**
  157. * Get menu code API.
  158. * @return array
  159. */
  160. public function getCodeAPI ()
  161. {
  162. $aItems = [];
  163. $aVars = $this->_getTemplateVars();
  164. if(!empty($aVars['bx_repeat:menu_items']))
  165. $aItems = $aVars['bx_repeat:menu_items'];
  166. return [
  167. 'object' => $this->_sObject,
  168. 'title' => _t($this->_aObject['title_public']),
  169. 'config' => $this->_aObject['config_api'],
  170. 'persistent' => $this->_aObject['persistent'],
  171. 'params' => $this->getContentParams(),
  172. 'items' => $aItems
  173. ];
  174. }
  175. /**
  176. * Get menu short code API.
  177. * @return array
  178. */
  179. public function getShortCodeAPI ()
  180. {
  181. return [
  182. 'object' => $this->_sObject,
  183. 'title' => _t($this->_aObject['title_public']),
  184. 'params' => $this->getContentParams()
  185. ];
  186. }
  187. protected function _getCode($sTmplName, $aTmplVars)
  188. {
  189. return $this->_oTemplate->parseHtmlByName($this->getTemplateName($sTmplName), $aTmplVars);
  190. }
  191. public function getCodeItem ($sName)
  192. {
  193. if(empty($sName))
  194. return '';
  195. $sCode = $this->_oTemplate->getHtml(str_replace('.html', '_item.html', $this->_aObject['template']));
  196. if(empty($sCode))
  197. return '';
  198. $mixedTmplVars = $this->getMenuItem($sName);
  199. if($mixedTmplVars === false)
  200. return '';
  201. return $this->_oTemplate->parseHtmlByContent($sCode, $mixedTmplVars);
  202. }
  203. /**
  204. * Get template variables array
  205. * @return array
  206. */
  207. protected function _getTemplateVars ()
  208. {
  209. $aMenuItems = $this->getMenuItems();
  210. $sMenuItemSelected = _t($aMenuItems && $this->_aSelected && !empty($this->_aSelected['title']) ? $this->_aSelected['title'] : '_Contents');
  211. return [
  212. 'object' => $this->_sObject,
  213. 'menu_item_selected' => $sMenuItemSelected,
  214. 'bx_repeat:menu_items' => $aMenuItems,
  215. ];
  216. }
  217. /**
  218. * Get menu items array, which is ready to pass to menu template.
  219. * @return array or false
  220. */
  221. public function getMenuItems ()
  222. {
  223. if (!isset($this->_aObject['menu_items']))
  224. $this->_aObject['menu_items'] = $this->getMenuItemsRaw ();
  225. $aItems = array();
  226. foreach ($this->_aObject['menu_items'] as $aItem) {
  227. $aItem = $this->_getMenuItem ($aItem);
  228. if($aItem !== false)
  229. $aItems[] = $aItem;
  230. }
  231. return $aItems;
  232. }
  233. /**
  234. * Get menu item array, which is ready to pass to whole menu or
  235. * single menu item template. May return false if single menu item
  236. * is requested but cannot be shown by circumstances.
  237. * @return array or false
  238. */
  239. public function getMenuItem ($sName)
  240. {
  241. if (!isset($this->_aObject['menu_items']))
  242. $this->_aObject['menu_items'] = $this->getMenuItemsRaw ();
  243. if(!empty($sName))
  244. return $this->_getMenuItem($this->_aObject['menu_items'][$sName]);
  245. }
  246. /**
  247. * Get menu items array, this is just a wrapper for DB function for make it easier to override.
  248. * It is used in @see BxBaseMenu::getMenuItems
  249. * @return array
  250. */
  251. protected function getMenuItemsRaw ()
  252. {
  253. if($this->isMultilevel())
  254. return $this->_oQuery->getMenuItemsHierarchy();
  255. else
  256. return $this->_oQuery->getMenuItems();
  257. }
  258. /**
  259. * Cache key part for the item this menu shows as selected: empty when none of its items is,
  260. * so pages that select nothing in this menu share one entry.
  261. * The items' module and name pairs are kept in the content cache next to the menu, under the
  262. * same key, so a cache hit needs no query and they go stale exactly when the cached menu does.
  263. * @param $sCacheKey the menu's own cache key
  264. */
  265. protected function _getCacheKeySelected ($sCacheKey)
  266. {
  267. $sItemsKey = $sCacheKey . '_items';
  268. $aItems = bx_content_cache_get($sItemsKey);
  269. if (!is_array($aItems)) {
  270. if (!isset($this->_aObject['menu_items'])) {
  271. $this->_aObject['menu_items'] = $this->getMenuItemsRaw();
  272. }
  273. $aItems = $this->_getSelectableItems($this->_aObject['menu_items']);
  274. bx_content_cache_set($sItemsKey, $aItems);
  275. }
  276. $sResult = '';
  277. foreach ($aItems as $a) {
  278. if ($this->_isSelected($a)) {
  279. // A short fixed-length suffix whatever the name's length, so the cache file name stays valid.
  280. $sResult = '_sel' . substr(hash('sha256', $a['module'] . ':' . $a['name']), 0, 16);
  281. break;
  282. }
  283. }
  284. return $sResult;
  285. }
  286. /**
  287. * Module and name of every item, sub-items included, which is all _isSelected() looks at.
  288. */
  289. protected function _getSelectableItems ($aItems)
  290. {
  291. $aResult = [];
  292. if (!is_array($aItems)) {
  293. return $aResult;
  294. }
  295. foreach ($aItems as $a) {
  296. if (!is_array($a) || !isset($a['name'])) {
  297. continue;
  298. }
  299. $aResult[] = ['module' => isset($a['module']) ? $a['module'] : '', 'name' => $a['name']];
  300. if (!empty($a['subitems'])) {
  301. $aResult = array_merge($aResult, $this->_getSelectableItems($a['subitems']));
  302. }
  303. }
  304. return $aResult;
  305. }
  306. protected function getMenuItemsRawProfileRelated ()
  307. {
  308. $oProfile = BxDolProfile::getInstance();
  309. if(!$oProfile)
  310. return [];
  311. $aItems = $this->_oQuery->getMenuItemsBy([
  312. 'type' => 'set_name',
  313. 'set_name' => $this->_aObject['set_name']
  314. ]);
  315. $aDuplicates = $this->_oQuery->getMenuItemsBy([
  316. 'type' => 'set_name_duplicates',
  317. 'set_name' => $this->_aObject['set_name']
  318. ]);
  319. $sModule = $oProfile->getModule();
  320. $aResult = [];
  321. foreach($aItems as $aItem) {
  322. if(in_array($aItem['name'], $aDuplicates) && $aItem['module'] != $sModule)
  323. continue;
  324. $aResult[$aItem['name']] = $aItem;
  325. }
  326. return $aResult;
  327. }
  328. protected function _getMenuItem ($a)
  329. {
  330. if (!$this->_isActive($a) || !$this->_isVisible($a))
  331. return false;
  332. if ($this->_bIsApi)
  333. return $this->_getMenuItemAPI ($a);
  334. $bIsSelected = $this->_isSelected($a);
  335. if($bIsSelected)
  336. $this->_aSelected = $a;
  337. $a['object'] = $this->_sObject;
  338. $a['title'] = _t($a['title']);
  339. $a['title_attr'] = $this->_getMenuTitle($a);
  340. $a['info'] = isset($a['info']) ? _t($a['info']) : '';
  341. $this->removeMarker('addon');
  342. $a = $this->_replaceMarkers($a);
  343. if ($this->_bDisplayAddons) {
  344. $mixedAddon = $this->_getMenuAddon($a);
  345. if (!is_array($mixedAddon)) {
  346. $this->addMarkers(array('addon' => $mixedAddon));
  347. $a = $this->_replaceMarkers($a);
  348. }
  349. }
  350. $aMarkers = $this->_getMenuMarkers($a);
  351. if ($aMarkers && is_array($aMarkers)) {
  352. $this->addMarkers($aMarkers);
  353. $a = $this->_replaceMarkers($a);
  354. }
  355. list ($sIcon, $sIconUrl, $sIconA, $sIconHtml) = $this->_getMenuIcon($a);
  356. if(!isset($a['class_add']))
  357. $a['class_add'] = '';
  358. if($bIsSelected)
  359. $a['class_add'] .= ' bx-menu-tab-active';
  360. $a['class_add'] .= $this->_getVisibilityClass($a);
  361. $a['class_link'] = '';
  362. $a['link'] = isset($a['link']) ? $this->_oPermalinks->permalink($a['link']) : 'javascript:void(0);';
  363. $aTmplVarsAddon = $this->_bDisplayAddons ? $this->_getTmplVarsAddon($mixedAddon, $a) : ['addon' => '', 'addonf' => ''];
  364. $a['bx_if:addon'] = [
  365. 'condition' => $this->_bDisplayAddons && !empty($aTmplVarsAddon['addon']),
  366. 'content' => $aTmplVarsAddon
  367. ];
  368. $a['bx_if:info'] = [
  369. 'condition' => (bool)$a['info'],
  370. 'content' => [
  371. 'info' => $a['info'],
  372. ],
  373. ];
  374. // The More item of a tab row ('more-auto', BxBaseMenuMoreAuto) opens a popup, which plays its own cue.
  375. if(($this->_bCueSelect || (isset($this->_aObject['template']) && in_array($this->_aObject['template'], $this->_aTemplatesCueSelect))) && $a['name'] != 'more-auto')
  376. $a['attrs_add'] = trim((!empty($a['attrs_add']) ? $a['attrs_add'] : '') . ' data-cuelume-select');
  377. $a['attrs'] = $this->_getMenuAttrs($a);
  378. $a['attrs_wrp'] = '';
  379. if($this->_bHx && !empty($a['link']) && strpos($a['link'], 'javascript:') === false) {
  380. $this->_aHx['get'] = $a['link'];
  381. $a['attrs'] .= bx_get_htmx_attrs($this->_aHx, $this->_mHxPreload);
  382. if(!bx_is_htmx_request() && !$bIsSelected)
  383. $a['attrs_wrp'] .= bx_get_htmx_attrs([
  384. 'get' => $a['link'],
  385. 'trigger' => 'load',
  386. 'target' => '#bx-content-preload',
  387. 'swap' => 'beforeend',
  388. ]);
  389. }
  390. $a['bx_if:image'] = [
  391. 'condition' => (bool)$sIconUrl,
  392. 'content' => [
  393. 'icon_url' => $sIconUrl,
  394. 'attrs' => $this->_getMenuIconAttrs('image', $a)
  395. ],
  396. ];
  397. $a['bx_if:image_inline'] = array (
  398. 'condition' => false,
  399. 'content' => array('image' => ''),
  400. );
  401. $a['bx_if:icon'] = array (
  402. 'condition' => (bool)$sIcon,
  403. 'content' => array('icon' => $sIcon),
  404. );
  405. $a['bx_if:icon-html'] = array (
  406. 'condition' => (bool)$sIconHtml,
  407. 'content' => array('icon' => $sIconHtml),
  408. );
  409. $a['bx_if:icon-a'] = array (
  410. 'condition' => (bool)$sIconA,
  411. 'content' => array('icon-a' => $sIconA),
  412. );
  413. $a['bx_if:title'] = [
  414. 'condition' => (bool)$a['title'] && (!isset($a['icon_only']) || (int)$a['icon_only'] == 0),
  415. 'content' => [
  416. 'title' => $a['title'],
  417. 'title_attr' => $a['title_attr'],
  418. 'bx_if:info' => $a['bx_if:info'],
  419. 'bx_if:addon' => $a['bx_if:addon']
  420. ],
  421. ];
  422. $bOnClick = !empty($a['onclick']);
  423. $aOnClick = $bOnClick ? [
  424. 'onclick' => $a['onclick'],
  425. ] : [];
  426. $a['bx_if:onclick'] = [
  427. 'condition' => $bOnClick,
  428. 'content' => $aOnClick
  429. ];
  430. $aTmplVarsSubitems = array('subitems' => '');
  431. $bTmplVarsSubitems = $this->isMultilevel() && !empty($a['subitems']);
  432. if($bTmplVarsSubitems) {
  433. $sClassCollpsed = 'bx-mi-collapsed';
  434. if(($iCollapsed = $this->getUserChoiceCollapsedSubmenu($a)) !== false)
  435. $a['class_add'] .= $iCollapsed ? ' ' . $sClassCollpsed : '';
  436. else if(isset($a['collapsed']) && $a['collapsed'])
  437. $a['class_add'] .= ' ' . $sClassCollpsed;
  438. $aSubitems = array();
  439. foreach($a['subitems'] as $aSubitem) {
  440. $aSubitem = $this->_getMenuItem($aSubitem);
  441. if($aSubitem !== false)
  442. $aSubitems[] = $aSubitem;
  443. }
  444. if(!$bOnClick)
  445. $a['bx_if:onclick'] = [
  446. 'condition' => true,
  447. 'content' => [
  448. 'onclick' => "javascript:return bx_menu_toggle(this, '" . $this->_sObject . "', '" . $a['name'] . "')"
  449. ]
  450. ];
  451. $aTmplVarsSubitems['subitems'] = $this->_oTemplate->parseHtmlByName(str_replace('.html', '_subitems.html', $this->getTemplateName()), array(
  452. 'bx_repeat:menu_items' => $aSubitems,
  453. ));
  454. }
  455. $a['bx_if:show_toggler'] = [
  456. 'condition' => $bTmplVarsSubitems,
  457. 'content' => []
  458. ];
  459. $a['bx_if:show_subitems'] = array (
  460. 'condition' => $bTmplVarsSubitems,
  461. 'content' => $aTmplVarsSubitems
  462. );
  463. unset($a['subitems']);
  464. foreach ($this->_aOptionalParams as $sName => $sDefaultValue)
  465. if (!isset($a[$sName]))
  466. $a[$sName] = $sDefaultValue;
  467. return $a;
  468. }
  469. protected function _getMenuItemAPI ($a)
  470. {
  471. list ($sIcon, $sIconUrl, $sIconA, $sIconHtml) = $this->_getMenuIcon($a);
  472. $aResult = [
  473. 'id' => $a['id'],
  474. 'name' => $a['name'],
  475. 'title' => _t($a['title']),
  476. 'info' => isset($a['info']) ? _t($a['info']) : '',
  477. 'link' => isset($a['link']) ? $a['link'] : '',
  478. 'icon' => $sIcon ? BxDolIconset::getObjectInstance()->getIcon($sIcon) : ($sIconHtml ? $sIconHtml : ''),
  479. 'image' => $sIconUrl ? $sIconUrl : '',
  480. 'submenu' => !empty($a['submenu_object']) ? $a['submenu_object'] : '',
  481. 'addon' => $this->_bDisplayAddons ? $this->_getMenuAddon($a) : '',
  482. 'hidden_on' => isset($a['hidden_on']) ? $a['hidden_on'] : false,
  483. 'hidden_on_col' => isset($a['hidden_on_col']) ? $a['hidden_on_col'] : false,
  484. 'config' => isset($a['config_api']) ? $a['config_api'] : '',
  485. 'primary' => isset($a['primary']) ? $a['primary'] : 0,
  486. 'persistent' => isset($a['persistent']) ? $a['persistent'] : 0,
  487. ];
  488. if(!empty($a['onclick']))
  489. $aResult = array_merge($aResult, [
  490. 'display_type' => 'callback',
  491. 'data' => $this->_getMenuCallbackDataAPI($a)
  492. ]);
  493. $this->_updateVisibilityParamsAPI($aResult);
  494. if(($aMarkers = $this->_getMenuMarkers($a)) && is_array($aMarkers))
  495. $this->addMarkers($aMarkers);
  496. $aResult = $this->_replaceMarkers($aResult);
  497. if(!empty($aResult['link'])) {
  498. if(($sSubstr = 'javascript') && substr($aResult['link'], 0, strlen($sSubstr)) == $sSubstr)
  499. $aResult['link'] = '';
  500. else
  501. $aResult['link'] = $this->_oPermalinks->permalink($aResult['link']);
  502. }
  503. if($this->isMultilevel() && !empty($a['subitems'])) {
  504. $aSubitems = [];
  505. foreach($a['subitems'] as $aSubitem) {
  506. $aSubitem = $this->_getMenuItem($aSubitem);
  507. if($aSubitem !== false)
  508. $aSubitems[] = $aSubitem;
  509. }
  510. $aResult['subitems'] = $aSubitems;
  511. }
  512. return $aResult;
  513. }
  514. protected function _getMenuIcon ($a)
  515. {
  516. return BxTemplFunctions::getInstanceWithTemplate($this->_oTemplate)->getIcon(!empty($a['icon']) ? $a['icon'] : '');
  517. }
  518. protected function _getMenuIconAttrs ($sType, $a)
  519. {
  520. $sAttrs = '';
  521. if($sType == 'image')
  522. $sAttrs .= ' alt="' . $this->_getMenuTitle($a) . '"';
  523. return $sAttrs;
  524. }
  525. protected function _getMenuTitle($a)
  526. {
  527. $sTitle = ($sTitleAttr = $a['title_attr'] ?? '') && ($sTitleAttr = _t($sTitleAttr)) ? $sTitleAttr : ($a['title'] ?? '');
  528. /**
  529. * An icon only menu item (the toolbar hamburger, [+ Add], [Notifications], the 'more' triggers, ...) has an
  530. * empty `title` in `sys_menu_items`, so it used to get no accessible name at all. Every menu item does have
  531. * `title_system` - the label the Studio menu builder shows - so it is used as the fallback name. No new
  532. * language keys are needed and every icon only menu item benefits, not just the toolbar.
  533. */
  534. if(trim(strip_tags($sTitle)) === '' && ($sTitleSystem = $a['title_system'] ?? ''))
  535. $sTitle = _t($sTitleSystem);
  536. return bx_html_attribute(strip_tags($sTitle));
  537. }
  538. protected function _getMenuAreaLabel($a)
  539. {
  540. return ($sAreaLabel = $a['area_label'] ?? '') && ($sAreaLabel = strip_tags(_t($sAreaLabel))) ? bx_html_attribute($sAreaLabel) : '';
  541. }
  542. /**
  543. * True when the visible content of the menu item link already carries its accessible name AND that content is
  544. * richer than the title alone (an addon counter is rendered next to it). In that case an aria-label repeating
  545. * only the title would hide the counter from the accessible name and break WCAG 2.5.3 Label in Name, so it is
  546. * omitted and the link is named by its own text. Icon only items - and items whose template never prints the
  547. * title - keep their aria-label.
  548. */
  549. protected function _isMenuLabelInContent($a)
  550. {
  551. $bTitle = !empty($a['title']) && (!isset($a['icon_only']) || (int)$a['icon_only'] == 0);
  552. return $bTitle && !empty($a['bx_if:addon']['condition']);
  553. }
  554. protected function _getMenuCallbackDataAPI($a)
  555. {
  556. return [];
  557. }
  558. public function getMenuIconHtml($sIcon)
  559. {
  560. list ($sIcon, $sIconUrl, $sIconA, $sIconHtml) = BxTemplFunctions::getInstanceWithTemplate($this->_oTemplate)->getIcon($sIcon);
  561. $a['bx_if:image'] = [
  562. 'condition' => (bool)$sIconUrl,
  563. 'content' => [
  564. 'icon_url' => $sIconUrl,
  565. 'attrs' => 'alt=""'
  566. ],
  567. ];
  568. $a['bx_if:icon'] = array (
  569. 'condition' => (bool)$sIcon,
  570. 'content' => array('icon' => $sIcon),
  571. );
  572. $a['bx_if:icon-a'] = array (
  573. 'condition' => (bool)$sIconA,
  574. 'content' => array('icon-a' => $sIconA),
  575. );
  576. $a['bx_if:icon-html'] = array (
  577. 'condition' => (bool)$sIconHtml,
  578. 'content' => array('icon' => $sIconHtml),
  579. );
  580. return $this->_oTemplate->parseHtmlByName('menu_icon.html', $a);
  581. }
  582. protected function _getMenuAddon ($aMenuItem)
  583. {
  584. if (empty($aMenuItem['addon']))
  585. return '';
  586. if (isset($aMenuItem['addon_cache']) && $aMenuItem['addon_cache']) {
  587. $oCache = BxDolDb::getInstance()->getDbCacheObject();
  588. $sKey = 'menu_' . $this->_sObject . '_' . $aMenuItem['name'] . '_' . bx_get_logged_profile_id() . '_' . bx_site_hash() . '.php';
  589. $s = $oCache->getData($sKey);
  590. if ($s !== null) {
  591. return $s;
  592. }
  593. else {
  594. $s = BxDolService::callSerialized($aMenuItem['addon'], $this->_aMarkers);
  595. $oCache->setData($sKey, $s);
  596. }
  597. return $s;
  598. }
  599. else {
  600. return BxDolService::callSerialized($aMenuItem['addon'], $this->_aMarkers);
  601. }
  602. }
  603. protected function _getMenuMarkers ($aMenuItem)
  604. {
  605. if (empty($aMenuItem['markers']))
  606. return '';
  607. return BxDolService::callSerialized($aMenuItem['markers'], $this->_aMarkers);
  608. }
  609. protected function _getMenuAttrs ($aMenuItem)
  610. {
  611. $sAttrs = '';
  612. $sTitleAttr = $this->_getMenuTitle($aMenuItem);
  613. if($sTitleAttr)
  614. $sAttrs .= ' title="' . $sTitleAttr . '"';
  615. if(!empty($aMenuItem['target']))
  616. $sAttrs .= ' target="' . $aMenuItem['target'] . '"';
  617. if($this->_bAddNoFollow && !empty($aMenuItem['link']) && preg_match('@^https?://@', $aMenuItem['link']) && strncmp($aMenuItem['link'], BX_DOL_URL_ROOT, strlen(BX_DOL_URL_ROOT)) !== 0)
  618. $sAttrs .= ' rel="noreferrer"';
  619. /**
  620. * `attrs_add` lets a menu item declare extra attributes of its own - the same convention as `class_add` -
  621. * so that a PHP built item (a disclosure trigger, for example) can add aria-haspopup/aria-expanded without
  622. * a mechanism of its own.
  623. */
  624. if(!empty($aMenuItem['attrs_add']))
  625. $sAttrs .= ' ' . $aMenuItem['attrs_add'];
  626. /**
  627. * An explicit `area_label` always wins. Otherwise the item title is used as the accessible name, but only
  628. * when the visible content of the link does not already provide one - an aria-label that repeats the title
  629. * while the link also renders an addon (a counter) hides that counter from the accessible name and breaks
  630. * WCAG 2.5.3 Label in Name.
  631. */
  632. if(!($sAreaLabel = $this->_getMenuAreaLabel($aMenuItem)) && !$this->_isMenuLabelInContent($aMenuItem))
  633. $sAreaLabel = $sTitleAttr;
  634. if($sAreaLabel)
  635. $sAttrs .= ' aria-label="' . $sAreaLabel . '"';
  636. return $sAttrs;
  637. }
  638. protected function _addMenuItemsMoreLess($aItems, $iMenuItemsMin)
  639. {
  640. $iMenuItems = count($aItems);
  641. if($iMenuItems <= $iMenuItemsMin)
  642. return $aItems;
  643. $mixedCollpsed = $this->getUserChoiceCollapsed();
  644. $bCollpsed = $mixedCollpsed === false || $mixedCollpsed == 1;
  645. for($i = $iMenuItemsMin; $i < $iMenuItems; $i++)
  646. $aItems[$i]['class_add'] .= ' bx-mi-aux' . ($bCollpsed ? ' bx-mi-hidden' : '');
  647. $aShowMoreLinks = [
  648. 'more' => ['title' => '_sys_show_more', 'icon' => 'chevron-down', 'class' => $bCollpsed ? '' : 'bx-mi-hidden'],
  649. 'less' => ['title' => '_sys_show_less', 'icon' => 'chevron-up', 'class' => !$bCollpsed ? '' : 'bx-mi-hidden']
  650. ];
  651. foreach($aShowMoreLinks as $sLink => $aLink)
  652. $aItems[] = [
  653. 'class_add' => 'bx-psmi-show-' . $sLink . ' ' . $aLink['class'],
  654. 'name' => 'show-' . $sLink,
  655. 'title' => _t($aLink['title']),
  656. 'link' => 'javascript:void(0)',
  657. 'bx_if:onclick' => [
  658. 'condition' => true,
  659. 'content' => [
  660. 'onclick' => 'bx_menu_show_more_less(this, \'' . $this->_sObject . '\', \'.bx-menu-object-' . $this->_sObject . '\')',
  661. ]
  662. ],
  663. 'attrs' => '',
  664. 'bx_if:image' => [
  665. 'condition' => false,
  666. 'content' => ['icon_url' => ''],
  667. ],
  668. 'bx_if:image_inline' => [
  669. 'condition' => false,
  670. 'content' => ['image' => ''],
  671. ],
  672. 'bx_if:icon' => [
  673. 'condition' => true,
  674. 'content' => ['icon' => $aLink['icon']],
  675. ],
  676. 'bx_if:icon-html' => [
  677. 'condition' => false,
  678. 'content' => ['icon-a' => ''],
  679. ],
  680. 'bx_if:icon-a' => [
  681. 'condition' => false,
  682. 'content' => ['icon-a' => ''],
  683. ],
  684. 'bx_if:addon' => [
  685. 'condition' => false,
  686. 'content' => []
  687. ]
  688. ];
  689. return $aItems;
  690. }
  691. /**
  692. * Add css/js files which are needed for menu display and functionality.
  693. */
  694. protected function _addJsCss()
  695. {
  696. $this->_oTemplate->addCss('menu.css');
  697. }
  698. protected function _getTmplVarsAddon($mixedAddon, $aMenuItem)
  699. {
  700. $sAddon = '';
  701. if(!is_array($mixedAddon))
  702. $sAddon = $mixedAddon;
  703. else if(!empty($mixedAddon['addon']))
  704. $sAddon = $mixedAddon['addon'];
  705. return array(
  706. 'addon' => $sAddon,
  707. 'addonf' => ''
  708. );
  709. }
  710. }
  711. /** @} */