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
|
|
|
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 used only for media renaming |
54
|
|
|
* |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
protected $mediaBeforeUpdate; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* NOTE: This is not a mapped field of entity metadata, just a simple property. |
61
|
|
|
* |
62
|
|
|
* @Vich\UploadableField( |
63
|
|
|
* mapping="media_media", |
64
|
|
|
* fileNameProperty="slug", |
65
|
|
|
* mimeType="mimeType", |
66
|
|
|
* size="size", |
67
|
|
|
* dimensions="dimensions" |
68
|
|
|
* ) |
69
|
|
|
* |
70
|
|
|
* @var File |
71
|
|
|
*/ |
72
|
|
|
protected $mediaFile; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @ORM\Column(type="string", length=100, unique=true) |
76
|
|
|
*/ |
77
|
|
|
protected $name; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @ORM\Column(type="text", nullable=true) |
81
|
|
|
*/ |
82
|
|
|
protected $names; |
83
|
|
|
|
84
|
|
|
protected $slug; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @ORM\OneToMany( |
88
|
|
|
* targetEntity="PiedWeb\CMSBundle\Entity\PageHasMediaInterface", |
89
|
|
|
* mappedBy="media", |
90
|
|
|
* cascade={"all"}, |
91
|
|
|
* orphanRemoval=true |
92
|
|
|
* ) |
93
|
|
|
*/ |
94
|
|
|
protected $pageHasMedias; |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @ORM\OneToMany( |
98
|
|
|
* targetEntity="PiedWeb\CMSBundle\Entity\PageInterface", |
99
|
|
|
* mappedBy="mainImage" |
100
|
|
|
* ) |
101
|
|
|
*/ |
102
|
|
|
protected $mainImagePages; |
103
|
|
|
|
104
|
|
|
public function __toString() |
105
|
|
|
{ |
106
|
|
|
return $this->name.' '; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
public function setSlug($slug, $force = false) |
110
|
|
|
{ |
111
|
|
|
if (!$slug) |
112
|
|
|
return $this; |
113
|
|
|
|
114
|
|
|
|
115
|
|
|
$slug = (new Slugify())->slugify($slug); |
116
|
|
|
|
117
|
|
|
if (true === $force) { |
118
|
|
|
$this->slug = $slug; |
119
|
|
|
|
120
|
|
|
return $this; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
$this->setMedia($slug.($this->media ? preg_replace('/.*(\\.[^.\\s]{3,4})$/', '$1', $this->media) : '')); |
124
|
|
|
|
125
|
|
|
return $this; |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
public function getSlug(): string |
129
|
|
|
{ |
130
|
|
|
if ($this->slug) { |
131
|
|
|
return $this->slug; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
if ($this->media) { |
135
|
|
|
return $this->slug = preg_replace('/\\.[^.\\s]{3,4}$/', '', $this->media); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$this->slug = (new Slugify())->slugify($this->getName()); //Urlizer::urlize($this->getName()); |
139
|
|
|
|
140
|
|
|
return $this->slug; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
public function setMediaFile(?File $media = null): void |
144
|
|
|
{ |
145
|
|
|
$this->mediaFile = $media; |
146
|
|
|
|
147
|
|
|
if (null !== $media) { |
148
|
|
|
$this->updatedAt = new \DateTimeImmutable(); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
public function getMediaFile(): ?File |
153
|
|
|
{ |
154
|
|
|
return $this->mediaFile; |
155
|
|
|
} |
156
|
|
|
|
157
|
3 |
|
public function getMedia(): ?string |
158
|
|
|
{ |
159
|
3 |
|
return $this->media; |
160
|
|
|
} |
161
|
3 |
|
|
162
|
|
|
public function setMedia($media): self |
163
|
3 |
|
{ |
164
|
|
|
if (!$media) |
165
|
|
|
return $this; |
166
|
3 |
|
|
167
|
|
|
if ($this->media!==null) { |
168
|
3 |
|
$this->setMediaBeforeUpdate($this->media); |
169
|
|
|
// TODO must rename media (via service ?!) var_dump($this->media); exit; |
170
|
|
|
} |
171
|
|
|
$this->media = $media; |
172
|
|
|
|
173
|
|
|
return $this; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
public function getName($getLocalized = null, $onlyLocalized = false): ?string |
177
|
|
|
{ |
178
|
|
|
$names = $this->getNames(true); |
179
|
|
|
|
180
|
|
|
return $getLocalized ? |
181
|
|
|
(isset($names[$getLocalized]) ? $names[$getLocalized] : ($onlyLocalized ? null : $this->name)) |
182
|
|
|
: $this->name; |
183
|
|
|
} |
184
|
|
|
|
185
|
3 |
|
public function getNames($yaml = false) |
186
|
|
|
{ |
187
|
3 |
|
return true === $yaml && null !== $this->names ? Yaml::parse($this->names) : $this->names; |
188
|
|
|
} |
189
|
3 |
|
|
190
|
|
|
public function setNames(?string $names): self |
191
|
|
|
{ |
192
|
|
|
$this->name = $names; |
193
|
|
|
|
194
|
|
|
return $this; |
195
|
|
|
} |
196
|
|
|
|
197
|
|
|
public function getNameByLocale($locale) |
198
|
|
|
{ |
199
|
|
|
$names = $this->getNames(true); |
200
|
|
|
|
201
|
|
|
return $names[$locale] ?? $this->getName(); |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
public function setName(?string $name): self |
205
|
|
|
{ |
206
|
|
|
$this->name = $name; |
207
|
|
|
|
208
|
|
|
return $this; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
public function getRelativeDir(): ?string |
212
|
|
|
{ |
213
|
|
|
return rtrim($this->relativeDir, '/'); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
public function setRelativeDir($relativeDir): self |
217
|
|
|
{ |
218
|
|
|
$this->relativeDir = rtrim($relativeDir, '/'); |
219
|
|
|
|
220
|
|
|
return $this; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
public function getMimeType(): ?string |
224
|
|
|
{ |
225
|
|
|
return $this->mimeType; |
226
|
|
|
} |
227
|
|
|
|
228
|
|
|
public function setMimeType($mimeType): self |
229
|
|
|
{ |
230
|
|
|
$this->mimeType = $mimeType; |
231
|
|
|
|
232
|
|
|
return $this; |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
public function getSize() |
236
|
|
|
{ |
237
|
|
|
return $this->size; |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
public function setSize($size): self |
241
|
|
|
{ |
242
|
|
|
$this->size = $size; |
243
|
|
|
|
244
|
|
|
return $this; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
public function setDimensions($dimensions): self |
248
|
|
|
{ |
249
|
|
|
if (isset($dimensions[0])) { |
250
|
|
|
$this->width = (int) $dimensions[0]; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
if (isset($dimensions[1])) { |
254
|
|
|
$this->height = (int) $dimensions[1]; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
return $this; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
public function getDimensions(): array |
261
|
|
|
{ |
262
|
|
|
return [$this->width, $this->height]; |
263
|
|
|
} |
264
|
|
|
|
265
|
|
|
public function getRatio(): float |
266
|
|
|
{ |
267
|
|
|
return $this->height / $this->width; |
268
|
|
|
} |
269
|
|
|
|
270
|
|
|
public function getWidth() |
271
|
|
|
{ |
272
|
|
|
return $this->width; |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
public function getHeight() |
276
|
|
|
{ |
277
|
|
|
return $this->height; |
278
|
|
|
} |
279
|
|
|
|
280
|
|
|
public function getMainColor(): ?string |
281
|
|
|
{ |
282
|
|
|
return $this->mainColor; |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
public function getMainColorOpposite() |
286
|
|
|
{ |
287
|
|
|
if (null === $this->getMainColor()) { |
288
|
|
|
return; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
return Color::fromHex($this->getMainColor())->invert(true); |
292
|
|
|
} |
293
|
|
|
|
294
|
|
|
public function setMainColor(?string $mainColor): self |
295
|
|
|
{ |
296
|
|
|
$this->mainColor = $mainColor; |
297
|
|
|
|
298
|
|
|
return $this; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
public function setPageHasMedias($pageHasMedias) |
302
|
|
|
{ |
303
|
|
|
$this->pageHasMedias = new ArrayCollection(); |
304
|
|
|
foreach ($pageHasMedias as $pageHasMedia) { |
305
|
|
|
$this->addPageHasMedia($pageHasMedia); |
306
|
|
|
} |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
public function getPageHasMedias() |
310
|
|
|
{ |
311
|
|
|
return $this->pageHasMedias; |
312
|
|
|
} |
313
|
|
|
|
314
|
|
|
public function addPageHasMedia(PageHasMedia $pageHasMedia) |
315
|
|
|
{ |
316
|
|
|
$pageHasMedia->setMedia($this); |
317
|
|
|
$this->pageHasMedias[] = $pageHasMedia; |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
public function removePageHasMedia(PageHasMedia $pageHasMedia) |
321
|
|
|
{ |
322
|
|
|
$this->pageHasMedias->removeElement($pageHasMedia); |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
public function getMainImagePages() |
326
|
|
|
{ |
327
|
|
|
return $this->mainImagePages; |
328
|
|
|
} |
329
|
|
|
|
330
|
|
|
/** |
331
|
|
|
* @ORM\PreRemove |
332
|
|
|
*/ |
333
|
|
|
public function removeMainImageFromPages() |
334
|
|
|
{ |
335
|
|
|
foreach ($this->mainImagePages as $page) { |
336
|
|
|
$page->setMainImage(null); |
337
|
|
|
} |
338
|
|
|
} |
339
|
|
|
|
340
|
|
|
public function getPath(): ?string |
341
|
|
|
{ |
342
|
|
|
return $this->getFullPath(); |
343
|
|
|
} |
344
|
|
|
|
345
|
|
|
public function getFullPath(): ?string |
346
|
|
|
{ |
347
|
|
|
return null !== $this->media |
348
|
|
|
? '/'.$this->getRelativeDir().($this->getMedia() ? '/'.$this->getMedia() : '') |
349
|
|
|
: null; |
350
|
|
|
} |
351
|
|
|
|
352
|
|
|
public function getFullPathWebP(): ?string |
353
|
|
|
{ |
354
|
|
|
return null !== $this->media |
355
|
|
|
? '/'.$this->getRelativeDir().($this->getSlug() ? '/'.$this->getSlug().'.webp' : '') |
356
|
|
|
: null; |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
/** |
360
|
|
|
* Get nOTE : this is used only for media renaming |
361
|
|
|
* |
362
|
|
|
* @return string |
363
|
|
|
*/ |
364
|
|
|
public function getMediaBeforeUpdate() |
365
|
|
|
{ |
366
|
|
|
return $this->mediaBeforeUpdate; |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
/** |
370
|
|
|
* Set nOTE : this is used only for media renaming |
371
|
|
|
* |
372
|
|
|
* @param string $mediaBeforeUpdate NOTE : this is used only for media renaming |
373
|
|
|
* |
374
|
|
|
* @return self |
375
|
|
|
*/ |
376
|
|
|
public function setMediaBeforeUpdate(string $mediaBeforeUpdate) |
377
|
|
|
{ |
378
|
|
|
$this->mediaBeforeUpdate = $mediaBeforeUpdate; |
379
|
|
|
|
380
|
|
|
return $this; |
381
|
|
|
} |
382
|
|
|
} |
383
|
|
|
|