1
0

BxBaseMenu.php 27 KB

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