BxBaseAccountForms.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  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. 'name' => $oAccount->getDisplayName(),
  90. )));
  91. $a = BxDolService::call($sProfileModule, 'entity_add', array($iProfileId, $aProfileInfo));
  92. // in case of successful profile add redirect to the page after profile creation
  93. if (0 == $a['code']) {
  94. if($bRelocateCustom)
  95. $this->_redirectAndExit($sRelocateCustom, false);
  96. BxDolService::call($sProfileModule, 'redirect_after_add', array($a['content']));
  97. return;
  98. }
  99. // if creation failed, redirect to create profile form
  100. }
  101. $sRelocate = !empty($sProfileModule) ? BxDolService::call($sProfileModule, 'profile_create_url', array(false)) : '';
  102. if(empty($sRelocate))
  103. $sRelocate = $bRelocateCustom ? $sRelocateCustom : getParam('sys_redirect_after_account_added');
  104. $this->_redirectAndExit($sRelocate, true, array(
  105. 'account_id' => $iAccountId,
  106. 'profile_id' => $iProfileId,
  107. ));
  108. }
  109. public function onAccountCreated ($iAccountId, $isSetPendingApproval, $iAction = BX_PROFILE_ACTION_MANUAL)
  110. {
  111. // alert
  112. bx_alert('account', 'add', $iAccountId, 0);
  113. // if email_confirmation procedure is enabled - send email confirmation letter
  114. $oAccount = BxDolAccount::getInstance($iAccountId);
  115. if (BxDolAccount::isNeedConfirmEmail() && $oAccount && !$oAccount->isConfirmedEmail())
  116. $oAccount->sendConfirmationEmail($iAccountId);
  117. // add account and content association
  118. $iProfileId = BxDolProfile::add(BX_PROFILE_ACTION_MANUAL, $iAccountId, $iAccountId, BX_PROFILE_STATUS_PENDING, 'system');
  119. $oProfile = BxDolProfile::getInstance($iProfileId);
  120. // approve profile if auto-approval is enabled and profile status is 'pending'
  121. $sStatus = $oProfile->getStatus();
  122. $isAutoApprove = !$isSetPendingApproval;
  123. if ($sStatus == BX_PROFILE_STATUS_PENDING && $isAutoApprove)
  124. $oProfile->approve(BX_PROFILE_ACTION_AUTO, $iProfileId, getParam('sys_account_activation_letter') == 'on');
  125. // alert
  126. bx_alert('account', 'added', $iAccountId);
  127. // login to the created account automatically
  128. bx_login($iAccountId);
  129. return $iProfileId;
  130. }
  131. public function editAccountEmailSettingsForm ($iAccountId)
  132. {
  133. return $this->_editAccountForm ($iAccountId, 'sys_account_settings_email');
  134. }
  135. public function editAccountPasswordSettingsForm ($iAccountId)
  136. {
  137. return $this->_editAccountForm ($iAccountId, 'sys_account_settings_pwd');
  138. }
  139. public function editAccountInfoForm ($iAccountId)
  140. {
  141. return $this->_editAccountForm ($iAccountId, 'sys_account_settings_info');
  142. }
  143. public function deleteAccountForm ($iAccountId)
  144. {
  145. $oAccount = BxDolAccount::getInstance($iAccountId);
  146. $aAccountInfo = $oAccount ? $oAccount->getInfo() : false;
  147. if (!$aAccountInfo)
  148. return MsgBox(_t('_sys_txt_error_account_is_not_defined'));
  149. // check access
  150. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = BxDolAccount::isAllowedDelete ($this->_iProfileId, $aAccountInfo)))
  151. return MsgBox($sMsg);
  152. // check and display form
  153. $oForm = $this->getObjectFormDelete();
  154. if(bx_get('content') !== false)
  155. $oForm->aInputs['delete_content']['value'] = (int)bx_get('content');
  156. if (!$oForm)
  157. return MsgBox(_t('_sys_txt_error_occured'));
  158. if (!$oForm->isSubmitted())
  159. unset($aAccountInfo['password']);
  160. $oForm->initChecker($aAccountInfo);
  161. if (!$oForm->isSubmittedAndValid())
  162. return $oForm->getCode();
  163. // delete account
  164. $oAccount = BxDolAccount::getInstance($aAccountInfo['id']);
  165. if (!$oAccount->delete((int)$oForm->getCleanValue('delete_content') != 0))
  166. return MsgBox(_t('_sys_txt_error_account_delete'));
  167. // logout from deleted account
  168. if($iAccountId == getLoggedId())
  169. bx_logout();
  170. // redirect to homepage
  171. $this->_redirectAndExit('', false);
  172. }
  173. protected function _editAccountForm ($iAccountId, $sDisplayName)
  174. {
  175. $oAccount = BxDolAccount::getInstance($iAccountId);
  176. $aAccountInfo = $oAccount ? $oAccount->getInfo() : false;
  177. if (!$aAccountInfo)
  178. return MsgBox(_t('_sys_txt_error_account_is_not_defined'));
  179. // check access
  180. if (CHECK_ACTION_RESULT_ALLOWED !== ($sMsg = BxDolAccount::isAllowedEdit ($this->_iProfileId, $aAccountInfo)))
  181. return MsgBox($sMsg);
  182. // check and display form
  183. $oForm = BxDolForm::getObjectInstance('sys_account', $sDisplayName);
  184. if (!$oForm)
  185. return MsgBox(_t('_sys_txt_error_occured'));
  186. if (!$oForm->isSubmitted())
  187. unset($aAccountInfo['password']);
  188. $oForm->initChecker($aAccountInfo);
  189. if (!$oForm->isSubmittedAndValid())
  190. return $oForm->getCode();
  191. $aTrackTextFieldsChanges = array (); // track text fields changes, not-null(for example empty array) - means track, null - means don't track
  192. // update email and email setting in DB
  193. if (!$oForm->update ($aAccountInfo['id'], array(), $aTrackTextFieldsChanges)) {
  194. if (!$oForm->isValid())
  195. return $oForm->getCode();
  196. else
  197. return MsgBox(_t('_sys_txt_error_account_update'));
  198. }
  199. // check if email was changed
  200. if (!empty($aTrackTextFieldsChanges['changed_fields']) && in_array('email', $aTrackTextFieldsChanges['changed_fields']))
  201. $oAccount->updateEmailConfirmed(false); // mark email as unconfirmed
  202. // check if password was changed
  203. if ($oForm->getCleanValue('password')) {
  204. // relogin with new password
  205. bx_alert('account', 'edited', $aAccountInfo['id'], $aAccountInfo['id'], array('action' => 'change_password'));
  206. bx_logout();
  207. bx_login($aAccountInfo['id']);
  208. }
  209. // check if other text info was changed - if auto-appproval is off
  210. $isAutoApprove = $oForm->isSetPendingApproval() ? false : true;
  211. if (!$isAutoApprove) {
  212. $oProfile = BxDolProfile::getInstanceAccountProfile($aAccountInfo['id']); // get profile associated with account, not current porfile
  213. $aProfileInfo = $oProfile->getInfo();
  214. unset($aTrackTextFieldsChanges['changed_fields']['email']); // email confirmation is automatic and separate, don't need to deactivate whole profile if email is changed
  215. if (BX_PROFILE_STATUS_ACTIVE == $aProfileInfo['status'] && !empty($aTrackTextFieldsChanges['changed_fields']))
  216. $oProfile->disapprove(BX_PROFILE_ACTION_AUTO); // change profile to 'pending' only if some text fields were changed and profile is active
  217. }
  218. // create an alert
  219. bx_alert('account', 'edited', $aAccountInfo['id'], $aAccountInfo['id'], array('display' => $sDisplayName));
  220. // display result message
  221. $sMsg = MsgBox(_t('_sys_txt_data_successfully_submitted'));
  222. return $sMsg . $oForm->getCode();
  223. }
  224. }
  225. /** @} */