BxBaseAccountForms.php 8.3 KB

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