BxBaseMenu.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. );
  111. if (isset($a['active']) && !$a['active'])
  112. return false;
  113. if (isset($a['visible_for_levels']) && !$this->_isVisible($a))
  114. return false;
  115. $a['title'] = _t($a['title']);
  116. $a['bx_if:title'] = array(
  117. 'condition' => !empty($a['title']),
  118. 'content' => array(
  119. 'title' => $a['title']
  120. )
  121. );
  122. $this->removeMarker('addon');
  123. $a = $this->_replaceMarkers($a);
  124. $mixedAddon = $this->_getMenuAddon($a);
  125. $this->addMarkers(array('addon' => $mixedAddon));
  126. $a = $this->_replaceMarkers($a);
  127. list ($sIcon, $sIconUrl, $sIconA) = $this->_getMenuIcon($a);
  128. $a['class_add'] = $this->_isSelected($a) ? 'bx-menu-tab-active' : '';
  129. $sHiddenOnCssClasses = '';
  130. if(!empty($a['hidden_on']))
  131. foreach($aHiddenOn as $iHiddenOn => $sClass)
  132. if((int)$a['hidden_on'] & $iHiddenOn)
  133. $sHiddenOnCssClasses .= ' ' . $sClass;
  134. $a['class_add'] .= $sHiddenOnCssClasses;
  135. $a['link'] = isset($a['link']) ? $this->_oPermalinks->permalink($a['link']) : 'javascript:void(0);';
  136. $a['title_attr'] = bx_html_attribute(strip_tags($a['title']));
  137. $a['bx_if:image'] = array (
  138. 'condition' => (bool)$sIconUrl,
  139. 'content' => array('icon_url' => $sIconUrl),
  140. );
  141. $a['bx_if:icon'] = array (
  142. 'condition' => (bool)$sIcon,
  143. 'content' => array('icon' => $sIcon),
  144. );
  145. $a['bx_if:icon-a'] = array (
  146. 'condition' => (bool)$sIconA,
  147. 'content' => array('icon-a' => $sIconA),
  148. );
  149. $a['bx_if:title'] = array (
  150. 'condition' => (bool)$a['title'],
  151. 'content' => array(
  152. 'title' => $a['title'],
  153. 'title_attr' => $a['title_attr']
  154. ),
  155. );
  156. $a['bx_if:addon'] = array (
  157. 'condition' => (bool)$mixedAddon,
  158. 'content' => $this->_getTmplVarsAddon($mixedAddon, $a)
  159. );
  160. foreach ($this->_aOptionalParams as $sName => $sDefaultValue)
  161. if (!isset($a[$sName]))
  162. $a[$sName] = $sDefaultValue;
  163. return $a;
  164. }
  165. protected function _getMenuIcon ($a)
  166. {
  167. $sIcon = false;
  168. $sIconA = false;
  169. $sIconUrl = false;
  170. if (!empty($a['icon'])) {
  171. if ((int)$a['icon'] > 0 ) {
  172. $oStorage = BxDolStorage::getObjectInstance(BX_DOL_STORAGE_OBJ_IMAGES);
  173. $sIconUrl = $oStorage ? $oStorage->getFileUrlById((int)$a['icon']) : false;
  174. } else {
  175. if (false === strpos($a['icon'], '.')) {
  176. if (0 === strncmp($a['icon'], 'a:', 2))
  177. $sIconA = substr($a['icon'], 2); // animated icon
  178. else
  179. $sIcon = $a['icon']; // font icons
  180. } else {
  181. $sIconUrl = $this->_oTemplate->getIconUrl($a['icon']);
  182. }
  183. }
  184. }
  185. return array ($sIcon, $sIconUrl, $sIconA);
  186. }
  187. protected function _getMenuAddon ($aMenuItem)
  188. {
  189. if (empty($aMenuItem['addon']))
  190. return '';
  191. return BxDolService::callSerialized($aMenuItem['addon'], $this->_aMarkers);
  192. }
  193. /**
  194. * Add css/js files which are needed for menu display and functionality.
  195. */
  196. protected function _addJsCss()
  197. {
  198. $this->_oTemplate->addCss('menu.css');
  199. }
  200. protected function _getTmplVarsAddon($mixedAddon, $aMenuItem)
  201. {
  202. return array(
  203. 'addon' => $mixedAddon,
  204. 'addonf' => ''
  205. );
  206. }
  207. }
  208. /** @} */