BxBaseMenuCustom.php 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  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. if(is_array($mixedItem))
  72. $mixedItem = array_merge($mixedItem, [
  73. 'primary' => isset($aItem['primary']) ? $aItem['primary'] : 0,
  74. ]);
  75. return $mixedItem;
  76. }
  77. $sItem = $sClass = '';
  78. if(is_array($mixedItem))
  79. list($sItem, $sClass) = $mixedItem;
  80. else
  81. $sItem = $mixedItem;
  82. if(!empty($sClass))
  83. $sClass = ' ' . $sClass;
  84. if($this->_isSelected($aItem))
  85. $sClass .= ' bx-menu-tab-active';
  86. $sClass .= $this->_getVisibilityClass($aItem);
  87. return [
  88. 'name' => $aItem['name'],
  89. 'class' => $sClass,
  90. 'item' => $sItem
  91. ];
  92. }
  93. protected function _getMenuItemMoreAuto ($aItem)
  94. {
  95. $aItem['onclick'] = $this->_sJsObjectMoreAuto . '.more(this);';
  96. $aItem = parent::_getMenuItem($aItem);
  97. if($aItem === false)
  98. return false;
  99. if($this->_bIsApi)
  100. return $aItem;
  101. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentItemMore(), array(
  102. 'item' => $this->_getMenuItemDefault($aItem),
  103. 'popup' => BxTemplFunctions::getInstance()->transBox($this->_aHtmlIds['more_auto_popup'], $this->_oTemplate->parseHtmlByContent($this->_getTmplContentItemMorePopup(), array(
  104. 'content' => ''
  105. )), true)
  106. ));
  107. }
  108. protected function _getMenuItemDefault ($aItem)
  109. {
  110. if(!isset($aItem['class_wrp']))
  111. $aItem['class_wrp'] = '';
  112. if(!isset($aItem['class_link']))
  113. $aItem['class_link'] = '';
  114. return $this->_oTemplate->parseHtmlByContent($this->_getTmplContentItem(), $aItem);
  115. }
  116. protected function _getMenuItemDefaultApi ($aItem)
  117. {
  118. return $aItem;
  119. }
  120. protected function _rewriteMenuItem ($aItem, $aExtrasAddon = [])
  121. {
  122. $mixedResult = false;
  123. $aExtras = array_merge([
  124. 'menu' => $this->_sObject,
  125. 'menu_object' => $this,
  126. 'item' => $aItem,
  127. 'res' => &$mixedResult,
  128. ], $aExtrasAddon);
  129. /**
  130. * @hooks
  131. * @hookdef hook-bx_base_general-menu_custom_item '{module_name}', 'menu_custom_item' - hook to override menu item
  132. * - $unit_name - module name
  133. * - $action - equals `menu_custom_item`
  134. * - $object_id - not used
  135. * - $sender_id - not used
  136. * - $extra_params - array of additional params with the following array keys:
  137. * - `menu` - [string] menu name
  138. * - `menu_object` - [object] an instance of menu, @see BxDolMenu
  139. * - `item` - [array] menu item array as key&value pairs
  140. * - `module` - [string] module name
  141. * - `content_id` - [int] content id
  142. * - `content_data` - [array] content info array as key&value pairs
  143. * - `res` - [string] by ref, menu item code, can be overridden in hook processing
  144. * @hook @ref hook-bx_base_general-menu_custom_item
  145. */
  146. bx_alert($this->_sModule, 'menu_custom_item', 0, 0, $aExtras);
  147. /**
  148. * @hooks
  149. * @hookdef hook-menu-menu_custom_item 'menu', 'menu_custom_item' - hook to override menu item
  150. * It's equivalent to @ref hook-bx_base_general-menu_custom_item
  151. * @hook @ref hook-menu-menu_custom_item
  152. */
  153. bx_alert('menu', 'menu_custom_item', 0, 0, $aExtras);
  154. return $mixedResult;
  155. }
  156. protected function _getTmplContentItem()
  157. {
  158. return $this->_sTmplContentItem;
  159. }
  160. }
  161. /** @} */