, * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU Affero General Public License as * published by the Free Software Foundation, either version 3 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU Affero General Public License for more details. * * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ namespace Chill\PersonBundle\Entity; use Chill\MainBundle\Entity\Scope; use Chill\PersonBundle\Entity\AccompanyingPeriod\ClosingMotive; use Chill\PersonBundle\Entity\AccompanyingPeriod\Comment; use Chill\PersonBundle\Entity\AccompanyingPeriod\Origin; use Chill\PersonBundle\Entity\AccompanyingPeriod\Resource; use Chill\ThirdPartyBundle\Entity\ThirdParty; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Doctrine\ORM\Mapping as ORM; use Symfony\Component\Validator\Context\ExecutionContextInterface; use Chill\MainBundle\Entity\User; /** * AccompanyingPeriod Class * * @ORM\Entity(repositoryClass="Chill\PersonBundle\Repository\AccompanyingPeriodRepository") * @ORM\Table(name="chill_person_accompanying_period") */ class AccompanyingPeriod { /** * Mark an accompanying period as "occasional" * * used in INTENSITY */ public const INTENSITY_OCCASIONAL = 'occasional'; /** * Mark an accompanying period as "regular" * * used in INTENSITY */ public const INTENSITY_REGULAR = 'regular'; public const INTENSITIES = [self::INTENSITY_OCCASIONAL, self::INTENSITY_REGULAR]; /** * Mark an accompanying period as "draft". * * This means that the accompanying period is not yet * confirmed by the creator */ public const STEP_DRAFT = 'DRAFT'; /** * Mark an accompanying period as "confirmed". * * This means that the accompanying period **is** * confirmed by the creator */ public const STEP_CONFIRMED = 'CONFIRMED'; /** * @var integer * * @ORM\Id * @ORM\Column(name="id", type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * @var \DateTime * * @ORM\Column(type="date") */ private $openingDate; /** * @var \DateTime * * @ORM\Column(type="date", nullable=true) */ private $closingDate = null; /** * @var string * * @ORM\Column(type="text") */ private $remark = ''; /** * @var Collection * * @ORM\OneToMany(targetEntity="Chill\PersonBundle\Entity\AccompanyingPeriod\Comment", * mappedBy="accompanyingPeriod" * ) */ private $comments; /** * @var Collection * * @ORM\OneToMany(targetEntity=AccompanyingPeriodParticipation::class, * mappedBy="accompanyingPeriod", * cascade={"persist", "remove", "merge", "detach"}) */ private $participations; /** * @var AccompanyingPeriod\ClosingMotive * * @ORM\ManyToOne( * targetEntity="Chill\PersonBundle\Entity\AccompanyingPeriod\ClosingMotive") * @ORM\JoinColumn(nullable=true) */ private $closingMotive = null; /** * @ORM\ManyToOne(targetEntity=User::class) * @ORM\JoinColumn(nullable=true) */ private $user; /** * @ORM\ManyToOne(targetEntity=User::class) * @ORM\JoinColumn(nullable=true) */ private $createdBy; /** * @var string * @ORM\Column(type="string", length=32, nullable=true) */ private $step = self::STEP_DRAFT; /** * @ORM\ManyToOne(targetEntity=Origin::class) * @ORM\JoinColumn(nullable=true) */ private $origin; /** * @var string * @ORM\Column(type="string", nullable=true) */ private $intensity; /** * @var Collection * @ORM\ManyToMany( * targetEntity=Scope::class, * cascade={} * ) * @ORM\JoinTable( * name="accompanying_periods_scopes", * joinColumns={@ORM\JoinColumn(name="accompanying_period_id", referencedColumnName="id")}, * inverseJoinColumns={@ORM\JoinColumn(name="scope_id", referencedColumnName="id")} * ) */ private $scopes; /** * @ORM\ManyToOne(targetEntity=Person::class) * @ORM\JoinColumn(nullable=true) */ private $requestorPerson; /** * @ORM\ManyToOne(targetEntity=ThirdParty::class) * @ORM\JoinColumn(nullable=true) */ private $requestorThirdParty; /** * @var bool * @ORM\Column(type="boolean") */ private $requestorAnonymous = false; /** * @var bool * @ORM\Column(type="boolean") */ private $emergency = false; /** * @var bool * @ORM\Column(type="boolean") */ private $confidential = false; /** * @var Collection * * @ORM\OneToMany( * targetEntity="Chill\PersonBundle\Entity\AccompanyingPeriod\Resource", * mappedBy="accompanyingPeriod" * ) */ private $resources; /** * AccompanyingPeriod constructor. * * @param \DateTime $dateOpening * @uses AccompanyingPeriod::setClosingDate() */ public function __construct(\DateTime $dateOpening) { $this->setOpeningDate($dateOpening); $this->participations = new ArrayCollection(); $this->scopes = new ArrayCollection(); } /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set openingDate * * @param \DateTime $dateOpening * @return AccompanyingPeriod */ public function setOpeningDate($openingDate) { $this->openingDate = $openingDate; return $this; } /** * Get openingDate * * @return \DateTime */ public function getOpeningDate() { return $this->openingDate; } /** * Set closingDate * * For closing a Person file, you should use Person::setClosed instead. * * @param \DateTime $dateClosing * @return AccompanyingPeriod * */ public function setClosingDate($closingDate) { $this->closingDate = $closingDate; return $this; } /** * Get closingDate * * @return \DateTime */ public function getClosingDate(): ?\DateTime { return $this->closingDate; } /** * @return boolean */ public function isOpen(): bool { if ($this->getOpeningDate() > new \DateTimeImmutable('now')) { return false; } if ($this->getClosingDate() === null) { return true; } return false; } public function setRemark(string $remark): self { if ($remark === null) { $remark = ''; } $this->remark = $remark; return $this; } public function getRemark(): string { return $this->remark; } public function getComments(): Collection { return $this->comments; } public function addComment(Comment $comment): self { $this->comments[] = $comment; return $this; } public function removeComment(Comment $comment): void { $this->comments->removeElement($comment); } /** * Get Participations Collection */ public function getParticipations(): Collection { return $this->participations; } /** * This private function scan Participations Collection, * searching for a given Person */ private function participationsContainsPerson(Person $person): ?AccompanyingPeriodParticipation { foreach ($this->participations as $participation) { /** @var AccompanyingPeriodParticipation $participation */ if ($person === $participation->getPerson()) { return $participation; }} return null; } /** * This public function is the same but return only true or false */ public function containsPerson(Person $person): bool { return ($this->participationsContainsPerson($person) === null) ? false : true; } /** * Add Person */ public function addPerson(Person $person = null): self { $participation = new AccompanyingPeriodParticipation($this, $person); $this->participations[] = $participation; return $this; } /** * Remove Person */ public function removePerson(Person $person): void { $participation = $this->participationsContainsPerson($person); if (! null === $participation) { $participation->setEndDate(new \DateTimeImmutable('now')); $this->participations->removeElement($participation); } } public function getClosingMotive(): ?ClosingMotive { return $this->closingMotive; } public function setClosingMotive(ClosingMotive $closingMotive = null): self { $this->closingMotive = $closingMotive; return $this; } /** * If the period can be reopened. * * This function test if the period is closed and if the period is the last * for the given person */ public function canBeReOpened(Person $person): bool { if ($this->isOpen() === true) { return false; } $participation = $this->participationsContainsPerson($person); if (!null === $participation) { $person = $participation->getPerson(); $periods = $person->getAccompanyingPeriodsOrdered(); return end($periods) === $this; } return false; } /** */ public function reOpen(): void { $this->setClosingDate(null); $this->setClosingMotive(null); } /** * Validation function */ public function isDateConsistent(ExecutionContextInterface $context) { if ($this->isOpen()) { return; } if (! $this->isClosingAfterOpening()) { $context->buildViolation('The date of closing is before the date of opening') ->atPath('dateClosing') ->addViolation(); } } /** * Returns true if the closing date is after the opening date. * * @return boolean */ public function isClosingAfterOpening(): bool { if (null === $this->getClosingDate()) { return false; } $diff = $this->getOpeningDate()->diff($this->getClosingDate()); if ($diff->invert === 0) { return true; } return false; } function getUser(): ?User { return $this->user; } function setUser(User $user): self { $this->user = $user; return $this; } public function getOrigin(): ?Origin { return $this->origin; } public function setOrigin(Origin $origin): self { $this->origin = $origin; return $this; } public function getRequestorPerson(): ?Person { return $this->requestorPerson; } public function setRequestorPerson(Person $requestorPerson): self { $this->requestorPerson = ($this->requestorThirdParty === null) ? $requestorPerson : null; return $this; } public function getRequestorThirdParty(): ?ThirdParty { return $this->requestorThirdParty; } public function setRequestorThirdParty(ThirdParty $requestorThirdParty): self { $this->requestorThirdParty = ($this->requestorPerson === null) ? $requestorThirdParty : null; return $this; } /** * @return Person|ThirdParty */ public function getRequestor() { return $this->requestorPerson ?? $this->requestorThirdParty; } public function isRequestorAnonymous(): bool { return $this->requestorAnonymous; } public function setRequestorAnonymous(bool $requestorAnonymous): self { $this->requestorAnonymous = $requestorAnonymous; return $this; } public function isEmergency(): bool { return $this->emergency; } public function setEmergency(bool $emergency): self { $this->emergency = $emergency; return $this; } public function isConfidential(): bool { return $this->confidential; } public function setConfidential(bool $confidential): self { $this->confidential = $confidential; return $this; } public function getCreatedBy(): ?User { return $this->createdBy; } public function setCreatedBy(User $createdBy): self { $this->createdBy = $createdBy; return $this; } public function getStep(): ?string { return $this->step; } public function setStep(string $step): self { $this->step = $step; return $this; } public function getIntensity(): ?string { return $this->intensity; } public function setIntensity(string $intensity): self { $this->intensity = $intensity; return $this; } public function getScopes(): Collection { return $this->scopes; } public function addScope(Scope $scope): self { $this->scopes[] = $scope; return $this; } public function removeScope(Scope $scope): void { $this->scopes->removeElement($scope); } public function getResources(): Collection { return $this->resources; } public function addResource(Resource $resource): self { $this->resources[] = $resource; return $this; } public function removeResource(Resource $resource): void { $this->resources->removeElement($resource); } }