BxBaseMenuMoreAuto.php 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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. define('BX_DEF_MENU_ITEM_MORE_AUTO', 'more-auto');
  11. class BxBaseMenuMoreAuto extends BxTemplMenu
  12. {
  13. protected static $_sTmplContentItemMore;
  14. protected static $_sTmplContentItemMorePopup;
  15. protected $_sTmplNameItemMore;
  16. protected $_sTmplNameItemMorePopup;
  17. protected $_bMoreAuto;
  18. protected $_iMoreAutoItemsStatic;
  19. protected $_bMoreAutoItemsStaticOnly;
  20. protected $_sJsClassMoreAuto;
  21. protected $_sJsObjectMoreAuto;
  22. protected $_sJsCallMoreAuto;
  23. protected $_aHtmlIds;
  24. public function __construct($aObject, $oTemplate = false)
  25. {
  26. parent::__construct($aObject, $oTemplate);
  27. $this->_sTmplNameItemMore = 'menu_item_more.html';
  28. $this->_sTmplNameItemMorePopup = 'menu_item_more_popup.html';
  29. $this->_bMoreAuto = null;
  30. $this->_iMoreAutoItemsStatic = (int)$aObject['persistent'];
  31. $this->_bMoreAutoItemsStaticOnly = false;
  32. $this->_sJsClassMoreAuto = 'BxDolMenuMoreAuto';
  33. $this->_sJsObjectMoreAuto = 'oMenuMoreAuto' . bx_gen_method_name($this->_sObject);
  34. $this->_sJsCallMoreAuto = "if(!{js_object}) {var {js_object} = new {js_class}({js_params}); {js_object}.init();}";
  35. $sPrefix = str_replace('_', '-', $this->_sObject);
  36. $this->_aHtmlIds = array(
  37. 'main' => $sPrefix,
  38. 'more_auto_popup' => $sPrefix . '-ma-popup',
  39. );
  40. }
  41. public function getCode()
  42. {
  43. $sResult = parent::getCode();
  44. if($this->_isMoreAuto()) {
  45. $this->_oTemplate->addJs(array('BxDolMenuMoreAuto.js'));
  46. if(!empty($sResult))
  47. $sResult = $this->_oTemplate->parseHtmlByName('menu_more_auto.html', array(
  48. 'menu' => $sResult
  49. ));
  50. }
  51. return $sResult;
  52. }
  53. public function isMoreAuto()
  54. {
  55. return $this->_isMoreAuto();
  56. }
  57. protected function _isMoreAuto()
  58. {
  59. if(is_bool($this->_bMoreAuto))
  60. return $this->_bMoreAuto;
  61. if(!isset($this->_aObject['menu_items']))
  62. $this->_aObject['menu_items'] = $this->getMenuItemsRaw();
  63. $sItem = BX_DEF_MENU_ITEM_MORE_AUTO;
  64. if(!empty($this->_aObject['menu_items'][$sItem]) && is_array($this->_aObject['menu_items'][$sItem]) && (int)$this->_aObject['menu_items'][$sItem]['active'] == 1)
  65. $this->_bMoreAuto = true;
  66. else
  67. $this->_bMoreAuto = false;
  68. return $this->_bMoreAuto;
  69. }
  70. protected function _getTemplateVars()
  71. {
  72. $aResult = parent::_getTemplateVars();
  73. $bMoreAuto = $this->_isMoreAuto();
  74. return array_merge($aResult, [
  75. 'html_id' => $this->_getHtmlIdMain(),
  76. 'bx_if:show_more_auto_class' => [
  77. 'condition' => $bMoreAuto,
  78. 'content' => []
  79. ],
  80. 'js_code' => $bMoreAuto ? $this->_getJsCodeMoreAuto() : ''
  81. ]);
  82. }
  83. protected function _getJsCodeMoreAuto()
  84. {
  85. return $this->_oTemplate->_wrapInTagJsCode(bx_replace_markers($this->_sJsCallMoreAuto, [
  86. 'js_class' => $this->_getJsClassMoreAuto(),
  87. 'js_object' => $this->_getJsObjectMoreAuto(),
  88. 'js_params' => json_encode([
  89. 'sObject' => $this->_sObject,
  90. 'iItemsStatic' => $this->_iMoreAutoItemsStatic,
  91. 'bItemsStaticOnly' => $this->_bMoreAutoItemsStaticOnly ? 1 : 0,
  92. 'aHtmlIds' => $this->_getHtmlIds()
  93. ])
  94. ]));
  95. }
  96. protected function _getMenuItem ($aItem)
  97. {
  98. $aItem['popup'] = '';
  99. if($aItem['name'] != BX_DEF_MENU_ITEM_MORE_AUTO)
  100. return parent::_getMenuItem($aItem);
  101. $aItem['onclick'] = $this->_getJsObjectMoreAuto() . '.more(this);';
  102. $aItem['popup'] = BxTemplFunctions::getInstance()->transBox($this->_aHtmlIds['more_auto_popup'], $this->_oTemplate->parseHtmlByContent($this->_getTmplContentItemMorePopup(), array(
  103. 'content' => ''
  104. )), true);
  105. return parent::_getMenuItem($aItem);
  106. }
  107. protected function _getHtmlIds()
  108. {
  109. return $this->_aHtmlIds;
  110. }
  111. protected function _getHtmlIdMain()
  112. {
  113. return $this->_aHtmlIds['main'];
  114. }
  115. protected function _getJsClassMoreAuto()
  116. {
  117. return $this->_sJsClassMoreAuto;
  118. }
  119. protected function _getJsObjectMoreAuto()
  120. {
  121. return $this->_sJsObjectMoreAuto;
  122. }
  123. protected function _getTmplContentItemMore()
  124. {
  125. if(empty(self::$_sTmplContentItemMore))
  126. self::$_sTmplContentItemMore = $this->_oTemplate->getHtml($this->_sTmplNameItemMore);
  127. return self::$_sTmplContentItemMore;
  128. }
  129. protected function _getTmplContentItemMorePopup()
  130. {
  131. if(empty(self::$_sTmplContentItemMorePopup))
  132. self::$_sTmplContentItemMorePopup = $this->_oTemplate->getHtml($this->_sTmplNameItemMorePopup);
  133. return self::$_sTmplContentItemMorePopup;
  134. }
  135. }
  136. /** @} */