BxBaseModGeneralGridAdministration.php 12 KB

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