|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Thinktomorrow\AssetLibrary\Traits; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Support\Collection; |
|
6
|
|
|
use Thinktomorrow\AssetLibrary\Models\Asset; |
|
7
|
|
|
use Thinktomorrow\Locale\Locale; |
|
8
|
|
|
|
|
9
|
|
|
trait AssetTrait |
|
10
|
|
|
{ |
|
11
|
20 |
|
public function assets() |
|
|
|
|
|
|
12
|
|
|
{ |
|
13
|
20 |
|
return $this->morphToMany(Asset::class, 'entity', 'asset_pivots')->withPivot('type', 'locale'); |
|
|
|
|
|
|
14
|
|
|
} |
|
15
|
|
|
|
|
16
|
4 |
|
public function hasFile($type = '', $locale = '') |
|
17
|
|
|
{ |
|
18
|
4 |
|
$filename = $this->getFilename($type, $locale); |
|
19
|
|
|
|
|
20
|
4 |
|
return (bool) $filename and basename($filename) != 'other.png'; |
|
|
|
|
|
|
21
|
|
|
} |
|
22
|
|
|
|
|
23
|
6 |
|
public function getFilename($type = '', $locale = '') |
|
24
|
|
|
{ |
|
25
|
6 |
|
return basename($this->getFileUrl($type, '', $locale)); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
18 |
|
/** |
|
29
|
|
|
* @param string $locale |
|
|
|
|
|
|
30
|
18 |
|
* |
|
31
|
2 |
|
* @return string |
|
32
|
|
|
*/ |
|
33
|
|
|
public function getFileUrl($type = '', $size = '', $locale = null) |
|
34
|
18 |
|
{ |
|
35
|
14 |
|
if ($this->assets->first() === null || $this->assets->first()->pivot === null) { |
|
|
|
|
|
|
36
|
|
|
return; |
|
37
|
|
|
} |
|
38
|
18 |
|
|
|
39
|
18 |
|
if (! $locale) { |
|
|
|
|
|
|
40
|
5 |
|
$locale = Locale::getDefault(); |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
18 |
|
$assets = $this->assets->where('pivot.type', $type); |
|
44
|
1 |
|
if ($assets->count() > 1) { |
|
45
|
|
|
$assets = $assets->where('pivot.locale', $locale); |
|
46
|
|
|
} |
|
47
|
18 |
|
|
|
48
|
|
|
if ($assets->isEmpty()) { |
|
49
|
|
|
return; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
return $assets->first()->getFileUrl($size); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Adds a file to this model, accepts a type and locale to be saved with the file. |
|
57
|
12 |
|
* |
|
58
|
|
|
* @param $file |
|
59
|
12 |
|
* @param $type |
|
60
|
|
|
* @param string $locale |
|
|
|
|
|
|
61
|
12 |
|
*/ |
|
62
|
|
|
public function addFile($file, $type = '', $locale = null) |
|
63
|
12 |
|
{ |
|
64
|
3 |
|
$locale = $this->normalizeLocale($locale); |
|
65
|
|
|
|
|
66
|
9 |
|
$asset = Asset::upload($file); |
|
67
|
|
|
|
|
68
|
12 |
|
if ($asset instanceof Collection) { |
|
69
|
|
|
$asset->each->attachToModel($this, $type, $locale); |
|
70
|
|
|
} else { |
|
71
|
|
|
$asset->attachToModel($this, $type, $locale); |
|
72
|
1 |
|
} |
|
73
|
1 |
|
} |
|
74
|
1 |
|
|
|
75
|
|
|
public function getAllImages() |
|
|
|
|
|
|
76
|
1 |
|
{ |
|
77
|
|
|
$images = $this->assets->filter(function ($asset) { |
|
78
|
|
|
return $asset->getExtensionForFilter() == 'image'; |
|
79
|
1 |
|
}); |
|
80
|
|
|
|
|
81
|
1 |
|
return $images; |
|
82
|
|
|
} |
|
83
|
1 |
|
|
|
84
|
|
|
public function getAllFiles($type = null, $locale = '') |
|
|
|
|
|
|
85
|
1 |
|
{ |
|
86
|
|
|
$locale = $this->normalizeLocale($locale); |
|
87
|
|
|
|
|
88
|
12 |
|
$files = $this->assets->where('pivot.type', $type)->where('pivot.locale', $locale); |
|
89
|
|
|
|
|
90
|
12 |
|
return $files; |
|
91
|
8 |
|
} |
|
92
|
|
|
|
|
93
|
12 |
|
private function normalizeLocale($locale) |
|
|
|
|
|
|
94
|
|
|
{ |
|
95
|
|
|
if ($locale == '' || $locale == null) { |
|
96
|
|
|
$locale = Locale::getDefault(); |
|
97
|
|
|
} |
|
98
|
|
|
return $locale; |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a
@returnannotation as described here.