BxBaseAccountForms.php 10 KB

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