<?php
namespace App\Entity;
use App\Repository\BlogPostGalleryItemRepository;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormTypeInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints as Assert;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @ORM\Entity(repositoryClass=BlogPostGalleryItemRepository::class)
* @ORM\HasLifecycleCallbacks
* @Vich\Uploadable
*/
class BlogPostGalleryItem
{
private const DEFAULT_LABEL = 'Média';
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity=BlogPostGallery::class, inversedBy="items")
* @ORM\JoinColumn(nullable=false)
*/
private $gallery;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $mediaName;
/**
* @Assert\File(
* maxSize = "2048k",
* mimeTypes = {"image/jpeg", "image/png", "image/bmp", "video/mp4", "application/pdf"},
* )
* @Vich\UploadableField(mapping="home_pageblock_carousel", fileNameProperty="mediaName")
* @var File
*/
private $mediaFile;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $title;
/**
* @ORM\Column(type="text", nullable=true)
*/
private $description;
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $position;
/**
* @ORM\Column(type="boolean", nullable=true, options={"default": false})
*/
private $isModified;
/**
* @ORM\Column(type="datetime_immutable", nullable=true)
*/
private ?\DateTimeImmutable $badgeActivatedAt = null;
/**
* @ORM\Column(type="datetime", options={"default": "CURRENT_TIMESTAMP"})
*/
private $updatedAt;
public function __construct()
{
$this->updatedAt = new \DateTimeImmutable();
}
public function getId(): ?int
{
return $this->id;
}
public function getGallery(): ?BlogPostGallery
{
return $this->gallery;
}
public function setGallery(?BlogPostGallery $gallery): self
{
$this->gallery = $gallery;
return $this;
}
public function getMediaName(): ?string
{
return $this->mediaName;
}
public function setMediaName(?string $mediaName): self
{
$this->mediaName = $mediaName;
return $this;
}
public function getMediaFile(): ?File
{
return $this->mediaFile;
}
/**
* @param File|null $image
*/
public function setMediaFile(File $mediaFile = null)
{
$this->mediaFile = $mediaFile;
// It is required that at least one field changes if you are using Doctrine,
// otherwise the event listeners won't be called and the file is lost
if ($mediaFile) {
$now = new \DateTimeImmutable();
$this->updatedAt = $now;
if ($this->isModified()) {
$this->badgeActivatedAt = $now;
}
}
}
public function getTitle(): ?string
{
return $this->title;
}
public function setTitle(?string $title): self
{
$this->title = $title;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
public function getPosition(): ?int
{
return $this->position;
}
public function setPosition(?int $position): self
{
$this->position = $position;
return $this;
}
public function isModified(): ?bool
{
return $this->isModified ?? false;
}
public function setIsModified(?bool $isModified): self
{
$normalizedValue = (bool) $isModified;
$currentValue = (bool) ($this->isModified ?? false);
if ($normalizedValue && !$currentValue) {
$this->badgeActivatedAt = new \DateTimeImmutable();
}
if (!$normalizedValue && $currentValue) {
$this->badgeActivatedAt = null;
}
if ($normalizedValue !== $currentValue) {
$this->updatedAt = new \DateTimeImmutable();
}
$this->isModified = $normalizedValue;
return $this;
}
public function getBadgeActivatedAt(): ?\DateTimeImmutable
{
return $this->badgeActivatedAt;
}
public function setBadgeActivatedAt(?\DateTimeImmutable $badgeActivatedAt): self
{
$this->badgeActivatedAt = $badgeActivatedAt;
return $this;
}
public function isBadgeDisplayWindowActive(?\DateTimeImmutable $referenceDate = null): bool
{
if (!$this->isModified() || null === $this->badgeActivatedAt) {
return false;
}
$referenceDate ??= new \DateTimeImmutable();
return $this->badgeActivatedAt->modify('+5 days') >= $referenceDate;
}
public function getUpdatedAt(): ?\DateTimeInterface
{
return $this->updatedAt;
}
public function setUpdatedAt(\DateTimeInterface $updatedAt): self
{
$this->updatedAt = $updatedAt;
return $this;
}
public function getAdminLabel(): string
{
$title = trim((string) $this->title);
if ($title !== '') {
return $title;
}
$mediaName = $this->getReadableMediaName();
if ($mediaName !== null) {
return $mediaName;
}
return self::DEFAULT_LABEL;
}
public function getReadableMediaName(): ?string
{
$mediaName = $this->extractReadableFileName($this->mediaName);
return $mediaName !== '' ? $mediaName : null;
}
private function extractReadableFileName(?string $value): string
{
$value = trim((string) $value);
if ($value === '') {
return '';
}
$decodedValue = rawurldecode($value);
$path = parse_url($decodedValue, PHP_URL_PATH);
$normalizedPath = is_string($path) && $path !== '' ? $path : $decodedValue;
$normalizedPath = str_replace('\\', '/', $normalizedPath);
return trim(basename($normalizedPath));
}
public function __toString(): string
{
return $this->getAdminLabel();
}
/**
* @ORM\PrePersist
*/
public function initializeTimestamps(): void
{
$this->updatedAt ??= new \DateTimeImmutable();
if ($this->isModified() && null === $this->badgeActivatedAt) {
$this->badgeActivatedAt = new \DateTimeImmutable();
}
}
/**
* @ORM\PreUpdate
*/
public function refreshUpdatedAt(): void
{
$this->updatedAt = new \DateTimeImmutable();
}
}