BxBaseAccountForms.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup UnaBaseView UNA Base Representation Classes
  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. bx_alert('account', 'add_form_check', 0, 0, array(
  46. 'form_object' => &$oForm
  47. ));
  48. if (!$oForm->isSubmittedAndValid()) {
  49. $sCode = $oForm->getCode();
  50. bx_alert('account', 'add_form', 0, 0, array(
  51. 'form_object' => &$oForm,
  52. 'form_code' => &$sCode
  53. ));
  54. return $sCode;
  55. }
  56. // insert data into database
  57. $aValsToAdd = array (
  58. 'email_confirmed' => 0,
  59. );
  60. $iAccountId = $oForm->insert ($aValsToAdd);
  61. if (!$iAccountId) {
  62. if (!$oForm->isValid())
  63. return $oForm->getCode();
  64. else
  65. return MsgBox(_t('_sys_txt_error_account_creation'));
  66. }
  67. $iProfileId = $this->onAccountCreated($iAccountId, $oForm->isSetPendingApproval());
  68. $sRelocateCustom = $oForm->getCleanValue('relocate');
  69. $bRelocateCustom = !empty($sRelocateCustom);
  70. // perform action
  71. BxDolAccount::isAllowedCreate ($iProfileId, true);
  72. $this->_iProfileId = bx_get_logged_profile_id();
  73. // check and redirect
  74. $aModulesProfile = array();
  75. $aModules = BxDolModuleQuery::getInstance()->getModulesBy(array('type' => 'modules', 'active' => 1));
  76. foreach($aModules as $aModule) {
  77. $oModule = BxDolModule::getInstance($aModule['name']);
  78. if($oModule instanceof iBxDolProfileService && BxDolService::call($aModule['name'], 'act_as_profile') === true)
  79. $aModulesProfile[] = $aModule;
  80. }
  81. $sDefaultProfileType = getParam('sys_account_default_profile_type');
  82. if(count($aModulesProfile) == 1)
  83. $sProfileModule = $aModulesProfile[0]['name'];
  84. else if(!empty($sDefaultProfileType))
  85. $sProfileModule = $sDefaultProfileType;
  86. if (getParam('sys_account_auto_profile_creation') && !empty($sProfileModule)) {
  87. $oAccount = BxDolAccount::getInstance($iAccountId);
  88. $aProfileInfo = BxDolService::call($sProfileModule, 'prepare_fields', array(array(
  89. 'author' => $iProfileId,
  90. 'name' => $oAccount->getDisplayName(),
  91. )));
  92. $a = BxDolService::call($sProfileModule, 'entity_add', array($iProfileId, $aProfileInfo));
  93. // in case of successful profile add redirect to the page after profile creation
  94. if (0 == $a['code']) {
  95. if($bRelocateCustom)
  96. $this->_redirectAndExit($sRelocateCustom, false);
  97. BxDolService::call($sProfileModule, 'redirect_after_add', array($a['content']));
  98. return;
  99. }
  100. // if creation failed, redirect to create profile form
  101. }
  102. $sRelocate = !empty($sProfileModule) ? BxDolService::call($sProfileModule, 'profile_create_url', array(false)) : '';
  103. if(empty($sRelocate))
  104. $sRelocate = $bRelocateCustom ? $sRelocateCustom : getParam('sys_redirect_after_account_added');
  105. $this->_redirectAndExit($sRelocate, true, array(
  106. 'account_id' => $iAccountId,
  107. 'profile_id' => $iProfileId,
  108. ));
  109. }
  110. public function onAccountCreated ($iAccountId, $isSetPendingApproval, $iAction = BX_PROFILE_ACTION_MANUAL)
  111. {
  112. // alert
  113. bx_alert('account', 'add', $iAccountId, 0);
  114. // if email_confirmation procedure is enabled - send email confirmation letter
  115. $oAccount = BxDolAccount::getInstance($iAccountId);
  116. if (BxDolAccount::isNeedConfirmEmail() && $oAccount && !$oAccount->isConfirmedEmail())
  117. $oAccount->sendConfirmationEmail($iAccountId);
  118. // add account and content association
  119. $iProfileId = BxDolProfile::add(BX_PROFILE_ACTION_MANUAL, $iAccountId, $iAccountId, BX_PROFILE_STATUS_PENDING, 'system');
  120. $oProfile = BxDolProfile::getInstance($iProfileId);
  121. // approve profile if auto-approval is enabled and profile status is 'pending'
  122. $sStatus = $oProfile->getStatus();
  123. $isAutoApprove = !$isSetPendingApproval;
  124. if ($sStatus == BX_PROFILE_STATUS_PENDING && $isAutoApprove)
  125. $oProfile->approve(BX_PROFILE_ACTION_AUTO, $iProfileId, getParam('sys_account_activation_letter') == 'on');
  126. // alert
  127. bx_alert('account', 'added', $iAccountId);
  128. // login to the created account automatically
  129. bx_login($iAccountId);
  130. return $iProfileId;
  131. }
  132. public function editAccountEmailSettingsForm ($iAccountId)
  133. {
  134. return $this->_editAccountForm ($iAccountId, 'sys_account_settings_email');
  135. }
  136. public function editAccountPasswordSettingsForm ($iAccountId)
  137. {
  138. return $this->_editAccountForm ($iAccountId, 'sys_account_settings_pwd');
  139. }
  140. public function editAccountInfoForm ($iAccountId)
  141. {
  142. return $this->_editAccountForm ($iAccountId, 'sys_account_settings_info');
  143. }
  144. public function deleteAccountForm ($iAccountId)
  145. {
  146. $oAccount = BxDolAccount::getInstance($iAccountId);
  147. $aAccountInfo = $oAccount ? $oAccount->getInfo() : false;
  148. if (!$aAccountInfo)
  149. return MsgBox(_t('_sys_txt_error_account_is_not_defined'));
  150. // check access
  151. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = BxDolAccount::isAllowedDelete ($this->_iProfileId, $aAccountInfo)))
  152. return MsgBox($sMsg);
  153. // check and display form
  154. $oForm = $this->getObjectFormDelete();
  155. if(bx_get('content') !== false)
  156. $oForm->aInputs['delete_content']['value'] = (int)bx_get('content');
  157. if (!$oForm)
  158. return MsgBox(_t('_sys_txt_error_occured'));
  159. if (!$oForm->isSubmitted())
  160. unset($aAccountInfo['password']);
  161. $oForm->initChecker($aAccountInfo);
  162. if (!$oForm->isSubmittedAndValid())
  163. return $oForm->getCode();
  164. // delete account
  165. $oAccount = BxDolAccount::getInstance($aAccountInfo['id']);
  166. if (!$oAccount->delete(false === bx_get('delete_content') ? true : (int)$oForm->getCleanValue('delete_content') != 0))
  167. return MsgBox(_t('_sys_txt_error_account_delete'));
  168. // logout from deleted account
  169. if($iAccountId == getLoggedId())
  170. bx_logout();
  171. // redirect to homepage
  172. $this->_redirectAndExit('', false);
  173. }
  174. protected function _editAccountForm ($iAccountId, $sDisplayName)
  175. {
  176. $oAccount = BxDolAccount::getInstance($iAccountId);
  177. $aAccountInfo = $oAccount ? $oAccount->getInfo() : false;
  178. if (!$aAccountInfo)
  179. return MsgBox(_t('_sys_txt_error_account_is_not_defined'));
  180. // check access
  181. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = BxDolAccount::isAllowedEdit ($this->_iProfileId, $aAccountInfo)))
  182. return MsgBox($sMsg);
  183. // check and display form
  184. $oForm = BxDolForm::getObjectInstance('sys_account', $sDisplayName);
  185. if (!$oForm)
  186. return MsgBox(_t('_sys_txt_error_occured'));
  187. if (!$oForm->isSubmitted())
  188. unset($aAccountInfo['password']);
  189. $oForm->initChecker($aAccountInfo);
  190. if (!$oForm->isSubmittedAndValid())
  191. return $oForm->getCode();
  192. $aTrackTextFieldsChanges = array (); // track text fields changes, not-null(for example empty array) - means track, null - means don't track
  193. // update email and email setting in DB
  194. if (!$oForm->update ($aAccountInfo['id'], array(), $aTrackTextFieldsChanges)) {
  195. if (!$oForm->isValid())
  196. return $oForm->getCode();
  197. else
  198. return MsgBox(_t('_sys_txt_error_account_update'));
  199. }
  200. // check if email was changed
  201. if (!empty($aTrackTextFieldsChanges['changed_fields']) && in_array('email', $aTrackTextFieldsChanges['changed_fields'])){
  202. $oAccount = BxDolAccount::getInstance($iAccountId, true); // refresh account to clear cache
  203. $oAccount->updateEmailConfirmed(false); // mark email as unconfirmed
  204. }
  205. // check if password was changed
  206. if ($oForm->getCleanValue('password')) {
  207. // relogin with new password
  208. bx_alert('account', 'edited', $aAccountInfo['id'], $aAccountInfo['id'], array('action' => 'change_password'));
  209. bx_logout();
  210. bx_login($aAccountInfo['id']);
  211. }
  212. // check if other text info was changed - if auto-appproval is off
  213. $isAutoApprove = $oForm->isSetPendingApproval() ? false : true;
  214. if (!$isAutoApprove) {
  215. $oProfile = BxDolProfile::getInstanceAccountProfile($aAccountInfo['id']); // get profile associated with account, not current porfile
  216. $aProfileInfo = $oProfile->getInfo();
  217. unset($aTrackTextFieldsChanges['changed_fields']['email']); // email confirmation is automatic and separate, don't need to deactivate whole profile if email is changed
  218. if (BX_PROFILE_STATUS_ACTIVE == $aProfileInfo['status'] && !empty($aTrackTextFieldsChanges['changed_fields']))
  219. $oProfile->disapprove(BX_PROFILE_ACTION_AUTO); // change profile to 'pending' only if some text fields were changed and profile is active
  220. }
  221. // create an alert
  222. bx_alert('account', 'edited', $aAccountInfo['id'], $aAccountInfo['id'], array('display' => $sDisplayName));
  223. // display result message
  224. $sMsg = MsgBox(_t('_' . $sDisplayName . '_successfully_submitted'));
  225. return $sMsg . $oForm->getCode();
  226. }
  227. }
  228. /** @} */