BxBaseAccountForms.php 12 KB

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