From 2f03f925ca3c7ed22cf04d4ea7d62904c918bab3 Mon Sep 17 00:00:00 2001 From: nobohan Date: Mon, 14 May 2018 12:17:33 +0200 Subject: [PATCH] add mobilenumber + validation on email and phonenumber (regex) --- Entity/Person.php | 240 ++++++++++++++++++-------------- Form/PersonType.php | 3 +- Resources/config/validation.yml | 11 ++ 3 files changed, 146 insertions(+), 108 deletions(-) diff --git a/Entity/Person.php b/Entity/Person.php index 92220091e..27ae1aac5 100644 --- a/Entity/Person.php +++ b/Entity/Person.php @@ -5,7 +5,7 @@ namespace Chill\PersonBundle\Entity; /* * Chill is a software for social workers * - * Copyright (C) 2014-2015, Champs Libres Cooperative SCRLFS, + * Copyright (C) 2014-2015, Champs Libres Cooperative SCRLFS, * , * * This program is free software: you can redistribute it and/or modify @@ -48,17 +48,17 @@ class Person implements HasCenterInterface { /** @var string The person's place of birth */ private $placeOfBirth = ''; - + /** @var \Chill\MainBundle\Entity\Country The person's country of birth */ private $countryOfBirth; /** @var \Chill\MainBundle\Entity\Country The person's nationality */ - private $nationality; + private $nationality; /** @var string The person's gender */ private $gender; - - const MALE_GENDER = 'man'; + + const MALE_GENDER = 'man'; const FEMALE_GENDER = 'woman'; /** @var \Chill\PersonBundle\Entity\MaritalStatus The marital status of the person */ @@ -72,27 +72,30 @@ class Person implements HasCenterInterface { /** @var string The person's phonenumber */ private $phonenumber = ''; + /** @var string The person's mobile phone number */ + private $mobilenumber = ''; + //TO-ADD : caseOpeningDate //TO-ADD nativeLanguag /** - * @var \Doctrine\Common\Collections\ArrayCollection The person's spoken + * @var \Doctrine\Common\Collections\ArrayCollection The person's spoken * languages (ArrayCollection of Languages) */ private $spokenLanguages; - + /** @var \Chill\MainBundle\Entity\Center The person's center */ private $center; /** - * @var \Doctrine\Common\Collections\ArrayCollection The person's + * @var \Doctrine\Common\Collections\ArrayCollection The person's * accompanying periods (when the person was accompanied by the center)*/ private $accompanyingPeriods; //TO-CHANGE in accompanyingHistory /** @var string A remark over the person */ private $memo = ''; // TO-CHANGE in remark - + /** * @var boolean * @deprecated @@ -102,25 +105,25 @@ class Person implements HasCenterInterface { /** @var array Array where customfield's data are stored */ private $cFData; - + /** * * @var \Doctrine\Common\Collections\Collection */ private $addresses; - + public function __construct(\DateTime $opening = null) { $this->accompanyingPeriods = new ArrayCollection(); $this->spokenLanguages = new ArrayCollection(); $this->addresses = new ArrayCollection(); - + if ($opening === null) { $opening = new \DateTime(); } - + $this->open(new AccompanyingPeriod($opening)); } - + /** * @param \Chill\PersonBundle\Entity\AccompanyingPeriod $accompanyingPeriod * @uses AccompanyingPeriod::setPerson @@ -129,21 +132,21 @@ class Person implements HasCenterInterface { $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) { @@ -152,42 +155,42 @@ class Person implements HasCenterInterface { } /** - * + * * Set the Person file as closed at the given date. - * - * For update a closing date, you should update AccompanyingPeriod instance + * + * 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) + 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 */ @@ -195,74 +198,74 @@ class Person implements HasCenterInterface { { return $this->getOpenedAccompanyingPeriod(); } - + /** - * + * * @return \Doctrine\Common\Collections\ArrayCollection */ public function getAccompanyingPeriods() { return $this->accompanyingPeriods; } - + /** - * Get the accompanying periods of a give person with the + * 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() + public function isOpen() { foreach ($this->getAccompanyingPeriods() as $period) { if ($period->isOpen()) { return true; } } - + return false; } /** * Get id * - * @return integer + * @return integer */ public function getId() { @@ -278,14 +281,14 @@ class Person implements HasCenterInterface { public function setFirstName($firstName) { $this->firstName = $firstName; - + return $this; } /** * Get firstName * - * @return string + * @return string */ public function getFirstName() { @@ -301,14 +304,14 @@ class Person implements HasCenterInterface { public function setLastName($lastName) { $this->lastName = $lastName; - + return $this; } /** * Get lastName * - * @return string + * @return string */ public function getLastName() { @@ -324,14 +327,14 @@ class Person implements HasCenterInterface { public function setBirthdate($birthdate) { $this->birthdate = $birthdate; - + return $this; } /** * Get birthdate * - * @return \DateTime + * @return \DateTime */ public function getBirthdate() { @@ -350,16 +353,16 @@ class Person implements HasCenterInterface { if ($placeOfBirth === null) { $placeOfBirth = ''; } - + $this->placeOfBirth = $placeOfBirth; - + return $this; } /** * Get placeOfBirth * - * @return string + * @return string */ public function getPlaceOfBirth() { @@ -375,20 +378,20 @@ class Person implements HasCenterInterface { public function setGender($gender) { $this->gender = $gender; - + return $this; } /** * Get gender * - * @return string + * @return string */ public function getGender() { return $this->gender; } - + /** * return gender as a Numeric form. * This is used for translations @@ -413,18 +416,18 @@ class Person implements HasCenterInterface { if ($memo === null) { $memo = ''; } - + if ($this->memo !== $memo) { $this->memo = $memo; } - + return $this; } /** * Get memo * - * @return string + * @return string */ public function getMemo() { @@ -446,7 +449,7 @@ class Person implements HasCenterInterface { /** * Get maritalStatus * - * @return \Chill\PersonBundle\Entity\MaritalStatus + * @return \Chill\PersonBundle\Entity\MaritalStatus */ public function getMaritalStatus() { @@ -465,16 +468,16 @@ class Person implements HasCenterInterface { if ($email === null) { $email = ''; } - + $this->email = $email; - + return $this; } /** * Get email * - * @return string + * @return string */ public function getEmail() { @@ -488,7 +491,7 @@ class Person implements HasCenterInterface { * @return Person */ public function setCountryOfBirth(Country $countryOfBirth = null) - { + { $this->countryOfBirth = $countryOfBirth; return $this; } @@ -496,7 +499,7 @@ class Person implements HasCenterInterface { /** * Get countryOfBirth * - * @return Chill\MainBundle\Entity\Country + * @return Chill\MainBundle\Entity\Country */ public function getCountryOfBirth() { @@ -512,27 +515,27 @@ class Person implements HasCenterInterface { public function setNationality(Country $nationality = null) { $this->nationality = $nationality; - + return $this; } /** * Get nationality * - * @return Chill\MainBundle\Entity\Country + * @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() @@ -542,7 +545,7 @@ class Person implements HasCenterInterface { /** * Set the center - * + * * @param \Chill\MainBundle\Entity\Center $center * @return \Chill\PersonBundle\Entity\Person */ @@ -552,7 +555,7 @@ class Person implements HasCenterInterface { return $this; } - + /** * Set cFData * @@ -589,20 +592,43 @@ class Person implements HasCenterInterface { public function setPhonenumber($phonenumber = '') { $this->phonenumber = $phonenumber; - + return $this; } /** * Get phonenumber * - * @return string + * @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; + } + public function __toString() { return $this->getLabel(); @@ -630,62 +656,62 @@ class Person implements HasCenterInterface { { return $this->spokenLanguages; } - + public function addAddress(Address $address) { $this->addresses[] = $address; - + return $this; } - + 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; } - + 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->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', @@ -693,27 +719,27 @@ class Person implements HasCenterInterface { } } } - + /** - * Return true if the person has two addresses with the + * 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) @@ -730,18 +756,18 @@ class Person implements HasCenterInterface { ); } } - - + + 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 */ @@ -750,12 +776,12 @@ class Person implements HasCenterInterface { $periods = $this->getAccompanyingPeriodsOrdered(); $periodsNbr = sizeof($periods); $i = 0; - + while($i < $periodsNbr - 1) { $periodI = $periods[$i]; $periodAfterI = $periods[$i + 1]; - - if($periodI->isOpen()) { + + if($periodI->isOpen()) { return array( 'result' => self::ERROR_ADDIND_PERIOD_AFTER_AN_OPEN_PERIOD, 'dateOpening' => $periodAfterI->getOpeningDate(), @@ -766,14 +792,14 @@ class Person implements HasCenterInterface { return array( 'result' => self::ERROR_PERIODS_ARE_COLLAPSING, 'dateOpening' => $periodI->getOpeningDate(), - + 'dateClosing' => $periodI->getClosingDate(), 'date' => $periodAfterI->getOpeningDate() ); } $i++; } - + return true; } } diff --git a/Form/PersonType.php b/Form/PersonType.php index a2ba67c42..7f7615d4b 100644 --- a/Form/PersonType.php +++ b/Form/PersonType.php @@ -27,6 +27,7 @@ use Symfony\Component\OptionsResolver\OptionsResolver; use Symfony\Component\Form\Extension\Core\Type\TextareaType; use Symfony\Component\Form\Extension\Core\Type\TextType; use Symfony\Component\Form\Extension\Core\Type\DateType; +use Symfony\Component\Form\Extension\Core\Type\EmailType; use Chill\PersonBundle\Form\Type\GenderType; use Chill\MainBundle\Form\Type\Select2CountryType; @@ -80,7 +81,7 @@ class PersonType extends AbstractType } if ($this->config['email'] === 'visible') { - $builder->add('email', TextareaType::class, array('required' => false)); + $builder->add('email', EmailType::class, array('required' => false)); } if ($this->config['country_of_birth'] === 'visible') { diff --git a/Resources/config/validation.yml b/Resources/config/validation.yml index 4a1625965..246e39f8c 100644 --- a/Resources/config/validation.yml +++ b/Resources/config/validation.yml @@ -30,6 +30,17 @@ Chill\PersonBundle\Entity\Person: accompanyingPeriods: - Valid: traverse: true + email: + - Email: + groups: [general, creation] + message: 'The email "{{ value }}" is not a valid email.' + phonenumber: + - Regex: + pattern: '/^([\+{1}]|[0])([0-9\s*]{4,20})$/' + groups: [general, creation] + message: 'Invalid phone number: it should begin with "0" or "+", hold only digits and be smaller than 20 characters ' + + constraints: - Callback: callback: isAccompanyingPeriodValid