make new relation many-to-many between Person and AccompagnyingPeriod

This commit is contained in:
2021-03-26 21:54:58 +01:00
parent 813ecb0201
commit f6801c0c4f
6 changed files with 196 additions and 96 deletions

View File

@@ -3,7 +3,7 @@
/*
* Chill is a software for social workers
*
* Copyright (C) 2014-2015, Champs Libres Cooperative SCRLFS,
* Copyright (C) 2014-2021, Champs Libres Cooperative SCRLFS,
* <http://www.champs-libres.coop>, <info@champs-libres.coop>
*
* This program is free software: you can redistribute it and/or modify
@@ -22,6 +22,8 @@
namespace Chill\PersonBundle\Entity;
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;
@@ -65,14 +67,13 @@ class AccompanyingPeriod
private $remark = '';
/**
* @var Person
* @var Collection
*
* @ORM\ManyToOne(
* @ORM\ManyToMany(
* targetEntity="Chill\PersonBundle\Entity\Person",
* inversedBy="accompanyingPeriods",
* cascade={"refresh"})
* mappedBy="accompanyingPeriods")
*/
private $person;
private $persons;
/**
* @var AccompanyingPeriod\ClosingMotive
@@ -101,12 +102,13 @@ class AccompanyingPeriod
*/
public function __construct(\DateTime $dateOpening) {
$this->setOpeningDate($dateOpening);
$this->persons = new ArrayCollection();
}
/**
* Get id
*
* @return integer
* @return integer
*/
public function getId()
{
@@ -129,7 +131,7 @@ class AccompanyingPeriod
/**
* Get openingDate
*
* @return \DateTime
* @return \DateTime
*/
public function getOpeningDate()
{
@@ -138,12 +140,12 @@ class AccompanyingPeriod
/**
* Set closingDate
*
*
* For closing a Person file, you should use Person::setClosed instead.
*
* @param \DateTime $dateClosing
* @return AccompanyingPeriod
*
*
*/
public function setClosingDate($closingDate)
{
@@ -155,7 +157,7 @@ class AccompanyingPeriod
/**
* Get closingDate
*
* @return \DateTime
* @return \DateTime
*/
public function getClosingDate()
{
@@ -165,7 +167,7 @@ class AccompanyingPeriod
/**
* @return boolean
*/
public function isOpen(): bool
public function isOpen(): bool
{
if ($this->getOpeningDate() > new \DateTime('now')) {
return false;
@@ -198,7 +200,7 @@ class AccompanyingPeriod
/**
* Get remark
*
* @return string
* @return string
*/
public function getRemark()
{
@@ -206,29 +208,53 @@ class AccompanyingPeriod
}
/**
* Set person.
* Set persons
*/
public function setPersons($persons) : AccompanyingPeriod
{
$this->persons = $persons;
return $this;
}
/**
* Get Persons
*/
public function getPersons() : Collection
{
return $this->persons;
}
/**
* Return true if a given Person is associated
*/
public function containsPerson(Person $person) : bool
{
foreach ($this->persons as $p) {
if ($p === $person) { return true; }
}
return false;
}
/**
* Add person.
*
* For consistency, you should use Person::addAccompanyingPeriod instead.
*
* @param Person $person
* @return AccompanyingPeriod
* @see Person::addAccompanyingPeriod
*/
public function setPerson(Person $person = null)
public function addPerson(Person $person = null) : AccompanyingPeriod
{
$this->person = $person;
$this->persons[] = $person;
return $this;
}
/**
* Get person
*
* @return Person
* Remove person.
*/
public function getPerson()
public function removePerson(Person $person) : void
{
return $this->person;
$this->persons->removeElement($person);
}
/**
@@ -251,21 +277,24 @@ class AccompanyingPeriod
/**
* If the period can be reopened.
*
* This function test if the period is closed and if the period is the last
* for the associated person
*
* @return boolean
*
* This function test if the period is closed and if the period is the last
* for the given person
*/
public function canBeReOpened()
public function canBeReOpened(Person $person) : bool
{
if ($this->isOpen() === true) {
return false;
}
$periods = $this->getPerson()->getAccompanyingPeriodsOrdered();
dump('parcours fermé: '. $this->getId());
return end($periods) === $this;
foreach ($this->getPersons() as $p) {
if ($p === $person) {
$periods = $p->getAccompanyingPeriodsOrdered();
return end($periods) === $this; // retourne TRUE si cette période est la dernière
}
}
}
/**
@@ -294,7 +323,7 @@ class AccompanyingPeriod
/**
* Returns true if the closing date is after the opening date.
*
*
* @return boolean
*/
public function isClosingAfterOpening()

View File

@@ -206,10 +206,13 @@ class Person implements HasCenterInterface
* The person's accompanying periods (when the person was accompanied by the center)
* @var ArrayCollection
*
* @ORM\OneToMany(
* @ORM\ManyToMany(
* targetEntity="Chill\PersonBundle\Entity\AccompanyingPeriod",
* mappedBy="person",
* inversedBy="persons",
* cascade={"persist", "remove", "merge", "detach"})
* @ORM\JoinTable(
* name="persons_accompanying_periods"
* )
*/
private $accompanyingPeriods; //TO-CHANGE in accompanyingHistory
@@ -277,19 +280,20 @@ class Person implements HasCenterInterface
}
/**
* @param AccompanyingPeriod $accompanyingPeriod
* @uses AccompanyingPeriod::setPerson
* Add AccompanyingPeriod
*
* @uses AccompanyingPeriod::addPerson
*/
public function addAccompanyingPeriod(AccompanyingPeriod $accompanyingPeriod)
public function addAccompanyingPeriod(AccompanyingPeriod $accompanyingPeriod) : void
{
$accompanyingPeriod->setPerson($this);
$accompanyingPeriod->addPerson($this);
$this->accompanyingPeriods->add($accompanyingPeriod);
}
/**
* @param AccompanyingPeriod $accompanyingPeriod
* Remove AccompanyingPeriod
*/
public function removeAccompanyingPeriod(AccompanyingPeriod $accompanyingPeriod)
public function removeAccompanyingPeriod(AccompanyingPeriod $accompanyingPeriod) : void
{
$this->accompanyingPeriods->remove($accompanyingPeriod);
}
@@ -303,10 +307,8 @@ class Person implements HasCenterInterface
* For closing a file, @see this::close
*
* To check if the Person and its accompanying period is consistent, use validation.
*
* @param AccompanyingPeriod $accompanyingPeriod
*/
public function open(AccompanyingPeriod $accompanyingPeriod)
public function open(AccompanyingPeriod $accompanyingPeriod) : void
{
$this->proxyAccompanyingPeriodOpenState = true;
$this->addAccompanyingPeriod($accompanyingPeriod);
@@ -320,20 +322,17 @@ class Person implements HasCenterInterface
*
* To check if the Person and its accompanying period are consistent, use validation.
*
* @param accompanyingPeriod
* @throws \Exception if two lines of the accompanying period are open.
*/
public function close(AccompanyingPeriod $accompanyingPeriod = null)
public function close(AccompanyingPeriod $accompanyingPeriod = null) : void
{
$this->proxyAccompanyingPeriodOpenState = false;
}
/**
* Return the opened accompanying period.
*
* @return AccompanyingPeriod
*/
public function getOpenedAccompanyingPeriod()
public function getOpenedAccompanyingPeriod() : AccompanyingPeriod
{
if ($this->isOpen() === false) {
return null;
@@ -349,29 +348,25 @@ class Person implements HasCenterInterface
/**
* Returns the opened accompanying period.
*
* @return AccompanyingPeriod
* @deprecated since 1.1 use `getOpenedAccompanyingPeriod instead
*/
public function getCurrentAccompanyingPeriod()
public function getCurrentAccompanyingPeriod() : AccompanyingPeriod
{
return $this->getOpenedAccompanyingPeriod();
}
/**
* @return ArrayCollection
* Get AccompanyingPeriods Collection
*/
public function getAccompanyingPeriods()
public function getAccompanyingPeriods() : Collection
{
return $this->accompanyingPeriods;
}
/**
* Get the accompanying periods of a give person with the
* chronological order.
*
* @return AccompanyingPeriod[]
* Get the accompanying periods of a give person with the chronological order.
*/
public function getAccompanyingPeriodsOrdered()
public function getAccompanyingPeriodsOrdered() : array
{
$periods = $this->getAccompanyingPeriods()->toArray();
@@ -406,11 +401,9 @@ class Person implements HasCenterInterface
}
/**
* check if the person is opened
*
* @return boolean
* Check if the person is opened
*/
public function isOpen()
public function isOpen() : bool
{
foreach ($this->getAccompanyingPeriods() as $period) {
if ($period->isOpen()) {
@@ -1051,4 +1044,15 @@ class Person implements HasCenterInterface
return true;
}
public function getFullnameCanonical() : string
{
return $this->fullnameCanonical;
}
public function setFullnameCanonical($fullnameCanonical) : Person
{
$this->fullnameCanonical = $fullnameCanonical;
return $this;
}
}