BxBaseMenu.php 13 KB

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