-
Notifications
You must be signed in to change notification settings - Fork 554
/
HasMedias.php
455 lines (390 loc) · 14.3 KB
/
HasMedias.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
<?php
namespace A17\Twill\Models\Behaviors;
use A17\Twill\Models\Media;
use A17\Twill\Services\MediaLibrary\ImageService;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Arr;
trait HasMedias
{
protected $cropParamsKeys = [
'crop_x',
'crop_y',
'crop_w',
'crop_h',
];
public function getMediasParams(): array
{
return (isset($this->mediasParams) && is_array($this->mediasParams))
? $this->mediasParams
: config('twill.default_crops');
}
public static function bootHasMedias(): void
{
self::deleted(static function (Model $model) {
if (!method_exists($model, 'isForceDeleting') || $model->isForceDeleting()) {
/* @var \A17\Twill\Models\Behaviors\HasMedias $model */
$model->medias()->detach();
}
});
}
/**
* Defines the many-to-many relationship for media objects.
*
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany
*/
public function medias()
{
return $this->morphToMany(
Media::class,
'mediable',
config('twill.mediables_table', 'twill_mediables')
)->withPivot([
'crop',
'role',
'crop_w',
'crop_h',
'crop_x',
'crop_y',
'lqip_data',
'ratio',
'metadatas',
'locale',
])->withTimestamps()->orderBy(config('twill.mediables_table', 'twill_mediables') . '.id', 'asc');
}
private function findMedia($role, $crop = 'default')
{
$media = $this->medias->first(function ($media) use ($role, $crop) {
if (config('twill.media_library.translated_form_fields', false)) {
$localeScope = $media->pivot->locale === app()->getLocale();
}
return $media->pivot->role === $role && $media->pivot->crop === $crop && ($localeScope ?? true);
});
if (!$media && config('twill.media_library.translated_form_fields', false)) {
$media = $this->medias->first(function ($media) use ($role, $crop) {
return $media->pivot->role === $role && $media->pivot->crop === $crop;
});
}
return $media;
}
/**
* Checks if an image has been attached for a role and crop.
*
* @param string $role Role name.
* @param string $crop Crop name.
* @return bool
*/
public function hasImage($role, $crop = 'default')
{
$media = $this->findMedia($role, $crop);
return !empty($media);
}
/**
* Returns the URL of the attached image for a role and crop.
*
* @param string $role Role name.
* @param string $crop Crop name.
* @param array $params Parameters compatible with the current image service, like `w` or `h`.
* @param bool $has_fallback Indicate that you can provide a fallback. Will return `null` instead of the default image fallback.
* @param bool $cms Indicate that you are displaying this image in the CMS views.
* @param Media|null $media Provide a media object if you already retrieved one to prevent more SQL queries.
* @return string|null
*/
public function image(
$role,
$crop = 'default',
$params = [],
$has_fallback = false,
$cms = false,
Media|null|bool $media = null
) {
if (!$media) {
$media = $this->findMedia($role, $crop);
}
if ($media) {
$crop_params = Arr::only($media->pivot->toArray(), $this->cropParamsKeys);
if ($cms) {
return ImageService::getCmsUrl($media->uuid, $crop_params + $params);
}
return ImageService::getUrlWithCrop($media->uuid, $crop_params, $params);
}
if ($has_fallback) {
return null;
}
return ImageService::getTransparentFallbackUrl();
}
/**
* Returns an array of URLs of all attached images for a role and crop.
*
* @param string $role Role name.
* @param string $crop Crop name.
* @param array $params Parameters compatible with the current image service, like `w` or `h`.
* @return array
*/
public function images($role, $crop = 'default', $params = [])
{
$medias = $this->medias->filter(function ($media) use ($role, $crop) {
return $media->pivot->role === $role && $media->pivot->crop === $crop;
});
$urls = [];
foreach ($medias as $media) {
$urls[] = $this->image($role, $crop, $params, false, false, $media);
}
return $urls;
}
/**
* Returns an array of URLs of all attached images for a role, including all crops.
*
* @param string $role Role name.
* @param array $params Parameters compatible with the current image service, like `w` or `h`.
* @return array
*/
public function imagesWithCrops($role, $params = [])
{
$medias = $this->medias->filter(function ($media) use ($role) {
return $media->pivot->role === $role;
});
$urls = [];
foreach ($medias as $media) {
$paramsForCrop = $params[$media->pivot->crop] ?? [];
$urls[$media->id][$media->pivot->crop] = $this->image(
$role,
$media->pivot->crop,
$paramsForCrop,
false,
false,
$media
);
}
return $urls;
}
/**
* Returns an array of meta information for the image attached for a role and crop.
*
* @param string $role Role name.
* @param string $crop Crop name.
* @param array $params Parameters compatible with the current image service, like `w` or `h`.
* @param Media|null $media Provide a media object if you already retrieved one to prevent more SQL queries.
* @return array
*/
public function imageAsArray($role, $crop = 'default', $params = [], $media = null)
{
if ($media === null) {
$media = $this->findMedia($role, $crop);
}
if ($media) {
return [
'src' => $this->image($role, $crop, $params, false, false, $media),
'width' => $media->pivot->crop_w ?? $media->width,
'height' => $media->pivot->crop_h ?? $media->height,
'alt' => $this->imageAltText($role, $media),
'caption' => $this->imageCaption($role, $media),
'video' => $this->imageVideo($role, $media),
];
}
return [];
}
/**
* Returns an array of meta information for all images attached for a role and crop.
*
* @param string $role Role name.
* @param string $crop Crop name.
* @param array $params Parameters compatible with the current image service, like `w` or `h`.
* @return array
*/
public function imagesAsArrays($role, $crop = 'default', $params = [])
{
$medias = $this->medias->filter(function ($media) use ($role, $crop) {
return $media->pivot->role === $role && $media->pivot->crop === $crop;
});
$arrays = [];
foreach ($medias as $media) {
$arrays[] = $this->imageAsArray($role, $crop, $params, $media);
}
return $arrays;
}
/**
* Returns an array of meta information for all images attached for a role, including all crops.
*
* @param string $role Role name.
* @param array $params Parameters compatible with the current image service, like `w` or `h`.
* @return array
*/
public function imagesAsArraysWithCrops($role, $params = [])
{
$medias = $this->medias->filter(function ($media) use ($role) {
return $media->pivot->role === $role;
});
$arrays = [];
foreach ($medias as $media) {
$paramsForCrop = $params[$media->pivot->crop] ?? [];
$arrays[$media->id][$media->pivot->crop] = $this->imageAsArray(
$role,
$media->pivot->crop,
$paramsForCrop,
$media
);
}
return $arrays;
}
/**
* Returns the alt text of the image attached for a role.
*
* @param string $role Role name.
* @param Media|null $media Provide a media object if you already retrieved one to prevent more SQL queries.
* @return string
*/
public function imageAltText($role, $media = null)
{
if ($media === null) {
$media = $this->medias->first(function ($media) use ($role) {
if (config('twill.media_library.translated_form_fields', false)) {
$localeScope = $media->pivot->locale === app()->getLocale();
}
return $media->pivot->role === $role && ($localeScope ?? true);
});
}
if ($media) {
return $media->getMetadata('altText', 'alt_text');
}
return '';
}
/**
* Returns the caption of the image attached for a role.
*
* @param string $role Role name.
* @param Media|null $media Provide a media object if you already retrieved one to prevent more SQL queries.
* @return string
*/
public function imageCaption($role, $media = null)
{
if ($media === null) {
$media = $this->medias->first(function ($media) use ($role) {
if (config('twill.media_library.translated_form_fields', false)) {
$localeScope = $media->pivot->locale === app()->getLocale();
}
return $media->pivot->role === $role && ($localeScope ?? true);
});
}
if ($media) {
return $media->getMetadata('caption');
}
return '';
}
/**
* Returns the video URL of the image attached for a role.
*
* @param string $role Role name.
* @param Media|null $media Provide a media object if you already retrieved one to prevent more SQL queries.
* @return string
*/
public function imageVideo($role, $media = null)
{
if ($media === null) {
$media = $this->medias->first(function ($media) use ($role) {
if (config('twill.media_library.translated_form_fields', false)) {
$localeScope = $media->pivot->locale === app()->getLocale();
}
return $media->pivot->role === $role && ($localeScope ?? true);
});
}
if ($media) {
$metadatas = (object)json_decode($media->pivot->metadatas);
$language = app()->getLocale();
return $metadatas->video->$language ?? (is_object($metadatas->video) ? '' : ($metadatas->video ?? ''));
}
return '';
}
/**
* Returns the media object attached for a role and crop.
*
* @param string $role Role name.
* @param string $crop Crop name.
* @return Media|null
*/
public function imageObject($role, $crop = 'default')
{
return $this->findMedia($role, $crop);
}
/**
* Returns the LQIP base64 encoded string for a role.
* Use this in conjunction with the `RefreshLQIP` Artisan command.
*
* @param string $role Role name.
* @param string $crop Crop name.
* @param array $params Parameters compatible with the current image service, like `w` or `h`.
* @param bool $has_fallback Indicate that you can provide a fallback. Will return `null` instead of the default image fallback.
* @return string|null
* @see \A17\Twill\Commands\RefreshLQIP
*/
public function lowQualityImagePlaceholder($role, $crop = 'default', $params = [], $has_fallback = false)
{
$media = $this->findMedia($role, $crop);
if ($media) {
return $media->pivot->lqip_data ?? ImageService::getTransparentFallbackUrl();
}
if ($has_fallback) {
return null;
}
return ImageService::getTransparentFallbackUrl();
}
/**
* Returns the URL of the social image for a role and crop.
*
* @param string $role Role name.
* @param string $crop Crop name.
* @param array $params Parameters compatible with the current image service, like `w` or `h`.
* @param bool $has_fallback Indicate that you can provide a fallback. Will return `null` instead of the default image fallback.
* @return string|null
*/
public function socialImage($role, $crop = 'default', $params = [], $has_fallback = false)
{
$media = $this->findMedia($role, $crop);
if ($media) {
$crop_params = Arr::only($media->pivot->toArray(), $this->cropParamsKeys);
return ImageService::getSocialUrl($media->uuid, $crop_params + $params);
}
if ($has_fallback) {
return null;
}
return ImageService::getSocialFallbackUrl();
}
/**
* Returns the URL of the CMS image for a role and crop.
*
* @param string $role Role name.
* @param string $crop Crop name.
* @param array $params Parameters compatible with the current image service, like `w` or `h`.
* @return string
*/
public function cmsImage($role, $crop = 'default', $params = [])
{
return $this->image($role, $crop, $params, false, true, false) ?? ImageService::getTransparentFallbackUrl();
}
/**
* Returns the URL of the default CMS image for this model.
*
* @param array $params Parameters compatible with the current image service, like `w` or `h`.
* @return string
*/
public function defaultCmsImage($params = [])
{
$media = $this->medias->first();
if ($media) {
return $this->image(null, null, $params, true, true, $media) ?? ImageService::getTransparentFallbackUrl();
}
return ImageService::getTransparentFallbackUrl();
}
/**
* Returns the media objects associated with a role and crop.
*
* @param string $role Role name.
* @param string $crop Crop name.
* @return \Illuminate\Database\Eloquent\Collection
*/
public function imageObjects($role, $crop = 'default')
{
return $this->medias->filter(function ($media) use ($role, $crop) {
return $media->pivot->role === $role && $media->pivot->crop === $crop;
});
}
}