1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace A17\Twill\Models; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
6
|
|
|
use Illuminate\Support\Facades\DB; |
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
use ImageService; |
|
|
|
|
9
|
|
|
|
10
|
|
|
class Media extends Model |
11
|
|
|
{ |
12
|
|
|
public $timestamps = true; |
13
|
|
|
|
14
|
|
|
protected $fillable = [ |
15
|
|
|
'uuid', |
16
|
|
|
'filename', |
17
|
|
|
'alt_text', |
18
|
|
|
'caption', |
19
|
|
|
'width', |
20
|
|
|
'height', |
21
|
|
|
]; |
22
|
|
|
|
23
|
49 |
|
public function __construct(array $attributes = []) |
24
|
|
|
{ |
25
|
|
|
$this->fillable(array_merge($this->fillable, Collection::make(config('twill.media_library.extra_metadatas_fields'))->map(function ($field) { |
26
|
|
|
return $field['name']; |
27
|
49 |
|
})->toArray())); |
28
|
|
|
|
29
|
|
|
Collection::make(config('twill.media_library.translatable_metadatas_fields'))->each(function ($field) { |
30
|
|
|
$this->casts[$field] = 'json'; |
31
|
49 |
|
}); |
32
|
|
|
|
33
|
49 |
|
parent::__construct($attributes); |
34
|
49 |
|
} |
35
|
|
|
|
36
|
|
|
public function scopeUnused ($query) |
37
|
|
|
{ |
38
|
|
|
$usedIds = DB::table(config('twill.mediables_table'))->get()->pluck('media_id'); |
39
|
|
|
return $query->whereNotIn('id', $usedIds->toArray())->get(); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
public function getDimensionsAttribute() |
43
|
|
|
{ |
44
|
|
|
return $this->width . 'x' . $this->height; |
|
|
|
|
45
|
|
|
} |
46
|
|
|
|
47
|
3 |
|
public function altTextFrom($filename) |
48
|
|
|
{ |
49
|
3 |
|
$filename = pathinfo($filename, PATHINFO_FILENAME); |
50
|
3 |
|
if (Str::endsWith($filename, '@2x')) { |
51
|
|
|
$filename = substr($filename, 0, -2); |
52
|
|
|
} |
53
|
|
|
|
54
|
3 |
|
return ucwords(preg_replace('/[^a-zA-Z0-9]/', ' ', sanitizeFilename($filename))); |
55
|
|
|
} |
56
|
|
|
|
57
|
3 |
|
public function canDeleteSafely() |
58
|
|
|
{ |
59
|
3 |
|
return DB::table(config('twill.mediables_table', 'twill_mediables'))->where('media_id', $this->id)->count() === 0; |
60
|
|
|
} |
61
|
|
|
|
62
|
3 |
|
public function toCmsArray() |
63
|
|
|
{ |
64
|
|
|
return [ |
65
|
3 |
|
'id' => $this->id, |
66
|
3 |
|
'name' => $this->filename, |
|
|
|
|
67
|
3 |
|
'thumbnail' => ImageService::getCmsUrl($this->uuid, ["h" => "256"]), |
|
|
|
|
68
|
3 |
|
'original' => ImageService::getRawUrl($this->uuid), |
69
|
3 |
|
'medium' => ImageService::getUrl($this->uuid, ["h" => "430"]), |
70
|
3 |
|
'width' => $this->width, |
|
|
|
|
71
|
3 |
|
'height' => $this->height, |
|
|
|
|
72
|
|
|
'tags' => $this->tags->map(function ($tag) { |
|
|
|
|
73
|
1 |
|
return $tag->name; |
74
|
3 |
|
}), |
75
|
3 |
|
'deleteUrl' => $this->canDeleteSafely() ? moduleRoute('medias', 'media-library', 'destroy', $this->id) : null, |
|
|
|
|
76
|
3 |
|
'updateUrl' => route('admin.media-library.medias.single-update'), |
77
|
3 |
|
'updateBulkUrl' => route('admin.media-library.medias.bulk-update'), |
78
|
3 |
|
'deleteBulkUrl' => route('admin.media-library.medias.bulk-delete'), |
79
|
|
|
'metadatas' => [ |
80
|
|
|
'default' => [ |
81
|
3 |
|
'caption' => $this->caption, |
|
|
|
|
82
|
3 |
|
'altText' => $this->alt_text, |
|
|
|
|
83
|
|
|
'video' => null, |
84
|
|
|
] + Collection::make(config('twill.media_library.extra_metadatas_fields'))->mapWithKeys(function ($field) { |
85
|
|
|
return [ |
86
|
|
|
$field['name'] => $this->{$field['name']}, |
87
|
|
|
]; |
88
|
3 |
|
})->toArray(), |
89
|
|
|
'custom' => [ |
90
|
|
|
'caption' => null, |
91
|
|
|
'altText' => null, |
92
|
|
|
'video' => null, |
93
|
|
|
], |
94
|
|
|
], |
95
|
|
|
]; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
public function getMetadata($name, $fallback = null) |
99
|
|
|
{ |
100
|
|
|
$metadatas = (object) json_decode($this->pivot->metadatas); |
|
|
|
|
101
|
|
|
$language = app()->getLocale(); |
|
|
|
|
102
|
|
|
|
103
|
|
|
if ($metadatas->$name->$language ?? false) { |
104
|
|
|
return $metadatas->$name->$language; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
$fallbackLocale = config('translatable.fallback_locale'); |
108
|
|
|
|
109
|
|
|
if (in_array($name, config('twill.media_library.translatable_metadatas_fields', [])) && config('translatable.use_property_fallback', false) && ($metadatas->$name->$fallbackLocale ?? false)) { |
110
|
|
|
return $metadatas->$name->$fallbackLocale; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$fallbackValue = $fallback ? $this->$fallback : $this->$name; |
114
|
|
|
|
115
|
|
|
$fallback = $fallback ?? $name; |
116
|
|
|
|
117
|
|
|
if (in_array($fallback, config('twill.media_library.translatable_metadatas_fields', []))) { |
118
|
|
|
$fallbackValue = $fallbackValue[$language] ?? ''; |
119
|
|
|
|
120
|
|
|
if ($fallbackValue === '' && config('translatable.use_property_fallback', false)) { |
121
|
|
|
$fallbackValue = $this->$fallback[config('translatable.fallback_locale')] ?? ''; |
122
|
|
|
} |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
if (is_object($metadatas->$name ?? null)) { |
126
|
|
|
return $fallbackValue ?? ''; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $metadatas->$name ?? $fallbackValue ?? ''; |
130
|
|
|
} |
131
|
|
|
|
132
|
49 |
|
public function getTable() |
133
|
|
|
{ |
134
|
49 |
|
return config('twill.medias_table', 'twill_medias'); |
135
|
|
|
} |
136
|
|
|
} |
137
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths