BxNtfsGridSettingsAdministration.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 Notifications Notifications
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. require_once(BX_DOL_DIR_STUDIO_INC . 'utils.inc.php');
  12. class BxNtfsGridSettingsAdministration extends BxTemplGrid
  13. {
  14. protected $_sModule;
  15. protected $_oModule;
  16. protected $_bAdministration;
  17. protected $_bGrouped;
  18. protected $_sDeliveryType;
  19. protected $_sTitleMask;
  20. protected $_sSource;
  21. public function __construct ($aOptions, $oTemplate = false)
  22. {
  23. parent::__construct ($aOptions, $oTemplate);
  24. $this->_sModule = 'bx_notifications';
  25. $this->_oModule = BxDolModule::getInstance($this->_sModule);
  26. $CNF = &$this->_oModule->_oConfig->CNF;
  27. $this->_bAdministration = true;
  28. $this->_bGrouped = $this->_oModule->_oConfig->isSettingsGrouped();
  29. if($this->_bGrouped)
  30. $this->_aOptions['show_total_count'] = 0;
  31. $this->_sDeliveryType = BX_BASE_MOD_NTFS_DTYPE_SITE;
  32. $this->_sTitleMask = _t('_bx_ntfs_setting_title_mask', '%s', '%s');
  33. $this->_sSource = '';
  34. $sDeliveryType = bx_get('delivery_type');
  35. if(!empty($sDeliveryType))
  36. $this->setDeliveryType($sDeliveryType);
  37. $this->init();
  38. }
  39. public function init()
  40. {
  41. if(!$this->_bGrouped && !isset($this->_aOptions['fields']['unit'], $this->_aOptions['fields']['type']))
  42. $this->_aOptions['fields'] = bx_array_insert_after(array(
  43. 'unit' => array('title' => _t('_bx_ntfs_grid_column_title_unit'), 'width' => '10%', 'translatable' => 0 ,'chars_limit' => 0),
  44. 'type' => array('title' => _t('_bx_ntfs_grid_column_title_type'), 'width' => '20%', 'translatable' => 0 ,'chars_limit' => 0),
  45. ), $this->_aOptions['fields'], 'switcher');
  46. }
  47. public function setGrouped($bGrouped)
  48. {
  49. $bReinit = $this->_bGrouped !== (bool)$bGrouped;
  50. $this->_bGrouped = $bGrouped;
  51. if($bReinit)
  52. $this->init();
  53. }
  54. public function setDeliveryType($sType)
  55. {
  56. $this->_sDeliveryType = bx_process_input($sType);
  57. $this->_aQueryAppend['delivery_type'] = $this->_sDeliveryType;
  58. }
  59. public function performActionActivate()
  60. {
  61. parent::performActionEnable(1);
  62. }
  63. public function performActionDeactivate()
  64. {
  65. parent::performActionEnable(0);
  66. }
  67. public function performActionChangeValue()
  68. {
  69. $aIds = bx_get('ids');
  70. if(empty($aIds) || !is_array($aIds))
  71. return echoJson(array());
  72. $iId = array_shift($aIds);
  73. $iValue = $this->_switcherChecked2State((int)bx_get('checked'));
  74. if(!$this->_bGrouped)
  75. $mixedResult = $this->_oModule->_oDb->updateSetting(array('value' => $iValue), array('id' => $iId));
  76. else
  77. $mixedResult = $this->_oModule->changeSettingsValueLike($iId, 'value', $iValue, $this->_bAdministration);
  78. $aResult = array();
  79. if($mixedResult === false)
  80. $aResult = array('msg' => _t('_bx_ntfs_txt_err_cannot_perform'));
  81. else if((int)$mixedResult == 0)
  82. $aResult = array('msg' => _t('_bx_ntfs_txt_err_nothing_changed'));
  83. return echoJson($aResult);
  84. }
  85. protected function _enable ($mixedId, $isChecked)
  86. {
  87. if(!$this->_bGrouped)
  88. return parent::_enable($mixedId, $isChecked);
  89. return $this->_oModule->enableSettingsLike($mixedId, $isChecked, $this->_bAdministration);
  90. }
  91. protected function _isCheckboxDisabled($aRow)
  92. {
  93. return false;
  94. }
  95. protected function _getFilterControls()
  96. {
  97. if($this->_bGrouped)
  98. return '';
  99. return parent::_getFilterControls();
  100. }
  101. protected function _getCellValue($mixedValue, $sKey, $aField, $aRow)
  102. {
  103. $sAttr = $this->_convertAttrs(
  104. $aField, 'attr_cell',
  105. 'bx-def-padding-sec-bottom bx-def-padding-sec-top',
  106. isset($aField['width']) ? 'width:' . $aField['width'] : false
  107. );
  108. $oForm = new BxTemplFormView(array(), $this->_oTemplate);
  109. $oForm->addCssJs();
  110. $aInput = array(
  111. 'type' => 'switcher',
  112. 'name' => $this->_sObject . '_change_value_' . $aRow[$this->_aOptions['field_id']],
  113. 'caption' => '',
  114. 'attrs' => array (
  115. 'bx_grid_action_single' => 'change_value',
  116. 'bx_grid_action_confirm' => '',
  117. 'bx_grid_action_data' => $aRow[$this->_aOptions['field_id']],
  118. ),
  119. 'value' => $aRow[$this->_aOptions['field_id']],
  120. 'checked' => $this->_isSwitcherOn($mixedValue, $sKey, $aField, $aRow),
  121. );
  122. $sSwitcher = $oForm->genInput($aInput);
  123. return '<td ' . $sAttr . '>' . $sSwitcher . '</td>';
  124. }
  125. protected function _getCellUnit($mixedValue, $sKey, $aField, $aRow)
  126. {
  127. return parent::_getCellDefault(_t($this->_oModule->_oConfig->getHandlersUnitTitle($mixedValue)), $sKey, $aField, $aRow);
  128. }
  129. protected function _getCellType($mixedValue, $sKey, $aField, $aRow)
  130. {
  131. $CNF = &$this->_oModule->_oConfig->CNF;
  132. return parent::_getCellDefault(_t($CNF['T']['setting_' . $mixedValue]), $sKey, $aField, $aRow);
  133. }
  134. protected function _getCellTitle($mixedValue, $sKey, $aField, $aRow)
  135. {
  136. if(empty($mixedValue)) {
  137. $mixedValueKey = $this->_oModule->_oConfig->getHandlersActionTitle($aRow['unit'], $aRow['action'], $aRow['type']);
  138. $mixedValue = _t($mixedValueKey);
  139. if(strcmp($mixedValueKey, $mixedValue) !== 0)
  140. $this->_updateSettingTitle($mixedValueKey, $aRow);
  141. }
  142. $sTitle = $mixedValue;
  143. if($this->_bGrouped) {
  144. if(($sKey = '_bx_ntfs_alert_action_group_' . $aRow['group']) && ($_sKey = _t($sKey)) && strcmp($sKey, $_sKey) !== 0)
  145. $sTitle = $_sKey;
  146. else if($aRow['type'] == BX_NTFS_STYPE_OTHER)
  147. $sTitle = sprintf($this->_sTitleMask, _t($this->_oModule->_oConfig->getHandlersUnitTitle($aRow['unit'])), $sTitle);
  148. }
  149. if(!$this->_bIsApi) {
  150. $iInfo = '';
  151. if(($sKey = '_bx_ntfs_alert_action_group_info_' . $aRow['group']) && ($_sKey = _t($sKey)) && strcmp($sKey, $_sKey) !== 0)
  152. $iInfo = $_sKey;
  153. else if(($sKey = '_bx_ntfs_alert_action_group_info_' . $aRow['group'] . '_' . $aRow['type']) && ($_sKey = _t($sKey)) && strcmp($sKey, $_sKey) !== 0)
  154. $iInfo = $_sKey;
  155. else
  156. $iInfo = $this->_oModule->_oConfig->getHandlersActionInfo($aRow['unit'], $aRow['action'], $aRow['type'], true);
  157. $sStylePrefix = $this->_oModule->_oConfig->getPrefix('style');
  158. $mixedValue = $this->_oModule->_oTemplate->parseHtmlByName('setting_title.html', [
  159. 'style_prefix' => $sStylePrefix,
  160. 'bx_if:show_warning' => [
  161. 'condition' => $this->_bGrouped && !$this->_isSettingsGroupValid($aRow),
  162. 'content' => [
  163. 'style_prefix' => $sStylePrefix
  164. ]
  165. ],
  166. 'title' => $sTitle,
  167. 'info' => $iInfo
  168. ]);
  169. }
  170. else
  171. $mixedValue = $sTitle;
  172. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  173. }
  174. protected function _getActionActivate($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  175. {
  176. return $this->_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  177. }
  178. protected function _getActionDeactivate($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  179. {
  180. return $this->_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  181. }
  182. protected function _getData ($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
  183. {
  184. if(!$this->_bIsApi)
  185. return parent::_getData($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
  186. $sSource = $this->_aOptions['source'];
  187. $aData = [];
  188. $aDeliveryTypes = $this->_oModule->_oConfig->getDeliveryTypes();
  189. foreach($aDeliveryTypes as $sDeliveryType) {
  190. $this->_sDeliveryType = $sDeliveryType;
  191. $this->_aOptions['source'] = $sSource;
  192. $aData[] = [
  193. 'delivery' => $sDeliveryType,
  194. 'items' => parent::_getData($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
  195. ];
  196. }
  197. return $aData;
  198. }
  199. protected function _getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
  200. {
  201. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString(" AND `ts`.`delivery`=?", $this->_sDeliveryType);
  202. if(!$this->_bGrouped)
  203. return parent::_getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
  204. $CNF = &$this->_oModule->_oConfig->CNF;
  205. $aResult = array();
  206. $this->_sSource = $this->_aOptions['source'];
  207. $aTypes = $this->_oModule->_oConfig->getSettingsTypes();
  208. foreach($aTypes as $sType) {
  209. $this->_aOptions['source'] = $this->_sSource . $this->_oModule->_oDb->prepareAsString(" AND `ts`.`type`=?", $sType) . " GROUP BY `ts`.`group`";
  210. $aRows = parent::_getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
  211. if(empty($aRows) || !is_array($aRows))
  212. continue;
  213. $aResult[] = _t($CNF['T']['setting_' . $sType]);
  214. $aResult = array_merge($aResult, $aRows);
  215. }
  216. return $aResult;
  217. }
  218. protected function _updateSettingTitle($sTitle, &$aRow)
  219. {
  220. return $this->_oModule->_oDb->updateSetting(array('title' => $sTitle), array('id' => $aRow['id']));
  221. }
  222. protected function _isSettingsGroupValid(&$aRow)
  223. {
  224. if(!$this->_bGrouped)
  225. return true;
  226. $iEnabled = $iDisabled = 0;
  227. $sSql = $this->_sSource . $this->_oModule->_oDb->prepareAsString(" AND `ts`.`group`=? AND `ts`.`type`=?", $aRow['group'], $aRow['type']);
  228. $aItems = $this->_oModule->_oDb->getAll($sSql);
  229. foreach($aItems as $aItem)
  230. if((int)$aItem['active'] != 0)
  231. $iEnabled += 1;
  232. else
  233. $iDisabled += 1;
  234. return $iEnabled == 0 || $iDisabled == 0;
  235. }
  236. public function decodeDataAPI($aData)
  237. {
  238. $sKeyId = $this->_aOptions['field_id'];
  239. $sMethodDefault = '_getCellDefault';
  240. $aDataR = [];
  241. foreach($aData as $aDataItem) {
  242. $aDataS = [];
  243. foreach($aDataItem['items'] as $iKey => $aRow) {
  244. if(!is_string($aRow)) {
  245. $aDataS[$iKey] = [$sKeyId => $aRow[$sKeyId]];
  246. foreach($this->_aOptions['fields'] as $sKey => $aField) {
  247. $sMethod = '_getCell' . $this->_genMethodName($sKey);
  248. if(!method_exists($this, $sMethod))
  249. $sMethod = $sMethodDefault;
  250. $aDataS[$iKey][$sKey] = $this->$sMethod(isset($aRow[$sKey]) ? $aRow[$sKey] : _t('_undefined'), $sKey, $aField, $aRow);
  251. }
  252. }
  253. else
  254. $aDataS[$iKey] = $aRow;
  255. }
  256. $aDataR[] = [
  257. 'delivery' => $aDataItem['delivery'],
  258. 'items' => $aDataS
  259. ];
  260. }
  261. return $aDataR;
  262. }
  263. }
  264. /** @} */