BxBaseModGeneralGridAdministration.php 11 KB

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