src/Entity/Extranet/DashboardNewsSlide.php line 33

Open in your IDE?
  1. <?php
  2. namespace App\Entity\Extranet;
  3. use App\Annotation\SiteAware;
  4. use App\Entity\AbstractBase;
  5. use App\Entity\Interfaces\ImageInterface;
  6. use App\Entity\Interfaces\PublishedInterface;
  7. use App\Entity\Interfaces\SiteInterface;
  8. use App\Entity\Traits\HasImageTrait;
  9. use App\Entity\Traits\NameTrait;
  10. use App\Entity\Traits\PositionTrait;
  11. use App\Entity\Traits\PublishedTrait;
  12. use App\Entity\Traits\SiteTrait;
  13. use App\Enum\SiteEnum;
  14. use App\Repository\Extranet\DashboardNewsSlideRepository;
  15. use DateTime;
  16. use Doctrine\ORM\Mapping as ORM;
  17. use Gedmo\Mapping\Annotation as Gedmo;
  18. use Symfony\Component\HttpFoundation\File\File;
  19. use Symfony\Component\Validator\Constraints\Callback;
  20. use Symfony\Component\Validator\Context\ExecutionContextInterface;
  21. use Symfony\Component\Validator\Mapping\ClassMetadata;
  22. use Vich\UploaderBundle\Mapping\Annotation as Vich;
  23. /**
  24.  * @ORM\Table(name="vulco_dashboard_news_slide", indexes={@ORM\Index(name="dashboard_news_slide_site_idx", columns={"site"})})
  25.  * @ORM\Entity(repositoryClass=DashboardNewsSlideRepository::class)
  26.  * @Gedmo\SoftDeleteable(fieldName="removedAt", timeAware=false)
  27.  * @SiteAware(siteFieldName="site")
  28.  * @Vich\Uploadable
  29.  */
  30. class DashboardNewsSlide extends AbstractBase implements SiteInterfacePublishedInterfaceImageInterface
  31. {
  32.     use NameTrait;
  33.     use HasImageTrait;
  34.     use SiteTrait;
  35.     use PublishedTrait;
  36.     use PositionTrait;
  37.     /**
  38.      * @ORM\Column(type="string", length=255, nullable=false)
  39.      */
  40.     private string $name;
  41.     /**
  42.      * @ORM\Column(type="integer", nullable=false)
  43.      */
  44.     private int $position 0;
  45.     /**
  46.      * @ORM\Column(type="datetime")
  47.      */
  48.     private \DateTimeInterface $begin;
  49.     /**
  50.      * @ORM\Column(type="datetime")
  51.      */
  52.     private \DateTimeInterface $end;
  53.     /**
  54.      * @ORM\Column(type="string", length=255, nullable=true)
  55.      */
  56.     private ?string $url null;
  57.     /**
  58.      * @Vich\UploadableField(mapping="dashboard_news_slide_image", fileNameProperty="imageName")
  59.      */
  60.     private ?File $image null;
  61.     public function __construct()
  62.     {
  63.     }
  64.     public static function loadValidatorMetadata(ClassMetadata $metadata): void
  65.     {
  66.         $metadata->addConstraint(new Callback('validate'));
  67.     }
  68.     public function validate(ExecutionContextInterface $context): void
  69.     {
  70.         if ($this->getBegin()->getTimestamp() > $this->getEnd()->getTimestamp()) {
  71.             $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.')
  72.                 ->atPath('begin')
  73.                 ->addViolation()
  74.             ;
  75.         }
  76.     }
  77.     public function getBegin(): \DateTimeInterface
  78.     {
  79.         return $this->begin;
  80.     }
  81.     public function setBegin(\DateTimeInterface $begin): self
  82.     {
  83.         $this->begin $begin;
  84.         return $this;
  85.     }
  86.     public function getEnd(): \DateTimeInterface
  87.     {
  88.         return $this->end;
  89.     }
  90.     public function setEnd(\DateTimeInterface $end): self
  91.     {
  92.         $this->end $end;
  93.         return $this;
  94.     }
  95.     public function getUrl(): ?string
  96.     {
  97.         return $this->url;
  98.     }
  99.     public function setUrl(?string $url): self
  100.     {
  101.         $this->url $url;
  102.         return $this;
  103.     }
  104.     public function isActive(): bool
  105.     {
  106.         $now = new \DateTime();
  107.         $nowStr $now->format('d-m-Y');
  108.         if ($this->getBegin()) {
  109.             $beginStr $this->getBegin()->format('d-m-Y');
  110.         } else {
  111.             return false;
  112.         }
  113.         if ($this->getEnd()) {
  114.             $endStr $this->getEnd()->format('d-m-Y');
  115.         } else {
  116.             return false;
  117.         }
  118.         return strtotime($beginStr) <= strtotime($nowStr) && strtotime($nowStr) <= strtotime($endStr) && $this->isPublished();
  119.     }
  120. }