forked from CostaRico/yii2-images
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ControllerTrait.php
93 lines (71 loc) · 2.62 KB
/
ControllerTrait.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
namespace bstuff\yii2images;
use Yii;
use yii\base\Exception;
use bstuff\yii2images\models\Image;
trait ControllerTrait
{
public function actionUploadImage($id) {
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$model = $this->findModel($id);
$name = Yii::$app->request->post('imageName', '0');
$uploadedFile = new \yii\web\UploadedFile;
$file = $uploadedFile->getInstanceByName($name);
if (!$file) $file = $uploadedFile->getInstanceByName(0);
if (!$file) $file = $uploadedFile->getInstanceByName(array_keys($_FILES)[0]);
if (!$file) throw new \yii\base\ErrorException('uploaded file not found');
$model->attachImage($file->tempName, [
'main' => Yii::$app->request->post('isMain', false),
'name' => ($name === '0') ? null : $name,
]);
return true;
}
public function actionDeleteImage() {
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
if ($imageId = Yii::$app->request->post('imageId')) {
$model = Image::findOne($imageId);
} else {
throw new Exception('Не нашли такую картинку');
}
return $model->delete();
}
public function actionMoveImage() {
Yii::$app->response->format = \yii\web\Response::FORMAT_JSON;
$direction = Yii::$app->request->post('direction');
if ($imageId = Yii::$app->request->post('imageId')) {
$image = Image::findOne($imageId);
} else {
throw new Exception('Не нашли такую картинку');
}
if (!$image->isMain) {
$image2 = Image::find()
->andWhere([($direction == 'up') ? '<' : '>', 'sortOrder', $image->sortOrder])
->andWhere(['=', 'modelTableName', $image->modelTableName])
->andWhere(['=', 'itemId', $image->itemId])
->andWhere('`isMain` != 1 OR `isMain` is NULL')
->orderBy(['sortOrder' => ($direction == 'up') ? SORT_DESC : SORT_ASC])
->one()
;
if ($image2) Image::swapImages($image2, $image);
}
return true;
}
public function actionMoveUp($id)
{
$image = RicoImage::findOne($id);
if ($image) {
if (!$image->isMain) {
$image2 = RicoImage::find()
->andWhere(['<', 'id', $image->id])
->andWhere(['=', 'modelName', $image->modelName])
->andWhere(['=', 'itemId', $image->itemId])
->andWhere('`isMain` != 1 OR `isMain` is NULL')
->orderBy(['id' => SORT_DESC])
->one()
;
if ($image2) Image::swapImages($image2, $image);
}
}
return $this->redirect(['post/view-gallery', 'id' => $image->itemId]);
}
}