BxBaseMenu.php 12 KB

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