예제 #1
0
 public function actionEdit()
 {
     $model = new UserModel();
     $model->scenario = 'edit';
     $user = Yii::$app->user->identity->getAttributes();
     $model->setAttributes($user, false);
     $serviceName = Yii::$app->getRequest()->getQueryParam('service');
     if (isset($serviceName)) {
         $eauth = Yii::$app->get('eauth')->getIdentity($serviceName);
         $eauth->setRedirectUrl(Yii::$app->getUrlManager()->createAbsoluteUrl('profile/edit'));
         $eauth->setCancelUrl(Yii::$app->getUrlManager()->createAbsoluteUrl('profile/edit'));
         try {
             if ($eauth->authenticate()) {
                 $attributes = User::getResponseEAuth($eauth);
                 // BIND SOCIAL ID
                 if (!empty($attributes['field']) && !empty($attributes['userId'])) {
                     $user = User::findOne($user['id']);
                     $user->load(['User' => [$attributes['field'] => $attributes['userId']]]);
                     $user->save();
                     Yii::$app->getSession()->setFlash('success', Yii::t('user', 'Profile successfully changed'));
                 }
                 // UPLOAD SOCIAL AVATAR
                 $imageUrl = $attributes['photo'];
                 if ($imageUrl) {
                     $filename = User2Image::copySocialImage($imageUrl);
                     $image = new User2Image();
                     $image->user_id = $user['id'];
                     $image->main_image = $filename;
                     $image->images = ['0' => $filename];
                     if (User2Image::hasImages($user['id'])) {
                         $db = Yii::$app->db;
                         $db->createCommand("UPDATE user_2_image SET is_main=0 WHERE user_id = {$user['id']}")->query();
                     }
                     $image->addImages();
                 }
                 return $eauth->redirect();
             } else {
                 return $eauth->redirect($eauth->getCancelUrl());
             }
         } catch (\nodge\eauth\ErrorException $e) {
             Yii::$app->getSession()->setFlash('error', 'EAuthException: ' . $e->getMessage());
             return $eauth->redirect($eauth->getCancelUrl());
         }
     }
     // IMAGES
     list($images, $mainImage) = User2Image::existsImages(Yii::$app->user->identity->id);
     if ($model->load(Yii::$app->request->post()) && $model->validate()) {
         $model->save();
         Yii::$app->getSession()->setFlash('success', Yii::t('user', 'Profile successfully changed'));
         return $this->redirect(['/profile']);
     } else {
         return $this->render('edit', ['model' => $model, 'images' => $images, 'mainImage' => $mainImage, 'user' => $user]);
     }
 }
예제 #2
0
파일: user.php 프로젝트: beatom/exampleYii2
<?php

use yii\helpers\Url;
use frontend\models\User2Image;
// USER ID
$userId = Yii::$app->user->id;
// AVATAR
$avatar = User2Image::getAvatar($userId);
?>

<section class="profile-box box block">

    <?php 
if ($avatar) {
    ?>
        <img src="<?php 
    echo $avatar;
    ?>
" alt="" />
    <?php 
} else {
    ?>
        <img src="/img/no-avatar.png" />
    <?php 
}
?>

    <div class="profile-box-options">
        <span><?php 
echo Yii::$app->user->identity->firstname;
?>
예제 #3
0
파일: User.php 프로젝트: beatom/exampleYii2
 public function save()
 {
     // USER
     $user = UserModel::findOne($this->id);
     $user->scenario = 'edit';
     $user->load(['User' => $this->attributes]);
     $user->save();
     $image = new User2Image();
     $image->user_id = $this->id;
     $image->main_image = $this->main_image;
     $image->images = $this->images;
     if (User2Image::hasImages($this->id)) {
         $image->updateImages();
     } else {
         $image->addImages();
     }
     return true;
 }