BxBaseMenu.php 12 KB

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