BxBaseAccountForms.php 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) BoonEx Pty Limited - http://www.boonex.com/
  4. * CC-BY License - http://creativecommons.org/licenses/by/3.0/
  5. *
  6. * @defgroup TridentCore Trident Core
  7. * @{
  8. */
  9. /**
  10. * System profile(account) forms functions
  11. * @see BxDolProfileForms
  12. */
  13. class BxBaseAccountForms extends BxDolProfileForms
  14. {
  15. protected $_iProfileId;
  16. public function __construct()
  17. {
  18. parent::__construct();
  19. $this->_iProfileId = bx_get_logged_profile_id();
  20. }
  21. public function createAccountForm ()
  22. {
  23. // check access
  24. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = BxDolAccount::isAllowedCreate (0)))
  25. return MsgBox($sMsg);
  26. // check and display form
  27. $oForm = BxDolForm::getObjectInstance('sys_account', 'sys_account_create');
  28. if (!$oForm)
  29. return MsgBox(_t('_sys_txt_error_occured'));
  30. $oForm->initChecker();
  31. if (!$oForm->isSubmittedAndValid()) {
  32. $sCode = $oForm->getCode();
  33. bx_alert('account', 'add_form', 0, 0, array(
  34. 'form_object' => &$oForm,
  35. 'form_code' => &$sCode
  36. ));
  37. return $sCode;
  38. }
  39. // insert data into database
  40. $aValsToAdd = array (
  41. 'email_confirmed' => 0,
  42. );
  43. $iAccountId = $oForm->insert ($aValsToAdd);
  44. if (!$iAccountId) {
  45. if (!$oForm->isValid())
  46. return $oForm->getCode();
  47. else
  48. return MsgBox(_t('_sys_txt_error_account_creation'));
  49. }
  50. // alert
  51. bx_alert('account', 'add', $iAccountId, 0);
  52. // if email_confirmation procedure is enabled - send email confirmation letter
  53. $oAccount = BxDolAccount::getInstance($iAccountId);
  54. if (getParam('sys_email_confirmation') && $oAccount && !$oAccount->isConfirmed())
  55. $oAccount->sendConfirmationEmail($iAccountId);
  56. // add account and content association
  57. $iProfileId = BxDolProfile::add(BX_PROFILE_ACTION_MANUAL, $iAccountId, $iAccountId, BX_PROFILE_STATUS_PENDING, 'system');
  58. $oProfile = BxDolProfile::getInstance($iProfileId);
  59. // approve profile if auto-approval is enabled and profile status is 'pending'
  60. $sStatus = $oProfile->getStatus();
  61. $isAutoApprove = $oForm->isSetPendingApproval() ? false : true;
  62. if ($sStatus == BX_PROFILE_STATUS_PENDING && $isAutoApprove)
  63. $oProfile->approve(BX_PROFILE_ACTION_AUTO);
  64. // perform action
  65. BxDolAccount::isAllowedCreate ($iProfileId, true);
  66. // alert
  67. bx_alert('account', 'added', $iAccountId);
  68. // login to the created account automatically
  69. bx_login($iAccountId);
  70. $this->_iProfileId = bx_get_logged_profile_id();
  71. // redirect
  72. $this->_redirectAndExit(getParam('sys_redirect_after_account_added'), true, array(
  73. 'account_id' => $iAccountId,
  74. 'profile_id' => $iProfileId,
  75. ));
  76. }
  77. public function editAccountEmailSettingsForm ($iAccountId)
  78. {
  79. return $this->_editAccountForm ($iAccountId, 'sys_account_settings_email');
  80. }
  81. public function editAccountPasswordSettingsForm ($iAccountId)
  82. {
  83. return $this->_editAccountForm ($iAccountId, 'sys_account_settings_pwd');
  84. }
  85. public function editAccountInfoForm ($iAccountId)
  86. {
  87. return $this->_editAccountForm ($iAccountId, 'sys_account_settings_info');
  88. }
  89. public function deleteAccountForm ($iAccountId)
  90. {
  91. $oAccount = BxDolAccount::getInstance($iAccountId);
  92. $aAccountInfo = $oAccount ? $oAccount->getInfo() : false;
  93. if (!$aAccountInfo)
  94. return MsgBox(_t('_sys_txt_error_account_is_not_defined'));
  95. // check access
  96. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = BxDolAccount::isAllowedDelete ($this->_iProfileId, $aAccountInfo)))
  97. return MsgBox($sMsg);
  98. // check and display form
  99. $oForm = BxDolForm::getObjectInstance('sys_account', 'sys_account_settings_del_account');
  100. if (!$oForm)
  101. return MsgBox(_t('_sys_txt_error_occured'));
  102. if (!$oForm->isSubmitted())
  103. unset($aAccountInfo['password']);
  104. $oForm->initChecker($aAccountInfo);
  105. if (!$oForm->isSubmittedAndValid())
  106. return $oForm->getCode();
  107. // delete account
  108. $oAccount = BxDolAccount::getInstance($aAccountInfo['id']);
  109. if (!$oAccount->delete())
  110. return MsgBox(_t('_sys_txt_error_account_delete'));
  111. // logout from deleted account
  112. if($iAccountId == getLoggedId())
  113. bx_logout();
  114. // redirect to homepage
  115. $this->_redirectAndExit('', false);
  116. }
  117. protected function _editAccountForm ($iAccountId, $sDisplayName)
  118. {
  119. $oAccount = BxDolAccount::getInstance($iAccountId);
  120. $aAccountInfo = $oAccount ? $oAccount->getInfo() : false;
  121. if (!$aAccountInfo)
  122. return MsgBox(_t('_sys_txt_error_account_is_not_defined'));
  123. // check access
  124. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = BxDolAccount::isAllowedEdit ($this->_iProfileId, $aAccountInfo)))
  125. return MsgBox($sMsg);
  126. // check and display form
  127. $oForm = BxDolForm::getObjectInstance('sys_account', $sDisplayName);
  128. if (!$oForm)
  129. return MsgBox(_t('_sys_txt_error_occured'));
  130. if (!$oForm->isSubmitted())
  131. unset($aAccountInfo['password']);
  132. $oForm->initChecker($aAccountInfo);
  133. if (!$oForm->isSubmittedAndValid())
  134. return $oForm->getCode();
  135. $aTrackTextFieldsChanges = array (); // track text fields changes, not-null(for example empty array) - means track, null - means don't track
  136. // update email and email setting in DB
  137. if (!$oForm->update ($aAccountInfo['id'], array(), $aTrackTextFieldsChanges)) {
  138. if (!$oForm->isValid())
  139. return $oForm->getCode();
  140. else
  141. return MsgBox(_t('_sys_txt_error_account_update'));
  142. }
  143. // check if email was changed
  144. if (!empty($aTrackTextFieldsChanges['changed_fields']) && in_array('email', $aTrackTextFieldsChanges['changed_fields']))
  145. $oAccount->updateEmailConfirmed(false); // mark email as unconfirmed
  146. // check if password was changed
  147. if ($oForm->getCleanValue('password')) {
  148. // relogin with new password
  149. bx_logout();
  150. bx_login($aAccountInfo['id']);
  151. }
  152. // check if other text info was changed - if auto-appproval is off
  153. $isAutoApprove = $oForm->isSetPendingApproval() ? false : true;
  154. if (!$isAutoApprove) {
  155. $oProfile = BxDolProfile::getInstanceAccountProfile($aAccountInfo['id']); // get profile associated with account, not current porfile
  156. $aProfileInfo = $oProfile->getInfo();
  157. unset($aTrackTextFieldsChanges['changed_fields']['email']); // email confirmation is automatic and separate, don't need to deactivate whole profile if email is changed
  158. if (BX_PROFILE_STATUS_ACTIVE == $aProfileInfo['status'] && !empty($aTrackTextFieldsChanges['changed_fields']))
  159. $oProfile->disapprove(BX_PROFILE_ACTION_AUTO); // change profile to 'pending' only if some text fields were changed and profile is active
  160. }
  161. // create an alert
  162. bx_alert('account', 'edited', $aAccountInfo['id'], $aAccountInfo['id'], array('display' => $sDisplayName));
  163. // display result message
  164. $sMsg = MsgBox(_t('_sys_txt_data_successfully_submitted'));
  165. return $sMsg . $oForm->getCode();
  166. }
  167. }
  168. /** @} */