PiedWeb /
CMS
| 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 | * Useed in twig AppExtension transformInlineImageToMedia. |
||
| 149 | */ |
||
| 150 | public static function loadFromSrc($src) |
||
| 151 | { |
||
| 152 | $src = substr($src, \strlen('/media/default/')); |
||
| 153 | |||
| 154 | $media = new self(); |
||
| 155 | $media->setRelativeDir('/media'); |
||
| 156 | $media->setMedia($src); |
||
| 157 | 3 | $media->slug = $media->removeExtension($src); |
|
| 158 | |||
| 159 | 3 | return $media; |
|
| 160 | } |
||
| 161 | 3 | ||
| 162 | /** |
||
| 163 | 3 | * Used by VichUploader. |
|
| 164 | */ |
||
| 165 | public function setSlug($slug) |
||
| 166 | 3 | { |
|
| 167 | if (! $slug) { |
||
| 168 | 3 | return $this; |
|
| 169 | } |
||
| 170 | |||
| 171 | $slugSlugify = $this->slugifyPreservingExtension($slug); |
||
| 172 | |||
| 173 | if ($this->getExtension($this->media) != $this->getExtension($slugSlugify)) { // 1; |
||
| 174 | // TODO manage problem URL move... quick validation impossible before file upload, and impossible after from |
||
| 175 | // here |
||
| 176 | |||
| 177 | //$this->media ? $this->removeExtension($slugSlugify).$this->getExtension($this->media) : |
||
| 178 | $this->setMedia($slugSlugify); |
||
| 179 | } |
||
| 180 | |||
| 181 | return $this; |
||
| 182 | } |
||
| 183 | |||
| 184 | /** |
||
| 185 | 3 | * @Assert\Callback |
|
| 186 | */ |
||
| 187 | 3 | public function validate(ExecutionContextInterface $context, $payload) |
|
|
0 ignored issues
–
show
|
|||
| 188 | { |
||
| 189 | 3 | if ($this->getMimeType() && $this->mediaFile && $this->mediaFile->getMimeType() != $this->getMimeType()) { |
|
| 190 | $context |
||
| 191 | ->buildViolation('Attention ! Vous essayez de remplacer un fichier d\'un type (' |
||
| 192 | .$this->getMimeType().') par un fichier d\'une autre type ('.$this->mediaFile->getMimeType().')') |
||
| 193 | ->atPath('fileName') |
||
| 194 | ->addViolation() |
||
| 195 | ; |
||
| 196 | } |
||
| 197 | } |
||
| 198 | |||
| 199 | protected function removeExtension($string) |
||
| 200 | { |
||
| 201 | return preg_replace('/\\.[^.\\s]{3,4}$/', '', $string); |
||
| 202 | } |
||
| 203 | |||
| 204 | public function getSlug(): string |
||
| 205 | { |
||
| 206 | if ($this->slug) { |
||
| 207 | return $this->slug; |
||
| 208 | } |
||
| 209 | |||
| 210 | if ($this->media) { |
||
| 211 | return $this->slug = $this->removeExtension($this->media); |
||
| 212 | } |
||
| 213 | |||
| 214 | $this->slug = (new Slugify())->slugify($this->getName()); //Urlizer::urlize($this->getName()); |
||
| 215 | |||
| 216 | return $this->slug; |
||
| 217 | } |
||
| 218 | |||
| 219 | public function setMediaFile(?File $media = null): void |
||
| 220 | { |
||
| 221 | $this->mediaFile = $media; |
||
| 222 | |||
| 223 | if (null !== $media) { |
||
| 224 | $this->updatedAt = new \DateTimeImmutable(); |
||
| 225 | } |
||
| 226 | } |
||
| 227 | |||
| 228 | public function getMediaFile(): ?File |
||
| 229 | { |
||
| 230 | return $this->mediaFile; |
||
| 231 | } |
||
| 232 | |||
| 233 | public function getMedia(): ?string |
||
| 234 | { |
||
| 235 | return $this->media; |
||
| 236 | } |
||
| 237 | |||
| 238 | public function setMedia($media): self |
||
| 239 | { |
||
| 240 | if (! $media) { |
||
| 241 | return $this; |
||
| 242 | } |
||
| 243 | |||
| 244 | if (null !== $this->media) { |
||
| 245 | $this->setMediaBeforeUpdate($this->media); |
||
| 246 | //$this->media = $this->removeExtension($media).$this->getExtension($this->media); |
||
| 247 | } //else |
||
| 248 | |||
| 249 | $this->media = $media; |
||
| 250 | |||
| 251 | return $this; |
||
| 252 | } |
||
| 253 | |||
| 254 | public function getName($getLocalized = null, $onlyLocalized = false): ?string |
||
| 255 | { |
||
| 256 | $names = $this->getNames(true); |
||
| 257 | |||
| 258 | return $getLocalized ? |
||
| 259 | (isset($names[$getLocalized]) ? $names[$getLocalized] : ($onlyLocalized ? null : $this->name)) |
||
| 260 | : $this->name; |
||
| 261 | } |
||
| 262 | |||
| 263 | public function getNames($yaml = false) |
||
| 264 | { |
||
| 265 | return true === $yaml && null !== $this->names ? Yaml::parse($this->names) : $this->names; |
||
| 266 | } |
||
| 267 | |||
| 268 | public function setNames(?string $names): self |
||
| 269 | { |
||
| 270 | $this->name = $names; |
||
| 271 | |||
| 272 | return $this; |
||
| 273 | } |
||
| 274 | |||
| 275 | public function getNameByLocale($locale) |
||
| 276 | { |
||
| 277 | $names = $this->getNames(true); |
||
| 278 | |||
| 279 | return $names[$locale] ?? $this->getName(); |
||
| 280 | } |
||
| 281 | |||
| 282 | public function setName(?string $name): self |
||
| 283 | { |
||
| 284 | $this->name = $name; |
||
| 285 | |||
| 286 | return $this; |
||
| 287 | } |
||
| 288 | |||
| 289 | public function getRelativeDir(): ?string |
||
| 290 | { |
||
| 291 | return rtrim($this->relativeDir, '/'); |
||
| 292 | } |
||
| 293 | |||
| 294 | public function setRelativeDir($relativeDir): self |
||
| 295 | { |
||
| 296 | $this->relativeDir = rtrim($relativeDir, '/'); |
||
| 297 | |||
| 298 | return $this; |
||
| 299 | } |
||
| 300 | |||
| 301 | public function getMimeType(): ?string |
||
| 302 | { |
||
| 303 | return $this->mimeType; |
||
| 304 | } |
||
| 305 | |||
| 306 | protected function getPreviousMimeType() |
||
| 307 | { |
||
| 308 | return $this->previousMimeType ?? null; |
||
| 309 | } |
||
| 310 | |||
| 311 | public function setMimeType($mimeType): self |
||
| 312 | { |
||
| 313 | $this->mimeType = $mimeType; |
||
| 314 | |||
| 315 | return $this; |
||
| 316 | } |
||
| 317 | |||
| 318 | public function getSize() |
||
| 319 | { |
||
| 320 | return $this->size; |
||
| 321 | } |
||
| 322 | |||
| 323 | public function setSize($size): self |
||
| 324 | { |
||
| 325 | $this->size = $size; |
||
| 326 | |||
| 327 | return $this; |
||
| 328 | } |
||
| 329 | |||
| 330 | public function setDimensions($dimensions): self |
||
| 331 | { |
||
| 332 | if (isset($dimensions[0])) { |
||
| 333 | $this->width = (int) $dimensions[0]; |
||
| 334 | } |
||
| 335 | |||
| 336 | if (isset($dimensions[1])) { |
||
| 337 | $this->height = (int) $dimensions[1]; |
||
| 338 | } |
||
| 339 | |||
| 340 | return $this; |
||
| 341 | } |
||
| 342 | |||
| 343 | public function getDimensions(): array |
||
| 344 | { |
||
| 345 | return [$this->width, $this->height]; |
||
| 346 | } |
||
| 347 | |||
| 348 | public function getRatio(): float |
||
| 349 | { |
||
| 350 | return $this->height / $this->width; |
||
| 351 | } |
||
| 352 | |||
| 353 | public function getWidth() |
||
| 354 | { |
||
| 355 | return $this->width; |
||
| 356 | } |
||
| 357 | |||
| 358 | public function getHeight() |
||
| 359 | { |
||
| 360 | return $this->height; |
||
| 361 | } |
||
| 362 | |||
| 363 | public function getMainColor(): ?string |
||
| 364 | { |
||
| 365 | return $this->mainColor; |
||
| 366 | } |
||
| 367 | |||
| 368 | public function getMainColorOpposite() |
||
| 369 | { |
||
| 370 | if (null === $this->getMainColor()) { |
||
| 371 | return; |
||
| 372 | } |
||
| 373 | |||
| 374 | return Color::fromHex($this->getMainColor())->invert(true); |
||
| 375 | } |
||
| 376 | |||
| 377 | public function setMainColor(?string $mainColor): self |
||
| 378 | { |
||
| 379 | $this->mainColor = $mainColor; |
||
| 380 | |||
| 381 | return $this; |
||
| 382 | } |
||
| 383 | |||
| 384 | public function setPageHasMedias($pageHasMedias) |
||
| 385 | { |
||
| 386 | $this->pageHasMedias = new ArrayCollection(); |
||
| 387 | foreach ($pageHasMedias as $pageHasMedia) { |
||
| 388 | $this->addPageHasMedia($pageHasMedia); |
||
| 389 | } |
||
| 390 | } |
||
| 391 | |||
| 392 | public function getPageHasMedias() |
||
| 393 | { |
||
| 394 | return $this->pageHasMedias; |
||
| 395 | } |
||
| 396 | |||
| 397 | public function addPageHasMedia(PageHasMedia $pageHasMedia) |
||
| 398 | { |
||
| 399 | $pageHasMedia->setMedia($this); |
||
| 400 | $this->pageHasMedias[] = $pageHasMedia; |
||
| 401 | } |
||
| 402 | |||
| 403 | public function removePageHasMedia(PageHasMedia $pageHasMedia) |
||
| 404 | { |
||
| 405 | $this->pageHasMedias->removeElement($pageHasMedia); |
||
| 406 | } |
||
| 407 | |||
| 408 | public function getMainImagePages() |
||
| 409 | { |
||
| 410 | return $this->mainImagePages; |
||
| 411 | } |
||
| 412 | |||
| 413 | /** |
||
| 414 | * @ORM\PreRemove |
||
| 415 | */ |
||
| 416 | public function removeMainImageFromPages() |
||
| 417 | { |
||
| 418 | foreach ($this->mainImagePages as $page) { |
||
| 419 | $page->setMainImage(null); |
||
| 420 | } |
||
| 421 | } |
||
| 422 | |||
| 423 | public function getPath(): ?string |
||
| 424 | { |
||
| 425 | return $this->getFullPath(); |
||
| 426 | } |
||
| 427 | |||
| 428 | public function getFullPath(): ?string |
||
| 429 | { |
||
| 430 | return null !== $this->media |
||
| 431 | ? '/'.$this->getRelativeDir().($this->getMedia() ? '/'.$this->getMedia() : '') |
||
| 432 | : null; |
||
| 433 | } |
||
| 434 | |||
| 435 | public function getFullPathWebP(): ?string |
||
| 436 | { |
||
| 437 | return null !== $this->media |
||
| 438 | ? '/'.$this->getRelativeDir().($this->getSlug() ? '/'.$this->getSlug().'.webp' : '') |
||
| 439 | : null; |
||
| 440 | } |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Get nOTE : this is used only for media renaming. |
||
| 444 | * |
||
| 445 | * @return string |
||
| 446 | */ |
||
| 447 | public function getMediaBeforeUpdate() |
||
| 448 | { |
||
| 449 | return $this->mediaBeforeUpdate; |
||
| 450 | } |
||
| 451 | |||
| 452 | /** |
||
| 453 | * Set nOTE : this is used only for media renaming. |
||
| 454 | * |
||
| 455 | * @param string $mediaBeforeUpdate NOTE : this is used only for media renaming |
||
| 456 | * |
||
| 457 | * @return self |
||
| 458 | */ |
||
| 459 | public function setMediaBeforeUpdate(string $mediaBeforeUpdate) |
||
| 460 | { |
||
| 461 | $this->mediaBeforeUpdate = $mediaBeforeUpdate; |
||
| 462 | |||
| 463 | return $this; |
||
| 464 | } |
||
| 465 | } |
||
| 466 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.