1
0

BxBaseMenu.php 13 KB

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