-
Notifications
You must be signed in to change notification settings - Fork 11
/
Media.php
310 lines (268 loc) · 7.1 KB
/
Media.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
<?php
namespace humhub\modules\gallery\models;
use humhub\modules\content\components\ContentActiveRecord;
use humhub\modules\content\models\Content;
use humhub\modules\file\handler\DownloadFileHandler;
use humhub\modules\file\models\File;
use humhub\modules\gallery\helpers\Url;
use humhub\modules\gallery\Module;
use humhub\modules\gallery\permissions\WriteAccess;
use humhub\modules\search\interfaces\Searchable;
use humhub\modules\user\models\User;
use Yii;
use yii\web\UploadedFile;
/**
* This is the model class for table "gallery_media".
*
* @property int $id
* @property int $gallery_id
* @property string $description
* @property string $title
* @property int $sort_order
* @property File $baseFile
* @property CustomGallery $parentGallery
*
* @package humhub.modules.gallery.models
* @since 1.0
* @author Sebastian Stumpf
*/
class Media extends ContentActiveRecord implements Searchable
{
/**
* @var BaseGallery used for instantiation
*/
public $gallery;
/**
* @inheritdoc
*/
public $autoAddToWall = true;
/**
* @inheritdoc
*/
public $managePermission = WriteAccess::class;
/**
* @inheritdoc
*/
public $wallEntryClass = "humhub\modules\gallery\widgets\WallEntryMedia";
/**
* @var ?int used for edit form
*/
public $hidden = null;
/**
* @inheritdoc
*/
public static function tableName()
{
return 'gallery_media';
}
/**
* @inheritdoc
*/
public function init()
{
parent::init();
if ($this->gallery) {
$this->gallery_id = $this->gallery->id;
$this->content->visibility = $this->gallery->content->visibility;
$this->content->container = $this->gallery->content->container;
}
}
public function getWallUrl()
{
return Url::to(['/content/perma', 'id' => $this->content->id], true);
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['gallery_id', 'sort_order'], 'integer'],
['title', 'string', 'max' => 255],
['description', 'string', 'max' => 1000],
['hidden', 'boolean'],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'gallery_id' => 'Gallery ID',
'description' => 'Description',
'sort_order' => 'Sort Order',
];
}
public function getItemId()
{
return 'media_' . $this->id;
}
public function getFileName()
{
return $this->baseFile->file_name;
}
public function getSize()
{
return $this->baseFile->size;
}
/**
* @param bool $download
* @return string|null
*/
public function getFileUrl($download = false)
{
if ($this->baseFile === null) {
return null;
}
return DownloadFileHandler::getUrl($this->baseFile, $download);
}
public function getCreator()
{
return User::findOne(['id' => $this->baseFile->created_by]);
}
public function getEditor()
{
return User::findOne(['id' => $this->baseFile->updated_by]);
}
public function getBaseFile()
{
$query = $this->hasOne(File::class, ['object_id' => 'id']);
$query->andWhere(['file.object_model' => self::class]);
return $query;
}
public static function getFallbackPreviewImageUrl()
{
$path = Yii::$app->getModule('gallery')->getAssetsUrl();
$path .= '/file-picture-o.svg';
return $path;
}
public function getSquarePreviewImageUrl()
{
try {
$previewImage = SquarePreviewImage::getSquarePreviewImageUrlFromFile($this->baseFile);
} catch (\Exception $e) {
}
return empty($previewImage) ? static::getFallbackPreviewImageUrl() : $previewImage;
}
/**
* @inheritdoc
*/
public function getContentName()
{
return Yii::t('GalleryModule.base', "Media");
}
/**
* @inheritdoc
*/
public function getContentDescription()
{
return $this->getTitle();
}
public function getTitle()
{
return $this->title;
}
public function getParentGallery()
{
return $this->hasOne(CustomGallery::class, ['id' => 'gallery_id']);
}
public function getEditUrl($fromWall = false)
{
return Url::toEditMedia($this->content->container, $this, $fromWall);
}
public function getIcon()
{
return 'fa-picture-o';
}
/**
* Saves the given uploaded file.
*
* @param UploadedFile $file
* @return MediaUpload
* @internal param UploadedFile $cfile
*/
public function handleUpload(UploadedFile $file)
{
$mediaUpload = new MediaUpload();
$mediaUpload->setUploadedFile($file);
$valid = $mediaUpload->validate();
if ($valid) {
$this->title = $mediaUpload->file_name;
$valid = $this->validate();
if ($valid) {
if ($this->save()) {
$mediaUpload->object_model = self::class;
$mediaUpload->object_id = $this->id;
$mediaUpload->show_in_stream = false;
$mediaUpload->save();
}
}
}
return $mediaUpload;
}
/**
* @inheritdoc
*/
public function afterFind()
{
$this->hidden = $this->content->hidden;
parent::afterFind();
}
/**
* @inheritdoc
*/
public function beforeSave($insert)
{
if ($insert && $this->hidden === null) {
/* @var Module $module */
$module = Yii::$app->getModule('gallery');
$this->hidden = $module->getContentHiddenDefault($this->content->container);
}
return parent::beforeSave($insert);
}
/**
* @inheritdoc
*/
public function afterSave($insert, $changedAttributes)
{
$this->content->hidden = $this->hidden;
parent::afterSave($insert, $changedAttributes);
}
/**
* @inheritdoc
*/
public function afterStateChange(?int $newState, ?int $previousState): void
{
// Parent gallery should be restored after at least one child media file was restored
if ($previousState === Content::STATE_DELETED && $newState === Content::STATE_PUBLISHED) {
$parentGallery = $this->parentGallery;
if ($parentGallery instanceof CustomGallery) {
$parentGallery->content->setState(Content::STATE_PUBLISHED);
$parentGallery->content->save();
}
}
parent::afterStateChange($newState, $previousState);
}
/**
* @inheritdoc
*/
public function afterDelete()
{
if ($this->baseFile) {
$this->baseFile->delete();
}
parent::afterDelete();
}
/**
* @inheritdoc
*/
public function getSearchAttributes()
{
return [
'title' => $this->title,
'description' => $this->description,
];
}
}