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 = [];
+ }
?>