1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Thinktomorrow\AssetLibrary; |
4
|
|
|
|
5
|
|
|
use Spatie\MediaLibrary\InteractsWithMedia; |
6
|
|
|
use Illuminate\Database\Eloquent\Relations\MorphToMany; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
|
9
|
|
|
trait AssetTrait |
10
|
|
|
{ |
11
|
|
|
use InteractsWithMedia; |
12
|
|
|
|
13
|
67 |
|
public static function bootAssetTrait() |
14
|
|
|
{ |
15
|
|
|
static::deleted(function ($model) { |
|
|
|
|
16
|
|
|
$model->assetRelation->each(function ($asset) use ($model) { |
17
|
2 |
|
$model->assetRelation()->updateExistingPivot($asset->id, ['unused'=> true]); |
18
|
2 |
|
}); |
19
|
67 |
|
}); |
20
|
67 |
|
} |
21
|
|
|
|
22
|
67 |
|
public function assetRelation(): MorphToMany |
23
|
|
|
{ |
24
|
67 |
|
return $this->morphToMany(Asset::class, 'entity', 'asset_pivots')->withPivot('type', 'locale', 'order')->orderBy('order'); |
|
|
|
|
25
|
|
|
} |
26
|
|
|
|
27
|
42 |
|
public function asset(string $type, ?string $locale = null): ?Asset |
28
|
|
|
{ |
29
|
42 |
|
return $this->assets($type, $locale)->first() ?? new NullAsset(); |
30
|
|
|
} |
31
|
|
|
|
32
|
56 |
|
public function assets(?string $type = null, ?string $locale = null): Collection |
33
|
|
|
{ |
34
|
56 |
|
$assets = $this->assetRelation; |
35
|
|
|
|
36
|
56 |
|
if ($type) { |
37
|
55 |
|
$assets = $assets->where('pivot.type', $type); |
38
|
|
|
} |
39
|
|
|
|
40
|
56 |
|
$locale = $locale ?? app()->getLocale(); |
|
|
|
|
41
|
|
|
|
42
|
|
|
$results = $assets->filter(function ($asset) use ($locale) { |
43
|
51 |
|
return $asset->pivot->locale == $locale; |
44
|
56 |
|
}); |
45
|
|
|
|
46
|
56 |
|
if ($this->getUseAssetFallbackLocale() && $locale != $this->getAssetFallbackLocale() && $results->isEmpty()) { |
47
|
|
|
$results = $assets->filter(function ($asset) { |
48
|
5 |
|
return $asset->pivot->locale == $this->getAssetFallbackLocale(); |
49
|
12 |
|
}); |
50
|
|
|
} |
51
|
|
|
|
52
|
56 |
|
return $results->sortBy('pivot.order'); |
53
|
|
|
} |
54
|
|
|
|
55
|
56 |
|
protected function getUseAssetFallbackLocale(): bool |
56
|
|
|
{ |
57
|
56 |
|
return $this->useAssetFallbackLocale ?? config('thinktomorrow.assetlibrary.use_fallback_locale', false); |
58
|
|
|
} |
59
|
|
|
|
60
|
55 |
|
protected function getAssetFallbackLocale(): string |
61
|
|
|
{ |
62
|
55 |
|
if (! config('thinktomorrow.assetlibrary.fallback_locale')) { |
63
|
53 |
|
config()->set('thinktomorrow.assetlibrary.fallback_locale', config('app.fallback_locale')); |
64
|
|
|
} |
65
|
|
|
|
66
|
55 |
|
return $this->assetFallbackLocale ?? config('thinktomorrow.assetlibrary.fallback_locale'); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
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.