BxBaseAccountForms.php 12 KB

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