BxAccntGridAdministration.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802
  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 Accounts Accounts
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxAccntGridAdministration extends BxBaseModProfileGridAdministration
  12. {
  13. protected $_sFilter2Name;
  14. protected $_sFilter2Value;
  15. protected $_aFilter2Values;
  16. public function __construct ($aOptions, $oTemplate = false)
  17. {
  18. $this->MODULE = 'bx_accounts';
  19. parent::__construct ($aOptions, $oTemplate);
  20. $this->_sPaginateType = 'load_more';
  21. $this->_bDeleteWithBgJobs = true;
  22. $CNF = &$this->_oModule->_oConfig->CNF;
  23. $this->_aFilter1Values = array_merge($this->_aFilter1Values, array(
  24. 'unconfirmed' => $CNF['T']['filter_item_unconfirmed'],
  25. 'locked' => $CNF['T']['filter_item_locked'],
  26. 'without_profile' => $CNF['T']['filter_item_without_profile'],
  27. ));
  28. $this->_sFilter2Name = 'filter2';
  29. $this->_aFilter2Values = array(
  30. 'operators' => $CNF['T']['filter_item_operators']
  31. );
  32. $sFilter2 = bx_get($this->_sFilter2Name);
  33. if(!empty($sFilter2)) {
  34. $this->_sFilter2Value = bx_process_input($sFilter2);
  35. $this->_aQueryAppend[$this->_sFilter2Name] = $this->_sFilter2Value;
  36. }
  37. }
  38. protected function _getDataSql($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage)
  39. {
  40. if(strpos($sFilter, $this->_sParamsDivider) !== false)
  41. list($this->_sFilter1Value, $this->_sFilter2Value, $sFilter) = explode($this->_sParamsDivider, $sFilter);
  42. $sSelectClause = $sJoinClause = '';
  43. if($this->_oModule->_oConfig->isLastActiveSorting()) {
  44. $sSelectClause = ', `ts`.`date` AS `last_active`';
  45. $sJoinClause = ' LEFT JOIN `sys_sessions` AS `ts` ON `ts`.`id` = (SELECT `id` FROM `sys_sessions` WHERE `user_id`=`ta`.`id` ORDER BY `date` DESC LIMIT 1)';
  46. }
  47. $this->_aOptions['source'] = bx_replace_markers($this->_aOptions['source'], [
  48. 'select' => $sSelectClause,
  49. 'join' => $sJoinClause
  50. ]);
  51. if(!empty($this->_sFilter1Value))
  52. switch($this->_sFilter1Value) {
  53. case 'unconfirmed':
  54. $sCnfnType = getParam('sys_account_confirmation_type');
  55. switch($sCnfnType) {
  56. case 'email':
  57. $this->_aOptions['source'] .= " AND `ta`.`email_confirmed`='0'";
  58. break;
  59. case 'phone':
  60. $this->_aOptions['source'] .= " AND `ta`.`phone_confirmed`='0'";
  61. break;
  62. case 'email_and_phone':
  63. $this->_aOptions['source'] .= " AND (`ta`.`email_confirmed`='0' OR `ta`.`phone_confirmed`='0')";
  64. break;
  65. case 'email_or_phone':
  66. $this->_aOptions['source'] .= " AND (`ta`.`email_confirmed`='0' AND `ta`.`phone_confirmed`='0')";
  67. break;
  68. }
  69. break;
  70. case 'locked':
  71. $this->_aOptions['source'] .= " AND `ta`.`locked` = 1";
  72. break;
  73. case 'without_profile':
  74. $this->_aOptions['source'] .= " AND `ta`.`id` NOT IN (SELECT `account_id` FROM `sys_profiles` WHERE `type` <> 'system')";
  75. break;
  76. default:
  77. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString(" AND `tp`.`status`=?", $this->_sFilter1Value);
  78. }
  79. if(!empty($this->_sFilter2Value))
  80. $this->_aOptions['source'] .= $this->_oModule->_oDb->prepareAsString(" AND `ta`.`role` & " . BX_DOL_ROLE_ADMIN ." = " . BX_DOL_ROLE_ADMIN);
  81. $this->_aOptions['source'] .= " AND `ta`.`email` <> '' ";
  82. return parent::_getDataSqlInner($sFilter, $sOrderField, $sOrderDir, $iStart, $iPerPage);
  83. }
  84. protected function _getFilterControls()
  85. {
  86. parent::_getFilterControls();
  87. return $this->_getFilterSelectOne($this->_sFilter1Name, $this->_sFilter1Value, $this->_aFilter1Values) . $this->_getFilterSelectOne($this->_sFilter2Name, $this->_sFilter2Value, $this->_aFilter2Values) . $this->_getSearchInput();
  88. }
  89. public function getCode($isDisplayHeader = true)
  90. {
  91. return $this->_oModule->_oTemplate->getJsCode('main', array(
  92. 'aHtmlIds' => $this->_oModule->_oConfig->getHtmlIds()
  93. )) . parent::getCode($isDisplayHeader);
  94. }
  95. public function performActionActivate()
  96. {
  97. $this->_performActionEnable(true);
  98. }
  99. public function performActionSuspend()
  100. {
  101. $this->_performActionEnable(false);
  102. }
  103. public function performActionEditEmail()
  104. {
  105. $CNF = &$this->_oModule->_oConfig->CNF;
  106. $aIds = bx_get('ids');
  107. if(!$aIds || !is_array($aIds)) {
  108. $iId = (int)bx_get('id');
  109. if(!$iId)
  110. return echoJson(array());
  111. $aIds = array($iId);
  112. }
  113. $iId = $aIds[0];
  114. $oAccount = BxDolAccount::getInstance($iId);
  115. if(!$oAccount)
  116. return echoJson(array());
  117. $aAccount = $oAccount->getInfo();
  118. $sAction = 'edit_email';
  119. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ACCOUNT'], $CNF['OBJECT_FORM_ACCOUNT_DISPLAY_SETTINGS_EMAIL']);
  120. $oForm->aFormAttrs['action'] = BX_DOL_URL_ROOT . 'grid.php?o=' . $this->_sObject . '&a=' . $sAction . '&id=' . $iId;
  121. $oForm->initChecker($aAccount);
  122. if($oForm->isSubmittedAndValid()) {
  123. $sEmail = $oForm->getCleanValue('email');
  124. if(strcmp($aAccount['email'], $sEmail) !== 0 && BxDolAccountQuery::getInstance()->getIdByEmail($sEmail))
  125. return echoJson(array('msg' => _t('_sys_form_account_input_email_uniq_error_loggedin')));
  126. if($oForm->update($aAccount['id'])) {
  127. bx_alert('account', 'edited', $aAccount['id'], BxDolAccount::getInstance()->id(), array('display' => $CNF['OBJECT_FORM_ACCOUNT_DISPLAY_SETTINGS_EMAIL']));
  128. $aRes = array('grid' => $this->getCode(false), 'blink' => $iId);
  129. $oAccount->doAudit($iId, '_sys_audit_action_account_email_changed', array('old_email' => $oAccount->getEmail(), 'new_email' => $sEmail));
  130. }
  131. else
  132. $aRes = array('msg' => _t('_sys_txt_error_account_update'));
  133. echoJson($aRes);
  134. }
  135. else {
  136. $sContent = BxTemplStudioFunctions::getInstance()->popupBox('bx-account-edit-email-popup', _t('_bx_accounts_form_display_account_settings_email_popup'), $this->_oModule->_oTemplate->parseHtmlByName('edit_email.html', array(
  137. 'form_id' => $oForm->id,
  138. 'form' => $oForm->getCode(true),
  139. 'object' => $this->_sObject,
  140. 'action' => $sAction
  141. )));
  142. echoJson(array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => false))));
  143. }
  144. }
  145. public function performActionSendMessage()
  146. {
  147. $sAction = 'send_message';
  148. $CNF = &$this->_oModule->_oConfig->CNF;
  149. $aIds = bx_get('ids');
  150. if(!$aIds || !is_array($aIds))
  151. return echoJson(array());
  152. $oForm = BxDolForm::getObjectInstance('bx_accounts_account', 'bx_accounts_send_test');
  153. if (!$oForm)
  154. return '';
  155. $oForm->aFormAttrs['action'] = BX_DOL_URL_ROOT . 'grid.php?' . bx_encode_url_params($_GET, array('_r'));
  156. $oForm->initChecker();
  157. if($oForm->isSubmittedAndValid()) {
  158. $aIdsAffected = array();
  159. foreach($aIds as $iId) {
  160. if(sendMail(BxDolAccount::getInstance()->getEmail($iId), $oForm->getCleanValue('message_subject'), $oForm->getCleanValue('message_text'), 0, [], BX_EMAIL_MASS, 'text', false, ['From' => "=?UTF-8?B?" . base64_encode(getParam('site_title')) . "?= <" . getParam('site_email_notify') . ">"], false)){
  161. $aIdsAffected[] = $iId;
  162. }
  163. }
  164. echoJson(count($aIdsAffected) > 0 ? array('grid' => $this->getCode(false), 'blink' => $aIdsAffected) : array('msg' => _t($CNF['T']['grid_action_err_perform'])));
  165. }
  166. else {
  167. $sContent = BxTemplStudioFunctions::getInstance()->popupBox('bx-account-send-message', _t('_bx_accounts_form_display_account_send_message'), $this->_oModule->_oTemplate->parseHtmlByName('manage_item.html', array(
  168. 'form_id' => $oForm->id,
  169. 'form' => $oForm->getCode(true),
  170. 'object' => $this->_sObject,
  171. 'action' => $sAction
  172. )));
  173. echoJson(array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => false))));
  174. }
  175. }
  176. public function performActionAdd()
  177. {
  178. $CNF = &$this->_oModule->_oConfig->CNF;
  179. $sAction = 'add';
  180. $oForm = BxDolForm::getObjectInstance($CNF['OBJECT_FORM_ACCOUNT'], $CNF['OBJECT_FORM_ACCOUNT_DISPLAY_CREATE']);
  181. $oForm->aFormAttrs['action'] = BX_DOL_URL_ROOT . 'grid.php?o=' . $this->_sObject . '&a=' . $sAction;
  182. $oForm->initChecker();
  183. if($oForm->isSubmittedAndValid()) {
  184. $iAccountId = $oForm->insert();
  185. if (!$iAccountId) {
  186. $aRes = array('msg' => _t('_sys_txt_error_account_insert'));
  187. }
  188. else{
  189. $aRes = array('grid' => $this->getCode(false), 'blink' => $iAccountId);
  190. $oBxTemplAccountForms = new BxTemplAccountForms();
  191. $iProfileId = $oBxTemplAccountForms->onAccountCreated($iAccountId, $oForm->isSetPendingApproval(), BX_PROFILE_ACTION_MANUAL, false);
  192. // perform action
  193. BxDolAccount::isAllowedCreate ($iProfileId, true);
  194. // check
  195. $aModulesProfile = bx_srv('system', 'get_modules_by_type', ['profile']);
  196. if(count($aModulesProfile) == 1)
  197. $sProfileModule = reset($aModulesProfile)['name'];
  198. else if(($sDefaultProfileType = getParam('sys_account_default_profile_type')) !== '')
  199. $sProfileModule = $sDefaultProfileType;
  200. if(!empty($sProfileModule) && getParam('sys_account_auto_profile_creation') == 'on') {
  201. $aProfileFields = bx_srv($sProfileModule, 'prepare_fields', [[
  202. 'author' => $iProfileId,
  203. 'name' => BxDolAccount::getInstance($iAccountId)->getDisplayName(),
  204. ]]);
  205. $a = bx_srv($sProfileModule, 'entity_add_forcedly', [$iProfileId, $aProfileFields]);
  206. if(isset($a['content']['profile_id']) && (int)$a['content']['profile_id'] > 0)
  207. BxDolAcl::getInstance()->setMembership((int)$a['content']['profile_id'], MEMBERSHIP_ID_STANDARD);
  208. echoJson($aRes);
  209. }
  210. }
  211. }
  212. else {
  213. $sContent = BxTemplStudioFunctions::getInstance()->popupBox('bx-account-edit-email-popup', _t('_bx_accounts_form_display_account_create_popup'), $this->_oModule->_oTemplate->parseHtmlByName('create.html', array(
  214. 'form_id' => $oForm->id,
  215. 'form' => $oForm->getCode(true),
  216. 'object' => $this->_sObject,
  217. 'action' => $sAction
  218. )));
  219. echoJson(array('popup' => array('html' => $sContent, 'options' => array('closeOnOuterClick' => false))));
  220. }
  221. }
  222. public function performActionExport()
  223. {
  224. $CNF = &$this->_oModule->_oConfig->CNF;
  225. $aFields = explode(',', getParam($CNF['PARAM_EXPORT_FIELDS']));
  226. if(empty($aFields) || !is_array($aFields))
  227. return echoJson(array());
  228. $aTitles = $aConverters = array();
  229. foreach($aFields as $sField) {
  230. $aTitles[$sField] = _t('_bx_accnt_txt_field_' . $sField);
  231. $sConverter = '_exportFieldConverter' . bx_gen_method_name($sField);
  232. if(method_exists($this, $sConverter))
  233. $aConverters[$sField] = $sConverter;
  234. }
  235. $sMethod = '_exportTo' . bx_gen_method_name(getParam($CNF['PARAM_EXPORT_TO']));
  236. if(!method_exists($this, $sMethod))
  237. return echoJson(array());
  238. $aIds = bx_get('ids');
  239. if(!$aIds || !is_array($aIds))
  240. $aIds = $this->_oModule->_oDb->getAccountIds();
  241. if(($aFile = $this->$sMethod($aIds, $aFields, $aTitles, $aConverters)) === false)
  242. return echoJson(array());
  243. list($sFilePath, $sFileUrl) = $aFile;
  244. $oStorage = BxDolStorage::getObjectInstance($CNF['OBJECT_STORAGE_FILES']);
  245. if($oStorage && ($iFileId = $oStorage->storeFileFromPath($sFilePath, true, bx_get_logged_profile_id())) !== false) {
  246. $sFileUrl = $oStorage->getFileUrlById($iFileId);
  247. @unlink($sFilePath);
  248. }
  249. return echoJson(array('redirect' => $sFileUrl));
  250. }
  251. public function performActionConfirm()
  252. {
  253. $CNF = &$this->_oModule->_oConfig->CNF;
  254. $aIds = bx_get('ids');
  255. if(!$aIds || !is_array($aIds))
  256. return echoJson(array());
  257. $oAccount = BxDolAccount::getInstance();
  258. $iAffected = 0;
  259. $aIdsAffected = array();
  260. foreach($aIds as $iId){
  261. if (BxDolAccount::isNeedConfirmEmail()) {
  262. if($oAccount->updateEmailConfirmed(true, true, $iId)) {
  263. $aIdsAffected[] = $iId;
  264. $iAffected++;
  265. }
  266. }
  267. if (BxDolAccount::isNeedConfirmPhone()){
  268. if($oAccount->updatePhoneConfirmed(true, $iId)) {
  269. $aIdsAffected[] = $iId;
  270. $iAffected++;
  271. }
  272. }
  273. }
  274. echoJson($iAffected ? array('grid' => $this->getCode(false), 'blink' => $aIdsAffected) : array('msg' => _t($CNF['T']['grid_action_err_perform'])));
  275. }
  276. public function performActionResendCemail()
  277. {
  278. $CNF = &$this->_oModule->_oConfig->CNF;
  279. $aIds = bx_get('ids');
  280. if(!$aIds || !is_array($aIds))
  281. return echoJson(array());
  282. $oAccount = BxDolAccount::getInstance();
  283. $iAffected = 0;
  284. $aIdsAffected = array();
  285. foreach($aIds as $iId)
  286. if($oAccount->sendConfirmationEmail($iId)) {
  287. $aIdsAffected[] = $iId;
  288. $iAffected++;
  289. }
  290. echoJson($iAffected ? array('grid' => $this->getCode(false), 'blink' => $aIdsAffected) : array('msg' => _t($CNF['T']['grid_action_err_perform'])));
  291. }
  292. public function performActionResetPassword()
  293. {
  294. $aIds = bx_get('ids');
  295. if(!$aIds || !is_array($aIds))
  296. return echoJson(array());
  297. $iId = $aIds[0];
  298. $oAccount = BxDolAccount::getInstance($iId);
  299. if(!$oAccount)
  300. return echoJson(array());
  301. $aRes = array();
  302. $sPwd = genRndPwd();
  303. if (BxDolAccount::getInstance()->updatePassword($sPwd, $iId)){
  304. $sPopupId = $this->_oModule->_oConfig->getHtmlIds('password_popup');
  305. $sPopupTitle = _t('_bx_accounts_form_display_account_settings_password_popup');
  306. $sPopupContent = $this->_oModule->_oTemplate->parseHtmlByName('reset_password.html', array(
  307. 'js_object' => $this->_oModule->_oConfig->getJsObject('main'),
  308. 'html_id_text' => $this->_oModule->_oConfig->getHtmlIds('password_text'),
  309. 'html_id_button' => $this->_oModule->_oConfig->getHtmlIds('password_button'),
  310. 'password' => $sPwd,
  311. ));
  312. $aRes = array('popup' => BxTemplStudioFunctions::getInstance()->popupBox($sPopupId, $sPopupTitle, $sPopupContent));
  313. }
  314. else
  315. $aRes = array('msg' => _t('_bx_accnt_grid_action_err_perform'));
  316. return echoJson($aRes);
  317. }
  318. public function performActionResendRemail()
  319. {
  320. $CNF = &$this->_oModule->_oConfig->CNF;
  321. $aIds = bx_get('ids');
  322. if(!$aIds || !is_array($aIds))
  323. return echoJson(array());
  324. $oAccount = BxDolAccount::getInstance();
  325. $iAffected = 0;
  326. $aIdsAffected = array();
  327. foreach($aIds as $iId)
  328. if($oAccount->sendResetPasswordEmail($iId)) {
  329. $aIdsAffected[] = $iId;
  330. $iAffected++;
  331. }
  332. echoJson($iAffected ? array('grid' => $this->getCode(false), 'blink' => $aIdsAffected) : array('msg' => _t($CNF['T']['grid_action_err_perform'])));
  333. }
  334. public function performActionUnlockAccount()
  335. {
  336. $aIds = bx_get('ids');
  337. if(!$aIds || !is_array($aIds)) {
  338. echoJson(array());
  339. return;
  340. }
  341. $oAccountQuery = BxDolAccountQuery::getInstance();
  342. foreach($aIds as $iId) {
  343. $oAccount = BxDolAccount::getInstance($iId);
  344. if(!$oAccount)
  345. continue;
  346. if ($oAccount->isLocked()){
  347. $oAccountQuery->unlockAccount($iId);
  348. }
  349. $oAccount->doAudit($iId, '_sys_audit_action_account_unlock');
  350. }
  351. return echoJson(array('grid' => $this->getCode(false), 'blink' => $aIds));
  352. }
  353. public function performActionSetOperatorRole()
  354. {
  355. $aIds = bx_get('ids');
  356. if(!$aIds || !is_array($aIds))
  357. return echoJson(array());
  358. $oUtils = BxDolStudioRolesUtils::getInstance();
  359. $aRoles = $oUtils->getRoles();
  360. if(empty($aRoles) || !is_array($aRoles))
  361. return echoJson(array('msg' => _t('_sys_txt_error_occured')));
  362. $iId = (int)array_shift($aIds);
  363. $sPopupName = str_replace('_', '-', $this->MODULE) . '-set-role-popup';
  364. $sPopupContent = $this->_oModule->_oTemplate->getPopupSetRole($aRoles, $iId, $oUtils->getRole($iId));
  365. return echoJson(array('popup' => array(
  366. 'html' => BxTemplFunctions::getInstance()->transBox($sPopupName, $sPopupContent),
  367. 'options' => array(
  368. 'closeOnOuterClick' => true,
  369. 'removeOnClose' => true
  370. )
  371. )));
  372. }
  373. public function performActionSetOperatorRoleSubmit()
  374. {
  375. $aIds = bx_get('ids');
  376. if(!$aIds || !is_array($aIds))
  377. return echoJson(array());
  378. $iId = (int)array_shift($aIds);
  379. if(!$iId)
  380. return echoJson(array('msg' => _t('_sys_txt_error_occured')));
  381. if(!BxDolStudioRolesUtils::getInstance()->setRole($iId, bx_process_input(bx_get('role'), BX_DATA_INT)))
  382. return echoJson(array('msg' => _t('_error occured')));
  383. echoJson(array('grid' => $this->getCode(false), 'blink' => $iId));
  384. }
  385. public function performActionMakeOperator()
  386. {
  387. $this->_performActionChangeRole(3);
  388. }
  389. public function performActionUnmakeOperator()
  390. {
  391. $this->_performActionChangeRole(1);
  392. }
  393. protected function _performActionChangeRole($iRole)
  394. {
  395. $CNF = &$this->_oModule->_oConfig->CNF;
  396. $aIds = bx_get('ids');
  397. if(!$aIds || !is_array($aIds))
  398. return echoJson(array());
  399. $oAccount = BxDolAccount::getInstance();
  400. $oUtils = BxDolStudioRolesUtils::getInstance();
  401. $iAffected = 0;
  402. $aIdsAffected = array();
  403. foreach($aIds as $iId) {
  404. if(!$this->_oModule->_oDb->updateAccount(array('role' => $iRole), array('id' => $iId)))
  405. continue;
  406. $oUtils->setRole($iId, $iRole == 3 ? BX_DOL_STUDIO_ROLE_OPERATOR : 0);
  407. $oAccount->doAudit($iId, '_sys_audit_action_account_change_role_to_' . $iRole);
  408. $aIdsAffected[] = $iId;
  409. $iAffected++;
  410. }
  411. echoJson($iAffected ? array('grid' => $this->getCode(false), 'blink' => $aIdsAffected) : array('msg' => _t($CNF['T']['grid_action_err_perform'])));
  412. }
  413. protected function _performActionEnable($isChecked)
  414. {
  415. $CNF = &$this->_oModule->_oConfig->CNF;
  416. $aIds = bx_get('ids');
  417. if(!$aIds || !is_array($aIds)) {
  418. echoJson(array());
  419. return;
  420. }
  421. $iAffected = 0;
  422. $aIdsAffected = array();
  423. foreach($aIds as $iId)
  424. if($this->_enable($iId, $isChecked)) {
  425. $aIdsAffected[] = $iId;
  426. $iAffected++;
  427. $oAccount = BxDolAccount::getInstance($iId);
  428. $oAccount->doAudit($iId, '_sys_audit_action_account_' . $isChecked ? 'activated' : 'suspended');
  429. }
  430. echoJson($iAffected ? array('grid' => $this->getCode(false), 'blink' => $aIdsAffected) : array('msg' => _t($CNF['T']['grid_action_err_perform'])));
  431. }
  432. protected function _enable($mixedId, $isChecked)
  433. {
  434. $oProfile = BxDolProfile::getInstanceAccountProfile($mixedId);
  435. if(!$oProfile)
  436. return false;
  437. $iAction = BX_PROFILE_ACTION_MANUAL;
  438. $sMethod = $isChecked ? 'activate' : 'suspend';
  439. if(!$oProfile->$sMethod($iAction, 0 ,false))
  440. return false;
  441. sendMailTemplate('t_ChangeStatusAccount' . ucfirst($sMethod), $oProfile->getAccountObject()->id(), $oProfile->id(), array(), BX_EMAIL_SYSTEM);
  442. $aProfiles = $oProfile->getAccountObject()->getProfiles();
  443. foreach($aProfiles as $aProfile){
  444. BxDolProfile::getInstance($aProfile['id'])->$sMethod($iAction, 0 ,false);
  445. }
  446. return true;
  447. }
  448. //--- Layout methods ---//
  449. protected function _getActionEditEmail($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  450. {
  451. return '';
  452. }
  453. protected function _getCellEmailConfirmed($mixedValue, $sKey, $aField, $aRow)
  454. {
  455. $mixedValue = (int)$mixedValue == 1 ? '_Yes' : '_No';
  456. return parent::_getCellDefault(_t($mixedValue), $sKey, $aField, $aRow);
  457. }
  458. protected function _getActionResetPassword($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  459. {
  460. return '';
  461. }
  462. protected function _getActionResendRemail($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  463. {
  464. if($sType == 'single')
  465. return '';
  466. return parent::_getActionDefault ($sType, $sKey, $a, $isSmall, $isDisabled, $aRow);
  467. }
  468. protected function _getActionUnlockAccount($sType, $sKey, $a, $isSmall = false, $isDisabled = false, $aRow = array())
  469. {
  470. return '';
  471. }
  472. protected function _getCellName($mixedValue, $sKey, $aField, $aRow)
  473. {
  474. $oAccount = BxDolAccount::getInstance($aRow['id']);
  475. if ($oAccount)
  476. $s = ($aRow['locked'] == 1 ? $this->_oTemplate->parseIcon("lock col-red1") . ' ' : '') . $oAccount->getDisplayName();
  477. return parent::_getCellDefault ($s, $sKey, $aField, $aRow);
  478. }
  479. protected function _getCellProfiles($mixedValue, $sKey, $aField, $aRow)
  480. {
  481. $s = $this->_oModule->_oTemplate->getProfilesByAccount($aRow);
  482. return parent::_getCellDefault ($s, $sKey, $aField, $aRow);
  483. }
  484. protected function _getCellLogged($mixedValue, $sKey, $aField, $aRow)
  485. {
  486. $mixedValue = !empty($mixedValue) ? bx_time_js($mixedValue) : _t('_sys_not_available');
  487. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  488. }
  489. protected function _getCellLastActive($mixedValue, $sKey, $aField, $aRow)
  490. {
  491. if(!isset($aRow['last_active']))
  492. $mixedValue = (new BxDolSessionQuery())->getLastActivityAccount($aRow['id']);
  493. $iTs = max((int)$mixedValue, (int)$aRow['active']);
  494. return parent::_getCellDefault($iTs ? bx_time_js($iTs) : _t('_sys_not_available'), $sKey, $aField, $aRow);
  495. }
  496. protected function _getCellAdded($mixedValue, $sKey, $aField, $aRow)
  497. {
  498. $mixedValue = !empty($mixedValue) ? bx_time_js($mixedValue) : _t('_sys_not_available');
  499. return parent::_getCellDefault($mixedValue, $sKey, $aField, $aRow);
  500. }
  501. protected function _getCellIsConfirmed($mixedValue, $sKey, $aField, $aRow)
  502. {
  503. $oAccount = BxDolAccount::getInstance($aRow['id']);
  504. $s = "";
  505. if ($oAccount)
  506. $s = $oAccount->getCurrentConfirmationStatusValue();
  507. return parent::_getCellDefault(_t('_bx_accnt_grid_confirmation_status_' . $s), $sKey, $aField, $aRow);
  508. }
  509. protected function _getOrderFields($bTranslatable = false)
  510. {
  511. $aResult = parent::_getOrderFields($bTranslatable);
  512. if(!$bTranslatable && $this->_oModule->_oConfig->isLastActiveSorting())
  513. $aResult[] = 'last_active';
  514. return $aResult;
  515. }
  516. protected function _isCheckboxDisabled($aRow)
  517. {
  518. return false;
  519. }
  520. protected function _getContentInfo($iId)
  521. {
  522. return BxDolAccountQuery::getInstance()->getInfoById($iId);
  523. }
  524. protected function _doDelete($iId, $aParams = [])
  525. {
  526. return ($oAccount = BxDolAccount::getInstance($iId)) !== false && $oAccount->delete(isset($aParams['with_content']) && $aParams['with_content'] === true, true);
  527. }
  528. protected function _addJsCss()
  529. {
  530. parent::_addJsCss();
  531. $this->_oTemplate->addJs(array(
  532. 'main.css'
  533. ));
  534. $this->_oTemplate->addJs(array(
  535. 'jquery.form.min.js',
  536. 'clipboard.min.js',
  537. 'main.js'
  538. ));
  539. }
  540. protected function _isVisibleGrid($a)
  541. {
  542. if(isAdmin())
  543. return true;
  544. return parent::_isVisibleGrid($a);
  545. }
  546. protected function _exportToCsv($aIds, $aFields, $aTitles, $aConverters)
  547. {
  548. $sFileDiv = ';';
  549. $sFileName = 'accounts-' . date('Y.m.d') . '.csv';
  550. $sFilePath = BX_DIRECTORY_PATH_TMP . $sFileName;
  551. $oFileHandler = fopen($sFilePath, 'w');
  552. if(!$oFileHandler)
  553. return false;
  554. fputcsv($oFileHandler, $aTitles, $sFileDiv);
  555. $aFieldsFlip = array_flip($aFields);
  556. $oAccountQuery = BxDolAccountQuery::getInstance();
  557. foreach($aIds as $iId) {
  558. $aAccount = $oAccountQuery->getInfoById($iId);
  559. if(empty($aAccount) || !is_array($aAccount))
  560. continue;
  561. $aConvert = array_intersect_key($aConverters, $aAccount);
  562. foreach($aConvert as $sField => $sMethod)
  563. $aAccount[$sField] = $this->$sMethod($aAccount[$sField]);
  564. fputcsv($oFileHandler, array_intersect_key($aAccount, $aFieldsFlip), $sFileDiv);
  565. }
  566. fclose($oFileHandler);
  567. return array($sFilePath, BX_DOL_URL_ROOT . 'tmp/' . $sFileName);
  568. }
  569. protected function _exportToXml($aIds, $aFields, $aTitles, $aConverters)
  570. {
  571. $sFileName = 'accounts-' . date('Y.m.d') . '.xml';
  572. $sFilePath = BX_DIRECTORY_PATH_TMP . $sFileName;
  573. $oFileHandler = fopen($sFilePath, 'w');
  574. if(!$oFileHandler)
  575. return false;
  576. $aFieldsFlip = array_flip($aFields);
  577. $oAccountQuery = BxDolAccountQuery::getInstance();
  578. $oXML = xmlwriter_open_memory();
  579. xmlwriter_set_indent($oXML, 1);
  580. xmlwriter_set_indent_string($oXML, ' ');
  581. xmlwriter_start_document($oXML, '1.0', 'UTF-8');
  582. xmlwriter_start_element($oXML, 'accounts');
  583. foreach($aIds as $iId) {
  584. $aAccount = $oAccountQuery->getInfoById($iId);
  585. if(empty($aAccount) || !is_array($aAccount))
  586. continue;
  587. $aConvert = array_intersect_key($aConverters, $aAccount);
  588. foreach($aConvert as $sField => $sMethod)
  589. $aAccount[$sField] = $this->$sMethod($aAccount[$sField]);
  590. xmlwriter_start_element($oXML, 'account');
  591. $aAccountFields = array_intersect_key($aAccount, $aFieldsFlip);
  592. foreach($aAccountFields as $sAccountField => $sAccountValue) {
  593. xmlwriter_start_element($oXML, $sAccountField);
  594. xmlwriter_write_cdata($oXML, $sAccountValue);
  595. xmlwriter_end_element($oXML);
  596. }
  597. xmlwriter_end_element($oXML);
  598. }
  599. xmlwriter_end_document($oXML);
  600. fwrite($oFileHandler, xmlwriter_output_memory($oXML));
  601. fclose($oFileHandler);
  602. return array($sFilePath, BX_DOL_URL_ROOT . 'tmp/' . $sFileName);
  603. }
  604. protected function _exportFieldConverterAdded($mixedValue)
  605. {
  606. return $this->__exportFieldConverterDate($mixedValue);
  607. }
  608. protected function _exportFieldConverterChanged($mixedValue)
  609. {
  610. return $this->__exportFieldConverterDate($mixedValue);
  611. }
  612. protected function _exportFieldConverterLogged($mixedValue)
  613. {
  614. return $this->__exportFieldConverterDateTime($mixedValue);
  615. }
  616. protected function _exportFieldConverterLangId($mixedValue)
  617. {
  618. return $mixedValue ? BxDolLanguages::getInstance()->getLangName($mixedValue) : '';
  619. }
  620. protected function __exportFieldConverterDate($mixedValue)
  621. {
  622. return date('Y.m.d', $mixedValue);
  623. }
  624. protected function __exportFieldConverterDateTime($mixedValue)
  625. {
  626. return date('Y.m.d H:i', $mixedValue);
  627. }
  628. }
  629. /** @} */