2021-11-23 14:08:50 +01:00

434 lines
10 KiB
PHP

<?php
/**
* Chill is a software for social workers
*
* For the full copyright and license information, please view
* the LICENSE file that was distributed with this source code.
*/
namespace Chill\CalendarBundle\Entity;
use Chill\ActivityBundle\Entity\Activity;
use Chill\CalendarBundle\Repository\CalendarRepository;
use Chill\MainBundle\Entity\Embeddable\CommentEmbeddable;
use Chill\MainBundle\Entity\Location;
use Chill\MainBundle\Entity\User;
use Chill\PersonBundle\Entity\AccompanyingPeriod;
use Chill\PersonBundle\Entity\Person;
use Chill\ThirdPartyBundle\Entity\ThirdParty;
use DateTimeImmutable;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Serializer\Annotation as Serializer;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Validator\Constraints\NotBlank;
use Symfony\Component\Validator\Constraints\Range;
use Symfony\Component\Validator\Mapping\ClassMetadata;
/**
* @ORM\Table(name="chill_calendar.calendar")
* @ORM\Entity(repositoryClass=CalendarRepository::class)
*/
class Calendar
{
public const STATUS_CANCELED = 'canceled';
public const STATUS_MOVED = 'moved';
public const STATUS_VALID = 'valid';
/**
* @ORM\ManyToOne(targetEntity="Chill\PersonBundle\Entity\AccompanyingPeriod")
* @Groups({"read"})
*/
private AccompanyingPeriod $accompanyingPeriod;
/**
* @ORM\ManyToOne(targetEntity="Chill\ActivityBundle\Entity\Activity")
*/
private ?Activity $activity = null;
/**
* @ORM\ManyToOne(targetEntity="CalendarRange", inversedBy="calendars")
*/
private ?CalendarRange $calendarRange = null;
/**
* @ORM\ManyToOne(targetEntity="CancelReason")
*/
private ?CancelReason $cancelReason = null;
/**
* @ORM\Embedded(class=CommentEmbeddable::class, columnPrefix="comment_")
* @Serializer\Groups({"calendar:read"})
*/
private CommentEmbeddable $comment;
/**
* @ORM\Column(type="datetimetz_immutable")
* @Serializer\Groups({"calendar:read"})
*/
private ?DateTimeImmutable $endDate = null;
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
* @Serializer\Groups({"calendar:read"})
*/
private ?int $id;
/**
* @ORM\ManyToMany(
* targetEntity="Invite",
* cascade={"persist", "remove", "merge", "detach"})
* @ORM\JoinTable(name="chill_calendar.calendar_to_invites")
* @Groups({"read"})
*/
private Collection $invites;
/**
* @ORM\ManyToOne(targetEntity="Chill\MainBundle\Entity\Location")
* @groups({"read"})
*/
private ?Location $location = null;
/**
* @ORM\ManyToOne(targetEntity="Chill\MainBundle\Entity\User")
* @Serializer\Groups({"calendar:read"})
*/
private ?User $mainUser;
/**
* @ORM\ManyToMany(
* targetEntity="Chill\PersonBundle\Entity\Person",
* cascade={"persist", "remove", "merge", "detach"})
* @ORM\JoinTable(name="chill_calendar.calendar_to_persons")
* @Groups({"read"})
* @Serializer\Groups({"calendar:read"})
*/
private Collection $persons;
/**
* @ORM\ManyToMany(
* targetEntity="Chill\ThirdPartyBundle\Entity\ThirdParty",
* cascade={"persist", "remove", "merge", "detach"})
* @ORM\JoinTable(name="chill_calendar.calendar_to_thirdparties")
* @Groups({"read"})
* @Serializer\Groups({"calendar:read"})
*/
private Collection $professionals;
/**
* @ORM\Column(type="boolean", nullable=true)
*/
private ?bool $sendSMS;
/**
* @ORM\Column(type="datetimetz_immutable")
* @Serializer\Groups({"calendar:read"})
*/
private ?DateTimeImmutable $startDate = null;
/**
* @ORM\Column(type="string", length=255)
*/
private ?string $status = null;
/**
* @ORM\ManyToOne(targetEntity="Chill\MainBundle\Entity\User")
* @Groups({"read"})
* @Serializer\Groups({"calendar:read"})
*/
private ?User $user = null;
public function __construct()
{
$this->comment = new CommentEmbeddable();
$this->persons = new ArrayCollection();
$this->professionals = new ArrayCollection();
$this->invites = new ArrayCollection();
}
public function addInvite(?Invite $invite): self
{
if (null !== $invite) {
$this->invites[] = $invite;
}
return $this;
}
public function addPerson(?Person $person): self
{
if (null !== $person) {
$this->persons[] = $person;
}
return $this;
}
public function addProfessional(?ThirdParty $professional): self
{
if (null !== $professional) {
$this->professionals[] = $professional;
}
return $this;
}
public function getAccompanyingPeriod(): ?AccompanyingPeriod
{
return $this->accompanyingPeriod;
}
public function getActivity(): ?Activity
{
return $this->activity;
}
public function getCalendarRange(): ?CalendarRange
{
return $this->calendarRange;
}
public function getCancelReason(): ?CancelReason
{
return $this->cancelReason;
}
public function getComment(): CommentEmbeddable
{
return $this->comment;
}
public function getEndDate(): ?DateTimeImmutable
{
return $this->endDate;
}
public function getId(): ?int
{
return $this->id;
}
/**
* @return Collection|Invite[]
*/
public function getInvites(): Collection
{
return $this->invites;
}
public function getLocation(): ?Location
{
return $this->location;
}
public function getMainUser(): ?User
{
return $this->mainUser;
}
/**
* @return Collection|Person[]
*/
public function getPersons(): Collection
{
return $this->persons;
}
public function getPersonsAssociated(): array
{
if (null !== $this->accompanyingPeriod) {
$personsAssociated = [];
foreach ($this->accompanyingPeriod->getParticipations() as $participation) {
if ($this->persons->contains($participation->getPerson())) {
$personsAssociated[] = $participation->getPerson();
}
}
return $personsAssociated;
}
return [];
}
public function getPersonsNotAssociated(): array
{
if (null !== $this->accompanyingPeriod) {
$personsNotAssociated = [];
foreach ($this->persons as $person) {
if (!in_array($person, $this->getPersonsAssociated())) {
$personsNotAssociated[] = $person;
}
}
return $personsNotAssociated;
}
return [];
}
/**
* @return Collection|ThirdParty[]
*/
public function getProfessionals(): Collection
{
return $this->professionals;
}
public function getSendSMS(): ?bool
{
return $this->sendSMS;
}
public function getStartDate(): ?DateTimeImmutable
{
return $this->startDate;
}
public function getStatus(): ?string
{
return $this->status;
}
public function getThirdParties(): Collection
{
return $this->getProfessionals();
}
public function getUser(): ?User
{
return $this->user;
}
public function getusers(): Collection
{
return $this->getInvites(); //TODO get users of the invite
}
public static function loadValidatorMetadata(ClassMetadata $metadata): void
{
$metadata->addPropertyConstraint('startDate', new NotBlank());
$metadata->addPropertyConstraint('startDate', new Range([
'min' => '2 years ago',
'max' => '+ 2 years',
]));
$metadata->addPropertyConstraint('endDate', new NotBlank());
$metadata->addPropertyConstraint('endDate', new Range([
'min' => '2 years ago',
'max' => '+ 2 years',
]));
}
public function removeInvite(Invite $invite): self
{
$this->invites->removeElement($invite);
return $this;
}
public function removePerson(Person $person): self
{
$this->persons->removeElement($person);
return $this;
}
public function removeProfessional(ThirdParty $professional): self
{
$this->professionals->removeElement($professional);
return $this;
}
public function setAccompanyingPeriod(?AccompanyingPeriod $accompanyingPeriod): self
{
$this->accompanyingPeriod = $accompanyingPeriod;
return $this;
}
public function setActivity(?Activity $activity): self
{
$this->activity = $activity;
return $this;
}
public function setCalendarRange(?CalendarRange $calendarRange): self
{
$this->calendarRange = $calendarRange;
return $this;
}
public function setCancelReason(?CancelReason $cancelReason): self
{
$this->cancelReason = $cancelReason;
return $this;
}
public function setComment(CommentEmbeddable $comment): self
{
$this->comment = $comment;
return $this;
}
public function setEndDate(DateTimeImmutable $endDate): self
{
$this->endDate = $endDate;
return $this;
}
public function setLocation(?Location $location): Calendar
{
$this->location = $location;
return $this;
}
public function setMainUser(?User $mainUser): self
{
$this->mainUser = $mainUser;
return $this;
}
public function setSendSMS(?bool $sendSMS): self
{
$this->sendSMS = $sendSMS;
return $this;
}
public function setStartDate(DateTimeImmutable $startDate): self
{
$this->startDate = $startDate;
return $this;
}
public function setStatus(string $status): self
{
$this->status = $status;
return $this;
}
public function setUser(?User $user): self
{
$this->user = $user;
return $this;
}
}