BxBaseMenu.php 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 $_aOptionalParams = array('target' => '', 'onclick' => '');
  18. protected $_bDisplayAddons = false;
  19. public function __construct ($aObject, $oTemplate)
  20. {
  21. parent::__construct ($aObject);
  22. if ($oTemplate)
  23. $this->_oTemplate = $oTemplate;
  24. else
  25. $this->_oTemplate = BxDolTemplate::getInstance();
  26. }
  27. public function getDisplayAddons()
  28. {
  29. return $this->_bDisplayAddons;
  30. }
  31. public function setDisplayAddons($b)
  32. {
  33. $bRet = $this->_bDisplayAddons;
  34. $this->_bDisplayAddons = $b;
  35. return $bRet;
  36. }
  37. /**
  38. * Get menu code.
  39. * @return string
  40. */
  41. public function getCode ()
  42. {
  43. $sMenuTitle = isset($this->_aObject['title']) ? _t($this->_aObject['title']) : 'Menu-' . rand(0, PHP_INT_MAX);
  44. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->beginMenu($sMenuTitle);
  45. $s = '';
  46. $aVars = $this->_getTemplateVars ();
  47. if (!empty($aVars['bx_repeat:menu_items'])) {
  48. $this->_addJsCss();
  49. $s = $this->_getCode($this->_aObject['template'], $aVars);
  50. }
  51. if (isset($GLOBALS['bx_profiler'])) $GLOBALS['bx_profiler']->endMenu($sMenuTitle);
  52. return $s;
  53. }
  54. protected function _getCode($sTmplName, $aTmplVars)
  55. {
  56. return $this->_oTemplate->parseHtmlByName($sTmplName, $aTmplVars);
  57. }
  58. public function getCodeItem ($sName)
  59. {
  60. if(empty($sName))
  61. return '';
  62. $sCode = $this->_oTemplate->getHtml(str_replace('.html', '_item.html', $this->_aObject['template']));
  63. if(empty($sCode))
  64. return '';
  65. $mixedTmplVars = $this->getMenuItem($sName);
  66. if($mixedTmplVars === false)
  67. return '';
  68. return $this->_oTemplate->parseHtmlByContent($sCode, $mixedTmplVars);
  69. }
  70. /**
  71. * Get template variables array
  72. * @return array
  73. */
  74. protected function _getTemplateVars ()
  75. {
  76. return array (
  77. 'object' => $this->_sObject,
  78. 'bx_repeat:menu_items' => $this->getMenuItems (),
  79. );
  80. }
  81. /**
  82. * Get menu items array, which is ready to pass to menu template.
  83. * @return array or false
  84. */
  85. public function getMenuItems ()
  86. {
  87. if (!isset($this->_aObject['menu_items']))
  88. $this->_aObject['menu_items'] = $this->getMenuItemsRaw ();
  89. $aItems = array();
  90. foreach ($this->_aObject['menu_items'] as $aItem) {
  91. $aItem = $this->_getMenuItem ($aItem);
  92. if($aItem !== false)
  93. $aItems[] = $aItem;
  94. }
  95. return $aItems;
  96. }
  97. /**
  98. * Get menu item array, which is ready to pass to whole menu or
  99. * single menu item template. May return false if single menu item
  100. * is requested but cannot be shown by circumstances.
  101. * @return array or false
  102. */
  103. public function getMenuItem ($sName)
  104. {
  105. if (!isset($this->_aObject['menu_items']))
  106. $this->_aObject['menu_items'] = $this->getMenuItemsRaw ();
  107. if(!empty($sName))
  108. return $this->_getMenuItem($this->_aObject['menu_items'][$sName]);
  109. }
  110. /**
  111. * Get menu items array, this is just a wrapper for DB function for make it easier to override.
  112. * It is used in @see BxBaseMenu::getMenuItems
  113. * @return array
  114. */
  115. protected function getMenuItemsRaw ()
  116. {
  117. return $this->_oQuery->getMenuItems();
  118. }
  119. protected function _getMenuItem ($a)
  120. {
  121. $aHiddenOn = array(
  122. pow(2, BX_DB_HIDDEN_PHONE - 1) => 'bx-def-media-phone-hide',
  123. pow(2, BX_DB_HIDDEN_TABLET - 1) => 'bx-def-media-tablet-hide',
  124. pow(2, BX_DB_HIDDEN_DESKTOP - 1) => 'bx-def-media-desktop-hide',
  125. pow(2, BX_DB_HIDDEN_MOBILE - 1) => 'bx-def-mobile-app-hide'
  126. );
  127. if (isset($a['active']) && !$a['active'])
  128. return false;
  129. if (isset($a['visible_for_levels']) && !$this->_isVisible($a))
  130. return false;
  131. $a['title'] = _t($a['title']);
  132. $a['bx_if:title'] = array(
  133. 'condition' => !empty($a['title']),
  134. 'content' => array(
  135. 'title' => $a['title']
  136. )
  137. );
  138. $this->removeMarker('addon');
  139. $a = $this->_replaceMarkers($a);
  140. if ($this->_bDisplayAddons) {
  141. $mixedAddon = $this->_getMenuAddon($a);
  142. if (!is_array($mixedAddon)) {
  143. $this->addMarkers(array('addon' => $mixedAddon));
  144. $a = $this->_replaceMarkers($a);
  145. }
  146. }
  147. $aMarkers = $this->_getMenuMarkers($a);
  148. if ($aMarkers && is_array($aMarkers)) {
  149. $this->addMarkers($aMarkers);
  150. $a = $this->_replaceMarkers($a);
  151. }
  152. list ($sIcon, $sIconUrl, $sIconA) = $this->_getMenuIcon($a);
  153. $a['class_add'] = $this->_isSelected($a) ? 'bx-menu-tab-active' : '';
  154. $sHiddenOnCssClasses = '';
  155. if(!empty($a['hidden_on']))
  156. foreach($aHiddenOn as $iHiddenOn => $sClass)
  157. if((int)$a['hidden_on'] & $iHiddenOn)
  158. $sHiddenOnCssClasses .= ' ' . $sClass;
  159. $a['class_add'] .= $sHiddenOnCssClasses;
  160. $a['link'] = isset($a['link']) ? $this->_oPermalinks->permalink($a['link']) : 'javascript:void(0);';
  161. $a['title_attr'] = bx_html_attribute(strip_tags($a['title']));
  162. $a['attrs'] = $this->_getMenuAttrs($a);
  163. $a['bx_if:image'] = array (
  164. 'condition' => (bool)$sIconUrl,
  165. 'content' => array('icon_url' => $sIconUrl),
  166. );
  167. $a['bx_if:image_inline'] = array (
  168. 'condition' => false,
  169. 'content' => array('image' => ''),
  170. );
  171. $a['bx_if:icon'] = array (
  172. 'condition' => (bool)$sIcon,
  173. 'content' => array('icon' => $sIcon),
  174. );
  175. $a['bx_if:icon-a'] = array (
  176. 'condition' => (bool)$sIconA,
  177. 'content' => array('icon-a' => $sIconA),
  178. );
  179. $a['bx_if:title'] = array (
  180. 'condition' => (bool)$a['title'],
  181. 'content' => array(
  182. 'title' => $a['title'],
  183. 'title_attr' => $a['title_attr']
  184. ),
  185. );
  186. $aTmplVarsAddon = $this->_bDisplayAddons ? $this->_getTmplVarsAddon($mixedAddon, $a) : array('addon' => '', 'addonf' => '');
  187. $a['bx_if:addon'] = array (
  188. 'condition' => $this->_bDisplayAddons && !empty($aTmplVarsAddon['addon']),
  189. 'content' => $aTmplVarsAddon
  190. );
  191. foreach ($this->_aOptionalParams as $sName => $sDefaultValue)
  192. if (!isset($a[$sName]))
  193. $a[$sName] = $sDefaultValue;
  194. return $a;
  195. }
  196. protected function _getMenuIcon ($a)
  197. {
  198. $sIcon = false;
  199. $sIconA = false;
  200. $sIconUrl = false;
  201. if (!empty($a['icon'])) {
  202. if ((int)$a['icon'] > 0 ) {
  203. $oStorage = BxDolStorage::getObjectInstance(BX_DOL_STORAGE_OBJ_IMAGES);
  204. $sIconUrl = $oStorage ? $oStorage->getFileUrlById((int)$a['icon']) : false;
  205. } else {
  206. if (false === strpos($a['icon'], '.')) {
  207. if (0 === strncmp($a['icon'], 'a:', 2))
  208. $sIconA = substr($a['icon'], 2); // animated icon
  209. else
  210. $sIcon = $a['icon']; // font icons
  211. } else {
  212. $sIconUrl = $this->_oTemplate->getIconUrl($a['icon']);
  213. }
  214. }
  215. }
  216. return array ($sIcon, $sIconUrl, $sIconA);
  217. }
  218. protected function _getMenuAddon ($aMenuItem)
  219. {
  220. if (empty($aMenuItem['addon']))
  221. return '';
  222. return BxDolService::callSerialized($aMenuItem['addon'], $this->_aMarkers);
  223. }
  224. protected function _getMenuMarkers ($aMenuItem)
  225. {
  226. if (empty($aMenuItem['markers']))
  227. return '';
  228. return BxDolService::callSerialized($aMenuItem['markers'], $this->_aMarkers);
  229. }
  230. protected function _getMenuAttrs ($aMenuItem)
  231. {
  232. $sAttrs = '';
  233. if(!empty($aMenuItem['target']))
  234. $sAttrs .= ' target="' . $aMenuItem['target'] . '"';
  235. if($this->_bAddNoFollow && !empty($aMenuItem['link']) && preg_match('@^https?://@', $aMenuItem['link']) && strncmp($aMenuItem['link'], BX_DOL_URL_ROOT, strlen(BX_DOL_URL_ROOT)) !== 0)
  236. $sAttrs .= ' rel="noreferrer"';
  237. return $sAttrs;
  238. }
  239. /**
  240. * Add css/js files which are needed for menu display and functionality.
  241. */
  242. protected function _addJsCss()
  243. {
  244. $this->_oTemplate->addCss('menu.css');
  245. }
  246. protected function _getTmplVarsAddon($mixedAddon, $aMenuItem)
  247. {
  248. $sAddon = '';
  249. if(!is_array($mixedAddon))
  250. $sAddon = $mixedAddon;
  251. else if(!empty($mixedAddon['addon']))
  252. $sAddon = $mixedAddon['addon'];
  253. return array(
  254. 'addon' => $sAddon,
  255. 'addonf' => ''
  256. );
  257. }
  258. }
  259. /** @} */