BxNtfsGridSettingsAdministration.php 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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 !== (boolean)$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. if($this->_bGrouped && $aRow['type'] == BX_NTFS_STYPE_OTHER)
  143. $mixedValue = sprintf($this->_sTitleMask, _t($this->_oModule->_oConfig->getHandlersUnitTitle($aRow['unit'])), $mixedValue);
  144. if($this->_bGrouped && !$this->_isSettingsGroupValid($aRow))
  145. $mixedValue = $this->_oTemplate->parseIcon('exclamation-triangle', array('class' => 'bx-def-margin-sec-right', 'title' => _t('_bx_ntfs_grid_column_title_title_warning'))) . $mixedValue;
  146. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  147. }
  148. protected function _getActionActivate($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  149. {
  150. return $this->_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  151. }
  152. protected function _getActionDeactivate($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  153. {
  154. return $this->_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  155. }
  156. protected function _getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
  157. {
  158. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString(" AND `ts`.`delivery`=?", $this->_sDeliveryType);
  159. if(!$this->_bGrouped)
  160. return parent::_getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
  161. $CNF = &$this->_oModule->_oConfig->CNF;
  162. $aResult = array();
  163. $this->_sSource = $this->_aOptions['source'];
  164. $aTypes = $this->_oModule->_oConfig->getSettingsTypes();
  165. foreach($aTypes as $sType) {
  166. $this->_aOptions['source'] = $this->_sSource . $this->_oModule->_oDb->prepareAsString(" AND `ts`.`type`=?", $sType) . " GROUP BY `ts`.`group`";
  167. $aRows = parent::_getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
  168. if(empty($aRows) || !is_array($aRows))
  169. continue;
  170. $aResult[] = _t($CNF['T']['setting_' . $sType]);
  171. $aResult = array_merge($aResult, $aRows);
  172. }
  173. return $aResult;
  174. }
  175. protected function _updateSettingTitle($sTitle, &$aRow)
  176. {
  177. return $this->_oModule->_oDb->updateSetting(array('title' => $sTitle), array('id' => $aRow['id']));
  178. }
  179. protected function _isSettingsGroupValid(&$aRow)
  180. {
  181. if(!$this->_bGrouped)
  182. return true;
  183. $iEnabled = $iDisabled = 0;
  184. $sSql = $this->_sSource . $this->_oModule->_oDb->prepareAsString(" AND `ts`.`group`=? AND `ts`.`type`=?", $aRow['group'], $aRow['type']);
  185. $aItems = $this->_oModule->_oDb->getAll($sSql);
  186. foreach($aItems as $aItem)
  187. if((int)$aItem['active'] != 0)
  188. $iEnabled += 1;
  189. else
  190. $iDisabled += 1;
  191. return $iEnabled == 0 || $iDisabled == 0;
  192. }
  193. }
  194. /** @} */