, * * 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 . */ use Doctrine\ORM\Mapping as ORM; use Doctrine\Common\Collections\Collection; use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Criteria; use Chill\MainBundle\Entity\Center; use Chill\MainBundle\Entity\Country; use Chill\PersonBundle\Entity\MaritalStatus; use Chill\MainBundle\Entity\HasCenterInterface; use Chill\MainBundle\Entity\Address; use Symfony\Component\Validator\Context\ExecutionContextInterface; /** * Person Class * * @ORM\Entity(repositoryClass="Chill\PersonBundle\Repository\PersonRepository") * @ORM\Table(name="chill_person_person", * indexes={@ORM\Index( * name="person_names", * columns={"firstName", "lastName"} * )}) * sf4 check index name * @ORM\HasLifecycleCallbacks() */ class Person implements HasCenterInterface { /** * The person's id * @var integer * * @ORM\Id * @ORM\Column(name="id", type="integer") * @ORM\GeneratedValue(strategy="AUTO") */ private $id; /** * The person's first name * @var string * * @ORM\Column(type="string", length=255) */ private $firstName; /** * The person's last name * @var string * * @ORM\Column(type="string", length=255) */ private $lastName; /** * @var Collection * * @ORM\OneToMany( * targetEntity="Chill\PersonBundle\Entity\PersonAltName", * mappedBy="person", * cascade={"persist", "remove", "merge", "detach"}, * orphanRemoval=true) */ private $altNames; /** * The person's birthdate * @var \DateTime * * @ORM\Column(type="date", nullable=true) */ private $birthdate; //to change in birthdate /** * The person's place of birth * @var string * * @ORM\Column(type="string", length=255, name="place_of_birth") */ private $placeOfBirth = ''; /** * The person's country of birth * @var Country * * @ORM\ManyToOne(targetEntity="Chill\MainBundle\Entity\Country") * * sf4 check: option inversedBy="birthsIn" return error mapping !! * * @ORM\JoinColumn(nullable=true) */ private $countryOfBirth; /** * The person's nationality * @var Country * * @ORM\ManyToOne(targetEntity="Chill\MainBundle\Entity\Country") * * sf4 check: option inversedBy="nationals" return error mapping !! * * @ORM\JoinColumn(nullable=true) */ private $nationality; /** * The person's gender * @var string * * @ORM\Column(type="string", length=9, nullable=true) */ private $gender; const MALE_GENDER = 'man'; const FEMALE_GENDER = 'woman'; const BOTH_GENDER = 'both'; /** * The marital status of the person * @var MaritalStatus * * @ORM\ManyToOne(targetEntity="Chill\PersonBundle\Entity\MaritalStatus") * @ORM\JoinColumn(nullable=true) */ private $maritalStatus; /** * Contact information for contacting the person * @var string * * @ORM\Column(type="text", nullable=true) */ private $contactInfo = ''; /** * The person's email * @var string * * @ORM\Column(type="text", nullable=true) */ private $email = ''; /** * The person's phonenumber * @var string * * @ORM\Column(type="text", length=40, nullable=true) */ private $phonenumber = ''; /** * The person's mobile phone number * @var string * * @ORM\Column(type="text", length=40, nullable=true) */ private $mobilenumber = ''; //TO-ADD caseOpeningDate //TO-ADD nativeLanguag /** * The person's spoken languages * @var ArrayCollection * * @ORM\ManyToMany(targetEntity="Chill\MainBundle\Entity\Language") * @ORM\JoinTable( * name="persons_spoken_languages", * joinColumns={@ORM\JoinColumn(name="person_id", referencedColumnName="id")}, * inverseJoinColumns={@ORM\JoinColumn(name="language_id", referencedColumnName="id")} * ) */ private $spokenLanguages; /** * The person's center * @var Center * * @ORM\ManyToOne(targetEntity="Chill\MainBundle\Entity\Center") * @ORM\JoinColumn(nullable=false) */ private $center; /** * The person's accompanying periods (when the person was accompanied by the center) * @var ArrayCollection * * @ORM\OneToMany( * targetEntity="Chill\PersonBundle\Entity\AccompanyingPeriod", * mappedBy="person", * cascade={"persist", "remove", "merge", "detach"}) */ private $accompanyingPeriods; //TO-CHANGE in accompanyingHistory /** * A remark over the person * @var string * * @ORM\Column(type="text") */ private $memo = ''; // TO-CHANGE in remark /** * @var boolean * @deprecated * * @ORM\Column(type="boolean") */ private $proxyAccompanyingPeriodOpenState = false; //TO-DELETE ? /** * Array where customfield's data are stored * @var array * * @ORM\Column(type="json_array") */ private $cFData; /** * Addresses * @var Collection * * @ORM\ManyToMany( * targetEntity="Chill\MainBundle\Entity\Address", * cascade={"persist", "remove", "merge", "detach"}) * @ORM\JoinTable(name="chill_person_persons_to_addresses") * @ORM\OrderBy({"validFrom" = "DESC"}) */ private $addresses; /** * @var string * * @ORM\Column(type="text", nullable=true) */ private $fullnameCanonical; /** * Person constructor. * * @param \DateTime|null $opening */ public function __construct(\DateTime $opening = null) { $this->accompanyingPeriods = new ArrayCollection(); $this->spokenLanguages = new ArrayCollection(); $this->addresses = new ArrayCollection(); $this->altNames = new ArrayCollection(); if ($opening === null) { $opening = new \DateTime(); } $this->open(new AccompanyingPeriod($opening)); } /** * @param AccompanyingPeriod $accompanyingPeriod * @uses AccompanyingPeriod::setPerson */ public function addAccompanyingPeriod(AccompanyingPeriod $accompanyingPeriod) { $accompanyingPeriod->setPerson($this); $this->accompanyingPeriods->add($accompanyingPeriod); } /** * @param AccompanyingPeriod $accompanyingPeriod */ public function removeAccompanyingPeriod(AccompanyingPeriod $accompanyingPeriod) { $this->accompanyingPeriods->remove($accompanyingPeriod); } /** * set the Person file as open at the given date. * * For updating a opening's date, you should update AccompanyingPeriod instance * directly. * * 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) { $this->proxyAccompanyingPeriodOpenState = true; $this->addAccompanyingPeriod($accompanyingPeriod); } /** * Set the Person file as closed at the given date. * * For update a closing date, you should update AccompanyingPeriod instance * directly. * * 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) { $this->proxyAccompanyingPeriodOpenState = false; } /** * Return the opened accompanying period. * * @return AccompanyingPeriod */ public function getOpenedAccompanyingPeriod() { if ($this->isOpen() === false) { return null; } foreach ($this->accompanyingPeriods as $period) { if ($period->isOpen()) { return $period; } } } /** * Returns the opened accompanying period. * * @return AccompanyingPeriod * @deprecated since 1.1 use `getOpenedAccompanyingPeriod instead */ public function getCurrentAccompanyingPeriod() { return $this->getOpenedAccompanyingPeriod(); } /** * @return ArrayCollection */ public function getAccompanyingPeriods() { return $this->accompanyingPeriods; } /** * Get the accompanying periods of a give person with the * chronological order. * * @return AccompanyingPeriod[] */ public function getAccompanyingPeriodsOrdered() { $periods = $this->getAccompanyingPeriods()->toArray(); //order by date : usort($periods, function($a, $b) { $dateA = $a->getOpeningDate(); $dateB = $b->getOpeningDate(); if ($dateA == $dateB) { $dateEA = $a->getClosingDate(); $dateEB = $b->getClosingDate(); if ($dateEA == $dateEB) { return 0; } if ($dateEA < $dateEB) { return -1; } else { return +1; } } if ($dateA < $dateB) { return -1 ; } else { return 1; } }); return $periods; } /** * check if the person is opened * * @return boolean */ public function isOpen() { foreach ($this->getAccompanyingPeriods() as $period) { if ($period->isOpen()) { return true; } } return false; } /** * Get id * * @return integer */ public function getId() { return $this->id; } /** * Set firstName * * @param string $firstName * @return Person */ public function setFirstName($firstName) { $this->firstName = $firstName; return $this; } /** * Get firstName * * @return string */ public function getFirstName() { return $this->firstName; } /** * Set lastName * * @param string $lastName * @return Person */ public function setLastName($lastName) { $this->lastName = $lastName; return $this; } /** * Get lastName * * @return string */ public function getLastName() { return $this->lastName; } /** * @return Collection */ public function getAltNames(): Collection { return $this->altNames; } /** * @param Collection $altNames * @return $this */ public function setAltNames(Collection $altNames) { $this->altNames = $altNames; return $this; } /** * @param PersonAltName $altName * @return $this */ public function addAltName(PersonAltName $altName) { if (FALSE === $this->altNames->contains($altName)) { $this->altNames->add($altName); $altName->setPerson($this); } return $this; } /** * @param PersonAltName $altName * @return $this */ public function removeAltName(PersonAltName $altName) { if ($this->altNames->contains($altName)) { $altName->setPerson(null); $this->altNames->removeElement($altName); } return $this; } /** * Set birthdate * * @param \DateTime $birthdate * @return Person */ public function setBirthdate($birthdate) { $this->birthdate = $birthdate; return $this; } /** * Get birthdate * * @return \DateTime */ public function getBirthdate() { return $this->birthdate; } /** * Set placeOfBirth * * @param string $placeOfBirth * @return Person */ public function setPlaceOfBirth($placeOfBirth) { if ($placeOfBirth === null) { $placeOfBirth = ''; } $this->placeOfBirth = $placeOfBirth; return $this; } /** * Get placeOfBirth * * @return string */ public function getPlaceOfBirth() { return $this->placeOfBirth; } /** * Set gender * * @param string $gender * @return Person */ public function setGender($gender) { $this->gender = $gender; return $this; } /** * Get gender * * @return string */ public function getGender() { return $this->gender; } /** * return gender as a Numeric form. * This is used for translations * @return int */ public function getGenderNumeric() { if ($this->getGender() == self::FEMALE_GENDER) { return 1; } else { return 0; } } /** * Set memo * * @param string $memo * @return Person */ public function setMemo($memo) { if ($memo === null) { $memo = ''; } if ($this->memo !== $memo) { $this->memo = $memo; } return $this; } /** * Get memo * * @return string */ public function getMemo() { return $this->memo; } /** * Set maritalStatus * * @param MaritalStatus $maritalStatus * @return Person */ public function setMaritalStatus(MaritalStatus $maritalStatus = null) { $this->maritalStatus = $maritalStatus; return $this; } /** * Get maritalStatus * * @return MaritalStatus */ public function getMaritalStatus() { return $this->maritalStatus; } /** * Set contactInfo * * @param string $contactInfo * @return Person */ public function setcontactInfo($contactInfo) { if ($contactInfo === null) { $contactInfo = ''; } $this->contactInfo = $contactInfo; return $this; } /** * Get contactInfo * * @return string */ public function getcontactInfo() { return $this->contactInfo; } /** * Set email * * @param string $email * @return Person */ public function setEmail($email) { if ($email === null) { $email = ''; } $this->email = $email; return $this; } /** * Get email * * @return string */ public function getEmail() { return $this->email; } /** * Set countryOfBirth * * @param Chill\MainBundle\Entity\Country $countryOfBirth * @return Person */ public function setCountryOfBirth(Country $countryOfBirth = null) { $this->countryOfBirth = $countryOfBirth; return $this; } /** * Get countryOfBirth * * @return Chill\MainBundle\Entity\Country */ public function getCountryOfBirth() { return $this->countryOfBirth; } /** * Set nationality * * @param Chill\MainBundle\Entity\Country $nationality * @return Person */ public function setNationality(Country $nationality = null) { $this->nationality = $nationality; return $this; } /** * Get nationality * * @return Chill\MainBundle\Entity\Country */ public function getNationality() { return $this->nationality; } /** * @return string */ public function getLabel() { return $this->getFirstName()." ".$this->getLastName(); } /** * Get center * * @return Center */ public function getCenter() { return $this->center; } /** * Set the center * * @param Center $center * @return \Chill\PersonBundle\Entity\Person */ public function setCenter(Center $center) { $this->center = $center; return $this; } /** * Set cFData * * @param array $cFData * * @return Report */ public function setCFData($cFData) { $this->cFData = $cFData; return $this; } /** * Get cFData * * @return array */ public function getCFData() { if ($this->cFData === null) { $this->cFData = []; } return $this->cFData; } /** * Set phonenumber * * @param string $phonenumber * @return Person */ public function setPhonenumber($phonenumber = '') { $this->phonenumber = $phonenumber; return $this; } /** * Get phonenumber * * @return string */ public function getPhonenumber() { return $this->phonenumber; } /** * Set mobilenumber * * @param string $mobilenumber * @return Person */ public function setMobilenumber($mobilenumber = '') { $this->mobilenumber = $mobilenumber; return $this; } /** * Get mobilenumber * * @return string */ public function getMobilenumber() { return $this->mobilenumber; } /** * @return string */ public function __toString() { return $this->getLabel(); } /** * Set spokenLanguages * * @param type $spokenLanguages * @return Person */ public function setSpokenLanguages($spokenLanguages) { $this->spokenLanguages = $spokenLanguages; return $this; } /** * Get spokenLanguages * * @return ArrayCollection */ public function getSpokenLanguages() { return $this->spokenLanguages; } /** * @param Address $address * @return $this */ public function addAddress(Address $address) { $this->addresses[] = $address; return $this; } /** * @param Address $address */ public function removeAddress(Address $address) { $this->addresses->removeElement($address); } /** * By default, the addresses are ordered by date, descending (the most * recent first) * * @return \Chill\MainBundle\Entity\Address[] */ public function getAddresses() { return $this->addresses; } /** * @param \DateTime|null $date * @return null */ public function getLastAddress(\DateTime $date = null) { if ($date === null) { $date = new \DateTime('now'); } $addresses = $this->getAddresses(); if ($addresses == null) { return null; } return $addresses->first(); } /** * Validation callback that checks if the accompanying periods are valid * * This method add violation errors. */ public function isAccompanyingPeriodValid(ExecutionContextInterface $context) { $r = $this->checkAccompanyingPeriodsAreNotCollapsing(); if ($r !== true) { if ($r['result'] === self::ERROR_PERIODS_ARE_COLLAPSING) { $context->buildViolation('Two accompanying periods have days in commun') ->atPath('accompanyingPeriods') ->addViolation(); } if ($r['result'] === self::ERROR_ADDIND_PERIOD_AFTER_AN_OPEN_PERIOD) { $context->buildViolation('A period is opened and a period is added after it') ->atPath('accompanyingPeriods') ->addViolation(); } } } /** * Return true if the person has two addresses with the * same validFrom date (in format 'Y-m-d') */ public function hasTwoAdressWithSameValidFromDate() { $validYMDDates = array(); foreach ($this->addresses as $ad) { $validDate = $ad->getValidFrom()->format('Y-m-d'); if (in_array($validDate, $validYMDDates)) { return true; } $validYMDDates[] = $validDate; } return false; } /** * Validation callback that checks if the addresses are valid (do not have * two addresses with the same validFrom date) * * This method add violation errors. */ public function isAddressesValid(ExecutionContextInterface $context) { if ($this->hasTwoAdressWithSameValidFromDate()) { $context ->buildViolation('Two addresses has the same validFrom date') ->atPath('addresses') ->addViolation() ; } } const ERROR_PERIODS_ARE_COLLAPSING = 1; // when two different periods // have days in commun const ERROR_ADDIND_PERIOD_AFTER_AN_OPEN_PERIOD = 2; // where there exist // a period opened and another one after it /** * Function used for validation that check if the accompanying periods of * the person are not collapsing (i.e. have not shared days) or having * a period after an open period. * * @return true | array True if the accompanying periods are not collapsing, * an array with data for displaying the error */ public function checkAccompanyingPeriodsAreNotCollapsing() { $periods = $this->getAccompanyingPeriodsOrdered(); $periodsNbr = sizeof($periods); $i = 0; while($i < $periodsNbr - 1) { $periodI = $periods[$i]; $periodAfterI = $periods[$i + 1]; if($periodI->isOpen()) { return array( 'result' => self::ERROR_ADDIND_PERIOD_AFTER_AN_OPEN_PERIOD, 'dateOpening' => $periodAfterI->getOpeningDate(), 'dateClosing' => $periodAfterI->getClosingDate(), 'date' => $periodI->getOpeningDate() ); } elseif ($periodI->getClosingDate() >= $periodAfterI->getOpeningDate()) { return array( 'result' => self::ERROR_PERIODS_ARE_COLLAPSING, 'dateOpening' => $periodI->getOpeningDate(), 'dateClosing' => $periodI->getClosingDate(), 'date' => $periodAfterI->getOpeningDate() ); } $i++; } return true; } }