Example #1
0
 /**
  * @return bool
  */
 public function upload()
 {
     if ($this->validate()) {
         $model = UserProfile::findOne(['user_id' => Yii::$app->user->identity->getId()]);
         if (!$model) {
             $model = new UserProfile();
             $model->user_id = Yii::$app->user->identity->getId();
         }
         $path = $this->getUserDirectory($model->user_id);
         $file = $this->imageFile;
         //удаляем старую аватарку
         if ($model->avatar) {
             $avatar = $path . '/' . $model->avatar;
             if (file_exists($avatar)) {
                 unlink($avatar);
             }
         }
         $name = time() . '.' . $file->extension;
         //$file->imageFile->baseName
         $tmp = '_' . $name;
         if ($file->saveAs($path . '/' . $tmp)) {
             $model->avatar = $name;
             Image::thumbnail($path . '/' . $tmp, 100, 100)->save($path . '/' . $model->avatar, ['quality' => 80]);
             if (file_exists($path . '/' . $tmp)) {
                 unlink($path . '/' . $tmp);
             }
             $model->save();
         }
         return true;
     } else {
         return false;
     }
 }
Example #2
0
 /**
  * @return bool
  */
 public function update()
 {
     if ($this->validate()) {
         $user = $this->_user;
         $user->email = $this->email;
         $profile = $this->_profile;
         if (!$profile) {
             $profile = new UserProfile();
             $profile->user_id = $user->id;
         }
         $profile->name = $this->name ? $this->name : null;
         $profile->surname = $this->surname ? $this->surname : null;
         if ($profile->save() && $user->save()) {
             return true;
         }
         return false;
     } else {
         return false;
     }
 }
 /**
  * @param $id
  * @param $name
  * @param $surname
  * @param int $sex
  * @return int
  * @throws \yii\db\Exception
  */
 private function insertProfile($id, $name = 'User', $surname = 'Name', $sex = 1)
 {
     $profile = new UserProfile(['user_id' => $id, 'name' => $name, 'surname' => $surname, 'sex' => $sex]);
     return $profile->save();
 }