BxBaseMenuMoreAuto.php 4.8 KB

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