1
0

BxBaseAccountForms.php 11 KB

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