|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Thinktomorrow\AssetLibrary\Models; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
6
|
|
|
use Illuminate\Http\File; |
|
7
|
|
|
use Illuminate\Http\UploadedFile; |
|
8
|
|
|
use Spatie\MediaLibrary\HasMedia\HasMediaTrait; |
|
9
|
|
|
use Spatie\MediaLibrary\HasMedia\Interfaces\HasMediaConversions; |
|
10
|
|
|
use Spatie\MediaLibrary\Media; |
|
11
|
|
|
use Thinktomorrow\Locale\Locale; |
|
12
|
|
|
|
|
13
|
|
|
class Asset extends Model implements HasMediaConversions |
|
14
|
|
|
{ |
|
15
|
|
|
use HasMediaTrait; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Attaches this asset instance to the given model and |
|
19
|
|
|
* sets the type and locale to the given values and |
|
20
|
|
|
* returns the model with the asset relationship. |
|
21
|
|
|
* |
|
22
|
|
|
* @param Model $model |
|
23
|
|
|
* @param string $type |
|
24
|
|
|
* @param null|string $locale |
|
25
|
|
|
* @return Model |
|
26
|
|
|
*/ |
|
27
|
|
|
public function attachToModel(Model $model, $type = '', $locale = null) |
|
28
|
|
|
{ |
|
29
|
|
|
$asset = $model->assets->where('pivot.type', $type)->where('pivot.locale', $locale); |
|
30
|
|
|
|
|
31
|
|
|
if (! $asset->isEmpty() && $asset->first()->pivot->type !== '') { |
|
32
|
|
|
$model->assets()->detach($asset->first()->id); |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
if (! $locale) { |
|
|
|
|
|
|
36
|
|
|
$locale = Locale::getDefault(); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
$model->assets()->attach($this, ['type' => $type, 'locale' => $locale]); |
|
40
|
|
|
|
|
41
|
|
|
return $model->load('assets'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
public function hasFile() |
|
45
|
|
|
{ |
|
46
|
|
|
return (bool) $this->getFileUrl(''); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function getFilename($size = '') |
|
50
|
|
|
{ |
|
51
|
|
|
return basename($this->getFileUrl($size)); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
/** |
|
55
|
|
|
* @return string |
|
56
|
|
|
*/ |
|
57
|
|
|
public function getFileUrl($size = '') |
|
58
|
|
|
{ |
|
59
|
|
|
$media = $this->getMedia(); |
|
60
|
|
|
|
|
61
|
|
|
if ($media->count() < 1) { |
|
62
|
|
|
return asset('assets/back/img/other.png'); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
if (config('assetlibrary.conversionPrefix') && $size != '') { |
|
66
|
|
|
$conversionName = $media->first()->name . '_' . $size; |
|
67
|
|
|
} else { |
|
68
|
|
|
$conversionName = $size; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
return $media->first()->getUrl($conversionName); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Returns the image url or a fallback specific per filetype. |
|
76
|
|
|
* |
|
77
|
|
|
* @param string $type |
|
78
|
|
|
* @return string |
|
79
|
|
|
*/ |
|
80
|
|
|
public function getImageUrl($type = '') |
|
81
|
|
|
{ |
|
82
|
|
|
if ($this->getMedia()->isEmpty()) { |
|
83
|
|
|
return asset('assets/back/img/other.png'); |
|
84
|
|
|
} |
|
85
|
|
|
$extension = $this->getExtensionType(); |
|
86
|
|
|
if ($extension === 'image') { |
|
87
|
|
|
return $this->getFileUrl($type); |
|
88
|
|
|
} elseif ($extension) { |
|
|
|
|
|
|
89
|
|
|
return asset('assets/back/img/'.$extension.'.png'); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
return asset('assets/back/img/other.png'); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
public function getExtensionForFilter() |
|
96
|
|
|
{ |
|
97
|
|
|
if ($extension = $this->getExtensionType()) { |
|
98
|
|
|
return $extension; |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return ''; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
public function getExtensionType() |
|
105
|
|
|
{ |
|
106
|
|
|
$extension = explode('.', $this->getMedia()[0]->file_name); |
|
107
|
|
|
$extension = end($extension); |
|
108
|
|
|
|
|
109
|
|
|
if (in_array($extension, ['xls', 'xlsx', 'numbers', 'sheets'])) { |
|
110
|
|
|
return 'xls'; |
|
111
|
|
|
} |
|
112
|
|
|
if (in_array($extension, ['png', 'jpg', 'jpeg', 'gif', 'svg', 'webp'])) { |
|
113
|
|
|
return 'image'; |
|
114
|
|
|
} |
|
115
|
|
|
if ($extension === 'pdf') { |
|
116
|
|
|
return 'pdf'; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
return false; |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
public function getMimeType() |
|
|
|
|
|
|
123
|
|
|
{ |
|
124
|
|
|
return $this->isMediaEmpty() ? '' : $this->getMedia()[0]->mime_type; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
public function isMediaEmpty() |
|
128
|
|
|
{ |
|
129
|
|
|
return $this->getMedia()->isEmpty(); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
public function getSize() |
|
|
|
|
|
|
133
|
|
|
{ |
|
134
|
|
|
return $this->isMediaEmpty() ? '' : $this->getMedia()[0]->human_readable_size; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
public function getDimensions() |
|
|
|
|
|
|
138
|
|
|
{ |
|
139
|
|
|
return $this->isMediaEmpty() ? '' : $this->getMedia()[0]->getCustomProperty('dimensions'); |
|
140
|
|
|
} |
|
141
|
|
|
|
|
142
|
|
|
/** |
|
143
|
|
|
* Removes one or more assets by their ids. |
|
144
|
|
|
* @param $image_ids |
|
145
|
|
|
*/ |
|
146
|
|
|
public static function remove($image_ids) |
|
|
|
|
|
|
147
|
|
|
{ |
|
148
|
|
|
if (is_array($image_ids)) { |
|
149
|
|
|
foreach ($image_ids as $id) { |
|
150
|
|
|
self::where('id', $id)->first()->delete(); |
|
151
|
|
|
} |
|
152
|
|
|
} else { |
|
153
|
|
|
self::find($image_ids)->first()->delete(); |
|
154
|
|
|
} |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* Returns a collection of all the assets in the library. |
|
159
|
|
|
* @return \Illuminate\Support\Collection |
|
160
|
|
|
*/ |
|
161
|
|
|
public static function getAllAssets() |
|
162
|
|
|
{ |
|
163
|
|
|
return self::all()->sortByDesc('created_at'); |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* Generates the hidden field that links the file to a specific type. |
|
168
|
|
|
* |
|
169
|
|
|
* @param string $type |
|
170
|
|
|
* @param null $locale |
|
171
|
|
|
* |
|
172
|
|
|
* @return string |
|
173
|
|
|
*/ |
|
174
|
|
|
public static function typeField($type = '', $locale = null, $name = 'type') |
|
175
|
|
|
{ |
|
176
|
|
|
$result = '<input type="hidden" value="'.$type.'" name="'; |
|
177
|
|
|
|
|
178
|
|
|
if (! $locale) { |
|
179
|
|
|
return $result.$name.'">'; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
return $result.'trans['.$locale.'][files][]">'; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Generates the hidden field that links the file to translations. |
|
187
|
|
|
* |
|
188
|
|
|
* @param string $locale |
|
189
|
|
|
* |
|
190
|
|
|
* @return string |
|
191
|
|
|
*/ |
|
192
|
|
|
public static function localeField($locale = '') |
|
193
|
|
|
{ |
|
194
|
|
|
return self::typeField($locale, null, 'locale'); |
|
195
|
|
|
} |
|
196
|
|
|
|
|
197
|
|
|
/** |
|
198
|
|
|
* Register the conversions that should be performed. |
|
199
|
|
|
* |
|
200
|
|
|
* @param Media|null $media |
|
201
|
|
|
* @return array |
|
|
|
|
|
|
202
|
|
|
*/ |
|
203
|
|
|
public function registerMediaConversions(Media $media = null) |
|
204
|
|
|
{ |
|
205
|
|
|
$conversions = config('assetlibrary.conversions'); |
|
206
|
|
|
$conversionPrefix = config('assetlibrary.conversionPrefix'); |
|
207
|
|
|
|
|
208
|
|
|
foreach ($conversions as $key => $value) { |
|
209
|
|
|
if ($conversionPrefix) { |
|
210
|
|
|
$conversionName = $media->name.'_'.$key; |
|
211
|
|
|
} else { |
|
212
|
|
|
$conversionName = $key; |
|
213
|
|
|
} |
|
214
|
|
|
|
|
215
|
|
|
$this->addMediaConversion($conversionName) |
|
216
|
|
|
->width($value['width']) |
|
217
|
|
|
->height($value['height']) |
|
218
|
|
|
->sharpen(15) |
|
219
|
|
|
->keepOriginalImageFormat() |
|
220
|
|
|
->optimize(); |
|
221
|
|
|
} |
|
222
|
|
|
} |
|
223
|
|
|
} |
|
224
|
|
|
|
In PHP, under loose comparison (like
==, or!=, orswitchconditions), values of different types might be equal.For
stringvalues, the empty string''is a special case, in particular the following results might be unexpected: