8f340aa7
Anastasia
- main page
|
1
2
3
4
5
6
7
8
9
10
11
|
<?php
/**
* Created by PhpStorm.
* User: stes
* Date: 07.06.18
* Time: 17:11
*/
namespace backend\controllers;
use artbox\core\admin\actions\Index;
|
d7245f5e
Anastasia
- create book
|
12
|
use common\models\Author;
|
8f340aa7
Anastasia
- main page
|
13
14
15
|
use common\models\Book;
use yii\filters\AccessControl;
use yii\filters\VerbFilter;
|
d7245f5e
Anastasia
- create book
|
16
|
use yii\helpers\ArrayHelper;
|
8f340aa7
Anastasia
- main page
|
17
18
|
use yii\web\Controller;
use yii\web\NotFoundHttpException;
|
d7245f5e
Anastasia
- create book
|
19
20
|
use yii\web\Response;
use yii\web\UploadedFile;
|
8f340aa7
Anastasia
- main page
|
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
|
class BookController extends Controller
{
public function behaviors()
{
return [
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'delete' => [ 'POST' ],
],
],
'access' => [
'class' => AccessControl::className(),
'rules' => [
[
'allow' => true,
'roles' => [ '@' ],
],
],
],
];
}
public function actions()
{
return [
'index' => [
'class' => Index::className(),
'columns' => [
'title' => [
'type' => Index::ACTION_COL,
],
'author' => [
'type' => Index::RELATION_COL,
'columnConfig' => [
'relationField' => 'secondname',
],
],
'created_at' => [
'type' => Index::DATETIME_COL,
],
'status' => [
'type' => Index::STRING_COL,
],
'on_main' => [
'type' => Index::STATUS_COL,
],
],
'model' => Book::className(),
'hasLanguage' => false,
'enableMassDelete' => true,
'modelPrimaryKey' => 'id',
|
69077fa9
Anastasia
- bug fix
|
73
74
75
|
'defaultSort' => [
'created_at' => 'DESC'
],
|
d7245f5e
Anastasia
- create book
|
76
|
'create' => true,
|
8f340aa7
Anastasia
- main page
|
77
78
79
80
|
],
];
}
|
d7245f5e
Anastasia
- create book
|
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
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
]
);
}
}
|
8f340aa7
Anastasia
- main page
|
102
103
104
|
public function actionUpdate($id)
{
|
d7245f5e
Anastasia
- create book
|
105
106
107
|
/**
* @var Book $model;
*/
|
8f340aa7
Anastasia
- main page
|
108
109
|
$model = $this->findModel($id);
|
8f340aa7
Anastasia
- main page
|
110
|
if ($model->load(\Yii::$app->request->post()) && $model->save()) {
|
d7245f5e
Anastasia
- create book
|
111
|
$model->saveImage(UploadedFile::getInstanceByName('file'));
|
8f340aa7
Anastasia
- main page
|
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
|
return $this->redirect('index');
} else {
return $this->render(
'update',
[
'model' => $model,
]
);
}
}
public function findModel($id)
{
$model = Book::find()
->where([ 'id' => $id ])
->one();
if ($model !== null) {
return $model;
} else {
throw new NotFoundHttpException('The requested page does not exist.');
}
}
|
dd3ef621
Anastasia
- delete books
|
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
|
public function actionDelete($id)
{
if (file_exists(\Yii::getAlias('@storage/books/') . $id)) {
$this->removeDirectory(\Yii::getAlias('@storage/books/') . $id);
}
$this->findModel($id)
->delete();
return $this->redirect([ 'index' ]);
}
public static function removeDirectory($dir)
{
if ($objs = glob($dir . "/*")) {
foreach ($objs as $obj) {
is_dir($obj) ? self::removeDirectory($obj) : unlink($obj);
}
}
rmdir($dir);
}
|
d7245f5e
Anastasia
- create book
|
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
|
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');
}
|
8f340aa7
Anastasia
- main page
|
180
|
}
|