BxBaseAccountForms.php 14 KB

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