1
0

BxBaseMenu.php 11 KB

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