1
0

BxBaseMenu.php 7.1 KB

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