1
0

BxBaseModProfileGridAdministration.php 14 KB

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