BxBaseAccountForms.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 UnaBaseView UNA Base Representation Classes
  7. * @{
  8. */
  9. /**
  10. * System profile(account) forms functions
  11. * @see BxDolProfileForms
  12. */
  13. class BxBaseAccountForms extends BxDolProfileForms
  14. {
  15. protected $_iProfileId;
  16. static public $PROFILE_FIELDS = array();
  17. public function __construct()
  18. {
  19. parent::__construct();
  20. $this->_iProfileId = bx_get_logged_profile_id();
  21. }
  22. public function getObjectFormAdd ()
  23. {
  24. return BxDolForm::getObjectInstance('sys_account', 'sys_account_create');
  25. }
  26. public function getObjectFormEdit ()
  27. {
  28. return BxDolForm::getObjectInstance('sys_account', 'sys_account_settings_info');
  29. }
  30. public function getObjectFormDelete ()
  31. {
  32. return BxDolForm::getObjectInstance('sys_account', 'sys_account_settings_del_account');
  33. }
  34. public function createAccountForm ($aParams = array())
  35. {
  36. // check access
  37. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = BxDolAccount::isAllowedCreate (0)))
  38. return MsgBox($sMsg);
  39. // check and display form
  40. $oForm = $this->getObjectFormAdd ();
  41. if (!$oForm)
  42. return MsgBox(_t('_sys_txt_error_occured'));
  43. $oForm->aFormAttrs['action'] = !empty($aParams['action']) ? $aParams['action'] : BX_DOL_URL_ROOT . BxDolPermalinks::getInstance()->permalink('page.php?i=create-account');
  44. $oForm->initChecker(self::$PROFILE_FIELDS);
  45. if (!$oForm->isSubmittedAndValid()) {
  46. $sCode = $oForm->getCode();
  47. bx_alert('account', 'add_form', 0, 0, array(
  48. 'form_object' => &$oForm,
  49. 'form_code' => &$sCode
  50. ));
  51. return $sCode;
  52. }
  53. // insert data into database
  54. $aValsToAdd = array (
  55. 'email_confirmed' => 0,
  56. );
  57. $iAccountId = $oForm->insert ($aValsToAdd);
  58. if (!$iAccountId) {
  59. if (!$oForm->isValid())
  60. return $oForm->getCode();
  61. else
  62. return MsgBox(_t('_sys_txt_error_account_creation'));
  63. }
  64. $iProfileId = $this->onAccountCreated($iAccountId, $oForm->isSetPendingApproval());
  65. // perform action
  66. BxDolAccount::isAllowedCreate ($iProfileId, true);
  67. $this->_iProfileId = bx_get_logged_profile_id();
  68. // check and redirect
  69. $aModulesProfile = array();
  70. $aModules = BxDolModuleQuery::getInstance()->getModulesBy(array('type' => 'modules', 'active' => 1));
  71. foreach($aModules as $aModule) {
  72. $oModule = BxDolModule::getInstance($aModule['name']);
  73. if($oModule instanceof iBxDolProfileService && BxDolService::call($aModule['name'], 'act_as_profile') === true)
  74. $aModulesProfile[] = $aModule;
  75. }
  76. $sDefaultProfileType = getParam('sys_account_default_profile_type');
  77. if(count($aModulesProfile) == 1)
  78. $sProfileModule = $aModulesProfile[0]['name'];
  79. else if(!empty($sDefaultProfileType))
  80. $sProfileModule = $sDefaultProfileType;
  81. if (getParam('sys_account_auto_profile_creation') && !empty($sProfileModule)) {
  82. $oAccount = BxDolAccount::getInstance($iAccountId);
  83. $aProfileInfo = BxDolService::call($sProfileModule, 'prepare_fields', array(array(
  84. 'name' => $oAccount->getDisplayName(),
  85. )));
  86. $a = BxDolService::call($sProfileModule, 'entity_add', array($iProfileId, $aProfileInfo));
  87. if (0 != $a['code'])
  88. return MsgBox(_t($a['message']));
  89. BxDolService::call($sProfileModule, 'redirect_after_add', array($a['content']));
  90. }
  91. else {
  92. $sRedirectUrl = !empty($sProfileModule) ? BxDolService::call($sProfileModule, 'profile_create_url', array(false)) : '';
  93. $this->_redirectAndExit(!empty($sRedirectUrl) ? $sRedirectUrl : getParam('sys_redirect_after_account_added'), true, array(
  94. 'account_id' => $iAccountId,
  95. 'profile_id' => $iProfileId,
  96. ));
  97. }
  98. }
  99. public function onAccountCreated ($iAccountId, $isSetPendingApproval, $iAction = BX_PROFILE_ACTION_MANUAL)
  100. {
  101. // alert
  102. bx_alert('account', 'add', $iAccountId, 0);
  103. // if email_confirmation procedure is enabled - send email confirmation letter
  104. $oAccount = BxDolAccount::getInstance($iAccountId);
  105. if (BxDolAccount::isNeedConfirmEmail() && $oAccount && !$oAccount->isConfirmedEmail())
  106. $oAccount->sendConfirmationEmail($iAccountId);
  107. // add account and content association
  108. $iProfileId = BxDolProfile::add(BX_PROFILE_ACTION_MANUAL, $iAccountId, $iAccountId, BX_PROFILE_STATUS_PENDING, 'system');
  109. $oProfile = BxDolProfile::getInstance($iProfileId);
  110. // approve profile if auto-approval is enabled and profile status is 'pending'
  111. $sStatus = $oProfile->getStatus();
  112. $isAutoApprove = !$isSetPendingApproval;
  113. if ($sStatus == BX_PROFILE_STATUS_PENDING && $isAutoApprove)
  114. $oProfile->approve(BX_PROFILE_ACTION_AUTO, $iProfileId, getParam('sys_account_activation_letter') == 'on');
  115. // alert
  116. bx_alert('account', 'added', $iAccountId);
  117. // login to the created account automatically
  118. bx_login($iAccountId);
  119. return $iProfileId;
  120. }
  121. public function editAccountEmailSettingsForm ($iAccountId)
  122. {
  123. return $this->_editAccountForm ($iAccountId, 'sys_account_settings_email');
  124. }
  125. public function editAccountPasswordSettingsForm ($iAccountId)
  126. {
  127. return $this->_editAccountForm ($iAccountId, 'sys_account_settings_pwd');
  128. }
  129. public function editAccountInfoForm ($iAccountId)
  130. {
  131. return $this->_editAccountForm ($iAccountId, 'sys_account_settings_info');
  132. }
  133. public function deleteAccountForm ($iAccountId)
  134. {
  135. $oAccount = BxDolAccount::getInstance($iAccountId);
  136. $aAccountInfo = $oAccount ? $oAccount->getInfo() : false;
  137. if (!$aAccountInfo)
  138. return MsgBox(_t('_sys_txt_error_account_is_not_defined'));
  139. // check access
  140. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = BxDolAccount::isAllowedDelete ($this->_iProfileId, $aAccountInfo)))
  141. return MsgBox($sMsg);
  142. // check and display form
  143. $oForm = $this->getObjectFormDelete();
  144. if(bx_get('content') !== false)
  145. $oForm->aInputs['delete_content']['value'] = (int)bx_get('content');
  146. if (!$oForm)
  147. return MsgBox(_t('_sys_txt_error_occured'));
  148. if (!$oForm->isSubmitted())
  149. unset($aAccountInfo['password']);
  150. $oForm->initChecker($aAccountInfo);
  151. if (!$oForm->isSubmittedAndValid())
  152. return $oForm->getCode();
  153. // delete account
  154. $oAccount = BxDolAccount::getInstance($aAccountInfo['id']);
  155. if (!$oAccount->delete((int)$oForm->getCleanValue('delete_content') != 0))
  156. return MsgBox(_t('_sys_txt_error_account_delete'));
  157. // logout from deleted account
  158. if($iAccountId == getLoggedId())
  159. bx_logout();
  160. // redirect to homepage
  161. $this->_redirectAndExit('', false);
  162. }
  163. protected function _editAccountForm ($iAccountId, $sDisplayName)
  164. {
  165. $oAccount = BxDolAccount::getInstance($iAccountId);
  166. $aAccountInfo = $oAccount ? $oAccount->getInfo() : false;
  167. if (!$aAccountInfo)
  168. return MsgBox(_t('_sys_txt_error_account_is_not_defined'));
  169. // check access
  170. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = BxDolAccount::isAllowedEdit ($this->_iProfileId, $aAccountInfo)))
  171. return MsgBox($sMsg);
  172. // check and display form
  173. $oForm = BxDolForm::getObjectInstance('sys_account', $sDisplayName);
  174. if (!$oForm)
  175. return MsgBox(_t('_sys_txt_error_occured'));
  176. if (!$oForm->isSubmitted())
  177. unset($aAccountInfo['password']);
  178. $oForm->initChecker($aAccountInfo);
  179. if (!$oForm->isSubmittedAndValid())
  180. return $oForm->getCode();
  181. $aTrackTextFieldsChanges = array (); // track text fields changes, not-null(for example empty array) - means track, null - means don't track
  182. // update email and email setting in DB
  183. if (!$oForm->update ($aAccountInfo['id'], array(), $aTrackTextFieldsChanges)) {
  184. if (!$oForm->isValid())
  185. return $oForm->getCode();
  186. else
  187. return MsgBox(_t('_sys_txt_error_account_update'));
  188. }
  189. // check if email was changed
  190. if (!empty($aTrackTextFieldsChanges['changed_fields']) && in_array('email', $aTrackTextFieldsChanges['changed_fields']))
  191. $oAccount->updateEmailConfirmed(false); // mark email as unconfirmed
  192. // check if password was changed
  193. if ($oForm->getCleanValue('password')) {
  194. // relogin with new password
  195. bx_logout();
  196. bx_login($aAccountInfo['id']);
  197. }
  198. // check if other text info was changed - if auto-appproval is off
  199. $isAutoApprove = $oForm->isSetPendingApproval() ? false : true;
  200. if (!$isAutoApprove) {
  201. $oProfile = BxDolProfile::getInstanceAccountProfile($aAccountInfo['id']); // get profile associated with account, not current porfile
  202. $aProfileInfo = $oProfile->getInfo();
  203. unset($aTrackTextFieldsChanges['changed_fields']['email']); // email confirmation is automatic and separate, don't need to deactivate whole profile if email is changed
  204. if (BX_PROFILE_STATUS_ACTIVE == $aProfileInfo['status'] && !empty($aTrackTextFieldsChanges['changed_fields']))
  205. $oProfile->disapprove(BX_PROFILE_ACTION_AUTO); // change profile to 'pending' only if some text fields were changed and profile is active
  206. }
  207. // create an alert
  208. bx_alert('account', 'edited', $aAccountInfo['id'], $aAccountInfo['id'], array('display' => $sDisplayName));
  209. // display result message
  210. $sMsg = MsgBox(_t('_sys_txt_data_successfully_submitted'));
  211. return $sMsg . $oForm->getCode();
  212. }
  213. }
  214. /** @} */