BxBaseAccountForms.php 7.5 KB

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