BxBaseStudioMenu.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 UnaView UNA Studio Representation classes
  7. * @ingroup UnaStudio
  8. * @{
  9. */
  10. class BxBaseStudioMenu extends BxDolStudioMenu
  11. {
  12. protected $_bMenuSide;
  13. protected $_bMenuToolbar;
  14. protected $_bInlineIcons;
  15. public function __construct ($aObject, $oTemplate)
  16. {
  17. parent::__construct ($aObject, $oTemplate);
  18. $this->_bMenuSide = $this->_aObject['template'] == 'menu_side.html';
  19. $this->_bMenuToolbar = $this->_aObject['template'] == 'menu_top_toolbar.html';
  20. $this->_bInlineIcons = in_array($this->_aObject['template'], array(
  21. 'menu_side.html',
  22. 'menu_top_toolbar.html',
  23. 'menu_launcher_browser.html',
  24. 'page_breadcrumb.html'
  25. ));
  26. }
  27. public function setInlineIcons($bInlineIcons)
  28. {
  29. $this->_bInlineIcons = $bInlineIcons;
  30. }
  31. protected function _getMenuItem ($aItem)
  32. {
  33. $aItem = parent::_getMenuItem($aItem);
  34. if($aItem === false)
  35. return $aItem;
  36. $aItem['class'] = isset($aItem['class']) ? $aItem['class'] : '';
  37. if(!isset($aItem['class_add']))
  38. $aItem['class_add'] = '';
  39. $aItem['class_add'] .= ' ' . str_replace('_', '-', $aItem['name']);
  40. if(!empty($aItem['attrs_add']))
  41. $aItem['attrs'] .= ' ' . $aItem['attrs_add'];
  42. // an item may keep its icon as an <img> (icon_inline => false): app tile artwork stays out of the document and its gradient ids never collide
  43. if($this->_bInlineIcons && ($aItem['icon_inline'] ?? true) && $aItem['bx_if:image']['condition'] && ($sImage = $this->_oTemplate->getIconContent($aItem['icon'])) !== false)
  44. $aItem = array_merge($aItem, [
  45. 'bx_if:image' => [
  46. 'condition' => false,
  47. 'content' => [],
  48. ],
  49. 'bx_if:image_inline' => [
  50. 'condition' => true,
  51. 'content' => [
  52. 'image' => $sImage
  53. ],
  54. ],
  55. ]);
  56. if($this->_bMenuSide) {
  57. $aItem['bx_if:show_icon'] = [
  58. 'condition' => $aItem['bx_if:icon']['condition'] || $aItem['bx_if:image']['condition'] || $aItem['bx_if:image_inline']['condition'],
  59. 'content' => []
  60. ];
  61. $aItem['bx_if:show_icon_bg'] = [
  62. 'condition' => (isset($aItem['icon_bg']) && $aItem['icon_bg'] === true) || strpos($aItem['icon'], '.') === false,
  63. 'content' => []
  64. ];
  65. }
  66. if(!$this->_bMenuToolbar)
  67. return $aItem;
  68. /*
  69. * Header toolbar: an item that only runs a script is a button, not a link. The template renders the two differently,
  70. * so it gets everything pre-rendered, because a bx_if block only sees its own content.
  71. */
  72. $bButton = empty($aItem['link']) || strncmp($aItem['link'], 'javascript:', 11) === 0;
  73. $sIconHtml = '';
  74. if($aItem['bx_if:icon']['condition'])
  75. $sIconHtml = '<i class="sys-icon ' . $aItem['bx_if:icon']['content']['icon'] . ' bx-def-round-corners bx-def-font-contrasted"></i>';
  76. else if($aItem['bx_if:image']['condition'])
  77. $sIconHtml = '<img src="' . $aItem['bx_if:image']['content']['icon_url'] . '" alt="" />';
  78. else if($aItem['bx_if:image_inline']['condition'])
  79. $sIconHtml = $aItem['bx_if:image_inline']['content']['image'];
  80. $aContent = [
  81. 'name' => $aItem['name'],
  82. 'link' => $aItem['link'],
  83. 'onclick' => !empty($aItem['onclick']) ? $aItem['onclick'] : '',
  84. 'title' => isset($aItem['title_attr']) ? $aItem['title_attr'] : bx_html_attribute($aItem['title']),
  85. 'attrs' => $aItem['attrs'],
  86. 'icon_html' => $sIconHtml,
  87. ];
  88. $aItem['bx_if:is_link'] = ['condition' => !$bButton, 'content' => $aContent];
  89. $aItem['bx_if:is_button'] = ['condition' => $bButton, 'content' => $aContent];
  90. return $aItem;
  91. }
  92. /**
  93. * Check if menu items is selected.
  94. * @param $a menu item array
  95. * @return boolean
  96. */
  97. protected function _isSelected ($a)
  98. {
  99. return isset($a['selected']) && $a['selected'] === true;
  100. }
  101. }
  102. /** @} */