public function run() { $user = $this->getController()->user; $form = new ProfileForm(); $formAttributes = $form->getAttributes(); unset($formAttributes['avatar'], $formAttributes['verifyCode']); $form->setAttributes($user->getAttributes(array_keys($formAttributes))); // Если у нас есть данные из POST - получаем их: if (($data = Yii::app()->getRequest()->getPost('ProfileForm')) !== null) { $transaction = Yii::app()->getDb()->beginTransaction(); try { $form->setAttributes($data); if ($form->validate()) { // Удаляем ненужные данные: unset($data['avatar']); // Заполняем модель данными: $user->setAttributes($data); // Если есть ошибки в профиле - перекинем их в форму if ($user->hasErrors()) { $form->addErrors($user->getErrors()); } // Если у нас есть дополнительные профили - проверим их foreach ((array) $this->getController()->module->profiles as $p) { $p->validate() || $form->addErrors($p->getErrors()); } // Если нет ошибок валидации: if ($form->hasErrors() === false) { Yii::log(Yii::t('UserModule.user', 'Profile for #{id}-{nick_name} was changed', ['{id}' => $user->id, '{nick_name}' => $user->email]), CLogger::LEVEL_INFO, UserModule::$logCategory); Yii::app()->getUser()->setFlash(yupe\widgets\YFlashMessages::SUCCESS_MESSAGE, Yii::t('UserModule.user', 'Your profile was changed successfully')); if (($uploadedFile = CUploadedFile::getInstance($form, 'avatar')) !== null) { $user->changeAvatar($uploadedFile); } elseif ($form->use_gravatar) { $user->removeOldAvatar(); } $user->save(); // И дополнительные профили, если они есть if (is_array($this->getController()->module->profiles)) { foreach ($this->getController()->module->profiles as $k => $p) { $p->save(false); } } Yii::app()->getUser()->setFlash(yupe\widgets\YFlashMessages::SUCCESS_MESSAGE, Yii::t('UserModule.user', 'Profile was updated')); $transaction->commit(); $this->getController()->redirect(['/user/profile/profile']); } else { Yii::log(Yii::t('UserModule.user', 'Error when save profile! #{id}', ['{id}' => $user->id]), CLogger::LEVEL_ERROR, UserModule::$logCategory); } } } catch (Exception $e) { $transaction->rollback(); Yii::app()->getUser()->setFlash(yupe\widgets\YFlashMessages::ERROR_MESSAGE, $e->getMessage()); } } $this->getController()->render('profile', ['model' => $form, 'module' => Yii::app()->getModule('user'), 'user' => $user]); }
public function run() { if (Yii::app()->user->isAuthenticated() === false) { $this->controller->redirect(Yii::app()->user->loginUrl); } if (($user = Yii::app()->user->getProfile()) === null) { Yii::app()->user->setFlash(yupe\widgets\YFlashMessages::ERROR_MESSAGE, Yii::t('UserModule.user', 'User not found.')); Yii::app()->user->logout(); $this->controller->redirect((array) '/user/account/login'); } $profile = $user->profile; $profile->scenario = 'edit-profile'; $form = new ProfileForm(); $formAttributes = $form->getAttributes(); $form->setAttributes($profile->getAttributes(array_keys($formAttributes))); $module = Yii::app()->getModule('user'); // Если у нас есть данные из POST - получаем их: if (($data = Yii::app()->getRequest()->getPost('ProfileForm')) !== null) { $transaction = Yii::app()->db->beginTransaction(); try { $form->setAttributes($data); if ($form->validate()) { // Заполняем модель данными: $profile->setAttributes($data); // Если есть ошибки в профиле - перекинем их в форму if ($profile->hasErrors()) { $form->addErrors($profile->getErrors()); } // Если нет ошибок валидации: if ($form->hasErrors() === false) { // Сохраняем профиль $profile->save(); Yii::app()->user->setFlash(yupe\widgets\YFlashMessages::SUCCESS_MESSAGE, Yii::t('UserModule.user', 'Your profile was changed successfully')); $transaction->commit(); $this->controller->redirect(array('/user/account/profile')); } } } catch (Exception $e) { $transaction->rollback(); Yii::app()->user->setFlash(yupe\widgets\YFlashMessages::ERROR_MESSAGE, $e->getMessage()); } } $this->controller->render('profile', array('model' => $form, 'user' => $user)); }
public function run() { if (($user = Yii::app()->user->getProfile()) === null) { Yii::app()->user->setFlash(yupe\widgets\YFlashMessages::ERROR_MESSAGE, Yii::t('UserModule.user', 'User not found.')); Yii::app()->user->logout(); $this->controller->redirect(array('/user/account/login')); } $form = new ProfileForm(); $formAttributes = $form->getAttributes(); unset($formAttributes['avatar'], $formAttributes['verifyCode']); $form->setAttributes($user->getAttributes(array_keys($formAttributes))); // Очищаем необходимые поля: $form->password = $form->cPassword = null; $module = Yii::app()->getModule('user'); // Если у нас есть данные из POST - получаем их: if (($data = Yii::app()->getRequest()->getPost('ProfileForm')) !== null) { $transaction = Yii::app()->db->beginTransaction(); try { $form->setAttributes($data); if ($form->validate()) { // Новый пароль? - ок, запоминаем: $newPass = isset($data['password']) ? $data['password'] : null; // Удаляем ненужные данные: unset($data['password'], $data['avatar']); // Запоминаем старую почту, $oldEmail = $user->email; // Заполняем модель данными: $user->setAttributes($data); // Новый пароль? - Генерируем хеш: if ($newPass) { $user->hash = Yii::app()->userManager->hasher->hashPassword($newPass); } // Если есть ошибки в профиле - перекинем их в форму if ($user->hasErrors()) { $form->addErrors($user->getErrors()); } // Если у нас есть дополнительные профили - проверим их foreach ((array) $this->controller->module->profiles as $p) { $p->validate() || $form->addErrors($p->getErrors()); } // Если нет ошибок валидации: if ($form->hasErrors() === false) { Yii::log(Yii::t('UserModule.user', 'Profile for #{id}-{nick_name} was changed', array('{id}' => $user->id, '{nick_name}' => $user->nick_name)), CLogger::LEVEL_INFO, UserModule::$logCategory); Yii::app()->user->setFlash(yupe\widgets\YFlashMessages::SUCCESS_MESSAGE, Yii::t('UserModule.user', 'Your profile was changed successfully')); if ($form->use_gravatar) { $user->avatar = null; } elseif (($uploadedFile = CUploadedFile::getInstance($form, 'avatar')) !== null) { $user->changeAvatar($uploadedFile); } // Сохраняем профиль $user->save(); // И дополнительные профили, если они есть if (is_array($this->controller->module->profiles)) { foreach ($this->controller->module->profiles as $k => $p) { $p->save(false); } } Yii::app()->user->setFlash(yupe\widgets\YFlashMessages::SUCCESS_MESSAGE, Yii::t('UserModule.user', 'Profile was updated')); $transaction->commit(); // Если включена верификация при смене почты: if ($module->emailAccountVerification && $oldEmail != $form->email) { if (Yii::app()->userManager->changeUserEmail($user, $form->email)) { Yii::app()->user->setFlash(yupe\widgets\YFlashMessages::SUCCESS_MESSAGE, Yii::t('UserModule.user', 'You need to confirm your e-mail. Please check the mail!')); } } $this->controller->redirect(array('/user/account/profile')); } else { Yii::log(Yii::t('UserModule.user', 'Error when save profile! #{id}', array('{id}' => $user->id)), CLogger::LEVEL_ERROR, UserModule::$logCategory); } } } catch (Exception $e) { $transaction->rollback(); Yii::app()->user->setFlash(yupe\widgets\YFlashMessages::ERROR_MESSAGE, $e->getMessage()); } } $this->controller->render('profile', array('model' => $form, 'module' => $module, 'user' => $user)); }