BxBaseMenu.php 14 KB

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