Commit d7245f5ed0f96d098ad4a43fede51d77aa3170eb
1 parent
dd3ef621
- create book
- update image book
Showing
3 changed files
with
148 additions
and
8 deletions
Show diff stats
backend/controllers/BookController.php
@@ -9,11 +9,15 @@ | @@ -9,11 +9,15 @@ | ||
9 | namespace backend\controllers; | 9 | namespace backend\controllers; |
10 | 10 | ||
11 | use artbox\core\admin\actions\Index; | 11 | use artbox\core\admin\actions\Index; |
12 | + use common\models\Author; | ||
12 | use common\models\Book; | 13 | use common\models\Book; |
13 | use yii\filters\AccessControl; | 14 | use yii\filters\AccessControl; |
14 | use yii\filters\VerbFilter; | 15 | use yii\filters\VerbFilter; |
16 | + use yii\helpers\ArrayHelper; | ||
15 | use yii\web\Controller; | 17 | use yii\web\Controller; |
16 | use yii\web\NotFoundHttpException; | 18 | use yii\web\NotFoundHttpException; |
19 | + use yii\web\Response; | ||
20 | + use yii\web\UploadedFile; | ||
17 | 21 | ||
18 | class BookController extends Controller | 22 | class BookController extends Controller |
19 | { | 23 | { |
@@ -66,21 +70,45 @@ | @@ -66,21 +70,45 @@ | ||
66 | 'hasLanguage' => false, | 70 | 'hasLanguage' => false, |
67 | 'enableMassDelete' => true, | 71 | 'enableMassDelete' => true, |
68 | 'modelPrimaryKey' => 'id', | 72 | 'modelPrimaryKey' => 'id', |
69 | - 'create' => false, | 73 | + 'create' => true, |
70 | ], | 74 | ], |
71 | ]; | 75 | ]; |
72 | } | 76 | } |
73 | 77 | ||
74 | - | 78 | + public function actionCreate() |
79 | + { | ||
80 | + /** | ||
81 | + * @var Book $model; | ||
82 | + */ | ||
83 | + $model = new Book(); | ||
84 | + $authors = Author::find()->all(); | ||
85 | + $data = ArrayHelper::map($authors, 'id', 'secondname'); | ||
86 | + if ($model->load(\Yii::$app->request->post()) && $model->save()) { | ||
87 | + $model->saveImage(UploadedFile::getInstanceByName('file')); | ||
88 | + return $this->redirect('index'); | ||
89 | + } else { | ||
90 | + return $this->render( | ||
91 | + 'create', | ||
92 | + [ | ||
93 | + 'model' => $model, | ||
94 | + 'data' => $data | ||
95 | + ] | ||
96 | + ); | ||
97 | + } | ||
98 | + } | ||
75 | 99 | ||
76 | public function actionUpdate($id) | 100 | public function actionUpdate($id) |
77 | { | 101 | { |
102 | + /** | ||
103 | + * @var Book $model; | ||
104 | + */ | ||
78 | $model = $this->findModel($id); | 105 | $model = $this->findModel($id); |
79 | 106 | ||
80 | $model->on_main = true; | 107 | $model->on_main = true; |
81 | $model->save(false, [ 'on_main' ]); | 108 | $model->save(false, [ 'on_main' ]); |
82 | 109 | ||
83 | if ($model->load(\Yii::$app->request->post()) && $model->save()) { | 110 | if ($model->load(\Yii::$app->request->post()) && $model->save()) { |
111 | + $model->saveImage(UploadedFile::getInstanceByName('file')); | ||
84 | return $this->redirect('index'); | 112 | return $this->redirect('index'); |
85 | } else { | 113 | } else { |
86 | return $this->render( | 114 | return $this->render( |
@@ -124,4 +152,29 @@ | @@ -124,4 +152,29 @@ | ||
124 | } | 152 | } |
125 | rmdir($dir); | 153 | rmdir($dir); |
126 | } | 154 | } |
155 | + | ||
156 | + public function actionDeleteImage() | ||
157 | + { | ||
158 | + \Yii::$app->response->format = Response::FORMAT_JSON; | ||
159 | + $book = Book::find() | ||
160 | + ->where([ 'id' => \Yii::$app->request->post('id') ]) | ||
161 | + ->one(); | ||
162 | + if ($book !== null){ | ||
163 | + if (file_exists( | ||
164 | + \Yii::getAlias('@storage/books/') . $book->id . '/' . \Yii::$app->request->post('image') | ||
165 | + )) { | ||
166 | + unlink( | ||
167 | + \Yii::getAlias('@storage/books/') . $book->id . '/' . \Yii::$app->request->post( | ||
168 | + 'image' | ||
169 | + ) | ||
170 | + ); | ||
171 | + } | ||
172 | + $book->image = null; | ||
173 | + $book->save(); | ||
174 | + return \GuzzleHttp\json_encode('success'); | ||
175 | + } | ||
176 | + | ||
177 | + | ||
178 | + return \GuzzleHttp\json_encode('error'); | ||
179 | + } | ||
127 | } | 180 | } |
128 | \ No newline at end of file | 181 | \ No newline at end of file |
backend/views/book/_form.php
1 | <?php | 1 | <?php |
2 | 2 | ||
3 | use artbox\core\admin\assets\Switchery; | 3 | use artbox\core\admin\assets\Switchery; |
4 | - use artbox\core\helpers\ImageHelper; | ||
5 | use dosamigos\tinymce\TinyMce; | 4 | use dosamigos\tinymce\TinyMce; |
5 | + use kartik\file\FileInput; | ||
6 | use kartik\select2\Select2; | 6 | use kartik\select2\Select2; |
7 | use yii\helpers\Html; | 7 | use yii\helpers\Html; |
8 | use yii\helpers\Url; | 8 | use yii\helpers\Url; |
@@ -13,6 +13,7 @@ | @@ -13,6 +13,7 @@ | ||
13 | /* @var $this yii\web\View */ | 13 | /* @var $this yii\web\View */ |
14 | /* @var $model \common\models\Book */ | 14 | /* @var $model \common\models\Book */ |
15 | /* @var $form yii\widgets\ActiveForm */ | 15 | /* @var $form yii\widgets\ActiveForm */ |
16 | + /* @var array $data*/ | ||
16 | 17 | ||
17 | Switchery::register($this); | 18 | Switchery::register($this); |
18 | $js = <<< JS | 19 | $js = <<< JS |
@@ -27,18 +28,57 @@ JS; | @@ -27,18 +28,57 @@ JS; | ||
27 | 28 | ||
28 | 29 | ||
29 | $this->registerJs($js, View::POS_READY); | 30 | $this->registerJs($js, View::POS_READY); |
31 | + | ||
32 | + if (!empty($model->image)) { | ||
33 | + $files[] ='<img src="/storage/books/' . $model->id . '/' . $model->image . '" class="file-preview-image kv-preview-data rotate-35921 is-portrait-gt4" style="width:200px;" title="' . $model->image . '">'; | ||
34 | + $config [] = ["url" => "delete-image", "key" => 0, 'extra' => ['image' => $model->image, 'id' => $model->id]]; | ||
35 | + } else { | ||
36 | + $files = []; | ||
37 | + $config = []; | ||
38 | + } | ||
30 | ?> | 39 | ?> |
31 | 40 | ||
32 | <div class="feedback-form"> | 41 | <div class="feedback-form"> |
33 | 42 | ||
34 | - <?php $form = ActiveForm::begin(); ?> | 43 | + <?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]); ?> |
35 | 44 | ||
36 | <?= $form->field($model, 'title') | 45 | <?= $form->field($model, 'title') |
37 | ->textInput([ 'maxlength' => true ]) ?> | 46 | ->textInput([ 'maxlength' => true ]) ?> |
38 | - <div class="form-group"> | ||
39 | - <?=ImageHelper::set('@storage/books/'.$model->id.'/'.$model->image)->cropResize(262, 390)->renderImage()?> | ||
40 | - </div> | ||
41 | - <?=$model->author->name.' '.$model->author->secondname?> | 47 | + <?php echo FileInput::widget( |
48 | + [ | ||
49 | + 'name' => 'file', | ||
50 | + 'options' => [ | ||
51 | + 'multiple' => false, | ||
52 | + 'accept' => 'image/*', | ||
53 | + ], | ||
54 | + 'pluginOptions' => [ | ||
55 | + 'maxFileCount' => 1, | ||
56 | + 'showUpload' => false, | ||
57 | + 'removeClass' => 'btn btn-danger', | ||
58 | + 'removeIcon' => '<i class="glyphicon glyphicon-trash"></i> ', | ||
59 | + 'initialPreview' => $files, | ||
60 | + 'overwriteInitial' => true, | ||
61 | + 'initialPreviewConfig' => $config | ||
62 | + ], | ||
63 | + ] | ||
64 | + ) ?> | ||
65 | + | ||
66 | + <?php | ||
67 | + if (!$model->isNewRecord){ | ||
68 | + echo $model->author->name.' '.$model->author->secondname; | ||
69 | + }else{ | ||
70 | + echo $form->field($model, 'author_id') | ||
71 | + ->widget( | ||
72 | + Select2::classname(), | ||
73 | + [ | ||
74 | + 'data' => $data, | ||
75 | + 'pluginOptions' => [ | ||
76 | + 'allowClear' => true, | ||
77 | + ], | ||
78 | + ] | ||
79 | + ); | ||
80 | + } | ||
81 | + ?> | ||
42 | 82 | ||
43 | <?= $form->field($model, 'description') | 83 | <?= $form->field($model, 'description') |
44 | ->widget( | 84 | ->widget( |
1 | +<?php | ||
2 | + | ||
3 | + use yiister\gentelella\widgets\Panel; | ||
4 | + | ||
5 | + /* @var $this yii\web\View */ | ||
6 | + /* @var $model \common\models\Book */ | ||
7 | + /* @var array $data*/ | ||
8 | + | ||
9 | + $this->title = Yii::t( | ||
10 | + 'core', | ||
11 | + 'Create {modelClass}: ', | ||
12 | + [ | ||
13 | + 'modelClass' => 'Book', | ||
14 | + ] | ||
15 | + ) . $model->title; | ||
16 | + $this->params[ 'breadcrumbs' ][] = [ | ||
17 | + 'label' => Yii::t('app', 'Books'), | ||
18 | + 'url' => [ 'index' ], | ||
19 | + ]; | ||
20 | + $this->params[ 'breadcrumbs' ][] = [ | ||
21 | + 'label' => $model->title, | ||
22 | + 'url' => [ | ||
23 | + 'view', | ||
24 | + 'id' => $model->id, | ||
25 | + ], | ||
26 | + ]; | ||
27 | + $this->params[ 'breadcrumbs' ][] = Yii::t('core', 'Create'); | ||
28 | +?> | ||
29 | + | ||
30 | +<?php $panel = Panel::begin( | ||
31 | + [ | ||
32 | + 'header' => $this->title, | ||
33 | + 'options' => [ | ||
34 | + 'class' => 'x_panel feedback-create', | ||
35 | + ], | ||
36 | + ] | ||
37 | +) ?> | ||
38 | + | ||
39 | +<?= $this->render( | ||
40 | + '_form', | ||
41 | + [ | ||
42 | + 'model' => $model, | ||
43 | + 'data' => $data | ||
44 | + ] | ||
45 | +) ?> | ||
46 | + | ||
47 | +<?php $panel::end(); ?> |