BxBaseModProfileGridAdministration.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 BaseProfile Base classes for profile modules
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxBaseModProfileGridAdministration extends BxBaseModGeneralGridAdministration
  12. {
  13. protected $_sFilter1Name;
  14. protected $_sFilter1Value;
  15. protected $_aFilter1Values;
  16. protected $_bSetAclWithDuration;
  17. public function __construct ($aOptions, $oTemplate = false)
  18. {
  19. parent::__construct ($aOptions, $oTemplate);
  20. $CNF = &$this->_oModule->_oConfig->CNF;
  21. $this->_aQueryReset = array($this->_aOptions['paginate_get_start'], $this->_aOptions['paginate_get_per_page']);
  22. $this->_sStatusField = !empty($CNF['FIELD_STATUS']) ? $CNF['FIELD_STATUS'] : 'status';
  23. $this->_aStatusValues = array('active', 'pending', 'suspended');
  24. $this->_sFilter1Name = 'filter1';
  25. $this->_aFilter1Values = array(
  26. 'active' => $CNF['T']['filter_item_active'],
  27. 'pending' => $CNF['T']['filter_item_pending'],
  28. 'suspended' => $CNF['T']['filter_item_suspended'],
  29. );
  30. $sFilter1 = bx_get($this->_sFilter1Name);
  31. if(!empty($sFilter1)) {
  32. $this->_sFilter1Value = bx_process_input($sFilter1);
  33. $this->_aQueryAppend[$this->_sFilter1Name] = $this->_sFilter1Value;
  34. }
  35. $this->_bSetAclWithDuration = true;
  36. }
  37. public function performActionSetAclLevel()
  38. {
  39. $oMenu = BxDolMenu::getObjectInstance('sys_set_acl_level');
  40. $aIds = bx_get('ids');
  41. if(!$aIds || !is_array($aIds) || !$oMenu)
  42. return echoJson([]);
  43. $aIdsResult = [];
  44. foreach($aIds as $iId) {
  45. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iId);
  46. if($this->_oModule->checkAllowedSetMembership($aContentInfo) !== CHECK_ACTION_RESULT_ALLOWED)
  47. continue;
  48. $aIdsResult[] = $this->_getProfileId($iId);
  49. }
  50. if(empty($aIdsResult))
  51. return echoJson([]);
  52. if(count($aIdsResult) == 1)
  53. $aIdsResult = $aIdsResult[0];
  54. $oMenu->setWithDuration($this->_bSetAclWithDuration);
  55. $oMenu->setHiddenByDefault(!$this->_bSetAclWithDuration);
  56. $sContent = $oMenu->getCode($aIdsResult);
  57. if(!$this->_bSetAclWithDuration)
  58. $sContent = BxTemplFunctions::getInstance()->transBox($this->_oModule->_oConfig->getName() . 'set_acl_level_popup', $this->_oTemplate->parseHtmlByName('set_acl_popup.html', [
  59. 'content' => $sContent
  60. ]));
  61. echoJson(['popup' => $sContent]);
  62. }
  63. public function performActionManageCf()
  64. {
  65. if(!$this->_oModule->_isModerator())
  66. return echoJson([]);
  67. $sModule = $this->_oModule->_oConfig->getName();
  68. $iContentId = $this->_getId();
  69. if($iContentId === false)
  70. return echoJson([]);
  71. $aProfileInfo = BxDolProfileQuery::getInstance()->getProfileByContentAndType($iContentId, $sModule, true);
  72. if(!$aProfileInfo || !is_array($aProfileInfo))
  73. return echoJson([]);
  74. $sAction = 'manage_cf';
  75. $sForm = 'sys_profile';
  76. $sFormDisplay = 'sys_profile_cf_manage';
  77. $oForm = BxDolForm::getObjectInstance($sForm, $sFormDisplay);
  78. if(!$oForm)
  79. return echoJson([]);
  80. $oForm->setAction(BX_DOL_URL_ROOT . 'grid.php', ['o' => $this->_sObject, 'a' => $sAction, 'id' => $iContentId]);
  81. $oForm->initChecker($aProfileInfo);
  82. if($oForm->isSubmittedAndValid()) {
  83. if(!$oForm->update($aProfileInfo['id']))
  84. $aResult = ['msg' => _t('_sys_txt_error_profile_update')];
  85. else
  86. $aResult = ['msg' => _t('_' . $sFormDisplay . '_successfully_submitted')];
  87. return echoJson($aResult);
  88. }
  89. $sPopupTitle = _t('_' . $sFormDisplay . '_popup');
  90. $sPopupContent = $this->_oTemplate->parseHtmlByName('content_filter_manage_popup.html', [
  91. 'form_id' => $oForm->getId(),
  92. 'form' => $oForm->getCode()
  93. ]);
  94. echoJson(['popup' => [
  95. 'html' => BxTemplFunctions::getInstance()->popupBox($sModule . '_manage_cf_popup', $sPopupTitle, $sPopupContent),
  96. 'options' => [
  97. 'closeOnOuterClick' => false,
  98. 'removeOnClose' => true
  99. ]
  100. ]]);
  101. }
  102. public function performActionDeleteWithContent()
  103. {
  104. $this->performActionDelete(array('with_content' => true));
  105. }
  106. protected function _getActionSetAclLevel($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  107. {
  108. if($this->_sManageType == BX_DOL_MANAGE_TOOLS_ADMINISTRATION && $this->_oModule->checkAllowedSetMembership($aRow) !== CHECK_ACTION_RESULT_ALLOWED)
  109. return '';
  110. return $this->_getActionDefault($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  111. }
  112. protected function _getActionManageCf($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  113. {
  114. return '';
  115. }
  116. protected function _getActionAuditProfile($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  117. {
  118. if (!getParam('sys_audit_enable') || getParam('sys_audit_acl_levels') == '')
  119. return;
  120. $iProfileId = bx_get_logged_profile_id();
  121. if (!BxDolAcl::getInstance()->isMemberLevelInSet(explode(',', getParam('sys_audit_acl_levels')), $iProfileId))
  122. return;
  123. $CNF = &$this->_oModule->_oConfig->CNF;
  124. $oProfile = $this->_getProfileObject($aRow[$CNF['FIELD_ID']]);
  125. $sUrl = BX_DOL_URL_ROOT . 'page/audit-administration?profile=' . $oProfile->id();
  126. $a['attr'] = array_merge($a['attr'], array(
  127. "onclick" => "window.open('" . $sUrl . "','_audit');"
  128. ));
  129. return $this->_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  130. }
  131. protected function _getActionSettings($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  132. {
  133. if (bx_is_api()) {
  134. return array_merge($a, ['name' => 'delete', 'type' => 'callback', 'action' => 'delete','on_callback' => 'hide_row', 'confirm' => 1, 'params' => '&id=' . $aRow[$this->_aOptions['field_id']] ]);
  135. }
  136. return parent::_getActionSettings($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  137. }
  138. protected function _getActionDeleteWithContent($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  139. {
  140. if($this->_sManageType != BX_DOL_MANAGE_TOOLS_ADMINISTRATION || !$this->_oModule->_isAdministrator())
  141. return '';
  142. return $this->_getActionDefault($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  143. }
  144. protected function _switcherChecked2State($isChecked)
  145. {
  146. return $isChecked ? 'active' : 'suspended';
  147. }
  148. protected function _enable ($mixedId, $isChecked)
  149. {
  150. $oProfile = $this->_getProfileObject($mixedId);
  151. if($oProfile instanceof BxDolProfileUndefined)
  152. return false;
  153. $iAction = BX_PROFILE_ACTION_MANUAL;
  154. $bSendEmailNotification = $this->_oModule->serviceActAsProfile();
  155. return $isChecked ? $oProfile->activate($iAction, 0, $bSendEmailNotification) : $oProfile->suspend($iAction, 0, $bSendEmailNotification);
  156. }
  157. protected function _getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
  158. {
  159. $aFilterParts = explode($this->_sParamsDivider, $sFilter);
  160. switch (substr_count($sFilter, $this->_sParamsDivider)) {
  161. case 1:
  162. list($this->_sFilter1Value, $sFilter) = $aFilterParts;
  163. break;
  164. case 2:
  165. list($this->_sFilter1Value, $this->_sFilter2Value, $sFilter) = $aFilterParts;
  166. break;
  167. }
  168. if(!empty($this->_sFilter1Value))
  169. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString(" AND `tp`.`status`=?", $this->_sFilter1Value);
  170. return $this->_getDataSqlInner($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
  171. }
  172. protected function _getDataSqlInner($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
  173. {
  174. return parent::_getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
  175. }
  176. //--- Layout methods ---//
  177. protected function _getFilterControls()
  178. {
  179. parent::_getFilterControls();
  180. return $this->_getFilterSelectOne($this->_sFilter1Name, $this->_sFilter1Value, $this->_aFilter1Values) . $this->_getSearchInput();
  181. }
  182. protected function _getFilterControlsAPI($aFilters = [])
  183. {
  184. $aFilters = [
  185. $this->_sFilter1Name => []
  186. ];
  187. return parent::_getFilterControlsAPI($aFilters);
  188. }
  189. protected function _getCellSwitcher ($mixedValue, $sKey, $aField, $aRow)
  190. {
  191. if (isset($aRow['profile_id']))
  192. if (!$this->_oModule->isAllowDeleteOrDisable(bx_get_logged_profile_id(), $aRow['profile_id']))
  193. return parent::_getCellDefault('', $sKey, $aField, $aRow);
  194. return parent::_getCellSwitcher ($mixedValue, $sKey, $aField, $aRow);
  195. }
  196. protected function _getCellFullname($mixedValue, $sKey, $aField, $aRow)
  197. {
  198. $oProfile = $this->_getProfileObject($aRow['id']);
  199. if (bx_is_api()){
  200. return ['type' => 'profile', 'data' => BxDolProfile::getData($oProfile->id())];
  201. }
  202. return parent::_getCellDefault($oProfile->getUnit(), $sKey, $aField, $aRow);
  203. }
  204. protected function _getCellLastOnline($mixedValue, $sKey, $aField, $aRow)
  205. {
  206. if (bx_is_api()){
  207. return ['type' => 'time', 'data' => $mixedValue];
  208. }
  209. return parent::_getCellDefault(bx_time_js($mixedValue), $sKey, $aField, $aRow);
  210. }
  211. protected function _getCellAddedTs($mixedValue, $sKey, $aField, $aRow)
  212. {
  213. return $this->_getCellLastOnline($mixedValue, $sKey, $aField, $aRow);
  214. }
  215. protected function _getCellAccount($mixedValue, $sKey, $aField, $aRow)
  216. {
  217. $sManageAccountUrl = $this->_getManageAccountUrl($aRow[$sKey]);
  218. if(!empty($sManageAccountUrl)) {
  219. $mixedValue = $this->_oTemplate->parseHtmlByName('account_link.html', array(
  220. 'href' => $sManageAccountUrl,
  221. 'title' => _t($this->_oModule->_oConfig->CNF['T']['grid_txt_account_manager']),
  222. 'content' => $mixedValue,
  223. 'class' => ''
  224. ));
  225. }
  226. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  227. }
  228. protected function _getProfileObject($iId)
  229. {
  230. return $this->_oModule->getProfileObject($iId);
  231. }
  232. protected function _getProfileId($iId)
  233. {
  234. return $this->_getProfileObject($iId)->id();
  235. }
  236. protected function _doDelete($iId, $aParams = array())
  237. {
  238. $oProfile = $this->_getProfileObject($iId);
  239. if(!$oProfile || $oProfile instanceof BxDolProfileUndefined)
  240. return false;
  241. if(!$this->_oModule->isAllowDeleteOrDisable(bx_get_logged_profile_id(), $oProfile->id()))
  242. return false;
  243. if($this->_oModule->checkMyself($iId))
  244. return false;
  245. return $oProfile->delete($oProfile->id(), isset($aParams['with_content']) && $aParams['with_content'] === true);
  246. }
  247. protected function _getId()
  248. {
  249. $aIds = bx_get('ids');
  250. if(!empty($aIds) && is_array($aIds))
  251. return (int)reset($aIds);
  252. $iId = bx_get('id');
  253. if($iId !== false)
  254. return (int)$iId;
  255. return false;
  256. }
  257. }
  258. /** @} */