|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace app\modules\image\models; |
|
4
|
|
|
|
|
5
|
|
|
use app\behaviors\ImageExist; |
|
6
|
|
|
use app\models\Object; |
|
7
|
|
|
use app\modules\image\widgets\ImageDropzone; |
|
8
|
|
|
use app\traits\FindById; |
|
9
|
|
|
use devgroup\TagDependencyHelper\ActiveRecordHelper; |
|
10
|
|
|
use Yii; |
|
11
|
|
|
use yii\base\Exception; |
|
12
|
|
|
use yii\caching\TagDependency; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* This is the model class for table "image". |
|
16
|
|
|
* @property integer $id |
|
17
|
|
|
* @property integer $object_id |
|
18
|
|
|
* @property integer $object_model_id |
|
19
|
|
|
* @property string $filename |
|
20
|
|
|
* @property string $image_title |
|
21
|
|
|
* @property string $image_alt |
|
22
|
|
|
* @property integer $sort_order |
|
23
|
|
|
*/ |
|
24
|
|
|
class Image extends \yii\db\ActiveRecord |
|
25
|
|
|
{ |
|
26
|
|
|
use FindById; |
|
27
|
|
|
|
|
28
|
|
|
private static $identityMap = []; |
|
29
|
|
|
|
|
30
|
|
|
public static function tableName() |
|
31
|
|
|
{ |
|
32
|
|
|
return '{{%image}}'; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
public function rules() |
|
36
|
|
|
{ |
|
37
|
|
|
return [ |
|
38
|
|
|
[['sort_order', 'filename'], 'required'], |
|
39
|
|
|
[['object_id', 'object_model_id', 'sort_order'], 'integer'], |
|
40
|
|
|
[['filename', 'image_title', 'image_alt'], 'string'], |
|
41
|
|
|
]; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
View Code Duplication |
public function attributeLabels() |
|
45
|
|
|
{ |
|
46
|
|
|
return [ |
|
47
|
|
|
'id' => Yii::t('app', 'ID'), |
|
48
|
|
|
'object_id' => Yii::t('app', 'Object ID'), |
|
49
|
|
|
'object_model_id' => Yii::t('app', 'Object Model ID'), |
|
50
|
|
|
'image_title' => Yii::t('app', 'Image Title'), |
|
51
|
|
|
'image_alt' => Yii::t('app', 'Image Alt'), |
|
52
|
|
|
'sort_order' => Yii::t('app', 'Sort Order'), |
|
53
|
|
|
'filename' => Yii::t('app', 'Filename') |
|
54
|
|
|
]; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
public function behaviors() |
|
58
|
|
|
{ |
|
59
|
|
|
return [ |
|
60
|
|
|
[ |
|
61
|
|
|
'class' => ImageExist::className(), |
|
62
|
|
|
'srcAttrName' => 'filename', |
|
63
|
|
|
], |
|
64
|
|
|
[ |
|
65
|
|
|
'class' => ActiveRecordHelper::className(), |
|
66
|
|
|
], |
|
67
|
|
|
]; |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Get images by objectId |
|
72
|
|
|
* @param integer $objectId |
|
73
|
|
|
* @return Image[] |
|
74
|
|
|
*/ |
|
75
|
|
|
public static function getForObjectId($objectId) |
|
76
|
|
|
{ |
|
77
|
|
|
$data = static::find()->where(['object_id' => $objectId])->all(); |
|
78
|
|
|
return $data; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Get images by objectId and objectModelId |
|
83
|
|
|
* @param integer $objectId |
|
84
|
|
|
* @param integer $objectModelId |
|
85
|
|
|
* @return Image[] |
|
86
|
|
|
*/ |
|
87
|
|
|
public static function getForModel($objectId, $objectModelId) |
|
88
|
|
|
{ |
|
89
|
|
|
if (!isset(self::$identityMap[$objectId][$objectModelId])) { |
|
90
|
|
|
$cacheName = 'Images:' . $objectId . ':' . $objectModelId; |
|
91
|
|
|
self::$identityMap[$objectId][$objectModelId] = Yii::$app->cache->get($cacheName); |
|
92
|
|
|
if (!is_array(self::$identityMap[$objectId][$objectModelId])) { |
|
93
|
|
|
if (!isset(self::$identityMap[$objectId])) { |
|
94
|
|
|
self::$identityMap[$objectId] = []; |
|
95
|
|
|
} |
|
96
|
|
|
self::$identityMap[$objectId][$objectModelId] = static::find()->where( |
|
97
|
|
|
[ |
|
98
|
|
|
'object_id' => $objectId, |
|
99
|
|
|
'object_model_id' => $objectModelId, |
|
100
|
|
|
] |
|
101
|
|
|
)->orderBy( |
|
102
|
|
|
[ |
|
103
|
|
|
'sort_order' => SORT_ASC, |
|
104
|
|
|
'id' => SORT_ASC |
|
105
|
|
|
] |
|
106
|
|
|
)->all(); |
|
107
|
|
|
$object = Object::findById($objectId); |
|
108
|
|
|
if (is_null($object)) { |
|
109
|
|
|
return self::$identityMap[$objectId][$objectModelId]; |
|
110
|
|
|
} |
|
111
|
|
|
Yii::$app->cache->set( |
|
112
|
|
|
$cacheName, |
|
113
|
|
|
self::$identityMap[$objectId][$objectModelId], |
|
114
|
|
|
86400, |
|
115
|
|
|
new TagDependency( |
|
116
|
|
|
[ |
|
117
|
|
|
'tags' => [ |
|
118
|
|
|
\devgroup\TagDependencyHelper\ActiveRecordHelper::getObjectTag( |
|
119
|
|
|
$object->object_class, |
|
120
|
|
|
$objectModelId |
|
121
|
|
|
), |
|
122
|
|
|
], |
|
123
|
|
|
] |
|
124
|
|
|
) |
|
125
|
|
|
); |
|
126
|
|
|
} |
|
127
|
|
|
} |
|
128
|
|
|
return self::$identityMap[$objectId][$objectModelId]; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Replaces images for specified model |
|
133
|
|
|
* $images array format: |
|
134
|
|
|
* [ |
|
135
|
|
|
* 0 => [ |
|
136
|
|
|
* 'filename' => 'something.png', |
|
137
|
|
|
* 'image_title' => 'title', |
|
138
|
|
|
* 'image_alt' => 'alt', |
|
139
|
|
|
* ], |
|
140
|
|
|
* 1 => [ |
|
141
|
|
|
* 'image_title' => 'title', |
|
142
|
|
|
* 'image_alt' => 'alt', |
|
143
|
|
|
* ], |
|
144
|
|
|
* ] |
|
145
|
|
|
* @param \yii\db\ActiveRecord $model |
|
146
|
|
|
* @param array $images array of data |
|
147
|
|
|
* @throws \Exception |
|
148
|
|
|
*/ |
|
149
|
|
|
public static function replaceForModel(\yii\db\ActiveRecord $model, array $images) |
|
150
|
|
|
{ |
|
151
|
|
|
$object = Object::getForClass($model->className()); |
|
152
|
|
|
if ($object) { |
|
153
|
|
|
$current_images = static::getForModel($object->id, $model->id); |
|
154
|
|
|
|
|
155
|
|
|
// first find existing images in input array |
|
156
|
|
|
foreach ($current_images as $current) { |
|
157
|
|
|
$found = false; |
|
158
|
|
|
foreach ($images as $key => $new) { |
|
159
|
|
|
if ($new['filename'] === $current->filename && !empty($new['filename'])) { |
|
160
|
|
|
$found = true; |
|
161
|
|
|
$current->setAttributes($new); |
|
162
|
|
|
$current->sort_order = $key; |
|
163
|
|
|
$current->save(); |
|
164
|
|
|
|
|
165
|
|
|
// delete processed image from input array |
|
166
|
|
|
unset($images[$key]); |
|
167
|
|
|
} |
|
168
|
|
|
} |
|
169
|
|
|
if (!$found) { |
|
170
|
|
|
$current->delete(); |
|
171
|
|
|
} |
|
172
|
|
|
} |
|
173
|
|
|
unset($current_images); |
|
174
|
|
|
|
|
175
|
|
|
// insert new images |
|
176
|
|
|
foreach ($images as $key => $new) { |
|
177
|
|
|
if (isset($new['filename'])) { |
|
178
|
|
|
if (!empty($new['filename'])) { |
|
179
|
|
|
$new['filename'] = urldecode(preg_replace("~[\\?#].*$~Usi", "", $new['filename'])); |
|
180
|
|
|
$image_model = new Image; |
|
181
|
|
|
$image_model->object_id = $object->id; |
|
182
|
|
|
$image_model->object_model_id = $model->id; |
|
183
|
|
|
$image_model->filename = basename($new['filename']); |
|
184
|
|
|
if (preg_match("#^https?://#Us", $new['filename'])) { |
|
185
|
|
|
$image_model->filename = basename( |
|
186
|
|
|
preg_replace( |
|
187
|
|
|
"#^https?://[^/]#Us", |
|
188
|
|
|
"", |
|
189
|
|
|
$new['filename'] |
|
190
|
|
|
) |
|
191
|
|
|
); |
|
192
|
|
|
try { |
|
193
|
|
|
$stream = fopen($new['filename'], 'r'); |
|
194
|
|
|
Yii::$app->getModule('image')->fsComponent->putStream($image_model->filename, $stream); |
|
195
|
|
|
} catch (\Exception $e) { |
|
196
|
|
|
// whoops :-( |
|
197
|
|
|
} |
|
198
|
|
|
} else { |
|
199
|
|
|
$image_model->filename = $new['filename']; |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
|
|
203
|
|
|
$image_model->image_title = isset($new['image_title']) ? $new['image_title'] : ''; |
|
204
|
|
|
$image_model->image_alt = isset($new['image_alt']) ? $new['image_alt'] : ''; |
|
205
|
|
|
$image_model->sort_order = $key; |
|
206
|
|
|
$image_model->save(); |
|
207
|
|
|
unset($image_model); |
|
208
|
|
|
} |
|
209
|
|
|
} |
|
210
|
|
|
} |
|
211
|
|
|
|
|
212
|
|
|
} |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
public function afterSave($insert, $changedAttributes) |
|
216
|
|
|
{ |
|
217
|
|
|
parent::afterSave($insert, $changedAttributes); |
|
218
|
|
|
$defaultSize = Yii::$app->getModule('image')->defaultThumbnailSize; |
|
219
|
|
|
$aSize = explode('x', $defaultSize); |
|
220
|
|
|
$size = ThumbnailSize::findOne(['width' => $aSize[0], 'height' => $aSize[1]]); |
|
221
|
|
|
if ($size !== null) { |
|
222
|
|
|
Thumbnail::getImageThumbnailBySize($this, $size); |
|
223
|
|
|
} |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* @param string $demand |
|
228
|
|
|
* @param bool|false $useWatermark |
|
229
|
|
|
* @return string |
|
230
|
|
|
* @throws Exception |
|
231
|
|
|
*/ |
|
232
|
|
|
public function getThumbnail($demand, $useWatermark = false) |
|
233
|
|
|
{ |
|
234
|
|
|
$size = ThumbnailSize::getByDemand($demand); |
|
235
|
|
|
$thumb = Thumbnail::getImageThumbnailBySize($this, $size); |
|
236
|
|
|
/** @var string $src */ |
|
237
|
|
|
$src = $thumb->file; |
|
238
|
|
|
if ($useWatermark === true) { |
|
239
|
|
|
try { |
|
240
|
|
|
$watermark = Watermark::findOne($size->default_watermark_id); |
|
241
|
|
|
if ($watermark !== null) { |
|
242
|
|
|
$water = ThumbnailWatermark::getThumbnailWatermark($thumb, $watermark); |
|
243
|
|
|
$src = $water->file; |
|
244
|
|
|
} else { |
|
245
|
|
|
throw new Exception(Yii::t('app', 'Set watermark id')); |
|
246
|
|
|
} |
|
247
|
|
|
} catch (\Exception $e) { |
|
248
|
|
|
if (YII_DEBUG) { |
|
249
|
|
|
throw $e; |
|
250
|
|
|
} |
|
251
|
|
|
} |
|
252
|
|
|
} |
|
253
|
|
|
return $src; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* @return mixed |
|
258
|
|
|
*/ |
|
259
|
|
|
public function getOriginalUrl() |
|
260
|
|
|
{ |
|
261
|
|
|
return $this->file; |
|
262
|
|
|
} |
|
263
|
|
|
|
|
264
|
|
View Code Duplication |
public function afterDelete() |
|
265
|
|
|
{ |
|
266
|
|
|
parent::afterDelete(); |
|
267
|
|
|
$sameImages = static::findAll(['filename' => $this->filename]); |
|
268
|
|
|
if (empty($sameImages) === true) { |
|
269
|
|
|
if (Yii::$app->getModule('image')->fsComponent->has($this->filename)) { |
|
270
|
|
|
Yii::$app->getModule('image')->fsComponent->delete($this->filename); |
|
271
|
|
|
} |
|
272
|
|
|
} |
|
273
|
|
|
$thumbnails = Thumbnail::findAll(['img_id' => $this->id]); |
|
274
|
|
|
foreach ($thumbnails as $thumbnail) { |
|
275
|
|
|
$thumbnail->delete(); |
|
276
|
|
|
} |
|
277
|
|
|
} |
|
278
|
|
|
} |
|
279
|
|
|
|