src/Entity/BlogPostGalleryItem.php line 21

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\BlogPostGalleryItemRepository;
  4. use Doctrine\ORM\Mapping as ORM;
  5. use Symfony\Component\Form\FormBuilderInterface;
  6. use Symfony\Component\Form\FormInterface;
  7. use Symfony\Component\Form\FormTypeInterface;
  8. use Symfony\Component\Form\FormView;
  9. use Symfony\Component\HttpFoundation\File\File;
  10. use Symfony\Component\OptionsResolver\OptionsResolver;
  11. use Symfony\Component\Validator\Constraints as Assert;
  12. use Vich\UploaderBundle\Mapping\Annotation as Vich;
  13. /**
  14.  * @ORM\Entity(repositoryClass=BlogPostGalleryItemRepository::class)
  15.  * @ORM\HasLifecycleCallbacks
  16.  * @Vich\Uploadable
  17.  */
  18. class BlogPostGalleryItem
  19. {
  20.     private const DEFAULT_LABEL 'Média';
  21.     /**
  22.      * @ORM\Id
  23.      * @ORM\GeneratedValue
  24.      * @ORM\Column(type="integer")
  25.      */
  26.     private $id;
  27.     /**
  28.      * @ORM\ManyToOne(targetEntity=BlogPostGallery::class, inversedBy="items")
  29.      * @ORM\JoinColumn(nullable=false)
  30.      */
  31.     private $gallery;
  32.     /**
  33.      * @ORM\Column(type="string", length=255, nullable=true)
  34.      */
  35.     private $mediaName;
  36.     /**
  37.      * @Assert\File(
  38.      *     maxSize = "2048k",
  39.      *     mimeTypes = {"image/jpeg", "image/png", "image/bmp", "video/mp4", "application/pdf"},
  40.      * )
  41.      * @Vich\UploadableField(mapping="home_pageblock_carousel", fileNameProperty="mediaName")
  42.      * @var File
  43.      */
  44.     private $mediaFile;
  45.     /**
  46.      * @ORM\Column(type="string", length=255, nullable=true)
  47.      */
  48.     private $title;
  49.     /**
  50.      * @ORM\Column(type="text", nullable=true)
  51.      */
  52.     private $description;
  53.     /**
  54.      * @ORM\Column(type="integer", nullable=true)
  55.      */
  56.     private $position;
  57.     /**
  58.      * @ORM\Column(type="boolean", nullable=true, options={"default": false})
  59.      */
  60.     private $isModified;
  61.     /**
  62.      * @ORM\Column(type="datetime_immutable", nullable=true)
  63.      */
  64.     private ?\DateTimeImmutable $badgeActivatedAt null;
  65.     /**
  66.      * @ORM\Column(type="datetime", options={"default": "CURRENT_TIMESTAMP"})
  67.      */
  68.     private $updatedAt;
  69.     public function __construct()
  70.     {
  71.         $this->updatedAt = new \DateTimeImmutable();
  72.     }
  73.     public function getId(): ?int
  74.     {
  75.         return $this->id;
  76.     }
  77.     public function getGallery(): ?BlogPostGallery
  78.     {
  79.         return $this->gallery;
  80.     }
  81.     public function setGallery(?BlogPostGallery $gallery): self
  82.     {
  83.         $this->gallery $gallery;
  84.         return $this;
  85.     }
  86.     public function getMediaName(): ?string
  87.     {
  88.         return $this->mediaName;
  89.     }
  90.     public function setMediaName(?string $mediaName): self
  91.     {
  92.         $this->mediaName $mediaName;
  93.         return $this;
  94.     }
  95.     public function getMediaFile(): ?File
  96.     {
  97.         return $this->mediaFile;
  98.     }
  99.     /**
  100.      * @param File|null $image
  101.      */
  102.     public function setMediaFile(File $mediaFile null)
  103.     {
  104.         $this->mediaFile $mediaFile;
  105.         // It is required that at least one field changes if you are using Doctrine,
  106.         // otherwise the event listeners won't be called and the file is lost
  107.         if ($mediaFile) {
  108.             $now = new \DateTimeImmutable();
  109.             $this->updatedAt $now;
  110.             if ($this->isModified()) {
  111.                 $this->badgeActivatedAt $now;
  112.             }
  113.         }
  114.     }
  115.     public function getTitle(): ?string
  116.     {
  117.         return $this->title;
  118.     }
  119.     public function setTitle(?string $title): self
  120.     {
  121.         $this->title $title;
  122.         return $this;
  123.     }
  124.     public function getDescription(): ?string
  125.     {
  126.         return $this->description;
  127.     }
  128.     public function setDescription(?string $description): self
  129.     {
  130.         $this->description $description;
  131.         return $this;
  132.     }
  133.     public function getPosition(): ?int
  134.     {
  135.         return $this->position;
  136.     }
  137.     public function setPosition(?int $position): self
  138.     {
  139.         $this->position $position;
  140.         return $this;
  141.     }
  142.     public function isModified(): ?bool
  143.     {
  144.         return $this->isModified ?? false;
  145.     }
  146.     public function setIsModified(?bool $isModified): self
  147.     {
  148.         $normalizedValue = (bool) $isModified;
  149.         $currentValue = (bool) ($this->isModified ?? false);
  150.         if ($normalizedValue && !$currentValue) {
  151.             $this->badgeActivatedAt = new \DateTimeImmutable();
  152.         }
  153.         if (!$normalizedValue && $currentValue) {
  154.             $this->badgeActivatedAt null;
  155.         }
  156.         if ($normalizedValue !== $currentValue) {
  157.             $this->updatedAt = new \DateTimeImmutable();
  158.         }
  159.         $this->isModified $normalizedValue;
  160.         return $this;
  161.     }
  162.     public function getBadgeActivatedAt(): ?\DateTimeImmutable
  163.     {
  164.         return $this->badgeActivatedAt;
  165.     }
  166.     public function setBadgeActivatedAt(?\DateTimeImmutable $badgeActivatedAt): self
  167.     {
  168.         $this->badgeActivatedAt $badgeActivatedAt;
  169.         return $this;
  170.     }
  171.     public function isBadgeDisplayWindowActive(?\DateTimeImmutable $referenceDate null): bool
  172.     {
  173.         if (!$this->isModified() || null === $this->badgeActivatedAt) {
  174.             return false;
  175.         }
  176.         $referenceDate ??= new \DateTimeImmutable();
  177.         return $this->badgeActivatedAt->modify('+5 days') >= $referenceDate;
  178.     }
  179.     public function getUpdatedAt(): ?\DateTimeInterface
  180.     {
  181.         return $this->updatedAt;
  182.     }
  183.     public function setUpdatedAt(\DateTimeInterface $updatedAt): self
  184.     {
  185.         $this->updatedAt $updatedAt;
  186.         return $this;
  187.     }
  188.     public function getAdminLabel(): string
  189.     {
  190.         $title trim((string) $this->title);
  191.         if ($title !== '') {
  192.             return $title;
  193.         }
  194.         $mediaName $this->getReadableMediaName();
  195.         if ($mediaName !== null) {
  196.             return $mediaName;
  197.         }
  198.         return self::DEFAULT_LABEL;
  199.     }
  200.     public function getReadableMediaName(): ?string
  201.     {
  202.         $mediaName $this->extractReadableFileName($this->mediaName);
  203.         return $mediaName !== '' $mediaName null;
  204.     }
  205.     private function extractReadableFileName(?string $value): string
  206.     {
  207.         $value trim((string) $value);
  208.         if ($value === '') {
  209.             return '';
  210.         }
  211.         $decodedValue rawurldecode($value);
  212.         $path parse_url($decodedValuePHP_URL_PATH);
  213.         $normalizedPath is_string($path) && $path !== '' $path $decodedValue;
  214.         $normalizedPath str_replace('\\''/'$normalizedPath);
  215.         return trim(basename($normalizedPath));
  216.     }
  217.     public function __toString(): string
  218.     {
  219.         return $this->getAdminLabel();
  220.     }
  221.     /**
  222.      * @ORM\PrePersist
  223.      */
  224.     public function initializeTimestamps(): void
  225.     {
  226.         $this->updatedAt ??= new \DateTimeImmutable();
  227.         if ($this->isModified() && null === $this->badgeActivatedAt) {
  228.             $this->badgeActivatedAt = new \DateTimeImmutable();
  229.         }
  230.     }
  231.     /**
  232.      * @ORM\PreUpdate
  233.      */
  234.     public function refreshUpdatedAt(): void
  235.     {
  236.         $this->updatedAt = new \DateTimeImmutable();
  237.     }
  238. }