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