BxBaseMenuMoreAuto.php 4.6 KB

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