|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Thinktomorrow\AssetLibrary\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Thinktomorrow\AssetLibrary\Models\Asset; |
|
6
|
|
|
use Spatie\MediaLibrary\HasMedia\HasMediaTrait; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphToMany; |
|
8
|
|
|
|
|
9
|
|
|
trait AssetTrait |
|
10
|
|
|
{ |
|
11
|
|
|
use HasMediaTrait; |
|
12
|
|
|
|
|
13
|
51 |
|
public static function bootAssetTrait() |
|
14
|
|
|
{ |
|
15
|
|
|
static::deleted(function ($model) { |
|
|
|
|
|
|
16
|
|
|
$model->assetRelation->each(function ($asset) use ($model) { |
|
17
|
1 |
|
$model->assetRelation()->updateExistingPivot($asset->id, ['unused'=> true]); |
|
18
|
1 |
|
}); |
|
19
|
51 |
|
}); |
|
20
|
51 |
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @return mixed |
|
24
|
|
|
*/ |
|
25
|
51 |
|
public function assetRelation(): MorphToMany |
|
26
|
|
|
{ |
|
27
|
51 |
|
return $this->morphToMany(Asset::class, 'entity', 'asset_pivots')->withPivot('type', 'locale', 'order')->orderBy('order'); |
|
|
|
|
|
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
39 |
|
public function asset(string $type = '', ?string $locale = null): ?Asset |
|
31
|
|
|
{ |
|
32
|
39 |
|
$this->load('assetRelation'); |
|
|
|
|
|
|
33
|
|
|
|
|
34
|
39 |
|
if ($this->assetRelation->first() === null || $this->assetRelation->first()->pivot === null) { |
|
35
|
3 |
|
return null; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
38 |
|
$assets = $this->assetRelation; |
|
39
|
|
|
|
|
40
|
38 |
|
if($type != '') |
|
41
|
|
|
{ |
|
42
|
12 |
|
$assets = $this->assetRelation->where('pivot.type', $type); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
38 |
|
if ($locale && $assets->count() > 1) { |
|
46
|
3 |
|
$assets = $assets->where('pivot.locale', $locale); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
38 |
|
if ($assets->isEmpty()) { |
|
50
|
1 |
|
return null; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
38 |
|
return $assets->first(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
14 |
|
public function assets(string $type = '', string $locale = null) |
|
57
|
|
|
{ |
|
58
|
14 |
|
$this->load('assetRelation'); |
|
59
|
|
|
|
|
60
|
14 |
|
$assets = $this->assetRelation; |
|
61
|
|
|
|
|
62
|
14 |
|
if ($type) { |
|
63
|
6 |
|
$assets = $assets->where('pivot.type', $type); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
14 |
|
if ($locale) { |
|
67
|
|
|
$assets = $assets->where('pivot.locale', $locale); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
14 |
|
return $assets->sortBy('pivot.order'); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
// /** |
|
74
|
|
|
// * @param string $type |
|
75
|
|
|
// * @param string|null $locale |
|
76
|
|
|
// * @return string |
|
77
|
|
|
// */ |
|
78
|
|
|
// public function getFilename($type = '', $locale = null): string |
|
79
|
|
|
// { |
|
80
|
|
|
// return basename($this->getFileUrl($type, '', $locale)); |
|
81
|
|
|
// } |
|
82
|
|
|
|
|
83
|
28 |
|
public function getFileUrl($type = '', $size = '', $locale = null): ?string |
|
84
|
|
|
{ |
|
85
|
28 |
|
return optional($this->asset($type, $locale))->url($size); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @param string|null $locale |
|
90
|
|
|
* @return string |
|
91
|
|
|
*/ |
|
92
|
|
|
private function normalizeLocaleString($locale = null): string |
|
93
|
|
|
{ |
|
94
|
|
|
$locale = $locale ?? config('app.fallback_locale'); |
|
95
|
|
|
|
|
96
|
|
|
return $locale; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
|
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.
This is most likely a typographical error or the method has been renamed.