BxBaseMenuCustom.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. */
  10. class BxBaseMenuCustom extends BxTemplMenuMoreAuto
  11. {
  12. protected $_sTmplContent;
  13. protected $_sTmplContentItem;
  14. public function __construct($aObject, $oTemplate = false)
  15. {
  16. parent::__construct($aObject, $oTemplate);
  17. $this->_sTmplContent = $this->_oTemplate->getHtml($this->getTemplateName());
  18. $this->_sTmplContentItem = $this->_oTemplate->getHtml($this->getTemplateNameItem());
  19. $this->_sTmplNameItemMore = 'menu_custom_item_more.html';
  20. }
  21. /**
  22. * Get template name with checking for custom template related to exactly this menu object.
  23. * @return string with template name.
  24. */
  25. public function getTemplateNameItem($sName = '')
  26. {
  27. if(empty($sName))
  28. $sName = 'menu_custom_item.html';
  29. $sNameCustom = str_replace('.html', '_' . $this->_sObject . '.html', $sName);
  30. return $this->_oTemplate->isHtml($sNameCustom) ? $sNameCustom : $sName;
  31. }
  32. public function setTemplateById ($iTemplateId)
  33. {
  34. $sTemplate = $this->_aObject['template'];
  35. switch($iTemplateId) {
  36. case BX_DB_MENU_TEMPLATE_TABS:
  37. $iTemplateId = BX_MENU_TEMPLATE_CUSTOM_HOR;
  38. break;
  39. case BX_DB_MENU_TEMPLATE_POPUP:
  40. $iTemplateId = BX_MENU_TEMPLATE_CUSTOM_VER;
  41. break;
  42. }
  43. parent::setTemplateById ($iTemplateId);
  44. if($sTemplate != $this->_aObject['template'])
  45. $this->_sTmplContent = $this->_oTemplate->getHtml($this->getTemplateName());
  46. }
  47. protected function _getCode($sTmplName, $aTmplVars)
  48. {
  49. if($sTmplName != $this->_aObject['template'])
  50. return parent::_getCode($sTmplName, $aTmplVars);
  51. return $this->_oTemplate->parseHtmlByContent($this->_sTmplContent, $aTmplVars);
  52. }
  53. protected function _getMenuItem ($aItem)
  54. {
  55. if (!$this->_isActive($aItem) || !$this->_isVisible($aItem))
  56. return false;
  57. $sMethod = '_getMenuItem' . str_replace(' ', '', ucwords(str_replace('-', ' ', $aItem['name'])));
  58. $bMethod = method_exists($this, $sMethod);
  59. $mixedItem = false;
  60. if($bMethod)
  61. $mixedItem = $this->$sMethod($aItem);
  62. if($mixedItem === true || !$bMethod) {
  63. $aItem = parent::_getMenuItem($aItem);
  64. if($aItem === false)
  65. return false;
  66. $mixedItem = $this->{'_getMenuItemDefault' . ($this->_bIsApi ? 'Api' : '')}($aItem);
  67. }
  68. if(empty($mixedItem))
  69. return false;
  70. if($this->_bIsApi)
  71. return $mixedItem;
  72. $sItem = $sClass = '';
  73. if(is_array($mixedItem))
  74. list($sItem, $sClass) = $mixedItem;
  75. else
  76. $sItem = $mixedItem;
  77. if(!empty($sClass))
  78. $sClass = ' ' . $sClass;
  79. if($this->_isSelected($aItem))
  80. $sClass .= ' bx-menu-tab-active';
  81. $sClass .= $this->_getVisibilityClass($aItem);
  82. return [
  83. 'name' => $aItem['name'],
  84. 'class' => $sClass,
  85. 'item' => $sItem
  86. ];
  87. }
  88. protected function _getMenuItemMoreAuto ($aItem)
  89. {
  90. $aItem['onclick'] = $this->_sJsObjectMoreAuto . '.more(this);';
  91. $aItem = parent::_getMenuItem($aItem);
  92. if($aItem === false)
  93. return false;
  94. if($this->_bIsApi)
  95. return $aItem;
  96. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentItemMore(), array(
  97. 'item' => $this->_getMenuItemDefault($aItem),
  98. 'popup' => BxTemplFunctions::getInstance()->transBox($this->_aHtmlIds['more_auto_popup'], $this->_oTemplate->parseHtmlByContent($this->_getTmplContentItemMorePopup(), array(
  99. 'content' => ''
  100. )), true)
  101. ));
  102. }
  103. protected function _getMenuItemDefault ($aItem)
  104. {
  105. if(!isset($aItem['class_wrp']))
  106. $aItem['class_wrp'] = '';
  107. if(!isset($aItem['class_link']))
  108. $aItem['class_link'] = '';
  109. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentItem(), $aItem);
  110. }
  111. protected function _getMenuItemDefaultApi ($aItem)
  112. {
  113. return $aItem;
  114. }
  115. protected function _rewriteMenuItem ($aItem, $aExtrasAddon = [])
  116. {
  117. $mixedResult = false;
  118. $aExtras = array_merge([
  119. 'menu' => $this->_sObject,
  120. 'menu_object' => $this,
  121. 'item' => $aItem,
  122. 'res' => &$mixedResult,
  123. ], $aExtrasAddon);
  124. /**
  125. * @hooks
  126. * @hookdef hook-bx_base_general-menu_custom_item '{module_name}', 'menu_custom_item' - hook to override menu item
  127. * - $unit_name - module name
  128. * - $action - equals `menu_custom_item`
  129. * - $object_id - not used
  130. * - $sender_id - not used
  131. * - $extra_params - array of additional params with the following array keys:
  132. * - `menu` - [string] menu name
  133. * - `menu_object` - [object] an instance of menu, @see BxDolMenu
  134. * - `item` - [array] menu item array as key&value pairs
  135. * - `module` - [string] module name
  136. * - `content_id` - [int] content id
  137. * - `content_data` - [array] content info array as key&value pairs
  138. * - `res` - [string] by ref, menu item code, can be overridden in hook processing
  139. * @hook @ref hook-bx_base_general-menu_custom_item
  140. */
  141. bx_alert($this->_sModule, 'menu_custom_item', 0, 0, $aExtras);
  142. /**
  143. * @hooks
  144. * @hookdef hook-menu-menu_custom_item 'menu', 'menu_custom_item' - hook to override menu item
  145. * It's equivalent to @ref hook-bx_base_general-menu_custom_item
  146. * @hook @ref hook-menu-menu_custom_item
  147. */
  148. bx_alert('menu', 'menu_custom_item', 0, 0, $aExtras);
  149. return $mixedResult;
  150. }
  151. protected function _getTmplContentItem()
  152. {
  153. return $this->_sTmplContentItem;
  154. }
  155. }
  156. /** @} */