BxBaseModGeneralGridAdministration.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  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 BaseGeneral Base classes for modules
  7. * @ingroup UnaModules
  8. * @{
  9. */
  10. define('BX_DOL_MANAGE_TOOLS_ADMINISTRATION', 'administration');
  11. define('BX_DOL_MANAGE_TOOLS_COMMON', 'common');
  12. class BxBaseModGeneralGridAdministration extends BxBaseModGeneralGrid
  13. {
  14. protected $_sManageType;
  15. protected $_sParamsDivider;
  16. protected $_sStatusField;
  17. protected $_aStatusValues;
  18. protected $_bDeleteWithBgJobs;
  19. public function __construct ($aOptions, $oTemplate = false)
  20. {
  21. parent::__construct ($aOptions, $oTemplate);
  22. $this->_aQueryReset = array($this->_aOptions['filter_get'], $this->_aOptions['paginate_get_start'], $this->_aOptions['paginate_get_per_page']);
  23. $this->_sManageType = BX_DOL_MANAGE_TOOLS_ADMINISTRATION;
  24. $this->_sParamsDivider = '#-#';
  25. $this->_aStatusValues = array('active');
  26. $this->_sDefaultSortingOrder = 'DESC';
  27. $this->_bDeleteWithBgJobs = false;
  28. }
  29. public function performActionDelete($aParams = array())
  30. {
  31. $CNF = &$this->_oModule->_oConfig->CNF;
  32. $iAffected = 0;
  33. $aResult = [];
  34. $aIds = bx_get('ids');
  35. if(!$aIds || !is_array($aIds))
  36. return echoJson($aResult);
  37. $aIdsAffected = array ();
  38. foreach($aIds as $iId) {
  39. $aContentInfo = $this->_getContentInfo($iId);
  40. if($this->_oModule->checkAllowedDelete($aContentInfo) !== CHECK_ACTION_RESULT_ALLOWED)
  41. continue;
  42. if(!$this->_doDelete($iId, $aParams))
  43. continue;
  44. if(!$this->_onDelete($iId, $aParams))
  45. continue;
  46. $this->_oModule->checkAllowedDelete($aContentInfo, true);
  47. $aIdsAffected[] = $iId;
  48. $iAffected++;
  49. }
  50. if($iAffected) {
  51. if(!$this->_bIsApi)
  52. $aResult = !$this->_bDeleteWithBgJobs ? ['grid' => $this->getCode(false), 'blink' => $aIdsAffected] : ['msg' => _t($CNF['T']['grid_action_msg_delete_scheduled'])];
  53. else
  54. $aResult = [];
  55. }
  56. else
  57. $aResult = ['msg' => _t($CNF['T']['grid_action_err_delete'])];
  58. return $this->_bIsApi ? $aResult : echoJson($aResult);
  59. }
  60. public function performActionClearReports($aParams = array())
  61. {
  62. $CNF = &$this->_oModule->_oConfig->CNF;
  63. $iAffected = 0;
  64. $aIds = bx_get('ids');
  65. if(!$aIds || !is_array($aIds)) {
  66. echoJson(array());
  67. exit;
  68. }
  69. $aIdsAffected = array ();
  70. foreach($aIds as $iId) {
  71. if (isset($CNF['OBJECT_REPORTS'])){
  72. $oReport = BxDolReport::getObjectInstance($CNF['OBJECT_REPORTS'], $iId);
  73. $oReport->actionClearReport();
  74. $aIdsAffected[] = $iId;
  75. $iAffected++;
  76. }
  77. }
  78. echoJson($iAffected ? array('grid' => $this->getCode(false), 'blink' => $aIdsAffected) : array('msg' => _t($CNF['T']['grid_action_err_clear_reports'])));
  79. }
  80. protected function _getActionAuditContent($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  81. {
  82. if (!getParam('sys_audit_enable') || getParam('sys_audit_acl_levels') == '')
  83. return;
  84. $iProfileId = bx_get_logged_profile_id();
  85. if (!BxDolAcl::getInstance()->isMemberLevelInSet(explode(',', getParam('sys_audit_acl_levels')), $iProfileId))
  86. return;
  87. $CNF = &$this->_oModule->_oConfig->CNF;
  88. $sUrl = BX_DOL_URL_ROOT . 'page/audit-administration?module=' . $this->_oModule->getName() . '&content_id=' . $aRow[$CNF['FIELD_ID']];
  89. $a['attr'] = array_merge($a['attr'], array(
  90. "onclick" => "window.open('" . $sUrl . "','_audit');"
  91. ));
  92. return $this->_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  93. }
  94. protected function _getActionDelete($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  95. {
  96. if($this->_sManageType == BX_DOL_MANAGE_TOOLS_ADMINISTRATION && !$this->_oModule->_isAdministrator())
  97. return '';
  98. $CNF = &$this->_oModule->_oConfig->CNF;
  99. if(isset($CNF['FIELD_ID']) && isset($aRow[$CNF['FIELD_ID']])){
  100. $aContentInfo = $this->_getContentInfo($aRow[$CNF['FIELD_ID']]);
  101. if($this->_oModule->checkAllowedDelete($aContentInfo) !== CHECK_ACTION_RESULT_ALLOWED)
  102. return '';
  103. }
  104. return parent::_getActionDelete($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  105. }
  106. protected function _getActionSettings($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  107. {
  108. $sJsObject = $this->_oModule->_oConfig->getJsObject('manage_tools');
  109. $sMenuName = $this->_oModule->_oConfig->CNF['OBJECT_MENU_MANAGE_TOOLS'];
  110. $oMenu = BxDolMenu::getObjectInstance($sMenuName);
  111. $oMenu->setContentId($aRow['id']);
  112. $sMenu = $oMenu->getCode();
  113. if(empty($sMenu))
  114. return '';
  115. $a['attr'] = array_merge($a['attr'], array(
  116. "bx-popup-id" => $sMenuName . "-" . $aRow['id'],
  117. "onclick" => "$(this).off('click'); " . $sJsObject . ".onClickSettings('" . $sMenuName . "', this);"
  118. ));
  119. return $this->_getActionDefault($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  120. }
  121. protected function _getFilterControlsAPI($aFilters = [])
  122. {
  123. $CNF = &$this->_oModule->_oConfig->CNF;
  124. if(!empty($aFilters) && is_array($aFilters))
  125. foreach($aFilters as $sName => &$aValues) {
  126. $aValues[] = ['value' => '', 'title' => _t($CNF['T']['filter_item_select_one_' . $sName])];
  127. $aFilterValues = $this->{'_a' . ucfirst($sName) . 'Values'};
  128. if(empty($aFilterValues) || !is_array($aFilterValues))
  129. continue;
  130. foreach($aFilterValues as $sKey => $sValue)
  131. $aValues[] = ['value' => $sKey, 'title' => _t($sValue)];
  132. }
  133. return parent::_getFilterControlsAPI($aFilters);
  134. }
  135. protected function _getFilterOnChange()
  136. {
  137. return $this->_oModule->_oConfig->getJsObject('manage_tools') . '.onChangeFilter(this)';
  138. }
  139. protected function _getContentInfo($iId)
  140. {
  141. return $this->_oModule->_oDb->getContentInfoById($iId);
  142. }
  143. protected function _getProfileObject($iId)
  144. {
  145. return BxDolProfile::getInstanceMagic($iId);
  146. }
  147. protected function _getManageAccountUrl($sFilter = '')
  148. {
  149. $sModuleAccounts = 'bx_accounts';
  150. if(!BxDolModuleQuery::getInstance()->isEnabledByName($sModuleAccounts))
  151. return '';
  152. $sTypeUpc = strtoupper($this->_sManageType);
  153. $oModuleAccounts = BxDolModule::getInstance($sModuleAccounts);
  154. if(!$oModuleAccounts || empty($oModuleAccounts->_oConfig->CNF['URL_MANAGE_' . $sTypeUpc]))
  155. return '';
  156. $sLink = $oModuleAccounts->_oConfig->CNF['URL_MANAGE_' . $sTypeUpc];
  157. $sLink = bx_absolute_url(BxDolPermalinks::getInstance()->permalink($sLink));
  158. if(!empty($sFilter))
  159. $sLink = bx_append_url_params($sLink, ['filter' => $sFilter]);
  160. return $sLink;
  161. }
  162. protected function _enable ($mixedId, $isChecked)
  163. {
  164. return $this->__enable ($mixedId, $isChecked);
  165. }
  166. protected function __enable ($mixedId, $isChecked)
  167. {
  168. $CNF = &$this->_oModule->_oConfig->CNF;
  169. $mixedResult = parent::_enable($mixedId, $isChecked);
  170. if(!$mixedResult)
  171. return $mixedResult;
  172. if(!empty($CNF['FIELD_CHANGED']))
  173. $this->_oModule->_oDb->updateEntriesBy([$CNF['FIELD_CHANGED'] => time()], [$this->_aOptions['field_id'] => $mixedId]);
  174. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($mixedId);
  175. $this->_oModule->alertAfterEdit($aContentInfo);
  176. $iContextId = isset($CNF['FIELD_ALLOW_VIEW_TO']) && (!empty($aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']]) && (int)$aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']] < 0) ? - $aContentInfo[$CNF['FIELD_ALLOW_VIEW_TO']] : 0;
  177. $AuditParams = [
  178. 'content_title' => (isset($CNF['FIELD_TITLE']) && isset($aContentInfo[$CNF['FIELD_TITLE']])) ? $aContentInfo[$CNF['FIELD_TITLE']] : '',
  179. 'context_profile_id' => $iContextId,
  180. 'content_info_object' => isset($CNF['OBJECT_CONTENT_INFO']) ? $CNF['OBJECT_CONTENT_INFO'] : '',
  181. 'data' => $aContentInfo
  182. ];
  183. if ($iContextId > 0)
  184. $AuditParams['context_profile_title'] = BxDolProfile::getInstance($iContextId)->getDisplayName();
  185. bx_audit(
  186. $mixedId,
  187. $this->_oModule->getName(),
  188. '_sys_audit_action_content_' . ($isChecked ? 'enabled': 'disabled'),
  189. $AuditParams
  190. );
  191. return $mixedResult;
  192. }
  193. protected function _doDelete($iId, $aParams = array())
  194. {
  195. return $this->_oModule->serviceDeleteEntity($iId) == '';
  196. }
  197. protected function _onDelete($iId, $aParams = array())
  198. {
  199. return true;
  200. }
  201. protected function _getCellSwitcher ($mixedValue, $sKey, $aField, $aRow)
  202. {
  203. $CNF = &$this->_oModule->_oConfig->CNF;
  204. if($this->_bIsApi)
  205. return ['type' => 'switcher', 'data' => $aRow[$this->_sStatusField], 'fld' => $this->_sStatusField];
  206. if(isset($aRow[$this->_sStatusField]) && !in_array($aRow[$this->_sStatusField], $this->_aStatusValues)) {
  207. $sStatusKey = '_sys_status_' . $aRow[$this->_sStatusField];
  208. if(!empty($CNF['T']['txt_status_' . $aRow[$this->_sStatusField]]))
  209. $sStatusKey = $CNF['T']['txt_status_' . $aRow[$this->_sStatusField]];
  210. return parent::_getCellDefault(_t($sStatusKey), $sKey, $aField, $aRow);
  211. }
  212. return parent::_getCellSwitcher ($mixedValue, $sKey, $aField, $aRow);
  213. }
  214. protected function _getCellHeaderReports ($sKey, $aField)
  215. {
  216. $s = parent::_getCellHeaderDefault($sKey, $aField);
  217. return $this->_bIsApi ? [
  218. 'name' => bx_process_output($aField['name']),
  219. 'title' => bx_process_output($aField['title']),
  220. 'width' => $aField['width']
  221. ] : preg_replace ('/<a(.*?)>(.*?)<\/a>/', '<a$1 title="' . bx_html_attribute(_t('_sys_txt_reports_title')) . '"><i class="sys-icon exclamation-triangle"></i></a>', $s);
  222. }
  223. protected function _getCellReports($mixedValue, $sKey, $aField, $aRow)
  224. {
  225. if ($mixedValue == 0){
  226. $mixedValue = '';
  227. }
  228. else{
  229. $CNF = &$this->_oModule->_oConfig->CNF;
  230. $oReports = isset($CNF['OBJECT_REPORTS']) ? BxDolReport::getObjectInstance($CNF['OBJECT_REPORTS'], $aRow[$CNF['FIELD_ID']]) : null;
  231. if ($oReports){
  232. $mixedValue = $oReports->getCounter().$oReports->getJsScript();
  233. }
  234. }
  235. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  236. }
  237. protected function _switcherState2Checked($mixedState)
  238. {
  239. return 'active' == $mixedState ? true : false;
  240. }
  241. }
  242. /** @} */