/** * @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; } }
/** * @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; } }
/** * @inheritdoc */ public function actions() { return ['index' => ['class' => Actions\IndexAction::className(), 'modelClass' => UserSearch::className(), 'roleArray' => function () { return User::roles(); }, 'statusArray' => function () { return User::statusLabels(); }], 'create' => ['class' => Actions\CreateAction::className(), 'modelClass' => User::className(), 'profileClass' => UserProfile::className(), 'scenario' => 'admin-create', 'roleArray' => function () { return User::roles(); }, 'statusArray' => function () { return User::statusLabels(); }], 'update' => ['class' => Actions\UpdateAction::className(), 'modelClass' => User::className(), 'scenario' => 'admin-update', 'profileRelation' => 'profile', 'roleArray' => function () { return User::roles(); }, 'statusArray' => function () { return User::statusLabels(); }], 'delete' => ['class' => Actions\DeleteAction::className(), 'modelClass' => User::className(), 'redirectRoute' => 'index'], 'batch-delete' => ['class' => Actions\BatchDeleteAction::className(), 'modelClass' => User::className(), 'redirectRoute' => 'index']]; }
/** * Creates data provider instance with search query applied. * * @param array $params Search params * * @return ActiveDataProvider DataProvider */ public function search($params) { $query = self::find()->joinWith(['profile', 'assignments']); $dataProvider = new ActiveDataProvider(['query' => $query->orderBy('created_at DESC')]); $dataProvider->sort->attributes['name'] = ['asc' => [UserProfile::tableName() . '.name' => SORT_ASC], 'desc' => [UserProfile::tableName() . '.name' => SORT_DESC]]; $dataProvider->sort->attributes['surname'] = ['asc' => [UserProfile::tableName() . '.surname' => SORT_ASC], 'desc' => [UserProfile::tableName() . '.surname' => SORT_DESC]]; if (!($this->load($params) && $this->validate())) { return $dataProvider; } $query->andFilterWhere(['id' => $this->id, 'status_id' => $this->status_id, 'FROM_UNIXTIME(created_at, "%d.%m.%Y")' => $this->created_at, 'FROM_UNIXTIME(updated_at, "%d.%m.%Y")' => $this->updated_at]); $query->andFilterWhere(['like', UserProfile::tableName() . '.name', $this->name]); $query->andFilterWhere(['like', UserProfile::tableName() . '.surname', $this->surname]); $query->andFilterWhere(['like', 'username', $this->username]); $query->andFilterWhere(['like', 'email', $this->email]); return $dataProvider; }
/** * @return \yii\db\ActiveQuery */ public function getProfile() { return $this->hasOne(UserProfile::className(), ['user_id' => 'id']); }
/** * @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(); }