diff --git a/common/models/Author.php b/common/models/Author.php index f26a5f4..78590c8 100644 --- a/common/models/Author.php +++ b/common/models/Author.php @@ -6,6 +6,7 @@ use yii\base\NotSupportedException; use yii\behaviors\TimestampBehavior; use yii\db\ActiveRecord; + use yii\helpers\FileHelper; use yii\web\IdentityInterface; /** @@ -82,6 +83,7 @@ 'password_hash', 'password_reset_token', 'auth_key', + 'avatar' ], 'string', 'max' => 255, @@ -276,4 +278,22 @@ $this->password_reset_token = null; } + public function saveImage($file){ + /** + * @var \yii\web\UploadedFile $file; + */ + if (!empty($file)){ + if (!file_exists(\Yii::getAlias('@storage/author/') . $this->id)) { + FileHelper::createDirectory(\Yii::getAlias('@storage/author/') . $this->id); + } + if ($file->saveAs(\Yii::getAlias('@storage/author/').$this->id.'/'.$this->id.'.'.$file->extension)){ + $this->avatar = $this->id.'.'.$file->extension; + return $this->save(); + } + return false; + + } + return true; + } + } diff --git a/frontend/controllers/AuthorController.php b/frontend/controllers/AuthorController.php index 00aaa1b..8db16cb 100644 --- a/frontend/controllers/AuthorController.php +++ b/frontend/controllers/AuthorController.php @@ -8,11 +8,50 @@ namespace frontend\controllers; + use common\models\Book; + use frontend\helpers\Url; + use frontend\models\ChangePassword; + use yii\data\ActiveDataProvider; use yii\web\Controller; - + use yii\web\Response; + use yii\web\UploadedFile; + class AuthorController extends Controller { - public function actionIndex(){ + public function actionIndex() + { + if (\Yii::$app->user->isGuest) { + return $this->redirect([ Url::home() ]); + } + /* @var \common\models\Author $user */ + $user = \Yii::$app->user->identity; + $dataProvider = new ActiveDataProvider( + [ + 'query' => Book::find() + ->where([ 'author_id' => $user->id ]), + 'pagination' => [ + 'pageSize' => 10, + ], + ] + ); + if ($user->load(\Yii::$app->request->post()) and $user->save()) { + $user->saveImage(UploadedFile::getInstanceByName('avatar')); + } + return $this->render( + 'index', + [ + 'user' => $user, + 'dataProvider' => $dataProvider, + ] + ); + } + + public function actionChangePassword(){ + \Yii::$app->response->format = Response::FORMAT_JSON; + $model = new ChangePassword(); + if ($model->load(\Yii::$app->request->post(), '')){ + return $model->changePassword(); + } } } \ No newline at end of file diff --git a/frontend/models/ChangePassword.php b/frontend/models/ChangePassword.php new file mode 100644 index 0000000..5de058c --- /dev/null +++ b/frontend/models/ChangePassword.php @@ -0,0 +1,37 @@ + 'password' + ] + ]; + } + + public function changePassword(){ + /* @var \common\models\Author $user*/ + $user = \Yii::$app->user->identity; + $user->setPassword($this->password); + return $user->save(); + } + } \ No newline at end of file diff --git a/frontend/views/author/_book.php b/frontend/views/author/_book.php new file mode 100644 index 0000000..aeb8d24 --- /dev/null +++ b/frontend/views/author/_book.php @@ -0,0 +1,13 @@ + +
diff --git a/frontend/views/author/index.php b/frontend/views/author/index.php new file mode 100644 index 0000000..0201a4a --- /dev/null +++ b/frontend/views/author/index.php @@ -0,0 +1,182 @@ +params[ 'breadcrumbs'][] = 'Особистий кабінет'; + $js = <<Пароль змінено
') + } + } + } + ); + + }) + }); function success(message) { document.querySelector('#success_form .txt-success').innerHTML = message; -- libgit2 0.21.4