<?php
namespace App\Entity;
use App\Repository\EventRepository;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
use Doctrine\ORM\EntityManagerInterface;
use App\Entity\BasePost;
use App\Entity\Period;
use App\Repository\PeriodRepository;
/**
* @ORM\Entity(repositoryClass=EventRepository::class)
* @UniqueEntity("name")
*/
class Event
{
use BasePost;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private $important;
/* overriden relations */
/**
* @see BasePost
* @ORM\ManyToMany(targetEntity="App\Entity\Tag", inversedBy="events")
*/
private $tags;
/* new relations */
/**
* @ORM\OneToMany(targetEntity="App\Entity\Period", mappedBy="event", cascade={"persist", "remove"}, orphanRemoval=true)
* @ORM\OrderBy({"periodStart" = "ASC"})
*/
private $periods;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Structure", mappedBy="hostedEvents")
* @ORM\OrderBy({"name" = "ASC"})
*/
private $eventHosters;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Structure", mappedBy="partneringEvents")
* @ORM\OrderBy({"name" = "ASC"})
*/
private $eventPartners;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Media", mappedBy="events", cascade={"persist", "remove"})
*/
private $medias;
public function __construct()
{
$this->periods = new ArrayCollection();
$this->eventHosters = new ArrayCollection();
$this->eventPartners = new ArrayCollection();
$this->medias = new ArrayCollection();
}
public function __toString()
{
return $this->name;
}
public function getId(): ?int
{
return $this->id;
}
public function getImportant(): ?bool
{
return $this->important;
}
public function isImportant(): ?bool
{
return $this->important == true;
}
public function setImportant(?bool $important): self
{
$this->important = $important;
return $this;
}
/**
* @return Collection|Period[]
*/
public function getPeriods(): Collection
{
return $this->periods;
}
public function addPeriod(Period $period): self
{
if (!$this->periods->contains($period)) {
$this->periods[] = $period;
$period->setEvent($this);
}
return $this;
}
public function removePeriod(Period $period): self
{
if ($this->periods->contains($period)) {
$this->periods->removeElement($period);
// set the owning side to null (unless already changed)
if ($period->getEvent() === $this) {
$period->setEvent(null);
}
}
return $this;
}
/**
* @return Collection|Structure[]
*/
public function getEventHosters(): Collection
{
return $this->eventHosters;
}
public function addEventHoster(Structure $eventHoster): self
{
if (!$this->eventHosters->contains($eventHoster)) {
$this->eventHosters[] = $eventHoster;
$eventHoster->addHostedEvent($this);
}
return $this;
}
public function removeEventHoster(Structure $eventHoster): self
{
if ($this->eventHosters->contains($eventHoster)) {
$this->eventHosters->removeElement($eventHoster);
$eventHoster->removeHostedEvent($this);
}
return $this;
}
/**
* @return Collection|Structure[]
*/
public function getEventPartners(): Collection
{
return $this->eventPartners;
}
public function addEventPartner(Structure $eventPartner): self
{
if (!$this->eventPartners->contains($eventPartner)) {
$this->eventPartners[] = $eventPartner;
$eventPartner->addPartneringEvent($this);
}
return $this;
}
public function removeEventPartner(Structure $eventPartner): self
{
if ($this->eventPartners->contains($eventPartner)) {
$this->eventPartners->removeElement($eventPartner);
$eventPartner->removePartneringEvent($this);
}
return $this;
}
/**
* @return Collection|Media[]
*/
public function getMedias(): Collection
{
return $this->medias;
}
public function addMedia(Media $media): self
{
if (!$this->medias->contains($media)) {
$this->medias[] = $media;
$media->addEvent($this);
}
return $this;
}
public function removeMedia(Media $media): self
{
if ($this->medias->contains($media)) {
$this->medias->removeElement($media);
$media->removeEvent($this);
}
return $this;
}
// custom methods
public function getUniqueDisplayedPeriod(): ?Period
{
$periods = $this->periods;
$uniqueDisplayedPeriod = null;
$now = new \DateTime();
$pastPeriods = [];
$futurePeriods = [];
foreach ($periods as $period) {
$periodStart = $period->getPeriodStart();
if($periodStart > $now) {
$futurePeriods[$periodStart->format("Y-m-d-H-i")] = $period;
} else {
$pastPeriods[$periodStart->format("Y-m-d-H-i")] = $period;
}
}
ksort($pastPeriods);
krsort($futurePeriods);
// on essaye de trouver la prochaine période à venir pour cet événement
if(!empty($futurePeriods)) $uniqueDisplayedPeriod = array_pop($futurePeriods);
// pas de période à venir pour cet événement, donc on récupère la dernière période
if( ! isset($uniqueDisplayedPeriod) ) $uniqueDisplayedPeriod = array_pop($pastPeriods);
return $uniqueDisplayedPeriod;
}
public function getEventStart(): ?\DateTimeInterface
{
$uniqueDisplayedPeriod = $this->getUniqueDisplayedPeriod();
$eventStart = $uniqueDisplayedPeriod ? $uniqueDisplayedPeriod->getPeriodStart() : null;
return $eventStart;
}
public function getEventEnd(): ?\DateTimeInterface
{
$uniqueDisplayedPeriod = $this->getUniqueDisplayedPeriod();
$eventEnd = $uniqueDisplayedPeriod ? $uniqueDisplayedPeriod->getPeriodEnd() : null;
return $eventEnd;
}
public function getAllDay(): ?bool
{
$uniqueDisplayedPeriod = $this->getUniqueDisplayedPeriod();
$allDay = $uniqueDisplayedPeriod ? $uniqueDisplayedPeriod->getAllDay() : null;
return $allDay;
}
}