forked from zxbodya/yii2-gallery-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GalleryManagerAction.php
176 lines (149 loc) · 4.66 KB
/
GalleryManagerAction.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
<?php
namespace zxbodya\yii2\galleryManager;
use Yii;
use yii\base\Action;
use yii\db\ActiveRecord;
use yii\helpers\Json;
use yii\web\HttpException;
use yii\web\UploadedFile;
/**
* Backend controller for GalleryManager widget.
* Provides following features:
* - Image removal
* - Image upload/Multiple upload
* - Arrange images in gallery
* - Changing name/description associated with image
*
* @author Bogdan Savluk <savluk.bogdan@gmail.com>
*/
class GalleryManagerAction extends Action
{
/**
* Glue used to implode composite primary keys
* @var string
*/
public $pkGlue = '_';
/**
* $types to be defined at Controller::actions()
* @var array Mapping between types and model class names
* @example 'post'=>'common\models\Post'
* @see GalleryManagerAction::run
*/
public $types = [];
protected $type;
protected $behaviorName;
protected $galleryId;
/** @var ActiveRecord */
protected $owner;
/** @var GalleryBehavior */
protected $behavior;
public function run($action)
{
$this->type = Yii::$app->request->get('type');
$this->behaviorName = Yii::$app->request->get('behaviorName');
$this->galleryId = Yii::$app->request->get('galleryId');
$pkNames = call_user_func([$this->types[$this->type], 'primaryKey']);
$pkValues = explode($this->pkGlue, $this->galleryId);
$pk = array_combine($pkNames, $pkValues);
$this->owner = call_user_func([$this->types[$this->type], 'findOne'], $pk);
$this->behavior = $this->owner->getBehavior($this->behaviorName);
switch ($action) {
case 'delete':
return $this->actionDelete(Yii::$app->request->post('id'));
break;
case 'ajaxUpload':
return $this->actionAjaxUpload();
break;
case 'changeData':
return $this->actionChangeData(Yii::$app->request->post('photo'));
break;
case 'order':
return $this->actionOrder(Yii::$app->request->post('order'));
break;
default:
throw new HttpException(400, 'Action do not exists');
break;
}
}
/**
* Removes image with ids specified in post request.
* On success returns 'OK'
*
* @param $ids
*
* @throws HttpException
* @return string
*/
protected function actionDelete($ids)
{
$this->behavior->deleteImages($ids);
return 'OK';
}
/**
* Method to handle file upload thought XHR2
* On success returns JSON object with image info.
*
* @return string
* @throws HttpException
*/
public function actionAjaxUpload()
{
$imageFile = UploadedFile::getInstanceByName('image');
$fileName = $imageFile->tempName;
$image = $this->behavior->addImage($fileName);
// not "application/json", because IE8 trying to save response as a file
Yii::$app->response->headers->set('Content-Type', 'text/html');
return Json::encode(
array(
'id' => $image->id,
'rank' => $image->rank,
'name' => (string)$image->name,
'description' => (string)$image->description,
'preview' => $image->getUrl('preview'),
)
);
}
/**
* Saves images order according to request.
*
* @param array $order new arrange of image ids, to be saved
*
* @return string
* @throws HttpException
*/
public function actionOrder($order)
{
if (count($order) == 0) {
throw new HttpException(400, 'No data, to save');
}
$res = $this->behavior->arrange($order);
return Json::encode($res);
}
/**
* Method to update images name/description via AJAX.
* On success returns JSON array of objects with new image info.
*
* @param $imagesData
*
* @throws HttpException
* @return string
*/
public function actionChangeData($imagesData)
{
if (count($imagesData) == 0) {
throw new HttpException(400, 'Nothing to save');
}
$images = $this->behavior->updateImagesData($imagesData);
$resp = array();
foreach ($images as $model) {
$resp[] = array(
'id' => $model->id,
'rank' => $model->rank,
'name' => (string)$model->name,
'description' => (string)$model->description,
'preview' => $model->getUrl('preview'),
);
}
return Json::encode($resp);
}
}