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\Validator\Constraints as Assert; |
11
|
|
|
use Symfony\Component\Validator\Context\ExecutionContextInterface; |
12
|
|
|
use Symfony\Component\Yaml\Yaml; |
13
|
|
|
use Vich\UploaderBundle\Mapping\Annotation as Vich; |
14
|
|
|
|
15
|
|
|
trait MediaTrait |
16
|
|
|
{ |
17
|
|
|
use TimestampableTrait; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @ORM\Column(type="string", length=50) |
21
|
|
|
*/ |
22
|
|
|
protected $mimeType; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @ORM\Column(type="string", length=255) |
26
|
|
|
*/ |
27
|
|
|
protected $relativeDir; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @ORM\Column(type="integer") |
31
|
|
|
*/ |
32
|
|
|
protected $size; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @ORM\Column(type="integer", nullable=true) |
36
|
|
|
*/ |
37
|
|
|
protected $height; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @ORM\Column(type="integer", nullable=true) |
41
|
|
|
*/ |
42
|
|
|
protected $width; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @ORM\Column(type="string", nullable=true) |
46
|
|
|
*/ |
47
|
|
|
protected $mainColor; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @ORM\Column(type="string", length=255) |
51
|
|
|
*/ |
52
|
|
|
protected $media; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* NOTE : this is used only for media renaming. |
56
|
|
|
* |
57
|
|
|
* @var string |
58
|
|
|
*/ |
59
|
|
|
protected $mediaBeforeUpdate; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* NOTE: This is not a mapped field of entity metadata, just a simple property. |
63
|
|
|
* |
64
|
|
|
* @Vich\UploadableField( |
65
|
|
|
* mapping="media_media", |
66
|
|
|
* fileNameProperty="slug", |
67
|
|
|
* mimeType="mimeType", |
68
|
|
|
* size="size", |
69
|
|
|
* dimensions="dimensions" |
70
|
|
|
* ) |
71
|
|
|
* |
72
|
|
|
* @var File |
73
|
|
|
*/ |
74
|
|
|
protected $mediaFile; |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @ORM\Column(type="string", length=100, unique=true) |
78
|
|
|
*/ |
79
|
|
|
protected $name; |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @ORM\Column(type="text", nullable=true) |
83
|
|
|
*/ |
84
|
|
|
protected $names; |
85
|
|
|
|
86
|
|
|
protected $slug; |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @ORM\OneToMany( |
90
|
|
|
* targetEntity="PiedWeb\CMSBundle\Entity\PageHasMediaInterface", |
91
|
|
|
* mappedBy="media", |
92
|
|
|
* cascade={"all"}, |
93
|
|
|
* orphanRemoval=true |
94
|
|
|
* ) |
95
|
|
|
*/ |
96
|
|
|
protected $pageHasMedias; |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* @ORM\OneToMany( |
100
|
|
|
* targetEntity="PiedWeb\CMSBundle\Entity\PageInterface", |
101
|
|
|
* mappedBy="mainImage" |
102
|
|
|
* ) |
103
|
|
|
*/ |
104
|
|
|
protected $mainImagePages; |
105
|
|
|
|
106
|
|
|
public function __toString() |
107
|
|
|
{ |
108
|
|
|
return $this->name.' '; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
protected function getExtension($string) |
112
|
|
|
{ |
113
|
|
|
return preg_replace('/.*(\\.[^.\\s]{3,4})$/', '$1', $string); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
protected function slugifyPreservingExtension($string) |
117
|
|
|
{ |
118
|
|
|
$extension = $this->getExtension($string); |
119
|
|
|
$stringSlugify = (new Slugify())->slugify($this->removeExtension($string)); |
120
|
|
|
|
121
|
|
|
return $stringSlugify.$extension; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
public function getSlugForce() |
125
|
|
|
{ |
126
|
|
|
return $this->slug; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Used by MediaAdmin. |
131
|
|
|
*/ |
132
|
|
|
public function setSlugForce($slug) |
133
|
|
|
{ |
134
|
|
|
if (!$slug) { |
135
|
|
|
return $this; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$this->slug = (new Slugify(['regexp' => '/([^A-Za-z0-9\.]|-)+/']))->slugify($slug); |
139
|
|
|
|
140
|
|
|
if ($this->media) { |
141
|
|
|
$this->setMedia($this->slug.$this->getExtension($this->media)); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $this; |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Used by VichUploader. |
149
|
|
|
*/ |
150
|
|
|
public function setSlug($slug) |
151
|
|
|
{ |
152
|
|
|
if (!$slug) { |
153
|
|
|
return $this; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
$slugSlugify = $this->slugifyPreservingExtension($slug); |
157
|
3 |
|
|
158
|
|
|
if ($this->getExtension($this->media) != $this->getExtension($slugSlugify)) { // 1; |
159
|
3 |
|
// TODO manage problem URL move... quick validation impossible before file upload, and impossible after from |
160
|
|
|
// here |
161
|
3 |
|
|
162
|
|
|
//$this->media ? $this->removeExtension($slugSlugify).$this->getExtension($this->media) : |
163
|
3 |
|
$this->setMedia($slugSlugify); |
164
|
|
|
} |
165
|
|
|
|
166
|
3 |
|
return $this; |
167
|
|
|
} |
168
|
3 |
|
|
169
|
|
|
/** |
170
|
|
|
* @Assert\Callback |
171
|
|
|
*/ |
172
|
|
|
public function validate(ExecutionContextInterface $context, $payload) |
|
|
|
|
173
|
|
|
{ |
174
|
|
|
if ($this->getMimeType() && $this->mediaFile && $this->mediaFile->getMimeType() != $this->getMimeType()) { |
175
|
|
|
$context |
176
|
|
|
->buildViolation('Attention ! Vous essayez de remplacer un fichier d\'un type (' |
177
|
|
|
.$this->getMimeType().') par un fichier d\'une autre type ('.$this->mediaFile->getMimeType().')') |
178
|
|
|
->atPath('fileName') |
179
|
|
|
->addViolation() |
180
|
|
|
; |
181
|
|
|
} |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
protected function removeExtension($string) |
185
|
3 |
|
{ |
186
|
|
|
return preg_replace('/\\.[^.\\s]{3,4}$/', '', $string); |
187
|
3 |
|
} |
188
|
|
|
|
189
|
3 |
|
public function getSlug(): string |
190
|
|
|
{ |
191
|
|
|
if ($this->slug) { |
192
|
|
|
return $this->slug; |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
if ($this->media) { |
196
|
|
|
return $this->slug = $this->removeExtension($this->media); |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
$this->slug = (new Slugify())->slugify($this->getName()); //Urlizer::urlize($this->getName()); |
200
|
|
|
|
201
|
|
|
return $this->slug; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
public function setMediaFile(?File $media = null): void |
205
|
|
|
{ |
206
|
|
|
$this->mediaFile = $media; |
207
|
|
|
|
208
|
|
|
if (null !== $media) { |
209
|
|
|
$this->updatedAt = new \DateTimeImmutable(); |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|
213
|
|
|
public function getMediaFile(): ?File |
214
|
|
|
{ |
215
|
|
|
return $this->mediaFile; |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
public function getMedia(): ?string |
219
|
|
|
{ |
220
|
|
|
return $this->media; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
public function setMedia($media): self |
224
|
|
|
{ |
225
|
|
|
if (!$media) { |
226
|
|
|
return $this; |
227
|
|
|
} |
228
|
|
|
|
229
|
|
|
if (null !== $this->media) { |
230
|
|
|
$this->setMediaBeforeUpdate($this->media); |
231
|
|
|
//$this->media = $this->removeExtension($media).$this->getExtension($this->media); |
232
|
|
|
} //else |
233
|
|
|
|
234
|
|
|
$this->media = $media; |
235
|
|
|
|
236
|
|
|
return $this; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
public function getName($getLocalized = null, $onlyLocalized = false): ?string |
240
|
|
|
{ |
241
|
|
|
$names = $this->getNames(true); |
242
|
|
|
|
243
|
|
|
return $getLocalized ? |
244
|
|
|
(isset($names[$getLocalized]) ? $names[$getLocalized] : ($onlyLocalized ? null : $this->name)) |
245
|
|
|
: $this->name; |
246
|
|
|
} |
247
|
|
|
|
248
|
|
|
public function getNames($yaml = false) |
249
|
|
|
{ |
250
|
|
|
return true === $yaml && null !== $this->names ? Yaml::parse($this->names) : $this->names; |
251
|
|
|
} |
252
|
|
|
|
253
|
|
|
public function setNames(?string $names): self |
254
|
|
|
{ |
255
|
|
|
$this->name = $names; |
256
|
|
|
|
257
|
|
|
return $this; |
258
|
|
|
} |
259
|
|
|
|
260
|
|
|
public function getNameByLocale($locale) |
261
|
|
|
{ |
262
|
|
|
$names = $this->getNames(true); |
263
|
|
|
|
264
|
|
|
return $names[$locale] ?? $this->getName(); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
public function setName(?string $name): self |
268
|
|
|
{ |
269
|
|
|
$this->name = $name; |
270
|
|
|
|
271
|
|
|
return $this; |
272
|
|
|
} |
273
|
|
|
|
274
|
|
|
public function getRelativeDir(): ?string |
275
|
|
|
{ |
276
|
|
|
return rtrim($this->relativeDir, '/'); |
277
|
|
|
} |
278
|
|
|
|
279
|
|
|
public function setRelativeDir($relativeDir): self |
280
|
|
|
{ |
281
|
|
|
$this->relativeDir = rtrim($relativeDir, '/'); |
282
|
|
|
|
283
|
|
|
return $this; |
284
|
|
|
} |
285
|
|
|
|
286
|
|
|
public function getMimeType(): ?string |
287
|
|
|
{ |
288
|
|
|
return $this->mimeType; |
289
|
|
|
} |
290
|
|
|
|
291
|
|
|
protected function getPreviousMimeType() |
292
|
|
|
{ |
293
|
|
|
return $this->previousMimeType ?? null; |
294
|
|
|
} |
295
|
|
|
|
296
|
|
|
public function setMimeType($mimeType): self |
297
|
|
|
{ |
298
|
|
|
$this->mimeType = $mimeType; |
299
|
|
|
|
300
|
|
|
return $this; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
public function getSize() |
304
|
|
|
{ |
305
|
|
|
return $this->size; |
306
|
|
|
} |
307
|
|
|
|
308
|
|
|
public function setSize($size): self |
309
|
|
|
{ |
310
|
|
|
$this->size = $size; |
311
|
|
|
|
312
|
|
|
return $this; |
313
|
|
|
} |
314
|
|
|
|
315
|
|
|
public function setDimensions($dimensions): self |
316
|
|
|
{ |
317
|
|
|
if (isset($dimensions[0])) { |
318
|
|
|
$this->width = (int) $dimensions[0]; |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
if (isset($dimensions[1])) { |
322
|
|
|
$this->height = (int) $dimensions[1]; |
323
|
|
|
} |
324
|
|
|
|
325
|
|
|
return $this; |
326
|
|
|
} |
327
|
|
|
|
328
|
|
|
public function getDimensions(): array |
329
|
|
|
{ |
330
|
|
|
return [$this->width, $this->height]; |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
public function getRatio(): float |
334
|
|
|
{ |
335
|
|
|
return $this->height / $this->width; |
336
|
|
|
} |
337
|
|
|
|
338
|
|
|
public function getWidth() |
339
|
|
|
{ |
340
|
|
|
return $this->width; |
341
|
|
|
} |
342
|
|
|
|
343
|
|
|
public function getHeight() |
344
|
|
|
{ |
345
|
|
|
return $this->height; |
346
|
|
|
} |
347
|
|
|
|
348
|
|
|
public function getMainColor(): ?string |
349
|
|
|
{ |
350
|
|
|
return $this->mainColor; |
351
|
|
|
} |
352
|
|
|
|
353
|
|
|
public function getMainColorOpposite() |
354
|
|
|
{ |
355
|
|
|
if (null === $this->getMainColor()) { |
356
|
|
|
return; |
357
|
|
|
} |
358
|
|
|
|
359
|
|
|
return Color::fromHex($this->getMainColor())->invert(true); |
360
|
|
|
} |
361
|
|
|
|
362
|
|
|
public function setMainColor(?string $mainColor): self |
363
|
|
|
{ |
364
|
|
|
$this->mainColor = $mainColor; |
365
|
|
|
|
366
|
|
|
return $this; |
367
|
|
|
} |
368
|
|
|
|
369
|
|
|
public function setPageHasMedias($pageHasMedias) |
370
|
|
|
{ |
371
|
|
|
$this->pageHasMedias = new ArrayCollection(); |
372
|
|
|
foreach ($pageHasMedias as $pageHasMedia) { |
373
|
|
|
$this->addPageHasMedia($pageHasMedia); |
374
|
|
|
} |
375
|
|
|
} |
376
|
|
|
|
377
|
|
|
public function getPageHasMedias() |
378
|
|
|
{ |
379
|
|
|
return $this->pageHasMedias; |
380
|
|
|
} |
381
|
|
|
|
382
|
|
|
public function addPageHasMedia(PageHasMedia $pageHasMedia) |
383
|
|
|
{ |
384
|
|
|
$pageHasMedia->setMedia($this); |
385
|
|
|
$this->pageHasMedias[] = $pageHasMedia; |
386
|
|
|
} |
387
|
|
|
|
388
|
|
|
public function removePageHasMedia(PageHasMedia $pageHasMedia) |
389
|
|
|
{ |
390
|
|
|
$this->pageHasMedias->removeElement($pageHasMedia); |
391
|
|
|
} |
392
|
|
|
|
393
|
|
|
public function getMainImagePages() |
394
|
|
|
{ |
395
|
|
|
return $this->mainImagePages; |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
/** |
399
|
|
|
* @ORM\PreRemove |
400
|
|
|
*/ |
401
|
|
|
public function removeMainImageFromPages() |
402
|
|
|
{ |
403
|
|
|
foreach ($this->mainImagePages as $page) { |
404
|
|
|
$page->setMainImage(null); |
405
|
|
|
} |
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
public function getPath(): ?string |
409
|
|
|
{ |
410
|
|
|
return $this->getFullPath(); |
411
|
|
|
} |
412
|
|
|
|
413
|
|
|
public function getFullPath(): ?string |
414
|
|
|
{ |
415
|
|
|
return null !== $this->media |
416
|
|
|
? '/'.$this->getRelativeDir().($this->getMedia() ? '/'.$this->getMedia() : '') |
417
|
|
|
: null; |
418
|
|
|
} |
419
|
|
|
|
420
|
|
|
public function getFullPathWebP(): ?string |
421
|
|
|
{ |
422
|
|
|
return null !== $this->media |
423
|
|
|
? '/'.$this->getRelativeDir().($this->getSlug() ? '/'.$this->getSlug().'.webp' : '') |
424
|
|
|
: null; |
425
|
|
|
} |
426
|
|
|
|
427
|
|
|
/** |
428
|
|
|
* Get nOTE : this is used only for media renaming. |
429
|
|
|
* |
430
|
|
|
* @return string |
431
|
|
|
*/ |
432
|
|
|
public function getMediaBeforeUpdate() |
433
|
|
|
{ |
434
|
|
|
return $this->mediaBeforeUpdate; |
435
|
|
|
} |
436
|
|
|
|
437
|
|
|
/** |
438
|
|
|
* Set nOTE : this is used only for media renaming. |
439
|
|
|
* |
440
|
|
|
* @param string $mediaBeforeUpdate NOTE : this is used only for media renaming |
441
|
|
|
* |
442
|
|
|
* @return self |
443
|
|
|
*/ |
444
|
|
|
public function setMediaBeforeUpdate(string $mediaBeforeUpdate) |
445
|
|
|
{ |
446
|
|
|
$this->mediaBeforeUpdate = $mediaBeforeUpdate; |
447
|
|
|
|
448
|
|
|
return $this; |
449
|
|
|
} |
450
|
|
|
} |
451
|
|
|
|
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.