BxBaseMenu.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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['bx_if:image'] = array (
  195. 'condition' => (bool)$sIconUrl,
  196. 'content' => array('icon_url' => $sIconUrl),
  197. );
  198. $a['bx_if:image_inline'] = array (
  199. 'condition' => false,
  200. 'content' => array('image' => ''),
  201. );
  202. $a['bx_if:icon'] = array (
  203. 'condition' => (bool)$sIcon,
  204. 'content' => array('icon' => $sIcon),
  205. );
  206. $a['bx_if:icon-html'] = array (
  207. 'condition' => (bool)$sIconHtml,
  208. 'content' => array('icon' => $sIconHtml),
  209. );
  210. $a['bx_if:icon-a'] = array (
  211. 'condition' => (bool)$sIconA,
  212. 'content' => array('icon-a' => $sIconA),
  213. );
  214. $a['bx_if:title'] = array (
  215. 'condition' => (bool)$a['title'],
  216. 'content' => array(
  217. 'title' => $a['title'],
  218. 'title_attr' => $a['title_attr']
  219. ),
  220. );
  221. $bOnClick = !empty($a['onclick']);
  222. $aOnClick = $bOnClick ? [
  223. 'onclick' => $a['onclick'],
  224. ] : [];
  225. $a['bx_if:onclick'] = [
  226. 'condition' => $bOnClick,
  227. 'content' => $aOnClick
  228. ];
  229. $aTmplVarsAddon = $this->_bDisplayAddons ? $this->_getTmplVarsAddon($mixedAddon, $a) : array('addon' => '', 'addonf' => '');
  230. $a['bx_if:addon'] = array (
  231. 'condition' => $this->_bDisplayAddons && !empty($aTmplVarsAddon['addon']),
  232. 'content' => $aTmplVarsAddon
  233. );
  234. $aTmplVarsSubitems = array('subitems' => '');
  235. $bTmplVarsSubitems = $this->_bMultilevel && !empty($a['subitems']);
  236. if($bTmplVarsSubitems) {
  237. $sClassCollpsed = 'bx-mi-collapsed';
  238. if(($iCollapsed = $this->getUserChoiceCollapsedSubmenu($a)) !== false)
  239. $a['class_add'] .= $iCollapsed ? ' ' . $sClassCollpsed : '';
  240. else if(isset($a['collapsed']) && $a['collapsed'])
  241. $a['class_add'] .= ' ' . $sClassCollpsed;
  242. $aSubitems = array();
  243. foreach($a['subitems'] as $aSubitem) {
  244. $aSubitem = $this->_getMenuItem($aSubitem);
  245. if($aSubitem !== false)
  246. $aSubitems[] = $aSubitem;
  247. }
  248. $aTmplVarsSubitems['subitems'] = $this->_oTemplate->parseHtmlByName(str_replace('.html', '_subitems.html', $this->getTemplateName()), array(
  249. 'bx_repeat:menu_items' => $aSubitems,
  250. ));
  251. }
  252. $a['bx_if:show_subitems'] = array (
  253. 'condition' => $bTmplVarsSubitems,
  254. 'content' => $aTmplVarsSubitems
  255. );
  256. unset($a['subitems']);
  257. foreach ($this->_aOptionalParams as $sName => $sDefaultValue)
  258. if (!isset($a[$sName]))
  259. $a[$sName] = $sDefaultValue;
  260. return $a;
  261. }
  262. protected function _getMenuIcon ($a)
  263. {
  264. return BxTemplFunctions::getInstanceWithTemplate($this->_oTemplate)->getIcon(!empty($a['icon']) ? $a['icon'] : '');
  265. }
  266. public function getMenuIconHtml($sIcon)
  267. {
  268. list ($sIcon, $sIconUrl, $sIconA, $sIconHtml) = BxTemplFunctions::getInstanceWithTemplate($this->_oTemplate)->getIcon($sIcon);
  269. $a['bx_if:image'] = array (
  270. 'condition' => (bool)$sIconUrl,
  271. 'content' => array('icon_url' => $sIconUrl),
  272. );
  273. $a['bx_if:icon'] = array (
  274. 'condition' => (bool)$sIcon,
  275. 'content' => array('icon' => $sIcon),
  276. );
  277. $a['bx_if:icon-a'] = array (
  278. 'condition' => (bool)$sIconA,
  279. 'content' => array('icon-a' => $sIconA),
  280. );
  281. $a['bx_if:icon-html'] = array (
  282. 'condition' => (bool)$sIconHtml,
  283. 'content' => array('icon' => $sIconHtml),
  284. );
  285. return $this->_oTemplate->parseHtmlByName('menu_icon.html', $a);
  286. }
  287. protected function _getMenuAddon ($aMenuItem)
  288. {
  289. if (empty($aMenuItem['addon']))
  290. return '';
  291. if (isset($aMenuItem['addon_cache']) && $aMenuItem['addon_cache']) {
  292. $oCache = BxDolDb::getInstance()->getDbCacheObject();
  293. $sKey = 'menu_' . $this->_sObject . '_' . $aMenuItem['name'] . '_' . bx_get_logged_profile_id() . '_' . bx_site_hash() . '.php';
  294. $s = $oCache->getData($sKey);
  295. if ($s !== null) {
  296. return $s;
  297. }
  298. else {
  299. $s = BxDolService::callSerialized($aMenuItem['addon'], $this->_aMarkers);
  300. $oCache->setData($sKey, $s);
  301. }
  302. return $s;
  303. }
  304. else {
  305. return BxDolService::callSerialized($aMenuItem['addon'], $this->_aMarkers);
  306. }
  307. }
  308. protected function _getMenuMarkers ($aMenuItem)
  309. {
  310. if (empty($aMenuItem['markers']))
  311. return '';
  312. return BxDolService::callSerialized($aMenuItem['markers'], $this->_aMarkers);
  313. }
  314. protected function _getMenuAttrs ($aMenuItem)
  315. {
  316. $sAttrs = '';
  317. if(!empty($aMenuItem['target']))
  318. $sAttrs .= ' target="' . $aMenuItem['target'] . '"';
  319. if($this->_bAddNoFollow && !empty($aMenuItem['link']) && preg_match('@^https?://@', $aMenuItem['link']) && strncmp($aMenuItem['link'], BX_DOL_URL_ROOT, strlen(BX_DOL_URL_ROOT)) !== 0)
  320. $sAttrs .= ' rel="noreferrer"';
  321. return $sAttrs;
  322. }
  323. /**
  324. * Add css/js files which are needed for menu display and functionality.
  325. */
  326. protected function _addJsCss()
  327. {
  328. $this->_oTemplate->addCss('menu.css');
  329. }
  330. protected function _getTmplVarsAddon($mixedAddon, $aMenuItem)
  331. {
  332. $sAddon = '';
  333. if(!is_array($mixedAddon))
  334. $sAddon = $mixedAddon;
  335. else if(!empty($mixedAddon['addon']))
  336. $sAddon = $mixedAddon['addon'];
  337. return array(
  338. 'addon' => $sAddon,
  339. 'addonf' => ''
  340. );
  341. }
  342. }
  343. /** @} */