1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ByTIC\MediaLibrary\Collections\Traits; |
4
|
|
|
|
5
|
|
|
use ByTIC\MediaLibrary\HasMedia\HasMediaTrait; |
6
|
|
|
use ByTIC\MediaLibrary\Media\Media; |
7
|
|
|
use ByTIC\MediaLibrary\Support\MediaModels; |
8
|
|
|
use Nip\Records\Record; |
9
|
|
|
use function asset; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Trait HasDefaultMediaTrait. |
13
|
|
|
* |
14
|
|
|
* @method HasMediaTrait|Record getRecord |
15
|
|
|
*/ |
16
|
|
|
trait HasDefaultMediaTrait |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var null|Media |
20
|
|
|
*/ |
21
|
|
|
protected $defaultMedia = null; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param Media $media |
25
|
|
|
*/ |
26
|
|
|
public function setDefaultMedia($media) |
27
|
|
|
{ |
28
|
|
|
$this->defaultMedia = $media; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param $mediaName |
33
|
|
|
* |
34
|
|
|
* @return mixed |
35
|
|
|
*/ |
36
|
|
|
public function persistDefaultMediaFromName($mediaName) |
37
|
|
|
{ |
38
|
|
|
$media = $this->get($mediaName, null); |
|
|
|
|
39
|
|
|
if ($media) { |
40
|
|
|
$this->setDefaultMedia($media); |
41
|
|
|
|
42
|
|
|
return $this->persistDefaultMedia(); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public function persistDefaultMedia() |
47
|
|
|
{ |
48
|
|
|
$media = $this->getDefaultMedia(); |
49
|
|
|
|
50
|
|
|
if (false === $media->getFile()->exists()) { |
51
|
|
|
return; |
52
|
|
|
} |
53
|
|
|
if (method_exists($this->getRecord(), 'persistDefaultMedia')) { |
54
|
|
|
return $this->getRecord()->persistDefaultMedia($this, $media); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
$propertiesRecord = MediaModels::properties()->forCollection($this); |
58
|
|
|
$propertiesRecord->defaultMedia($media->getName()); |
59
|
|
|
$propertiesRecord->save(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @return Media|null |
64
|
|
|
*/ |
65
|
|
|
public function getDefaultMedia() |
66
|
|
|
{ |
67
|
|
|
if ($this->defaultMedia === null) { |
68
|
|
|
$this->initDefaultMedia(); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $this->defaultMedia; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
protected function initDefaultMedia() |
75
|
|
|
{ |
76
|
|
|
$this->setDefaultMedia($this->generateDefaultMedia()); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return Media |
81
|
|
|
*/ |
82
|
|
|
public function generateDefaultMedia() |
83
|
|
|
{ |
84
|
|
|
$propertiesRecord = MediaModels::properties()->forCollection($this); |
85
|
|
|
if ($propertiesRecord) { |
86
|
|
|
$defaultMedia = $propertiesRecord->defaultMedia(); |
87
|
|
|
if ($defaultMedia && $this->has($defaultMedia)) { |
|
|
|
|
88
|
|
|
return $this->get($defaultMedia); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
if (count($this->items)) { |
93
|
|
|
return reset($this->items); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $this->compileDefaultMedia(); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return Media |
101
|
|
|
*/ |
102
|
|
|
protected function compileDefaultMedia() |
103
|
|
|
{ |
104
|
|
|
$media = $this->newMedia(); |
|
|
|
|
105
|
|
|
|
106
|
|
|
return $media; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* @return string |
111
|
|
|
*/ |
112
|
|
|
public function getDefaultMediaUrl() |
113
|
|
|
{ |
114
|
|
|
if (method_exists($this->getRecord(), 'getDefaultMediaUrl')) { |
115
|
|
|
return $this->getRecord()->getDefaultMediaUrl($this); |
|
|
|
|
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
if (method_exists($this->getRecord()->getManager(), 'getDefaultMediaUrl')) { |
|
|
|
|
119
|
|
|
return $this->getRecord()->getManager()->getDefaultMediaUrl($this); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
return $this->getDefaultMediaGenericUrl(); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
|
|
public function getDefaultMediaGenericUrl() |
129
|
|
|
{ |
130
|
|
|
return asset('/images/' |
131
|
|
|
. $this->getRecord()->getManager()->getTable() . '/' |
132
|
|
|
. $this->getDefaultFileName()); |
133
|
|
|
} |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @return string |
137
|
|
|
*/ |
138
|
|
|
public function getDefaultFileName() |
139
|
|
|
{ |
140
|
|
|
$name = inflector()->singularize($this->getName()); |
|
|
|
|
141
|
|
|
$extension = $this->getName() == 'logos' ? 'png' : 'jpg'; |
142
|
|
|
|
143
|
|
|
return $name . '.' . $extension; |
144
|
|
|
} |
145
|
|
|
} |
146
|
|
|
|