src/Entity/Event.php line 22

Open in your IDE?
  1. <?php
  2. namespace App\Entity;
  3. use App\Repository\EventRepository;
  4. use Doctrine\Common\Collections\Collection;
  5. use Doctrine\ORM\Mapping as ORM;
  6. use Doctrine\Common\Collections\ArrayCollection;
  7. use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
  8. use Doctrine\ORM\EntityManagerInterface;
  9. use App\Entity\BasePost;
  10. use App\Entity\Period;
  11. use App\Repository\PeriodRepository;
  12. /**
  13.  * @ORM\Entity(repositoryClass=EventRepository::class)
  14.  * @UniqueEntity("name")
  15.  */
  16. class Event
  17. {
  18.     use BasePost;
  19.     /**
  20.      * @ORM\Column(type="boolean", nullable=true)
  21.      */
  22.     private $important;
  23.     
  24.     /* overriden relations */
  25.     /**
  26.      * @see BasePost
  27.      * @ORM\ManyToMany(targetEntity="App\Entity\Tag", inversedBy="events")
  28.      */
  29.     private $tags;
  30.     /* new relations */
  31.     /**
  32.      * @ORM\OneToMany(targetEntity="App\Entity\Period", mappedBy="event", cascade={"persist", "remove"}, orphanRemoval=true)
  33.      * @ORM\OrderBy({"periodStart" = "ASC"})
  34.      */
  35.     private $periods;
  36.     
  37.     /**
  38.      * @ORM\ManyToMany(targetEntity="App\Entity\Structure", mappedBy="hostedEvents")
  39.      * @ORM\OrderBy({"name" = "ASC"})
  40.      */
  41.     private $eventHosters;
  42.     
  43.     /**
  44.      * @ORM\ManyToMany(targetEntity="App\Entity\Structure", mappedBy="partneringEvents")
  45.      * @ORM\OrderBy({"name" = "ASC"})
  46.      */
  47.     private $eventPartners;
  48.     /**
  49.      * @ORM\ManyToMany(targetEntity="App\Entity\Media", mappedBy="events", cascade={"persist", "remove"})
  50.      */
  51.     private $medias;
  52.     
  53.     public function __construct()
  54.     {
  55.         $this->periods = new ArrayCollection();
  56.         $this->eventHosters = new ArrayCollection();
  57.         $this->eventPartners = new ArrayCollection();
  58.         $this->medias = new ArrayCollection();
  59.     }
  60.     public function __toString()
  61.     {
  62.         return $this->name;
  63.     }
  64.     public function getId(): ?int
  65.     {
  66.         return $this->id;
  67.     }
  68.     
  69.     public function getImportant(): ?bool
  70.     {
  71.         return $this->important;
  72.     }
  73.     public function isImportant(): ?bool
  74.     {
  75.         return $this->important == true;
  76.     }
  77.     public function setImportant(?bool $important): self
  78.     {
  79.         $this->important $important;
  80.         return $this;
  81.     }
  82.     /**
  83.      * @return Collection|Period[]
  84.      */
  85.     public function getPeriods(): Collection
  86.     {
  87.         return $this->periods;
  88.     }
  89.     public function addPeriod(Period $period): self
  90.     {
  91.         if (!$this->periods->contains($period)) {
  92.             $this->periods[] = $period;
  93.             $period->setEvent($this);
  94.         }
  95.         return $this;
  96.     }
  97.     public function removePeriod(Period $period): self
  98.     {
  99.         if ($this->periods->contains($period)) {
  100.             $this->periods->removeElement($period);
  101.             // set the owning side to null (unless already changed)
  102.             if ($period->getEvent() === $this) {
  103.                 $period->setEvent(null);
  104.             }
  105.         }
  106.         return $this;
  107.     }
  108.     /**
  109.      * @return Collection|Structure[]
  110.      */
  111.     public function getEventHosters(): Collection
  112.     {
  113.         return $this->eventHosters;
  114.     }
  115.     public function addEventHoster(Structure $eventHoster): self
  116.     {
  117.         if (!$this->eventHosters->contains($eventHoster)) {
  118.             $this->eventHosters[] = $eventHoster;
  119.             $eventHoster->addHostedEvent($this);
  120.         }
  121.         return $this;
  122.     }
  123.     public function removeEventHoster(Structure $eventHoster): self
  124.     {
  125.         if ($this->eventHosters->contains($eventHoster)) {
  126.             $this->eventHosters->removeElement($eventHoster);
  127.             $eventHoster->removeHostedEvent($this);
  128.         }
  129.         return $this;
  130.     }
  131.     /**
  132.      * @return Collection|Structure[]
  133.      */
  134.     public function getEventPartners(): Collection
  135.     {
  136.         return $this->eventPartners;
  137.     }
  138.     public function addEventPartner(Structure $eventPartner): self
  139.     {
  140.         if (!$this->eventPartners->contains($eventPartner)) {
  141.             $this->eventPartners[] = $eventPartner;
  142.             $eventPartner->addPartneringEvent($this);
  143.         }
  144.         return $this;
  145.     }
  146.     public function removeEventPartner(Structure $eventPartner): self
  147.     {
  148.         if ($this->eventPartners->contains($eventPartner)) {
  149.             $this->eventPartners->removeElement($eventPartner);
  150.             $eventPartner->removePartneringEvent($this);
  151.         }
  152.         return $this;
  153.     }
  154.     /**
  155.      * @return Collection|Media[]
  156.      */
  157.     public function getMedias(): Collection
  158.     {
  159.         return $this->medias;
  160.     }
  161.     public function addMedia(Media $media): self
  162.     {
  163.         if (!$this->medias->contains($media)) {
  164.             $this->medias[] = $media;
  165.             $media->addEvent($this);
  166.         }
  167.         return $this;
  168.     }
  169.     public function removeMedia(Media $media): self
  170.     {
  171.         if ($this->medias->contains($media)) {
  172.             $this->medias->removeElement($media);
  173.             $media->removeEvent($this);
  174.         }
  175.         return $this;
  176.     }
  177.     // custom methods
  178.     public function getUniqueDisplayedPeriod(): ?Period
  179.     {
  180.         $periods $this->periods;
  181.         $uniqueDisplayedPeriod null;
  182.         $now = new \DateTime();
  183.         
  184.         $pastPeriods = [];
  185.         $futurePeriods = [];
  186.         foreach ($periods as $period) {
  187.             $periodStart $period->getPeriodStart();
  188.             if($periodStart $now) {
  189.                 $futurePeriods[$periodStart->format("Y-m-d-H-i")] = $period;
  190.             } else {
  191.                 $pastPeriods[$periodStart->format("Y-m-d-H-i")] = $period;
  192.             }
  193.         }
  194.         ksort($pastPeriods);
  195.         krsort($futurePeriods);
  196.         // on essaye de trouver la prochaine période à venir pour cet événement
  197.         if(!empty($futurePeriods)) $uniqueDisplayedPeriod array_pop($futurePeriods);
  198.         // pas de période à venir pour cet événement, donc on récupère la dernière période
  199.         if( ! isset($uniqueDisplayedPeriod) ) $uniqueDisplayedPeriod array_pop($pastPeriods);
  200.         return $uniqueDisplayedPeriod;
  201.     }
  202.     public function getEventStart(): ?\DateTimeInterface
  203.     {
  204.         $uniqueDisplayedPeriod $this->getUniqueDisplayedPeriod();
  205.         $eventStart $uniqueDisplayedPeriod $uniqueDisplayedPeriod->getPeriodStart() : null
  206.         return $eventStart;
  207.     }
  208.     public function getEventEnd(): ?\DateTimeInterface
  209.     {
  210.         $uniqueDisplayedPeriod $this->getUniqueDisplayedPeriod();
  211.         $eventEnd $uniqueDisplayedPeriod $uniqueDisplayedPeriod->getPeriodEnd() : null
  212.         return $eventEnd;
  213.     }
  214.     public function getAllDay(): ?bool
  215.     {
  216.         $uniqueDisplayedPeriod $this->getUniqueDisplayedPeriod();
  217.         $allDay $uniqueDisplayedPeriod $uniqueDisplayedPeriod->getAllDay() : null
  218.         return $allDay;
  219.     }
  220. }