BxBaseModGeneralGridAdministration.php 13 KB

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