BxBaseAccountForms.php 7.7 KB

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