<?php
namespace App\Entity\Extranet;
use App\Annotation\SiteAware;
use App\Entity\AbstractBase;
use App\Entity\Interfaces\ImageInterface;
use App\Entity\Interfaces\PublishedInterface;
use App\Entity\Interfaces\SiteInterface;
use App\Entity\Traits\HasImageTrait;
use App\Entity\Traits\NameTrait;
use App\Entity\Traits\PositionTrait;
use App\Entity\Traits\PublishedTrait;
use App\Entity\Traits\SiteTrait;
use App\Enum\SiteEnum;
use App\Repository\Extranet\DashboardNewsSlideRepository;
use DateTime;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\HttpFoundation\File\File;
use Symfony\Component\Validator\Constraints\Callback;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @ORM\Table(name="vulco_dashboard_news_slide", indexes={@ORM\Index(name="dashboard_news_slide_site_idx", columns={"site"})})
* @ORM\Entity(repositoryClass=DashboardNewsSlideRepository::class)
* @Gedmo\SoftDeleteable(fieldName="removedAt", timeAware=false)
* @SiteAware(siteFieldName="site")
* @Vich\Uploadable
*/
class DashboardNewsSlide extends AbstractBase implements SiteInterface, PublishedInterface, ImageInterface
{
use NameTrait;
use HasImageTrait;
use SiteTrait;
use PublishedTrait;
use PositionTrait;
/**
* @ORM\Column(type="string", length=255, nullable=false)
*/
private string $name;
/**
* @ORM\Column(type="integer", nullable=false)
*/
private int $position = 0;
/**
* @ORM\Column(type="datetime")
*/
private \DateTimeInterface $begin;
/**
* @ORM\Column(type="datetime")
*/
private \DateTimeInterface $end;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $url = null;
/**
* @Vich\UploadableField(mapping="dashboard_news_slide_image", fileNameProperty="imageName")
*/
private ?File $image = null;
public function __construct()
{
}
public static function loadValidatorMetadata(ClassMetadata $metadata): void
{
$metadata->addConstraint(new Callback('validate'));
}
public function validate(ExecutionContextInterface $context): void
{
if ($this->getBegin()->getTimestamp() > $this->getEnd()->getTimestamp()) {
$context->buildViolation(SiteEnum::SITE_STR_ES === $this->getSite() ? 'La fecha inicial tiene que ser anterior a la fecha final.' : 'A data inicial deve ser antes da data final.')
->atPath('begin')
->addViolation()
;
}
}
public function getBegin(): \DateTimeInterface
{
return $this->begin;
}
public function setBegin(\DateTimeInterface $begin): self
{
$this->begin = $begin;
return $this;
}
public function getEnd(): \DateTimeInterface
{
return $this->end;
}
public function setEnd(\DateTimeInterface $end): self
{
$this->end = $end;
return $this;
}
public function getUrl(): ?string
{
return $this->url;
}
public function setUrl(?string $url): self
{
$this->url = $url;
return $this;
}
public function isActive(): bool
{
$now = new \DateTime();
$nowStr = $now->format('d-m-Y');
if ($this->getBegin()) {
$beginStr = $this->getBegin()->format('d-m-Y');
} else {
return false;
}
if ($this->getEnd()) {
$endStr = $this->getEnd()->format('d-m-Y');
} else {
return false;
}
return strtotime($beginStr) <= strtotime($nowStr) && strtotime($nowStr) <= strtotime($endStr) && $this->isPublished();
}
}