BxBaseAccountForms.php 7.9 KB

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