BxReputationGridManage.php 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  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 Reputation Reputation
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxReputationGridManage extends BxTemplGrid
  12. {
  13. protected $_sModule;
  14. protected $_oModule;
  15. protected $_sFilter;
  16. protected $_sFilter1Name;
  17. protected $_sFilter1Value;
  18. protected $_aFilter1Values;
  19. protected $_sParamsDivider;
  20. public function __construct ($aOptions, $oTemplate = false)
  21. {
  22. $this->_sModule = 'bx_reputation';
  23. $this->_oModule = BxDolModule::getInstance($this->_sModule);
  24. parent::__construct ($aOptions, $oTemplate);
  25. $this->_aQueryReset = array($this->_aOptions['filter_get'], $this->_aOptions['paginate_get_start'], $this->_aOptions['paginate_get_per_page']);
  26. $this->_sParamsDivider = '#-#';
  27. $this->_sFilter = '';
  28. if(($sFilter = $this->_getFilterValue()))
  29. $this->_sFilter = $sFilter;
  30. $this->_sFilter1Name = 'filter1';
  31. $this->_aFilter1Values = [];
  32. $aUnits = $this->_oModule->_oDb->getHandlers(['type' => 'alert_units_list']);
  33. if(!empty($aUnits) && is_array($aUnits))
  34. foreach($aUnits as $sUnit)
  35. $this->_aFilter1Values[$sUnit] = _t('_' . $sUnit);
  36. $sFilter1 = bx_get($this->_sFilter1Name);
  37. if(!empty($sFilter1)) {
  38. $this->_sFilter1Value = bx_process_input($sFilter1);
  39. $this->_aQueryAppend['filter1'] = $this->_sFilter1Value;
  40. }
  41. }
  42. public function getCode($isDisplayHeader = true)
  43. {
  44. $mixedResult = parent::getCode($isDisplayHeader);
  45. if(!$mixedResult)
  46. return $mixedResult;
  47. return $this->_oModule->_oTemplate->getJsCode('manage', ['sObjNameGrid' => $this->_sObject]) . $mixedResult;
  48. }
  49. public function performActionEdit()
  50. {
  51. $CNF = &$this->_oModule->_oConfig->CNF;
  52. $sAction = 'edit';
  53. $aIds = $this->_getIds();
  54. if($aIds === false)
  55. return $this->_getActionResult([]);
  56. $iHandler = array_shift($aIds);
  57. $aHandler = $this->_oModule->_oDb->getHandlers(['type' => 'by_id', 'value' => $iHandler]);
  58. if(!is_array($aHandler) || empty($aHandler))
  59. return $this->_getActionResult([]);
  60. $sForm = $CNF['OBJECT_FORM_HANDLER_DISPLAY_EDIT'];
  61. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_HANDLER'], $CNF['OBJECT_FORM_HANDLER_DISPLAY_EDIT']);
  62. $oForm->setId($sForm);
  63. $oForm->setName($sForm);
  64. $oForm->setAction(BX_DOL_URL_ROOT . bx_append_url_params('grid.php', [
  65. 'o' => $this->_sObject,
  66. 'a' => $sAction,
  67. $this->_aOptions['filter_get'] => $this->_sFilter,
  68. 'id' => $iHandler
  69. ]));
  70. $oForm->initChecker($aHandler);
  71. if($oForm->isSubmittedAndValid()) {
  72. if(!$oForm->update($iHandler))
  73. return $this->_getActionResult(['msg' => _t($CNF['T']['err_cannot_perform'])]);
  74. return $this->_bIsApi ? [] : echoJson(['grid' => $this->getCode(false), 'blink' => $iHandler]);
  75. }
  76. if($this->_bIsApi)
  77. return $this->getFormBlockAPI($oForm, $sAction, $iHandler);
  78. $sContent = BxTemplStudioFunctions::getInstance()->popupBox($this->_oModule->_oConfig->getHtmlIds('handler_popup'), _t($CNF['T']['popup_title_handler_edit']), $this->_oModule->_oTemplate->parseHtmlByName('popup_handler.html', [
  79. 'form_id' => $oForm->getId(),
  80. 'form' => $oForm->getCode(true),
  81. 'object' => $this->_sObject,
  82. 'action' => $sAction
  83. ]));
  84. return echoJson(['popup' => ['html' => $sContent, 'options' => ['closeOnOuterClick' => false]]]);
  85. }
  86. public function performActionActivate()
  87. {
  88. $this->_performActionEnable(true);
  89. }
  90. public function performActionDeactivate()
  91. {
  92. $this->_performActionEnable(false);
  93. }
  94. protected function _isCheckboxDisabled($aRow)
  95. {
  96. return false;
  97. }
  98. protected function _getCellAlertUnit($mixedValue, $sKey, $aField, $aRow)
  99. {
  100. return self::_getCellDefault(_t('_' . $mixedValue), $sKey, $aField, $aRow);
  101. }
  102. protected function _getCellAlertAction($mixedValue, $sKey, $aField, $aRow)
  103. {
  104. return self::_getCellDefault(_t('_bx_reputation_grid_column_value_aa_' . $mixedValue), $sKey, $aField, $aRow);
  105. }
  106. protected function _getFilterControls()
  107. {
  108. parent::_getFilterControls();
  109. $sContent = $this->_getFilterSelectOne($this->_sFilter1Name, $this->_sFilter1Value, $this->_aFilter1Values);
  110. $sContent .= $this->_getSearchInput();
  111. return $sContent;
  112. }
  113. protected function _getFilterSelectOne($sFilterName, $sFilterValue, $aFilterValues, $bAddSelectOne = true)
  114. {
  115. if(empty($sFilterName) || empty($aFilterValues))
  116. return '';
  117. $CNF = &$this->_oModule->_oConfig->CNF;
  118. $sJsObject = $this->_oModule->_oConfig->getJsObject('manage');
  119. $aInputValues = [];
  120. if($bAddSelectOne)
  121. $aInputValues[''] = _t($CNF['T']['filter_item_select_one_' . $sFilterName]);
  122. foreach($aFilterValues as $sKey => $sValue)
  123. $aInputValues[$sKey] = _t($sValue);
  124. $aInputModules = [
  125. 'type' => 'select',
  126. 'name' => $sFilterName,
  127. 'attrs' => [
  128. 'id' => 'bx-grid-' . $sFilterName . '-' . $this->_sObject,
  129. 'onChange' => 'javascript:' . $sJsObject . '.onChangeFilter(this)'
  130. ],
  131. 'value' => $sFilterValue,
  132. 'values' => $aInputValues
  133. ];
  134. $oForm = new BxTemplFormView([]);
  135. return $oForm->genRow($aInputModules);
  136. }
  137. protected function _getSearchInput()
  138. {
  139. $sJsObject = $this->_oModule->_oConfig->getJsObject('manage_tools');
  140. $aInputSearch = [
  141. 'type' => 'text',
  142. 'name' => 'search',
  143. 'attrs' => [
  144. 'id' => 'bx-grid-search-' . $this->_sObject,
  145. 'onKeyup' => 'javascript:$(this).off(\'keyup focusout\'); ' . $sJsObject . '.onChangeFilter(this)',
  146. 'onBlur' => 'javascript:' . $sJsObject . '.onChangeFilter(this)',
  147. ]
  148. ];
  149. $oForm = new BxTemplFormView([]);
  150. return $oForm->genRow($aInputSearch);
  151. }
  152. protected function _getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
  153. {
  154. if(strpos($sFilter, $this->_sParamsDivider) !== false)
  155. list($this->_sFilter1Value, $sFilter) = explode($this->_sParamsDivider, $sFilter);
  156. if(!empty($this->_sFilter1Value))
  157. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString(" AND `alert_unit`=?", $this->_sFilter1Value);
  158. return parent::_getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
  159. }
  160. protected function _performActionEnable($isChecked)
  161. {
  162. $CNF = &$this->_oModule->_oConfig->CNF;
  163. $aIds = bx_get('ids');
  164. if(!$aIds || !is_array($aIds))
  165. return $this->_getActionResult([]);
  166. $iAffected = 0;
  167. $aIdsAffected = array();
  168. foreach($aIds as $iId)
  169. if($this->_enable($iId, $isChecked)) {
  170. $aIdsAffected[] = $iId;
  171. $iAffected++;
  172. }
  173. if(!$iAffected)
  174. return $this->_getActionResult(['msg' => _t($CNF['T']['grid_action_err_perform'])]);
  175. return $this->_bIsApi ? [] : echoJson(['grid' => $this->getCode(false), 'blink' => $aIdsAffected]);
  176. }
  177. protected function _getIds()
  178. {
  179. $aIds = bx_get('ids');
  180. if(!$aIds || !is_array($aIds)) {
  181. $iId = (int)bx_get('id');
  182. if(!$iId)
  183. return false;
  184. $aIds = [$iId];
  185. }
  186. return $aIds;
  187. }
  188. }
  189. /** @} */