1
0

BxBaseMenu.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  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. ];
  156. if(($aMarkers = $this->_getMenuMarkers($a)) && is_array($aMarkers))
  157. $this->addMarkers($aMarkers);
  158. $aResult = $this->_replaceMarkers($aResult);
  159. if(!empty($aResult['link']))
  160. $aResult['link'] = $this->_oPermalinks->permalink($aResult['link']);
  161. return $aResult;
  162. }
  163. $a['object'] = $this->_sObject;
  164. $a['title'] = _t($a['title']);
  165. $a['bx_if:title'] = array(
  166. 'condition' => !empty($a['title']),
  167. 'content' => array(
  168. 'title' => $a['title']
  169. )
  170. );
  171. $this->removeMarker('addon');
  172. $a = $this->_replaceMarkers($a);
  173. if ($this->_bDisplayAddons) {
  174. $mixedAddon = $this->_getMenuAddon($a);
  175. if (!is_array($mixedAddon)) {
  176. $this->addMarkers(array('addon' => $mixedAddon));
  177. $a = $this->_replaceMarkers($a);
  178. }
  179. }
  180. $aMarkers = $this->_getMenuMarkers($a);
  181. if ($aMarkers && is_array($aMarkers)) {
  182. $this->addMarkers($aMarkers);
  183. $a = $this->_replaceMarkers($a);
  184. }
  185. list ($sIcon, $sIconUrl, $sIconA, $sIconHtml) = $this->_getMenuIcon($a);
  186. $a['class_add'] = $this->_isSelected($a) ? 'bx-menu-tab-active' : '';
  187. $a['class_add'] .= $this->_getVisibilityClass($a);
  188. $a['link'] = isset($a['link']) ? $this->_oPermalinks->permalink($a['link']) : 'javascript:void(0);';
  189. $a['title_attr'] = bx_html_attribute(strip_tags($a['title']));
  190. $a['attrs'] = $this->_getMenuAttrs($a);
  191. $a['bx_if:image'] = array (
  192. 'condition' => (bool)$sIconUrl,
  193. 'content' => array('icon_url' => $sIconUrl),
  194. );
  195. $a['bx_if:image_inline'] = array (
  196. 'condition' => false,
  197. 'content' => array('image' => ''),
  198. );
  199. $a['bx_if:icon'] = array (
  200. 'condition' => (bool)$sIcon,
  201. 'content' => array('icon' => $sIcon),
  202. );
  203. $a['bx_if:icon-html'] = array (
  204. 'condition' => (bool)$sIconHtml,
  205. 'content' => array('icon' => $sIconHtml),
  206. );
  207. $a['bx_if:icon-a'] = array (
  208. 'condition' => (bool)$sIconA,
  209. 'content' => array('icon-a' => $sIconA),
  210. );
  211. $a['bx_if:title'] = array (
  212. 'condition' => (bool)$a['title'],
  213. 'content' => array(
  214. 'title' => $a['title'],
  215. 'title_attr' => $a['title_attr']
  216. ),
  217. );
  218. $aTmplVarsAddon = $this->_bDisplayAddons ? $this->_getTmplVarsAddon($mixedAddon, $a) : array('addon' => '', 'addonf' => '');
  219. $a['bx_if:addon'] = array (
  220. 'condition' => $this->_bDisplayAddons && !empty($aTmplVarsAddon['addon']),
  221. 'content' => $aTmplVarsAddon
  222. );
  223. $aTmplVarsSubitems = array('subitems' => '');
  224. $bTmplVarsSubitems = $this->_bMultilevel && !empty($a['subitems']);
  225. if($bTmplVarsSubitems) {
  226. $sClassCollpsed = 'bx-mi-collapsed';
  227. if(($iCollapsed = $this->getUserChoiceCollapsedSubmenu($a)) !== false)
  228. $a['class_add'] .= $iCollapsed ? ' ' . $sClassCollpsed : '';
  229. else if(isset($a['collapsed']) && $a['collapsed'])
  230. $a['class_add'] .= ' ' . $sClassCollpsed;
  231. $aSubitems = array();
  232. foreach($a['subitems'] as $aSubitem) {
  233. $aSubitem = $this->_getMenuItem($aSubitem);
  234. if($aSubitem !== false)
  235. $aSubitems[] = $aSubitem;
  236. }
  237. $aTmplVarsSubitems['subitems'] = $this->_oTemplate->parseHtmlByName(str_replace('.html', '_subitems.html', $this->getTemplateName()), array(
  238. 'bx_repeat:menu_items' => $aSubitems,
  239. ));
  240. }
  241. $a['bx_if:show_subitems'] = array (
  242. 'condition' => $bTmplVarsSubitems,
  243. 'content' => $aTmplVarsSubitems
  244. );
  245. unset($a['subitems']);
  246. foreach ($this->_aOptionalParams as $sName => $sDefaultValue)
  247. if (!isset($a[$sName]))
  248. $a[$sName] = $sDefaultValue;
  249. return $a;
  250. }
  251. protected function _getMenuIcon ($a)
  252. {
  253. return BxTemplFunctions::getInstanceWithTemplate($this->_oTemplate)->getIcon(!empty($a['icon']) ? $a['icon'] : '');
  254. }
  255. public function getMenuIconHtml($sIcon)
  256. {
  257. list ($sIcon, $sIconUrl, $sIconA, $sIconHtml) = BxTemplFunctions::getInstanceWithTemplate($this->_oTemplate)->getIcon($sIcon);
  258. $a['bx_if:image'] = array (
  259. 'condition' => (bool)$sIconUrl,
  260. 'content' => array('icon_url' => $sIconUrl),
  261. );
  262. $a['bx_if:icon'] = array (
  263. 'condition' => (bool)$sIcon,
  264. 'content' => array('icon' => $sIcon),
  265. );
  266. $a['bx_if:icon-a'] = array (
  267. 'condition' => (bool)$sIconA,
  268. 'content' => array('icon-a' => $sIconA),
  269. );
  270. $a['bx_if:icon-html'] = array (
  271. 'condition' => (bool)$sIconHtml,
  272. 'content' => array('icon' => $sIconHtml),
  273. );
  274. return $this->_oTemplate->parseHtmlByName('menu_icon.html', $a);
  275. }
  276. protected function _getMenuAddon ($aMenuItem)
  277. {
  278. if (empty($aMenuItem['addon']))
  279. return '';
  280. if (isset($aMenuItem['addon_cache']) && $aMenuItem['addon_cache']) {
  281. $oCache = BxDolDb::getInstance()->getDbCacheObject();
  282. $sKey = 'menu_' . $this->_sObject . '_' . $aMenuItem['name'] . '_' . bx_get_logged_profile_id() . '_' . bx_site_hash() . '.php';
  283. $s = $oCache->getData($sKey);
  284. if ($s !== null) {
  285. return $s;
  286. }
  287. else {
  288. $s = BxDolService::callSerialized($aMenuItem['addon'], $this->_aMarkers);
  289. $oCache->setData($sKey, $s);
  290. }
  291. return $s;
  292. }
  293. else {
  294. return BxDolService::callSerialized($aMenuItem['addon'], $this->_aMarkers);
  295. }
  296. }
  297. protected function _getMenuMarkers ($aMenuItem)
  298. {
  299. if (empty($aMenuItem['markers']))
  300. return '';
  301. return BxDolService::callSerialized($aMenuItem['markers'], $this->_aMarkers);
  302. }
  303. protected function _getMenuAttrs ($aMenuItem)
  304. {
  305. $sAttrs = '';
  306. if(!empty($aMenuItem['target']))
  307. $sAttrs .= ' target="' . $aMenuItem['target'] . '"';
  308. if($this->_bAddNoFollow && !empty($aMenuItem['link']) && preg_match('@^https?://@', $aMenuItem['link']) && strncmp($aMenuItem['link'], BX_DOL_URL_ROOT, strlen(BX_DOL_URL_ROOT)) !== 0)
  309. $sAttrs .= ' rel="noreferrer"';
  310. return $sAttrs;
  311. }
  312. /**
  313. * Add css/js files which are needed for menu display and functionality.
  314. */
  315. protected function _addJsCss()
  316. {
  317. $this->_oTemplate->addCss('menu.css');
  318. }
  319. protected function _getTmplVarsAddon($mixedAddon, $aMenuItem)
  320. {
  321. $sAddon = '';
  322. if(!is_array($mixedAddon))
  323. $sAddon = $mixedAddon;
  324. else if(!empty($mixedAddon['addon']))
  325. $sAddon = $mixedAddon['addon'];
  326. return array(
  327. 'addon' => $sAddon,
  328. 'addonf' => ''
  329. );
  330. }
  331. }
  332. /** @} */