BxBaseMenu.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588
  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. $bCacheUsed = false;
  48. $sRet = '';
  49. $sMenuTitle = isset($this->_aObject['title']) ? _t($this->_aObject['title']) : 'Menu-' . rand(0, PHP_INT_MAX);
  50. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->beginMenu($sMenuTitle);
  51. $sCacheKey = '';
  52. $mixedCache = null;
  53. // Use cache only if:
  54. // - cache mode is set, AND
  55. // - it's not disabled (`off`), AND
  56. // - it's not "guest cache" while a user is logged in
  57. if (isset($this->_aObject['cache']) && !($this->_aObject['cache'] == 'off' || ($this->_aObject['cache'] == 'guest' && isLogged()))) {
  58. switch ($this->_aObject['cache']) {
  59. case 'global': // one cache for everyone
  60. case 'guest': // one cache for guests only
  61. $sCacheKey = "menu_{$this->_sObject}";
  62. break;
  63. case 'per_user': // separate cache for each user
  64. $sCacheKey = "menu_{$this->_sObject}_p" . bx_get_logged_profile_id();
  65. break;
  66. case 'per_acl': // separate cache for each ACL
  67. $aAcl = BxDolAcl::getInstance()->getMemberMembershipInfo(bx_get_logged_profile_id());
  68. $sCacheKey = "menu_{$this->_sObject}" . "_acl" . $aAcl['id'];
  69. break;
  70. default:
  71. trigger_error("Unknown cache mode for menu ({$this->_sObject}): {$this->_aObject['cache']}", E_USER_ERROR);
  72. break;
  73. }
  74. $mixedCache = bx_content_cache_get($sCacheKey);
  75. }
  76. if ($mixedCache !== null) {
  77. if ($mixedCache)
  78. $this->_addJsCss();
  79. $bCacheUsed = true;
  80. $sRet = $mixedCache;
  81. }
  82. else {
  83. /**
  84. * @hooks
  85. * @hookdef hook-menu-get_code_before 'menu', 'get_code_before' - hook before menu output
  86. * - $unit_name - equals `menu`
  87. * - $action - equals `get_code_before`
  88. * - $object_id - not used
  89. * - $sender_id - not used
  90. * - $extra_params - array of additional params with the following array keys:
  91. * - `object_name` - menu object name
  92. * - `object_array` - menu object array
  93. * - `object` - menu object
  94. * - `override_result` - menu code
  95. * @hook @ref hook-menu-get_code_before
  96. */
  97. $mixedRes = null;
  98. bx_alert('menu', 'get_code_before', 0, 0, [
  99. 'object_name' => $this->_sObject,
  100. 'object_array' => $this->_aObject,
  101. 'object' => $this,
  102. 'override_result' => &$mixedRes,
  103. ]);
  104. if ($mixedRes !== null) {
  105. $sRet = $mixedRes;
  106. }
  107. else {
  108. if(!$this->_iPageType)
  109. $this->_iPageType = BxDolTemplate::getInstance()->getPageType();
  110. $s = '';
  111. $aVars = $this->_getTemplateVars ();
  112. if (!empty($aVars['bx_repeat:menu_items'])) {
  113. $this->_addJsCss();
  114. $s = $this->_getCode($this->_aObject['template'], $aVars);
  115. }
  116. /**
  117. * @hooks
  118. * @hookdef hook-menu-get_code_after 'menu', 'get_code_after' - hook after menu output
  119. * - $unit_name - equals `menu`
  120. * - $action - equals `get_code_after`
  121. * - $object_id - not used
  122. * - $sender_id - not used
  123. * - $extra_params - array of additional params with the following array keys:
  124. * - `object_name` - menu object name
  125. * - `object_array` - menu object array
  126. * - `object` - menu object
  127. * - `override_result` - menu code
  128. * @hook @ref hook-menu-get_code_after
  129. */
  130. $mixedRes = null;
  131. bx_alert('menu', 'get_code_after', 0, 0, [
  132. 'object_name' => $this->_sObject,
  133. 'object_array' => $this->_aObject,
  134. 'object' => $this,
  135. 'vars' => $aVars,
  136. 'override_result' => &$s,
  137. ]);
  138. $sRet = $s;
  139. }
  140. if ($sCacheKey)
  141. bx_content_cache_set($sCacheKey, $sRet);
  142. }
  143. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->endMenu($sMenuTitle, $this->_aObject['cache'] ?? 'undefined', $bCacheUsed);
  144. return $sRet;
  145. }
  146. /**
  147. * Get menu code API.
  148. * @return array
  149. */
  150. public function getCodeAPI ()
  151. {
  152. $aItems = [];
  153. $aVars = $this->_getTemplateVars();
  154. if(!empty($aVars['bx_repeat:menu_items']))
  155. $aItems = $aVars['bx_repeat:menu_items'];
  156. return [
  157. 'object' => $this->_sObject,
  158. 'config' => $this->_aObject['config_api'],
  159. 'persistent' => $this->_aObject['persistent'],
  160. 'params' => $this->getContentParams(),
  161. 'items' => $aItems
  162. ];
  163. }
  164. /**
  165. * Get menu short code API.
  166. * @return array
  167. */
  168. public function getShortCodeAPI ()
  169. {
  170. return [
  171. 'object' => $this->_sObject,
  172. 'params' => $this->getContentParams()
  173. ];
  174. }
  175. protected function _getCode($sTmplName, $aTmplVars)
  176. {
  177. return $this->_oTemplate->parseHtmlByName($this->getTemplateName($sTmplName), $aTmplVars);
  178. }
  179. public function getCodeItem ($sName)
  180. {
  181. if(empty($sName))
  182. return '';
  183. $sCode = $this->_oTemplate->getHtml(str_replace('.html', '_item.html', $this->_aObject['template']));
  184. if(empty($sCode))
  185. return '';
  186. $mixedTmplVars = $this->getMenuItem($sName);
  187. if($mixedTmplVars === false)
  188. return '';
  189. return $this->_oTemplate->parseHtmlByContent($sCode, $mixedTmplVars);
  190. }
  191. /**
  192. * Get template variables array
  193. * @return array
  194. */
  195. protected function _getTemplateVars ()
  196. {
  197. $aMenuItems = $this->getMenuItems();
  198. $sMenuItemSelected = _t($aMenuItems && $this->_aSelected && !empty($this->_aSelected['title']) ? $this->_aSelected['title'] : '_Contents');
  199. return [
  200. 'object' => $this->_sObject,
  201. 'menu_item_selected' => $sMenuItemSelected,
  202. 'bx_repeat:menu_items' => $aMenuItems,
  203. ];
  204. }
  205. /**
  206. * Get menu items array, which is ready to pass to menu template.
  207. * @return array or false
  208. */
  209. public function getMenuItems ()
  210. {
  211. if (!isset($this->_aObject['menu_items']))
  212. $this->_aObject['menu_items'] = $this->getMenuItemsRaw ();
  213. $aItems = array();
  214. foreach ($this->_aObject['menu_items'] as $aItem) {
  215. $aItem = $this->_getMenuItem ($aItem);
  216. if($aItem !== false)
  217. $aItems[] = $aItem;
  218. }
  219. return $aItems;
  220. }
  221. /**
  222. * Get menu item array, which is ready to pass to whole menu or
  223. * single menu item template. May return false if single menu item
  224. * is requested but cannot be shown by circumstances.
  225. * @return array or false
  226. */
  227. public function getMenuItem ($sName)
  228. {
  229. if (!isset($this->_aObject['menu_items']))
  230. $this->_aObject['menu_items'] = $this->getMenuItemsRaw ();
  231. if(!empty($sName))
  232. return $this->_getMenuItem($this->_aObject['menu_items'][$sName]);
  233. }
  234. /**
  235. * Get menu items array, this is just a wrapper for DB function for make it easier to override.
  236. * It is used in @see BxBaseMenu::getMenuItems
  237. * @return array
  238. */
  239. protected function getMenuItemsRaw ()
  240. {
  241. if($this->_bMultilevel)
  242. return $this->_oQuery->getMenuItemsHierarchy();
  243. else
  244. return $this->_oQuery->getMenuItems();
  245. }
  246. protected function _getMenuItem ($a)
  247. {
  248. if (!$this->_isActive($a) || !$this->_isVisible($a))
  249. return false;
  250. if ($this->_bIsApi) {
  251. list ($sIcon, $sIconUrl, $sIconA, $sIconHtml) = $this->_getMenuIcon($a);
  252. $aResult = [
  253. 'id' => $a['id'],
  254. 'name' => $a['name'],
  255. 'title' => _t($a['title']),
  256. 'info' => isset($a['info']) ? _t($a['info']) : '',
  257. 'link' => isset($a['link']) ? $a['link'] : '',
  258. 'icon' => $sIcon ? $sIcon : ($sIconHtml ? $sIconHtml : ''),
  259. 'image' => $sIconUrl ? $sIconUrl : '',
  260. 'submenu' => !empty($a['submenu_object']) ? $a['submenu_object'] : '',
  261. 'addon' => $this->_bDisplayAddons ? $this->_getMenuAddon($a) : '',
  262. 'config' => isset($a['config_api']) ? $a['config_api'] : '',
  263. 'primary' => isset($a['primary']) ? $a['primary'] : 0,
  264. 'persistent' => isset($a['persistent']) ? $a['persistent'] : 0,
  265. ];
  266. if(($aMarkers = $this->_getMenuMarkers($a)) && is_array($aMarkers))
  267. $this->addMarkers($aMarkers);
  268. $aResult = $this->_replaceMarkers($aResult);
  269. if(!empty($aResult['link']))
  270. $aResult['link'] = $this->_oPermalinks->permalink($aResult['link']);
  271. return $aResult;
  272. }
  273. $bIsSelected = $this->_isSelected($a);
  274. if($bIsSelected)
  275. $this->_aSelected = $a;
  276. $a['object'] = $this->_sObject;
  277. $a['title'] = _t($a['title']);
  278. $a['title_attr'] = $this->_getMenuTitle($a);
  279. $a['info'] = isset($a['info']) ? _t($a['info']) : '';
  280. $this->removeMarker('addon');
  281. $a = $this->_replaceMarkers($a);
  282. if ($this->_bDisplayAddons) {
  283. $mixedAddon = $this->_getMenuAddon($a);
  284. if (!is_array($mixedAddon)) {
  285. $this->addMarkers(array('addon' => $mixedAddon));
  286. $a = $this->_replaceMarkers($a);
  287. }
  288. }
  289. $aMarkers = $this->_getMenuMarkers($a);
  290. if ($aMarkers && is_array($aMarkers)) {
  291. $this->addMarkers($aMarkers);
  292. $a = $this->_replaceMarkers($a);
  293. }
  294. list ($sIcon, $sIconUrl, $sIconA, $sIconHtml) = $this->_getMenuIcon($a);
  295. if(!isset($a['class_add']))
  296. $a['class_add'] = '';
  297. if($bIsSelected)
  298. $a['class_add'] .= ' bx-menu-tab-active';
  299. $a['class_add'] .= $this->_getVisibilityClass($a);
  300. $a['class_link'] = '';
  301. $a['link'] = isset($a['link']) ? $this->_oPermalinks->permalink($a['link']) : 'javascript:void(0);';
  302. $a['attrs'] = $this->_getMenuAttrs($a);
  303. $a['attrs_wrp'] = '';
  304. if($this->_bHx && !empty($a['link']) && strpos($a['link'], 'javascript:') === false) {
  305. $this->_aHx['get'] = $a['link'];
  306. $a['attrs'] .= bx_get_htmx_attrs($this->_aHx, $this->_mHxPreload);
  307. if(!bx_is_htmx_request() && !$bIsSelected)
  308. $a['attrs_wrp'] .= bx_get_htmx_attrs([
  309. 'get' => $a['link'],
  310. 'trigger' => 'load',
  311. 'target' => '#bx-content-preload',
  312. 'swap' => 'beforeend',
  313. ]);
  314. }
  315. $a['bx_if:image'] = [
  316. 'condition' => (bool)$sIconUrl,
  317. 'content' => [
  318. 'icon_url' => $sIconUrl,
  319. 'attrs' => $this->_getMenuIconAttrs('image', $a)
  320. ],
  321. ];
  322. $a['bx_if:image_inline'] = array (
  323. 'condition' => false,
  324. 'content' => array('image' => ''),
  325. );
  326. $a['bx_if:icon'] = array (
  327. 'condition' => (bool)$sIcon,
  328. 'content' => array('icon' => $sIcon),
  329. );
  330. $a['bx_if:icon-html'] = array (
  331. 'condition' => (bool)$sIconHtml,
  332. 'content' => array('icon' => $sIconHtml),
  333. );
  334. $a['bx_if:icon-a'] = array (
  335. 'condition' => (bool)$sIconA,
  336. 'content' => array('icon-a' => $sIconA),
  337. );
  338. $a['bx_if:title'] = [
  339. 'condition' => (bool)$a['title'] && (!isset($a['icon_only']) || (int)$a['icon_only'] == 0),
  340. 'content' => [
  341. 'title' => $a['title'],
  342. 'title_attr' => $a['title_attr']
  343. ],
  344. ];
  345. $a['bx_if:info'] = [
  346. 'condition' => (bool)$a['info'],
  347. 'content' => [
  348. 'info' => $a['info'],
  349. ],
  350. ];
  351. $bOnClick = !empty($a['onclick']);
  352. $aOnClick = $bOnClick ? [
  353. 'onclick' => $a['onclick'],
  354. ] : [];
  355. $a['bx_if:onclick'] = [
  356. 'condition' => $bOnClick,
  357. 'content' => $aOnClick
  358. ];
  359. $aTmplVarsAddon = $this->_bDisplayAddons ? $this->_getTmplVarsAddon($mixedAddon, $a) : array('addon' => '', 'addonf' => '');
  360. $a['bx_if:addon'] = array (
  361. 'condition' => $this->_bDisplayAddons && !empty($aTmplVarsAddon['addon']),
  362. 'content' => $aTmplVarsAddon
  363. );
  364. $aTmplVarsSubitems = array('subitems' => '');
  365. $bTmplVarsSubitems = $this->_bMultilevel && !empty($a['subitems']);
  366. if($bTmplVarsSubitems) {
  367. $sClassCollpsed = 'bx-mi-collapsed';
  368. if(($iCollapsed = $this->getUserChoiceCollapsedSubmenu($a)) !== false)
  369. $a['class_add'] .= $iCollapsed ? ' ' . $sClassCollpsed : '';
  370. else if(isset($a['collapsed']) && $a['collapsed'])
  371. $a['class_add'] .= ' ' . $sClassCollpsed;
  372. $aSubitems = array();
  373. foreach($a['subitems'] as $aSubitem) {
  374. $aSubitem = $this->_getMenuItem($aSubitem);
  375. if($aSubitem !== false)
  376. $aSubitems[] = $aSubitem;
  377. }
  378. if(!$bOnClick)
  379. $a['bx_if:onclick'] = [
  380. 'condition' => true,
  381. 'content' => [
  382. 'onclick' => "javascript:return bx_menu_toggle(this, '" . $this->_sObject . "', '" . $a['name'] . "')"
  383. ]
  384. ];
  385. $aTmplVarsSubitems['subitems'] = $this->_oTemplate->parseHtmlByName(str_replace('.html', '_subitems.html', $this->getTemplateName()), array(
  386. 'bx_repeat:menu_items' => $aSubitems,
  387. ));
  388. }
  389. $a['bx_if:show_toggler'] = [
  390. 'condition' => $bTmplVarsSubitems,
  391. 'content' => []
  392. ];
  393. $a['bx_if:show_subitems'] = array (
  394. 'condition' => $bTmplVarsSubitems,
  395. 'content' => $aTmplVarsSubitems
  396. );
  397. unset($a['subitems']);
  398. foreach ($this->_aOptionalParams as $sName => $sDefaultValue)
  399. if (!isset($a[$sName]))
  400. $a[$sName] = $sDefaultValue;
  401. return $a;
  402. }
  403. protected function _getMenuIcon ($a)
  404. {
  405. return BxTemplFunctions::getInstanceWithTemplate($this->_oTemplate)->getIcon(!empty($a['icon']) ? $a['icon'] : '');
  406. }
  407. protected function _getMenuIconAttrs ($sType, $a)
  408. {
  409. $sAttrs = '';
  410. if($sType == 'image' && ($sTitleAttr = $this->_getMenuTitle($a)))
  411. $sAttrs .= ' alt="' . $sTitleAttr . '"';
  412. return $sAttrs;
  413. }
  414. protected function _getMenuTitle($a)
  415. {
  416. return bx_html_attribute(strip_tags(!empty($a['title_attr']) && ($sTitleAttr = _t($a['title_attr'])) ? $sTitleAttr : $a['title']));
  417. }
  418. public function getMenuIconHtml($sIcon)
  419. {
  420. list ($sIcon, $sIconUrl, $sIconA, $sIconHtml) = BxTemplFunctions::getInstanceWithTemplate($this->_oTemplate)->getIcon($sIcon);
  421. $a['bx_if:image'] = [
  422. 'condition' => (bool)$sIconUrl,
  423. 'content' => [
  424. 'icon_url' => $sIconUrl,
  425. 'attrs' => ''
  426. ],
  427. ];
  428. $a['bx_if:icon'] = array (
  429. 'condition' => (bool)$sIcon,
  430. 'content' => array('icon' => $sIcon),
  431. );
  432. $a['bx_if:icon-a'] = array (
  433. 'condition' => (bool)$sIconA,
  434. 'content' => array('icon-a' => $sIconA),
  435. );
  436. $a['bx_if:icon-html'] = array (
  437. 'condition' => (bool)$sIconHtml,
  438. 'content' => array('icon' => $sIconHtml),
  439. );
  440. return $this->_oTemplate->parseHtmlByName('menu_icon.html', $a);
  441. }
  442. protected function _getMenuAddon ($aMenuItem)
  443. {
  444. if (empty($aMenuItem['addon']))
  445. return '';
  446. if (isset($aMenuItem['addon_cache']) && $aMenuItem['addon_cache']) {
  447. $oCache = BxDolDb::getInstance()->getDbCacheObject();
  448. $sKey = 'menu_' . $this->_sObject . '_' . $aMenuItem['name'] . '_' . bx_get_logged_profile_id() . '_' . bx_site_hash() . '.php';
  449. $s = $oCache->getData($sKey);
  450. if ($s !== null) {
  451. return $s;
  452. }
  453. else {
  454. $s = BxDolService::callSerialized($aMenuItem['addon'], $this->_aMarkers);
  455. $oCache->setData($sKey, $s);
  456. }
  457. return $s;
  458. }
  459. else {
  460. return BxDolService::callSerialized($aMenuItem['addon'], $this->_aMarkers);
  461. }
  462. }
  463. protected function _getMenuMarkers ($aMenuItem)
  464. {
  465. if (empty($aMenuItem['markers']))
  466. return '';
  467. return BxDolService::callSerialized($aMenuItem['markers'], $this->_aMarkers);
  468. }
  469. protected function _getMenuAttrs ($aMenuItem)
  470. {
  471. $sAttrs = '';
  472. if(($sTitleAttr = $this->_getMenuTitle($aMenuItem)))
  473. $sAttrs .= ' title="' . $sTitleAttr . '"';
  474. if(!empty($aMenuItem['target']))
  475. $sAttrs .= ' target="' . $aMenuItem['target'] . '"';
  476. if($this->_bAddNoFollow && !empty($aMenuItem['link']) && preg_match('@^https?://@', $aMenuItem['link']) && strncmp($aMenuItem['link'], BX_DOL_URL_ROOT, strlen(BX_DOL_URL_ROOT)) !== 0)
  477. $sAttrs .= ' rel="noreferrer"';
  478. if(!empty($aMenuItem['area_label']))
  479. $sAttrs .= ' area-label="' . bx_html_attribute(_t($aMenuItem['area_label'])) . '"';
  480. return $sAttrs;
  481. }
  482. /**
  483. * Add css/js files which are needed for menu display and functionality.
  484. */
  485. protected function _addJsCss()
  486. {
  487. $this->_oTemplate->addCss('menu.css');
  488. }
  489. protected function _getTmplVarsAddon($mixedAddon, $aMenuItem)
  490. {
  491. $sAddon = '';
  492. if(!is_array($mixedAddon))
  493. $sAddon = $mixedAddon;
  494. else if(!empty($mixedAddon['addon']))
  495. $sAddon = $mixedAddon['addon'];
  496. return array(
  497. 'addon' => $sAddon,
  498. 'addonf' => ''
  499. );
  500. }
  501. }
  502. /** @} */