BxBaseMenu.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  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. 'items' => $aItems
  74. ];
  75. }
  76. protected function _getCode($sTmplName, $aTmplVars)
  77. {
  78. return $this->_oTemplate->parseHtmlByName($this->getTemplateName($sTmplName), $aTmplVars);
  79. }
  80. public function getCodeItem ($sName)
  81. {
  82. if(empty($sName))
  83. return '';
  84. $sCode = $this->_oTemplate->getHtml(str_replace('.html', '_item.html', $this->_aObject['template']));
  85. if(empty($sCode))
  86. return '';
  87. $mixedTmplVars = $this->getMenuItem($sName);
  88. if($mixedTmplVars === false)
  89. return '';
  90. return $this->_oTemplate->parseHtmlByContent($sCode, $mixedTmplVars);
  91. }
  92. /**
  93. * Get template variables array
  94. * @return array
  95. */
  96. protected function _getTemplateVars ()
  97. {
  98. $aMenuItems = $this->getMenuItems();
  99. $sMenuItemSelected = _t($aMenuItems && $this->_aSelected && !empty($this->_aSelected['title']) ? $this->_aSelected['title'] : '_Contents');
  100. return [
  101. 'object' => $this->_sObject,
  102. 'menu_item_selected' => $sMenuItemSelected,
  103. 'bx_repeat:menu_items' => $aMenuItems,
  104. ];
  105. }
  106. /**
  107. * Get menu items array, which is ready to pass to menu template.
  108. * @return array or false
  109. */
  110. public function getMenuItems ()
  111. {
  112. if (!isset($this->_aObject['menu_items']))
  113. $this->_aObject['menu_items'] = $this->getMenuItemsRaw ();
  114. $aItems = array();
  115. foreach ($this->_aObject['menu_items'] as $aItem) {
  116. $aItem = $this->_getMenuItem ($aItem);
  117. if($aItem !== false)
  118. $aItems[] = $aItem;
  119. }
  120. return $aItems;
  121. }
  122. /**
  123. * Get menu item array, which is ready to pass to whole menu or
  124. * single menu item template. May return false if single menu item
  125. * is requested but cannot be shown by circumstances.
  126. * @return array or false
  127. */
  128. public function getMenuItem ($sName)
  129. {
  130. if (!isset($this->_aObject['menu_items']))
  131. $this->_aObject['menu_items'] = $this->getMenuItemsRaw ();
  132. if(!empty($sName))
  133. return $this->_getMenuItem($this->_aObject['menu_items'][$sName]);
  134. }
  135. /**
  136. * Get menu items array, this is just a wrapper for DB function for make it easier to override.
  137. * It is used in @see BxBaseMenu::getMenuItems
  138. * @return array
  139. */
  140. protected function getMenuItemsRaw ()
  141. {
  142. if($this->_bMultilevel)
  143. return $this->_oQuery->getMenuItemsHierarchy();
  144. else
  145. return $this->_oQuery->getMenuItems();
  146. }
  147. protected function _getMenuItem ($a)
  148. {
  149. if (!$this->_isActive($a) || !$this->_isVisible($a))
  150. return false;
  151. if ($this->_bIsApi) {
  152. list ($sIcon, $sIconUrl) = $this->_getMenuIcon($a);
  153. $aResult = [
  154. 'id' => $a['id'],
  155. 'name' => $a['name'],
  156. 'title' => _t($a['title']),
  157. 'link' => isset($a['link']) ? $a['link'] : '',
  158. 'icon' => $sIcon ? $sIcon : '',
  159. 'image' => $sIconUrl ? $sIconUrl : '',
  160. 'submenu' => !empty($a['submenu_object']) ? $a['submenu_object'] : '',
  161. 'addon' => $this->_bDisplayAddons ? $this->_getMenuAddon($a) : ''
  162. ];
  163. if(($aMarkers = $this->_getMenuMarkers($a)) && is_array($aMarkers))
  164. $this->addMarkers($aMarkers);
  165. $aResult = $this->_replaceMarkers($aResult);
  166. if(!empty($aResult['link']))
  167. $aResult['link'] = $this->_oPermalinks->permalink($aResult['link']);
  168. return $aResult;
  169. }
  170. $bIsSelected = $this->_isSelected($a);
  171. if($bIsSelected)
  172. $this->_aSelected = $a;
  173. $a['object'] = $this->_sObject;
  174. $a['title'] = _t($a['title']);
  175. $a['bx_if:title'] = array(
  176. 'condition' => !empty($a['title']),
  177. 'content' => array(
  178. 'title' => $a['title']
  179. )
  180. );
  181. $this->removeMarker('addon');
  182. $a = $this->_replaceMarkers($a);
  183. if ($this->_bDisplayAddons) {
  184. $mixedAddon = $this->_getMenuAddon($a);
  185. if (!is_array($mixedAddon)) {
  186. $this->addMarkers(array('addon' => $mixedAddon));
  187. $a = $this->_replaceMarkers($a);
  188. }
  189. }
  190. $aMarkers = $this->_getMenuMarkers($a);
  191. if ($aMarkers && is_array($aMarkers)) {
  192. $this->addMarkers($aMarkers);
  193. $a = $this->_replaceMarkers($a);
  194. }
  195. list ($sIcon, $sIconUrl, $sIconA, $sIconHtml) = $this->_getMenuIcon($a);
  196. $a['class_add'] = $bIsSelected ? 'bx-menu-tab-active' : '';
  197. $a['class_add'] .= $this->_getVisibilityClass($a);
  198. $a['class_link'] = '';
  199. $a['link'] = isset($a['link']) ? $this->_oPermalinks->permalink($a['link']) : 'javascript:void(0);';
  200. $a['title_attr'] = bx_html_attribute(strip_tags($a['title']));
  201. $a['attrs'] = $this->_getMenuAttrs($a);
  202. $a['attrs_wrp'] = '';
  203. if($this->_bHx && !empty($a['link']) && strpos($a['link'], 'javascript:') === false) {
  204. $this->_aHx['get'] = $a['link'];
  205. $a['attrs'] .= bx_get_htmx_attrs($this->_aHx, $this->_mHxPreload);
  206. if(!bx_is_htmx_request() && !$bIsSelected)
  207. $a['attrs_wrp'] .= bx_get_htmx_attrs([
  208. 'get' => $a['link'],
  209. 'trigger' => 'load',
  210. 'target' => '#bx-content-preload',
  211. 'swap' => 'beforeend',
  212. ]);
  213. }
  214. $a['bx_if:image'] = array (
  215. 'condition' => (bool)$sIconUrl,
  216. 'content' => array('icon_url' => $sIconUrl),
  217. );
  218. $a['bx_if:image_inline'] = array (
  219. 'condition' => false,
  220. 'content' => array('image' => ''),
  221. );
  222. $a['bx_if:icon'] = array (
  223. 'condition' => (bool)$sIcon,
  224. 'content' => array('icon' => $sIcon),
  225. );
  226. $a['bx_if:icon-html'] = array (
  227. 'condition' => (bool)$sIconHtml,
  228. 'content' => array('icon' => $sIconHtml),
  229. );
  230. $a['bx_if:icon-a'] = array (
  231. 'condition' => (bool)$sIconA,
  232. 'content' => array('icon-a' => $sIconA),
  233. );
  234. $a['bx_if:title'] = array (
  235. 'condition' => (bool)$a['title'],
  236. 'content' => array(
  237. 'title' => $a['title'],
  238. 'title_attr' => $a['title_attr']
  239. ),
  240. );
  241. $bOnClick = !empty($a['onclick']);
  242. $aOnClick = $bOnClick ? [
  243. 'onclick' => $a['onclick'],
  244. ] : [];
  245. $a['bx_if:onclick'] = [
  246. 'condition' => $bOnClick,
  247. 'content' => $aOnClick
  248. ];
  249. $aTmplVarsAddon = $this->_bDisplayAddons ? $this->_getTmplVarsAddon($mixedAddon, $a) : array('addon' => '', 'addonf' => '');
  250. $a['bx_if:addon'] = array (
  251. 'condition' => $this->_bDisplayAddons && !empty($aTmplVarsAddon['addon']),
  252. 'content' => $aTmplVarsAddon
  253. );
  254. $aTmplVarsSubitems = array('subitems' => '');
  255. $bTmplVarsSubitems = $this->_bMultilevel && !empty($a['subitems']);
  256. if($bTmplVarsSubitems) {
  257. $sClassCollpsed = 'bx-mi-collapsed';
  258. if(($iCollapsed = $this->getUserChoiceCollapsedSubmenu($a)) !== false)
  259. $a['class_add'] .= $iCollapsed ? ' ' . $sClassCollpsed : '';
  260. else if(isset($a['collapsed']) && $a['collapsed'])
  261. $a['class_add'] .= ' ' . $sClassCollpsed;
  262. $aSubitems = array();
  263. foreach($a['subitems'] as $aSubitem) {
  264. $aSubitem = $this->_getMenuItem($aSubitem);
  265. if($aSubitem !== false)
  266. $aSubitems[] = $aSubitem;
  267. }
  268. if(!$bOnClick)
  269. $a['bx_if:onclick'] = [
  270. 'condition' => true,
  271. 'content' => [
  272. 'onclick' => "javascript:return bx_menu_toggle(this, '" . $this->_sObject . "', '" . $a['name'] . "')"
  273. ]
  274. ];
  275. $aTmplVarsSubitems['subitems'] = $this->_oTemplate->parseHtmlByName(str_replace('.html', '_subitems.html', $this->getTemplateName()), array(
  276. 'bx_repeat:menu_items' => $aSubitems,
  277. ));
  278. }
  279. $a['bx_if:show_toggler'] = [
  280. 'condition' => $bTmplVarsSubitems,
  281. 'content' => []
  282. ];
  283. $a['bx_if:show_subitems'] = array (
  284. 'condition' => $bTmplVarsSubitems,
  285. 'content' => $aTmplVarsSubitems
  286. );
  287. unset($a['subitems']);
  288. foreach ($this->_aOptionalParams as $sName => $sDefaultValue)
  289. if (!isset($a[$sName]))
  290. $a[$sName] = $sDefaultValue;
  291. return $a;
  292. }
  293. protected function _getMenuIcon ($a)
  294. {
  295. return BxTemplFunctions::getInstanceWithTemplate($this->_oTemplate)->getIcon(!empty($a['icon']) ? $a['icon'] : '');
  296. }
  297. public function getMenuIconHtml($sIcon)
  298. {
  299. list ($sIcon, $sIconUrl, $sIconA, $sIconHtml) = BxTemplFunctions::getInstanceWithTemplate($this->_oTemplate)->getIcon($sIcon);
  300. $a['bx_if:image'] = array (
  301. 'condition' => (bool)$sIconUrl,
  302. 'content' => array('icon_url' => $sIconUrl),
  303. );
  304. $a['bx_if:icon'] = array (
  305. 'condition' => (bool)$sIcon,
  306. 'content' => array('icon' => $sIcon),
  307. );
  308. $a['bx_if:icon-a'] = array (
  309. 'condition' => (bool)$sIconA,
  310. 'content' => array('icon-a' => $sIconA),
  311. );
  312. $a['bx_if:icon-html'] = array (
  313. 'condition' => (bool)$sIconHtml,
  314. 'content' => array('icon' => $sIconHtml),
  315. );
  316. return $this->_oTemplate->parseHtmlByName('menu_icon.html', $a);
  317. }
  318. protected function _getMenuAddon ($aMenuItem)
  319. {
  320. if (empty($aMenuItem['addon']))
  321. return '';
  322. if (isset($aMenuItem['addon_cache']) && $aMenuItem['addon_cache']) {
  323. $oCache = BxDolDb::getInstance()->getDbCacheObject();
  324. $sKey = 'menu_' . $this->_sObject . '_' . $aMenuItem['name'] . '_' . bx_get_logged_profile_id() . '_' . bx_site_hash() . '.php';
  325. $s = $oCache->getData($sKey);
  326. if ($s !== null) {
  327. return $s;
  328. }
  329. else {
  330. $s = BxDolService::callSerialized($aMenuItem['addon'], $this->_aMarkers);
  331. $oCache->setData($sKey, $s);
  332. }
  333. return $s;
  334. }
  335. else {
  336. return BxDolService::callSerialized($aMenuItem['addon'], $this->_aMarkers);
  337. }
  338. }
  339. protected function _getMenuMarkers ($aMenuItem)
  340. {
  341. if (empty($aMenuItem['markers']))
  342. return '';
  343. return BxDolService::callSerialized($aMenuItem['markers'], $this->_aMarkers);
  344. }
  345. protected function _getMenuAttrs ($aMenuItem)
  346. {
  347. $sAttrs = '';
  348. if(!empty($aMenuItem['target']))
  349. $sAttrs .= ' target="' . $aMenuItem['target'] . '"';
  350. if($this->_bAddNoFollow && !empty($aMenuItem['link']) && preg_match('@^https?://@', $aMenuItem['link']) && strncmp($aMenuItem['link'], BX_DOL_URL_ROOT, strlen(BX_DOL_URL_ROOT)) !== 0)
  351. $sAttrs .= ' rel="noreferrer"';
  352. return $sAttrs;
  353. }
  354. /**
  355. * Add css/js files which are needed for menu display and functionality.
  356. */
  357. protected function _addJsCss()
  358. {
  359. $this->_oTemplate->addCss('menu.css');
  360. }
  361. protected function _getTmplVarsAddon($mixedAddon, $aMenuItem)
  362. {
  363. $sAddon = '';
  364. if(!is_array($mixedAddon))
  365. $sAddon = $mixedAddon;
  366. else if(!empty($mixedAddon['addon']))
  367. $sAddon = $mixedAddon['addon'];
  368. return array(
  369. 'addon' => $sAddon,
  370. 'addonf' => ''
  371. );
  372. }
  373. }
  374. /** @} */