BxBaseMenu.php 18 KB

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