BxBaseMenu.php 8.3 KB

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