BxBaseAccountForms.php 7.7 KB

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