BxBaseAccountForms.php 7.8 KB

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