chill-bundles/Entity/Person.php
Julien Fastré 513d9aefbc Implement user right refs #263
Merge of branch add_acl

Squashed commit of the following:

commit a3265a71014fc74bcc456aa9436494fc4ed434ea
Merge: 2522634 b231b14
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Tue Jun 30 09:58:23 2015 +0200

    Merge remote-tracking branch 'origin/master' into add_acl

    in order to future merging of add_acl into master

    Conflicts:
    	Tests/Controller/PersonControllerCreateTest.php

commit 252263400b1fb54091a0cd36d9ab84812c3a9942
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Mon Jun 29 23:00:25 2015 +0200

    check acl on timeline page

commit 5f08806585ce4ce1d58c6c507b191221b924d861
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Fri Jun 19 22:24:18 2015 +0200

    remove dead code

commit 860d62d9c0d9db2186eee725e7ef553ddf1f98f3
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Fri Jun 19 22:19:02 2015 +0200

    add access condition for link to person view in person view

commit e7b295000dd420309288e83920f6656679d36173
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Fri Jun 19 22:13:04 2015 +0200

    add ACL on person view

commit 7b1f90a2a28c1aec5ee0eb78206c75a9f1e09c60
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Fri Jun 19 21:47:49 2015 +0200

    add autorization for filtering person results in search

commit c6669164e2e36b555bed0745d5c7c12b53468b97
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Fri Jun 19 21:47:26 2015 +0200

    add fixtures for roles defined by person bundle

commit 8f30f5222efe531fb77a2e22b6af8069c5814d00
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Fri Jun 19 20:13:17 2015 +0200

    [transfer purpose][ci-skip]

commit e8ca4ce4842e619342a90d4343b725c2946f7d70
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Fri Jun 19 17:31:27 2015 +0200

    should fix reference problem

commit 8c32524a0fd61d504e9640705b97d10bd9b2b993
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Fri Jun 19 17:29:48 2015 +0200

    fix test about inheritance

commit d93cef2c596aba7fc977cc8db354241d6a77e6e2
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Thu Jun 18 21:38:59 2015 +0200

    add role hierarchy + test

commit 5066cc5318d67f20e3cff9f91f6d2b6a94d5f9c1
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Thu Jun 18 01:28:40 2015 +0200

    add tests for person voter

commit cf3359b11b16972db3c55102a3c8b2aae2c27789
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Thu Jun 18 01:07:30 2015 +0200

    fix "pass by reference" error

commit 94942eb34330813a10124ba58ec814ca685930c5
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Wed Jun 17 22:04:49 2015 +0200

    fix syntax error ($this->$is not correct)

commit 82d25df6b0e21b4032f4fc82dee203ccd1a067dd
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Wed Jun 17 21:51:41 2015 +0200

    add first test for person voter

commit 84e0d05be07477175db1ef95f3716a3c420291ce
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Wed Jun 17 21:50:06 2015 +0200

    define person voter as a service + CHILL_PERSON_SEE

commit bd55e505a179b0df4133903f766d9a8feb469900
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Tue Jun 16 20:57:25 2015 +0200

    add test for creating person with user multi center

commit d29147c8e70b2ae8313d973fe4b3d45d3f064d9b
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Mon Jun 15 09:11:56 2015 +0200

    fix tests - add center to persons

    since a center is associated with center, people used in tests must be associated with a center.
    => associate a center to people used in tests

commit cfac1ea68aaf615b5f8e9e67388f5407e5095d72
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Mon Jun 15 09:06:24 2015 +0200

    remove errors on deprecation

commit e1df87df87e831f3827721313603bcf41b3da891
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Fri Jun 12 22:31:03 2015 +0200

    fix migration error

commit 733fa96a2d874b84d2a0ece6664e4a64eed10b4d
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Wed Jun 10 21:17:15 2015 +0200

    add center type on creation forms

    tests are not adapted yet [ci skip]

commit c0d1d1b1c8fd58d9c15bf59d66de28a848df6d35
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Tue Jun 9 23:11:07 2015 +0200

    fix bug in migration

commit 693abfb7253dfe955beb63de3aebc05a15363cdb
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Tue Jun 9 22:41:21 2015 +0200

    add checks on migration

    check that there are data or not in database and, if there are data,
    create default center if required.

    If not required, the default center is the first in the list.

commit 8127ea8d49e094c81ce65e838416953bc8d47678
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Mon Jun 8 00:33:39 2015 +0200

    add first impl of PersonVoter (not tested) [ci skip]

commit 2ea674dbcf8848603f0fefa109abf2412722013f
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Mon Jun 8 00:32:46 2015 +0200

    add center to person

commit 0b1e3f137ace9eeb959b80cdbdfa3636c75af715
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Sat Jun 6 11:02:49 2015 +0200

    add phpunit bridge to handle deprecation msg

commit 70a4fd16181badad8ac9b87fd24e1000c7b1a734
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Fri Jun 5 12:42:01 2015 +0200

    remove deprecdation warning in phpunit

commit b3b8d1deefbdc72505606df7ca217771277e3555
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Fri Jun 5 11:41:36 2015 +0200

    fix deprecation of pattern in routing

    pattern => path in routing.yml

commit eea1252d8517ee582c958dc6045f05c1438dd9d1
Author: Julien Fastré <julien.fastre@champs-libres.coop>
Date:   Fri Jun 5 10:56:02 2015 +0200

    switch to symfony 2.7 [ci-skip]
2015-06-30 14:41:31 +02:00

666 lines
15 KiB
PHP

