1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace A17\Twill\Models; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Illuminate\Support\Collection; |
7
|
|
|
use Illuminate\Support\Facades\DB; |
8
|
|
|
use Illuminate\Support\Str; |
9
|
|
|
use ImageService; |
|
|
|
|
10
|
|
|
|
11
|
|
|
class Media extends Model |
12
|
|
|
{ |
13
|
|
|
public $timestamps = true; |
14
|
|
|
|
15
|
|
|
protected $fillable = [ |
16
|
|
|
'uuid', |
17
|
|
|
'filename', |
18
|
|
|
'alt_text', |
19
|
|
|
'caption', |
20
|
|
|
'width', |
21
|
|
|
'height', |
22
|
|
|
]; |
23
|
49 |
|
|
24
|
|
|
public function __construct(array $attributes = []) |
25
|
49 |
|
{ |
26
|
|
|
$this->fillable(array_merge($this->fillable, Collection::make(config('twill.media_library.extra_metadatas_fields'))->map(function ($field) { |
27
|
49 |
|
return $field['name']; |
28
|
|
|
})->toArray())); |
29
|
49 |
|
|
30
|
|
|
Collection::make(config('twill.media_library.translatable_metadatas_fields'))->each(function ($field) { |
31
|
49 |
|
$this->casts[$field] = 'json'; |
32
|
|
|
}); |
33
|
49 |
|
|
34
|
49 |
|
parent::__construct($attributes); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function scopeUnused ($query) |
38
|
|
|
{ |
39
|
|
|
$usedIds = DB::table(config('twill.mediables_table'))->get()->pluck('media_id'); |
40
|
|
|
return $query->whereNotIn('id', $usedIds->toArray())->get(); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
public function getDimensionsAttribute() |
44
|
|
|
{ |
45
|
|
|
return $this->width . 'x' . $this->height; |
|
|
|
|
46
|
|
|
} |
47
|
3 |
|
|
48
|
|
|
public function altTextFrom($filename) |
49
|
3 |
|
{ |
50
|
3 |
|
$filename = pathinfo($filename, PATHINFO_FILENAME); |
51
|
|
|
if (Str::endsWith($filename, '@2x')) { |
|
|
|
|
52
|
|
|
$filename = substr($filename, 0, -2); |
|
|
|
|
53
|
|
|
} |
54
|
3 |
|
|
55
|
|
|
return ucwords(preg_replace('/[^a-zA-Z0-9]/', ' ', sanitizeFilename($filename))); |
|
|
|
|
56
|
|
|
} |
57
|
3 |
|
|
58
|
|
|
public function canDeleteSafely() |
59
|
3 |
|
{ |
60
|
|
|
return DB::table(config('twill.mediables_table', 'twill_mediables'))->where('media_id', $this->id)->count() === 0; |
61
|
|
|
} |
62
|
3 |
|
|
63
|
|
|
public function isReferenced() |
64
|
|
|
{ |
65
|
3 |
|
return DB::table(config('twill.mediables_table', 'twill_mediables'))->where('media_id', $this->id)->count() > 0; |
66
|
3 |
|
} |
67
|
3 |
|
|
68
|
3 |
|
public function toCmsArray() |
69
|
3 |
|
{ |
70
|
3 |
|
return [ |
71
|
3 |
|
'id' => $this->id, |
72
|
3 |
|
'name' => $this->filename, |
|
|
|
|
73
|
1 |
|
'thumbnail' => ImageService::getCmsUrl($this->uuid, ["h" => "256"]), |
|
|
|
|
74
|
3 |
|
'original' => ImageService::getRawUrl($this->uuid), |
75
|
3 |
|
'medium' => ImageService::getUrl($this->uuid, ["h" => "430"]), |
76
|
3 |
|
'width' => $this->width, |
|
|
|
|
77
|
3 |
|
'height' => $this->height, |
|
|
|
|
78
|
3 |
|
'tags' => $this->tags->map(function ($tag) { |
|
|
|
|
79
|
|
|
return $tag->name; |
80
|
|
|
}), |
81
|
3 |
|
'deleteUrl' => $this->canDeleteSafely() ? moduleRoute('medias', 'media-library', 'destroy', $this->id) : null, |
|
|
|
|
82
|
3 |
|
'updateUrl' => route('admin.media-library.medias.single-update'), |
83
|
|
|
'updateBulkUrl' => route('admin.media-library.medias.bulk-update'), |
84
|
3 |
|
'deleteBulkUrl' => route('admin.media-library.medias.bulk-delete'), |
85
|
|
|
'metadatas' => [ |
86
|
|
|
'default' => [ |
87
|
|
|
'caption' => $this->caption, |
|
|
|
|
88
|
3 |
|
'altText' => $this->alt_text, |
|
|
|
|
89
|
|
|
'video' => null, |
90
|
|
|
] + Collection::make(config('twill.media_library.extra_metadatas_fields'))->mapWithKeys(function ($field) { |
91
|
|
|
return [ |
92
|
|
|
$field['name'] => $this->{$field['name']}, |
93
|
|
|
]; |
94
|
|
|
})->toArray(), |
95
|
|
|
'custom' => [ |
96
|
|
|
'caption' => null, |
97
|
|
|
'altText' => null, |
98
|
|
|
'video' => null, |
99
|
|
|
], |
100
|
|
|
], |
101
|
|
|
]; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
public function getMetadata($name, $fallback = null) |
105
|
|
|
{ |
106
|
|
|
$metadatas = (object) json_decode($this->pivot->metadatas); |
|
|
|
|
107
|
|
|
$language = app()->getLocale(); |
|
|
|
|
108
|
|
|
|
109
|
|
|
if ($metadatas->$name->$language ?? false) { |
110
|
|
|
return $metadatas->$name->$language; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
$fallbackLocale = config('translatable.fallback_locale'); |
114
|
|
|
|
115
|
|
|
if (in_array($name, config('twill.media_library.translatable_metadatas_fields', [])) && config('translatable.use_property_fallback', false) && ($metadatas->$name->$fallbackLocale ?? false)) { |
116
|
|
|
return $metadatas->$name->$fallbackLocale; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$fallbackValue = $fallback ? $this->$fallback : $this->$name; |
120
|
|
|
|
121
|
|
|
$fallback = $fallback ?? $name; |
122
|
|
|
|
123
|
|
|
if (in_array($fallback, config('twill.media_library.translatable_metadatas_fields', []))) { |
124
|
|
|
$fallbackValue = $fallbackValue[$language] ?? ''; |
125
|
|
|
|
126
|
|
|
if ($fallbackValue === '' && config('translatable.use_property_fallback', false)) { |
127
|
|
|
$fallbackValue = $this->$fallback[config('translatable.fallback_locale')] ?? ''; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
if (is_object($metadatas->$name ?? null)) { |
132
|
49 |
|
return $fallbackValue ?? ''; |
133
|
|
|
} |
134
|
49 |
|
|
135
|
|
|
return $metadatas->$name ?? $fallbackValue ?? ''; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
public function replace($fields) |
139
|
|
|
{ |
140
|
|
|
$prevHeight = $this->height; |
|
|
|
|
141
|
|
|
$prevWidth = $this->width; |
|
|
|
|
142
|
|
|
|
143
|
|
|
if ($this->update($fields) && $this->isReferenced()) |
144
|
|
|
{ |
145
|
|
|
DB::table(config('twill.mediables_table', 'twill_mediables'))->where('media_id', $this->id)->get()->each(function ($mediable) use ($prevWidth, $prevHeight) { |
146
|
|
|
|
147
|
|
|
if ($prevWidth != $this->width) { |
|
|
|
|
148
|
|
|
$mediable->crop_x = 0; |
149
|
|
|
$mediable->crop_w = $this->width; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
if ($prevHeight != $this->height) { |
|
|
|
|
153
|
|
|
$mediable->crop_y = 0; |
154
|
|
|
$mediable->crop_h = $this->height; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
DB::table(config('twill.mediables_table', 'twill_mediables'))->where('id', $mediable->id)->update((array)$mediable); |
158
|
|
|
}); |
159
|
|
|
} |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
public function getTable() |
163
|
|
|
{ |
164
|
|
|
return config('twill.medias_table', 'twill_medias'); |
165
|
|
|
} |
166
|
|
|
} |
167
|
|
|
|
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