1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace PiedWeb\CMSBundle\Entity; |
4
|
|
|
|
5
|
|
|
use Cocur\Slugify\Slugify; |
6
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
7
|
|
|
use Doctrine\ORM\Mapping as ORM; |
8
|
|
|
use InvertColor\Color; |
9
|
|
|
use Symfony\Component\HttpFoundation\File\File; |
10
|
|
|
use Symfony\Component\Yaml\Yaml; |
11
|
|
|
use Vich\UploaderBundle\Mapping\Annotation as Vich; |
12
|
|
|
|
13
|
|
|
trait MediaTrait |
14
|
|
|
{ |
15
|
1 |
|
use TimestampableTrait; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @ORM\Column(type="string", length=50) |
19
|
|
|
*/ |
20
|
|
|
protected $mimeType; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @ORM\Column(type="string", length=255) |
24
|
|
|
*/ |
25
|
|
|
protected $relativeDir; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @ORM\Column(type="integer") |
29
|
|
|
*/ |
30
|
|
|
protected $size; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @ORM\Column(type="integer", nullable=true) |
34
|
|
|
*/ |
35
|
|
|
protected $height; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @ORM\Column(type="integer", nullable=true) |
39
|
|
|
*/ |
40
|
|
|
protected $width; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @ORM\Column(type="string", nullable=true) |
44
|
|
|
*/ |
45
|
|
|
protected $mainColor; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @ORM\Column(type="string", length=255) |
49
|
|
|
*/ |
50
|
|
|
protected $media; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* NOTE: This is not a mapped field of entity metadata, just a simple property. |
54
|
|
|
* |
55
|
|
|
* @Vich\UploadableField( |
56
|
|
|
* mapping="media_media", |
57
|
|
|
* fileNameProperty="slug", |
58
|
|
|
* mimeType="mimeType", |
59
|
|
|
* size="size", |
60
|
|
|
* dimensions="dimensions" |
61
|
|
|
* ) |
62
|
|
|
* |
63
|
|
|
* @var File |
64
|
|
|
*/ |
65
|
|
|
protected $mediaFile; |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @ORM\Column(type="string", length=100, unique=true) |
69
|
|
|
*/ |
70
|
|
|
protected $name; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @ORM\Column(type="text", nullable=true) |
74
|
|
|
*/ |
75
|
|
|
protected $names; |
76
|
|
|
|
77
|
|
|
protected $slug; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @ORM\OneToMany( |
81
|
|
|
* targetEntity="PiedWeb\CMSBundle\Entity\PageHasMediaInterface", |
82
|
|
|
* mappedBy="media", |
83
|
|
|
* cascade={"all"}, |
84
|
|
|
* orphanRemoval=true |
85
|
|
|
* ) |
86
|
|
|
*/ |
87
|
|
|
protected $pageHasMedias; |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @ORM\OneToMany( |
91
|
|
|
* targetEntity="PiedWeb\CMSBundle\Entity\PageInterface", |
92
|
|
|
* mappedBy="mainImage" |
93
|
|
|
* ) |
94
|
|
|
*/ |
95
|
|
|
protected $mainImagePages; |
96
|
|
|
|
97
|
|
|
public function __toString() |
98
|
|
|
{ |
99
|
|
|
return $this->name.' '; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
public function setSlug($slug, $force = false) |
103
|
|
|
{ |
104
|
|
|
if (true === $force) { |
105
|
|
|
$this->slug = $slug; |
106
|
|
|
|
107
|
|
|
return $this; |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
$this->setMedia($slug); |
111
|
|
|
|
112
|
|
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
public function getSlug(): string |
116
|
|
|
{ |
117
|
|
|
if ($this->slug) { |
118
|
|
|
return $this->slug; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if ($this->media) { |
122
|
|
|
return $this->slug = preg_replace('/\\.[^.\\s]{3,4}$/', '', $this->media); |
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
$slugify = new Slugify(); |
126
|
|
|
$this->slug = $slugify->slugify($this->getName()); //Urlizer::urlize($this->getName()); |
127
|
|
|
|
128
|
|
|
return $this->slug; |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
public function setMediaFile(?File $media = null): void |
132
|
|
|
{ |
133
|
|
|
$this->mediaFile = $media; |
134
|
|
|
|
135
|
|
|
if (null !== $media) { |
136
|
|
|
$this->updatedAt = new \DateTimeImmutable(); |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
public function getMediaFile(): ?File |
141
|
|
|
{ |
142
|
|
|
return $this->mediaFile; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
public function getMedia(): ?string |
146
|
|
|
{ |
147
|
|
|
return $this->media; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
public function setMedia($media): self |
151
|
|
|
{ |
152
|
|
|
$this->media = $media; |
153
|
|
|
|
154
|
|
|
return $this; |
155
|
|
|
} |
156
|
|
|
|
157
|
3 |
|
public function getName($getLocalized = null, $onlyLocalized = false): ?string |
158
|
|
|
{ |
159
|
3 |
|
$names = $this->getNames(true); |
160
|
|
|
|
161
|
3 |
|
return $getLocalized ? |
162
|
|
|
(isset($names[$getLocalized]) ? $names[$getLocalized] : ($onlyLocalized ? null : $this->name)) |
163
|
3 |
|
: $this->name; |
164
|
|
|
} |
165
|
|
|
|
166
|
3 |
|
public function getNames($yaml = false) |
167
|
|
|
{ |
168
|
3 |
|
return true === $yaml && null !== $this->names ? Yaml::parse($this->names) : $this->names; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
public function setNames(?string $names): self |
172
|
|
|
{ |
173
|
|
|
$this->name = $names; |
174
|
|
|
|
175
|
|
|
return $this; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
public function getNameByLocale($locale) |
179
|
|
|
{ |
180
|
|
|
$names = $this->getNames(true); |
181
|
|
|
|
182
|
|
|
return $names[$locale] ?? $this->getName(); |
183
|
|
|
} |
184
|
|
|
|
185
|
3 |
|
public function setName(?string $name): self |
186
|
|
|
{ |
187
|
3 |
|
$this->name = $name; |
188
|
|
|
|
189
|
3 |
|
return $this; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
public function getRelativeDir(): ?string |
193
|
|
|
{ |
194
|
|
|
return rtrim($this->relativeDir, '/'); |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
public function setRelativeDir($relativeDir): self |
198
|
|
|
{ |
199
|
|
|
$this->relativeDir = rtrim($relativeDir, '/'); |
200
|
|
|
|
201
|
|
|
return $this; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
public function getMimeType(): ?string |
205
|
|
|
{ |
206
|
|
|
return $this->mimeType; |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
public function setMimeType($mimeType): self |
210
|
|
|
{ |
211
|
|
|
$this->mimeType = $mimeType; |
212
|
|
|
|
213
|
|
|
return $this; |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
public function getSize() |
217
|
|
|
{ |
218
|
|
|
return $this->size; |
219
|
|
|
} |
220
|
|
|
|
221
|
|
|
public function setSize($size): self |
222
|
|
|
{ |
223
|
|
|
$this->size = $size; |
224
|
|
|
|
225
|
|
|
return $this; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
public function setDimensions($dimensions): self |
229
|
|
|
{ |
230
|
|
|
if (isset($dimensions[0])) { |
231
|
|
|
$this->width = (int) $dimensions[0]; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
if (isset($dimensions[1])) { |
235
|
|
|
$this->height = (int) $dimensions[1]; |
236
|
|
|
} |
237
|
|
|
|
238
|
|
|
return $this; |
239
|
|
|
} |
240
|
|
|
|
241
|
|
|
public function getDimensions(): array |
242
|
|
|
{ |
243
|
|
|
return [$this->width, $this->height]; |
244
|
|
|
} |
245
|
|
|
|
246
|
|
|
public function getRatio(): float |
247
|
|
|
{ |
248
|
|
|
return $this->height / $this->width; |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
public function getWidth() |
252
|
|
|
{ |
253
|
|
|
return $this->width; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
public function getHeight() |
257
|
|
|
{ |
258
|
|
|
return $this->height; |
259
|
|
|
} |
260
|
|
|
|
261
|
|
|
public function getMainColor(): ?string |
262
|
|
|
{ |
263
|
|
|
return $this->mainColor; |
264
|
|
|
} |
265
|
|
|
|
266
|
|
|
public function getMainColorOpposite() |
267
|
|
|
{ |
268
|
|
|
if (null === $this->getMainColor()) { |
269
|
|
|
return; |
270
|
|
|
} |
271
|
|
|
|
272
|
|
|
return Color::fromHex($this->getMainColor())->invert(true); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
public function setMainColor(?string $mainColor): self |
276
|
|
|
{ |
277
|
|
|
$this->mainColor = $mainColor; |
278
|
|
|
|
279
|
|
|
return $this; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
public function setPageHasMedias($pageHasMedias) |
283
|
|
|
{ |
284
|
|
|
$this->pageHasMedias = new ArrayCollection(); |
285
|
|
|
foreach ($pageHasMedias as $pageHasMedia) { |
286
|
|
|
$this->addPageHasMedia($pageHasMedia); |
287
|
|
|
} |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
public function getPageHasMedias() |
291
|
|
|
{ |
292
|
|
|
return $this->pageHasMedias; |
293
|
|
|
} |
294
|
|
|
|
295
|
|
|
public function addPageHasMedia(PageHasMedia $pageHasMedia) |
296
|
|
|
{ |
297
|
|
|
$pageHasMedia->setMedia($this); |
298
|
|
|
$this->pageHasMedias[] = $pageHasMedia; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
public function removePageHasMedia(PageHasMedia $pageHasMedia) |
302
|
|
|
{ |
303
|
|
|
$this->pageHasMedias->removeElement($pageHasMedia); |
304
|
|
|
} |
305
|
|
|
|
306
|
|
|
public function getMainImagePages() |
307
|
|
|
{ |
308
|
|
|
return $this->mainImagePages; |
309
|
|
|
} |
310
|
|
|
|
311
|
|
|
/** |
312
|
|
|
* @ORM\PreRemove |
313
|
|
|
*/ |
314
|
|
|
public function removeMainImageFromPages() |
315
|
|
|
{ |
316
|
|
|
foreach ($this->mainImagePages as $page) { |
317
|
|
|
$page->setMainImage(null); |
318
|
|
|
} |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
public function getPath(): ?string |
322
|
|
|
{ |
323
|
|
|
return $this->getFullPath(); |
324
|
|
|
} |
325
|
|
|
|
326
|
|
|
public function getFullPath(): ?string |
327
|
|
|
{ |
328
|
|
|
return null !== $this->media |
329
|
|
|
? '/'.$this->getRelativeDir().($this->getMedia() ? '/'.$this->getMedia() : '') |
330
|
|
|
: null; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
public function getFullPathWebP(): ?string |
334
|
|
|
{ |
335
|
|
|
return null !== $this->media |
336
|
|
|
? '/'.$this->getRelativeDir().($this->getSlug() ? '/'.$this->getSlug().'.webp' : '') |
337
|
|
|
: null; |
338
|
|
|
} |
339
|
|
|
} |
340
|
|
|
|