<?php
namespace Chill\PersonBundle\Entity;
/*
* Chill is a software for social workers
*
* Copyright (C) 2014-2015, 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
* 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 <http://www.gnu.org/licenses/>.
*/
use Symfony\Component\Validator\ExecutionContextInterface;
use Chill\MainBundle\Entity\Country;
use Doctrine\Common\Collections\ArrayCollection;
use Chill\MainBundle\Entity\HasCenterInterface;
/**
* Person
*/
class Person implements HasCenterInterface {
/**
* @var integer
*/
private $id;
/**
* @var string
*/
private $firstName;
/**
* @var string
*/
private $lastName;
/**
* @var \DateTime
*/
private $dateOfBirth;
/**
* @var string
*/
private $placeOfBirth = '';
/**
* @var string
*/
private $genre;
/**
*
* @var \Chill\MainBundle\Entity\Center
*/
private $center;
const GENRE_MAN = 'man';
const GENRE_WOMAN = 'woman';
/**
* @var string
*/
private $memo = '';
/**
* @var string
*/
private $email = '';
/**
* @var \CL\Chill\MainBundle\Entity\Country
*/
private $countryOfBirth;
/**
* @var \CL\Chill\MainBundle\Entity\Country
*/
private $nationality;
/**
*
* @var \Doctrine\Common\Collections\ArrayCollection
*/
private $accompanyingPeriods;
/**
*
* @var boolean
*/
private $proxyAccompanyingPeriodOpenState = false;
/**
* The array where customfields data is stored
* @var array
*/
private $cFData;
/**
* @var string
*/
private $phonenumber = '';
/**
* The spoken languages (ArrayCollection of Languages)
* @var \Doctrine\Common\Collections\ArrayCollection
*/
private $spokenLanguages;
public function __construct(\DateTime $opening = null) {
$this->accompanyingPeriods = new ArrayCollection();
$this->spokenLanguages = new ArrayCollection();
if ($opening === null) {
$opening = new \DateTime();
}
$this->open(new AccompanyingPeriod($opening));
}
/**
*
* @param \Chill\PersonBundle\Entity\AccompanyingPeriod $accompanyingPeriod
* @uses AccompanyingPeriod::setPerson
*/
public function addAccompanyingPeriod(AccompanyingPeriod $accompanyingPeriod) {
$accompanyingPeriod->setPerson($this);
$this->accompanyingPeriods->add($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 \Chill\PersonBundle\Entity\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)
{
$this->proxyAccompanyingPeriodOpenState = false;
}
/**
*
* @return null|AccompanyingPeriod
*/
public function getCurrentAccompanyingPeriod() {
if ($this->proxyAccompanyingPeriodOpenState === false) {
return null;
}
foreach ($this->accompanyingPeriods as $period) {
if ($period->isOpen()) {
return $period;
}
}
}
/**
*
* @return \Doctrine\Common\Collections\ArrayCollection
*/
public function getAccompanyingPeriods() {
return $this->accompanyingPeriods;
}
/**
*
* @return AccompanyingPeriod[]
*/
public function getAccompanyingPeriodsOrdered() {
$periods = $this->getAccompanyingPeriods()->toArray();
//order by date :
usort($periods, function($a, $b) {
$dateA = $a->getDateOpening();
$dateB = $b->getDateOpening();
if ($dateA == $dateB) {
$dateEA = $a->getDateClosing();
$dateEB = $b->getDateClosing();
if ($dateEA == $dateEB) {
return 0;
}
if ($dateEA < $dateEB) {
return -1;
} else {
return +1;
}
}
if ($dateA < $dateB) {
return -1 ;
} else {
return 1;
}
});
return $periods;
}
public function isOpen() {
return $this->proxyAccompanyingPeriodOpenState;
}
/**
* 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;
}
/**
* Set dateOfBirth
*
* @param \DateTime $dateOfBirth
* @return Person
*/
public function setDateOfBirth($dateOfBirth)
{
$this->dateOfBirth = $dateOfBirth;
return $this;
}
/**
* Get dateOfBirth
*
* @return \DateTime
*/
public function getDateOfBirth()
{
return $this->dateOfBirth;
}
/**
* 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 genre
*
* @param string $genre
* @return Person
*/
public function setGenre($genre)
{
$this->genre = $genre;
return $this;
}
/**
* Get genre
*
* @return string
*/
public function getGenre()
{
return $this->genre;
}
/**
* return gender as a Numeric form.
* This is used for translations
* @return int
*/
public function getGenreNumeric() {
if ($this->getGenre() == self::GENRE_WOMAN) {
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 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;
}
public function getLabel() {
return $this->getFirstName()." ".$this->getLastName();
}
/**
* Get center
*
* @return \Chill\MainBundle\Entity\Center
*/
public function getCenter()
{
return $this->center;
}
/**
* Set the center
*
* @param \Chill\MainBundle\Entity\Center $center
* @return \Chill\PersonBundle\Entity\Person
*/
public function setCenter(\Chill\MainBundle\Entity\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;
}
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;
}
/**
* 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->addViolationAt('accompanyingPeriods',
'Two accompanying periods have days in commun',
array());
}
if ($r['result'] === self::ERROR_ADDIND_PERIOD_AFTER_AN_OPEN_PERIOD) {
$context->addViolationAt('accompanyingPeriods',
'A period is opened and a period is added after it',
array());
}
}
}
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->getDateOpening(),
'dateClosing' => $periodAfterI->getDateClosing(),
'date' => $periodI->getDateOpening()
);
} elseif ($periodI->getDateClosing() >= $periodAfterI->getDateOpening()) {
return array(
'result' => self::ERROR_PERIODS_ARE_COLLAPSING,
'dateOpening' => $periodI->getDateOpening(),
'dateClosing' => $periodI->getDateClosing(),
'date' => $periodAfterI->getDateOpening()
);
}
$i++;
}
return true;
}
}