<?php
namespace App\Entity\Calendar;
use App\Annotation\SiteAware;
use App\Entity\AbstractBase;
use App\Entity\Garages\Garage;
use App\Entity\Interfaces\PublishedInterface;
use App\Entity\Interfaces\SiteInterface;
use App\Entity\LearningCourses\LearningCourseSession;
use App\Entity\Traits\PublishedTrait;
use App\Entity\Traits\SiteTrait;
use App\Entity\Traits\TitleTrait;
use App\Repository\Calendar\CalendarEventRepository;
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\Security\Core\User\UserInterface;
/**
* @ORM\Table(name="vulco_calendar_event", indexes={@ORM\Index(name="calendar_event_site_idx", columns={"site"})})
* @ORM\Entity(repositoryClass=CalendarEventRepository::class)
* @Gedmo\SoftDeleteable(fieldName="removedAt", timeAware=false)
* @SiteAware(siteFieldName="site")
*/
class CalendarEvent extends AbstractBase implements PublishedInterface, SiteInterface
{
use TitleTrait;
use SiteTrait;
use PublishedTrait;
/**
* @ORM\Column(type="string", length=255, nullable=false)
*/
private string $title;
/**
* @ORM\Column(type="datetime")
*/
private \DateTimeInterface $beginAt;
/**
* @ORM\Column(type="datetime", nullable=true)
*/
private ?\DateTimeInterface $endAt = null;
/**
* @ORM\Column(type="text", length=10000, nullable=true, options={"collate"="utf8mb4_unicode_ci", "charset"="utf8mb4"})
*/
private ?string $description = null;
/**
* @ORM\Column(type="text", length=10000, nullable=true)
*/
private ?string $link = null;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private ?string $linkLabel = null;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\Calendar\CalendarEventCategory")
* @ORM\JoinColumn(name="calendar_event_category_id", referencedColumnName="id")
*/
private CalendarEventCategory $calendarEventCategory;
/**
* @ORM\ManyToMany(targetEntity="App\Entity\Garages\Garage", cascade={"persist"})
*
* @ORM\JoinTable(name="vulco_calendar_event_garage",
* joinColumns={@ORM\JoinColumn(name="calendar_event_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="garage_id", referencedColumnName="id")}
* )
*
* @ORM\OrderBy({"name": "ASC"})
*/
private ?Collection $garages;
/**
* @ORM\ManyToOne(targetEntity="App\Entity\User", fetch="EAGER")
* @ORM\JoinColumn(name="user_id", referencedColumnName="id")
*
* User who has created the CalendarEvent
*/
private ?UserInterface $user = null;
public function __construct()
{
$this->garages = new ArrayCollection();
}
public function getBeginAt(): \DateTimeInterface
{
return $this->beginAt;
}
public function getBeginAtTimeAsString(): string
{
return $this->getBeginAt()->format('H:i');
}
public function setBeginAt(\DateTimeInterface $beginAt): self
{
$this->beginAt = $beginAt;
return $this;
}
public function getEndAt(): ?\DateTimeInterface
{
return $this->endAt;
}
public function getEndAtTimeAsString(): ?string
{
if (!is_null($this->getEndAt())) {
return $this->getEndAt()->format('H:i');
}
return null;
}
public function setEndAt(?\DateTimeInterface $endAt): self
{
$this->endAt = $endAt;
return $this;
}
public function getDescription(): ?string
{
return $this->description;
}
public function setDescription(?string $description): self
{
$this->description = $description;
return $this;
}
public function getLink(): ?string
{
return $this->link;
}
public function setLink(?string $link): self
{
$this->link = $link;
return $this;
}
public function getLinkLabel(): ?string
{
return $this->linkLabel;
}
public function setLinkLabel(?string $linkLabel): self
{
$this->linkLabel = $linkLabel;
return $this;
}
public function getCalendarEventCategory(): CalendarEventCategory
{
return $this->calendarEventCategory;
}
public function setCalendarEventCategory(CalendarEventCategory $calendarEventCategory): self
{
$this->calendarEventCategory = $calendarEventCategory;
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 getUser(): ?UserInterface
{
return $this->user;
}
public function setUser(UserInterface $user): self
{
$this->user = $user;
return $this;
}
public function setDatesFromLearningCourseSession(LearningCourseSession $session)
{
if ($session->getEndDate()) {
// if session has a date range
if ($session->getBeginHour()) {
$this->setBeginAt($session->getDateWithBeginHour());
} else {
$this->setBeginAt($session->getDate());
}
if ($session->getEndHour()) {
$this->setEndAt($session->getEndDateWithEndHour());
} else {
$endAt = clone $session->getEndDate();
$endAt->setTime(23, 59, 59);
$this->setEndAt($endAt);
}
} else {
// one day session
$this->setBeginAt($session->getDateWithBeginHour());
if ($session->getEndHour()) {
$this->setEndAt($session->getDateWithEndHour());
} else {
$endAt = clone $session->getDate();
$endAt->setTime(23, 59, 59);
$this->setEndAt($endAt);
}
}
}
public function isAllDayEvent(): bool
{
if (is_null($this->getEndAt())) {
return true;
} else {
$beginAt = clone $this->getBeginAt();
$beginAt->setTime(0, 0, 0);
$endAt = clone $this->getEndAt();
$endAt->setTime(0, 0, 0);
if ($beginAt->format('Y-m-d') === $endAt->format('Y-m-d')) {
// equal begin and end date
if ('00:00' == $this->getBeginAtTimeAsString()) {
// case for LearningCourseSessions ( see setDatesFromLearningCourseSession )
return true;
}
}
}
return false;
}
}