From d7245f5ed0f96d098ad4a43fede51d77aa3170eb Mon Sep 17 00:00:00 2001 From: Anastasia Date: Thu, 14 Jun 2018 16:33:31 +0300 Subject: [PATCH] - create book - update image book --- backend/controllers/BookController.php | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++++-- backend/views/book/_form.php | 52 ++++++++++++++++++++++++++++++++++++++++++++++------ backend/views/book/create.php | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 148 insertions(+), 8 deletions(-) create mode 100644 backend/views/book/create.php diff --git a/backend/controllers/BookController.php b/backend/controllers/BookController.php index e67c239..e0902fa 100644 --- a/backend/controllers/BookController.php +++ b/backend/controllers/BookController.php @@ -9,11 +9,15 @@ namespace backend\controllers; use artbox\core\admin\actions\Index; + use common\models\Author; use common\models\Book; use yii\filters\AccessControl; use yii\filters\VerbFilter; + use yii\helpers\ArrayHelper; use yii\web\Controller; use yii\web\NotFoundHttpException; + use yii\web\Response; + use yii\web\UploadedFile; class BookController extends Controller { @@ -66,21 +70,45 @@ 'hasLanguage' => false, 'enableMassDelete' => true, 'modelPrimaryKey' => 'id', - 'create' => false, + 'create' => true, ], ]; } - + public function actionCreate() + { + /** + * @var Book $model; + */ + $model = new Book(); + $authors = Author::find()->all(); + $data = ArrayHelper::map($authors, 'id', 'secondname'); + if ($model->load(\Yii::$app->request->post()) && $model->save()) { + $model->saveImage(UploadedFile::getInstanceByName('file')); + return $this->redirect('index'); + } else { + return $this->render( + 'create', + [ + 'model' => $model, + 'data' => $data + ] + ); + } + } public function actionUpdate($id) { + /** + * @var Book $model; + */ $model = $this->findModel($id); $model->on_main = true; $model->save(false, [ 'on_main' ]); if ($model->load(\Yii::$app->request->post()) && $model->save()) { + $model->saveImage(UploadedFile::getInstanceByName('file')); return $this->redirect('index'); } else { return $this->render( @@ -124,4 +152,29 @@ } rmdir($dir); } + + public function actionDeleteImage() + { + \Yii::$app->response->format = Response::FORMAT_JSON; + $book = Book::find() + ->where([ 'id' => \Yii::$app->request->post('id') ]) + ->one(); + if ($book !== null){ + if (file_exists( + \Yii::getAlias('@storage/books/') . $book->id . '/' . \Yii::$app->request->post('image') + )) { + unlink( + \Yii::getAlias('@storage/books/') . $book->id . '/' . \Yii::$app->request->post( + 'image' + ) + ); + } + $book->image = null; + $book->save(); + return \GuzzleHttp\json_encode('success'); + } + + + return \GuzzleHttp\json_encode('error'); + } } \ No newline at end of file diff --git a/backend/views/book/_form.php b/backend/views/book/_form.php index bc79c0c..e163063 100644 --- a/backend/views/book/_form.php +++ b/backend/views/book/_form.php @@ -1,8 +1,8 @@ registerJs($js, View::POS_READY); + + if (!empty($model->image)) { + $files[] =''; + $config [] = ["url" => "delete-image", "key" => 0, 'extra' => ['image' => $model->image, 'id' => $model->id]]; + } else { + $files = []; + $config = []; + } ?>
- + ['enctype' => 'multipart/form-data']]); ?> field($model, 'title') ->textInput([ 'maxlength' => true ]) ?> -
- id.'/'.$model->image)->cropResize(262, 390)->renderImage()?> -
- author->name.' '.$model->author->secondname?> + 'file', + 'options' => [ + 'multiple' => false, + 'accept' => 'image/*', + ], + 'pluginOptions' => [ + 'maxFileCount' => 1, + 'showUpload' => false, + 'removeClass' => 'btn btn-danger', + 'removeIcon' => ' ', + 'initialPreview' => $files, + 'overwriteInitial' => true, + 'initialPreviewConfig' => $config + ], + ] + ) ?> + + isNewRecord){ + echo $model->author->name.' '.$model->author->secondname; + }else{ + echo $form->field($model, 'author_id') + ->widget( + Select2::classname(), + [ + 'data' => $data, + 'pluginOptions' => [ + 'allowClear' => true, + ], + ] + ); + } + ?> field($model, 'description') ->widget( diff --git a/backend/views/book/create.php b/backend/views/book/create.php new file mode 100644 index 0000000..bcc7dbc --- /dev/null +++ b/backend/views/book/create.php @@ -0,0 +1,47 @@ +title = Yii::t( + 'core', + 'Create {modelClass}: ', + [ + 'modelClass' => 'Book', + ] + ) . $model->title; + $this->params[ 'breadcrumbs' ][] = [ + 'label' => Yii::t('app', 'Books'), + 'url' => [ 'index' ], + ]; + $this->params[ 'breadcrumbs' ][] = [ + 'label' => $model->title, + 'url' => [ + 'view', + 'id' => $model->id, + ], + ]; + $this->params[ 'breadcrumbs' ][] = Yii::t('core', 'Create'); +?> + + $this->title, + 'options' => [ + 'class' => 'x_panel feedback-create', + ], + ] +) ?> + +render( + '_form', + [ + 'model' => $model, + 'data' => $data + ] +) ?> + + -- libgit2 0.21.4