<?php
namespace App\Entity\Promotions;
use App\Annotation\SiteAware;
use App\Entity\AbstractBase;
use App\Entity\Garages\Garage;
use App\Entity\Interfaces\Image2Interface;
use App\Entity\Interfaces\ImageInterface;
use App\Entity\Interfaces\SiteInterface;
use App\Entity\MediaLibrary\CooperativeMedia;
use App\Entity\Traits\HasImage2Trait;
use App\Entity\Traits\HasImageTrait;
use App\Entity\Traits\NameTrait;
use App\Entity\Traits\PublishedTrait;
use App\Entity\Traits\SiteTrait;
use App\Repository\Promotions\NationalPromotionRepository;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Symfony\Component\HttpFoundation\File\File;
use Vich\UploaderBundle\Mapping\Annotation as Vich;
/**
* @ORM\Table(name="vulco_national_promotion_v2", indexes={@ORM\Index(name="national_promotion_v2_site_idx", columns={"site"})})
* @ORM\Entity(repositoryClass=NationalPromotionRepository::class)
* @Gedmo\SoftDeleteable(fieldName="removedAt", timeAware=false)
* @SiteAware(siteFieldName="site")
* @Vich\Uploadable
*/
class NationalPromotion extends AbstractBase implements ImageInterface, Image2Interface, SiteInterface
{
use HasImageTrait;
use HasImage2Trait;
use NameTrait;
use SiteTrait;
use PublishedTrait;
/**
* @ORM\Column(type="string", length=255, nullable=false)
*/
private string $name;
/**
* @ORM\Column(type="datetime")
*/
private \DateTimeInterface $begin;
/**
* @ORM\Column(type="datetime")
*/
private \DateTimeInterface $end;
/**
* @ORM\Column(type="text", length=10000, nullable=true)
*/
private ?string $link = null;
/**
* @Vich\UploadableField(mapping="national_promotion_image", fileNameProperty="imageName")
*/
private ?File $image = null;
/**
* @Vich\UploadableField(mapping="national_promotion_image", fileNameProperty="image2Name")
*/
private ?File $image2 = null;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\MediaLibrary\CooperativeMedia")
* @ORM\JoinColumn(name="cooperative_media_id", referencedColumnName="id")
*/
private ?CooperativeMedia $cooperativeMedia = null;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Garages\Garage", cascade={"persist"})
*
* @ORM\JoinTable(name="vulco_national_promotion_v2_garage",
* joinColumns={@ORM\JoinColumn(name="national_promotion_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="garage_id", referencedColumnName="id")}
* )
*/
private ?Collection $garages;
public function __construct()
{
$this->garages = new ArrayCollection();
}
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 getLink(): ?string
{
return $this->link;
}
public function setLink(?string $link): self
{
$this->link = $link;
return $this;
}
public function getGarages(): ?Collection
{
return $this->garages;
}
public function setGarages(?Collection $garages): self
{
$this->garages = $garages;
return $this;
}
public function addGarage(Garage $garage): self
{
if (!$this->garages->contains($garage)) {
$this->garages->add($garage);
}
return $this;
}
public function removeGarage(Garage $garage): self
{
if ($this->garages->contains($garage)) {
$this->garages->removeElement($garage);
}
return $this;
}
public function getCooperativeMedia(): ?CooperativeMedia
{
return $this->cooperativeMedia;
}
public function setCooperativeMedia(?CooperativeMedia $cooperativeMedia): self
{
$this->cooperativeMedia = $cooperativeMedia;
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();
}
}