Ejemplo n.º 1
0
 public function actionProfile($update = false)
 {
     if (Yii::app()->user->isGuest) {
         $this->redirect(array('login'));
     }
     $model = new ProfileForm();
     $model->setAttributes($model->getIdentity()->getAttributes());
     $passwordForm = new PasswordForm();
     if (isset($_POST['ajax']) && $_POST['ajax'] === 'profile-form') {
         $models = array($model);
         if (isset($_POST['PasswordForm']) && trim($_POST['PasswordForm']['newPassword']) !== '') {
             $models[] = $passwordForm;
         }
         echo CActiveForm::validate($models);
         Yii::app()->end();
     }
     $flashes = array('success' => array(), 'error' => array());
     if (isset($_POST['PasswordForm']) && trim($_POST['PasswordForm']['newPassword']) !== '') {
         $passwordForm->setAttributes($_POST['PasswordForm']);
         if ($passwordForm->validate()) {
             if ($passwordForm->resetPassword($model->getIdentity())) {
                 $flashes['success'][] = Yii::t('UsrModule.usr', 'Changes have been saved successfully.');
             } else {
                 $flashes['error'][] = Yii::t('UsrModule.usr', 'Failed to change password.');
             }
         }
     }
     if (isset($_POST['ProfileForm']) && empty($flashes['error'])) {
         $model->setAttributes($_POST['ProfileForm']);
         if ($model->validate()) {
             $oldEmail = $model->getIdentity()->getEmail();
             if ($model->save()) {
                 if ($this->module->requireVerifiedEmail && $oldEmail != $model->email) {
                     if ($this->sendEmail($model, 'verify')) {
                         $flashes['success'][] = Yii::t('UsrModule.usr', 'An email containing further instructions has been sent to provided email address.');
                     } else {
                         $flashes['error'][] = Yii::t('UsrModule.usr', 'Failed to send an email.') . ' ' . Yii::t('UsrModule.usr', 'Try again or contact the site administrator.');
                     }
                 }
                 $flashes['success'][] = Yii::t('UsrModule.usr', 'Changes have been saved successfully.');
                 if (!empty($flashes['success'])) {
                     Yii::app()->user->setFlash('success', implode('<br/>', $flashes['success']));
                 }
                 if (!empty($flashes['error'])) {
                     Yii::app()->user->setFlash('error', implode('<br/>', $flashes['error']));
                 }
                 $this->redirect(array('profile'));
             } else {
                 $flashes['error'][] = Yii::t('UsrModule.usr', 'Failed to update profile.') . ' ' . Yii::t('UsrModule.usr', 'Try again or contact the site administrator.');
             }
         }
     }
     if (!empty($flashes['success'])) {
         Yii::app()->user->setFlash('success', implode('<br/>', $flashes['success']));
     }
     if (!empty($flashes['error'])) {
         Yii::app()->user->setFlash('error', implode('<br/>', $flashes['error']));
     }
     if ($update) {
         $this->render('updateProfile', array('model' => $model, 'passwordForm' => $passwordForm));
     } else {
         $this->render('viewProfile', array('model' => $model));
     }
 }