From 50d686f0869ea45fc281f3929dbea2bc8fd1df60 Mon Sep 17 00:00:00 2001 From: Jean-Francois Monfort Date: Thu, 25 Mar 2021 15:04:59 +0100 Subject: [PATCH] Add other phone number --- .../Controller/PersonController.php | 19 ++- .../ChillPersonBundle/Entity/Person.php | 96 ++++++++++--- .../ChillPersonBundle/Entity/PersonPhone.php | 133 ++++++++++++++++++ .../ChillPersonBundle/Form/PersonType.php | 45 +++--- .../Form/Type/PersonPhoneType.php | 44 ++++++ .../Resources/views/Person/edit.html.twig | 3 + .../ChillPersonBundle/config/validation.yaml | 16 ++- .../migrations/Version20210318095831.php | 36 +++++ 8 files changed, 345 insertions(+), 47 deletions(-) create mode 100644 src/Bundle/ChillPersonBundle/Entity/PersonPhone.php create mode 100644 src/Bundle/ChillPersonBundle/Form/Type/PersonPhoneType.php create mode 100644 src/Bundle/ChillPersonBundle/migrations/Version20210318095831.php diff --git a/src/Bundle/ChillPersonBundle/Controller/PersonController.php b/src/Bundle/ChillPersonBundle/Controller/PersonController.php index 28a960520..e031458d9 100644 --- a/src/Bundle/ChillPersonBundle/Controller/PersonController.php +++ b/src/Bundle/ChillPersonBundle/Controller/PersonController.php @@ -51,25 +51,24 @@ class PersonController extends AbstractController * @var SimilarPersonMatcher */ protected $similarPersonMatcher; - + /** * * @var TranslatorInterface */ protected $translator; - - + /** * @var EventDispatcherInterface */ protected $eventDispatcher; - + /** * * @var PersonRepository; */ protected $personRepository; - + /** * * @var ConfigPersonAltNamesHelper @@ -120,13 +119,13 @@ class PersonController extends AbstractController throw $this->createNotFoundException("Person with id $person_id not" . " found on this server"); } - + $this->denyAccessUnlessGranted('CHILL_PERSON_SEE', $person, "You are not allowed to see this person."); - + $event = new PrivacyEvent($person); $this->eventDispatcher->dispatch(PrivacyEvent::PERSON_PRIVACY_EVENT, $event); - + return $this->render('ChillPersonBundle:Person:view.html.twig', array( "person" => $person, @@ -293,7 +292,7 @@ class PersonController extends AbstractController $r->setStatusCode(400); return $r; } - + $form = $this->createForm( //CreationPersonType::NAME, CreationPersonType::class, @@ -306,7 +305,7 @@ class PersonController extends AbstractController $form->handleRequest($request); $person = $this->_bindCreationForm($form); - + $errors = $this->_validatePersonAndAccompanyingPeriod($person); $this->logger->info(sprintf('Person created with %d errors ', count($errors))); diff --git a/src/Bundle/ChillPersonBundle/Entity/Person.php b/src/Bundle/ChillPersonBundle/Entity/Person.php index 36993ce3b..c3a2edff6 100644 --- a/src/Bundle/ChillPersonBundle/Entity/Person.php +++ b/src/Bundle/ChillPersonBundle/Entity/Person.php @@ -72,7 +72,7 @@ class Person implements HasCenterInterface * @ORM\Column(type="string", length=255) */ private $lastName; - + /** * @var Collection * @@ -177,6 +177,18 @@ class Person implements HasCenterInterface */ private $mobilenumber = ''; + /** + * @var Collection + * + * @ORM\OneToMany( + * targetEntity="Chill\PersonBundle\Entity\PersonPhone", + * mappedBy="person", + * cascade={"persist", "remove", "merge", "detach"}, + * orphanRemoval=true + * ) + */ + private $otherPhoneNumbers; + //TO-ADD caseOpeningDate //TO-ADD nativeLanguag @@ -248,15 +260,14 @@ class Person implements HasCenterInterface * @ORM\OrderBy({"validFrom" = "DESC"}) */ private $addresses; - + /** * @var string * * @ORM\Column(type="text", nullable=true) */ private $fullnameCanonical; - - + /** * Person constructor. * @@ -268,6 +279,7 @@ class Person implements HasCenterInterface $this->spokenLanguages = new ArrayCollection(); $this->addresses = new ArrayCollection(); $this->altNames = new ArrayCollection(); + $this->otherPhoneNumbers = new ArrayCollection(); if ($opening === null) { $opening = new \DateTime(); @@ -285,7 +297,7 @@ class Person implements HasCenterInterface $accompanyingPeriod->setPerson($this); $this->accompanyingPeriods->add($accompanyingPeriod); } - + /** * @param AccompanyingPeriod $accompanyingPeriod */ @@ -476,7 +488,7 @@ class Person implements HasCenterInterface { return $this->lastName; } - + /** * @return Collection */ @@ -484,7 +496,7 @@ class Person implements HasCenterInterface { return $this->altNames; } - + /** * @param Collection $altNames * @return $this @@ -492,10 +504,10 @@ class Person implements HasCenterInterface public function setAltNames(Collection $altNames) { $this->altNames = $altNames; - + return $this; } - + /** * @param PersonAltName $altName * @return $this @@ -506,24 +518,24 @@ class Person implements HasCenterInterface $this->altNames->add($altName); $altName->setPerson($this); } - + return $this; } - + /** * @param PersonAltName $altName * @return $this */ - public function removeAltName(PersonAltName $altName) + public function removeAltName(PersonAltName $altName) { if ($this->altNames->contains($altName)) { $altName->setPerson(null); $this->altNames->removeElement($altName); } - + return $this; } - + /** * Set birthdate * @@ -760,7 +772,7 @@ class Person implements HasCenterInterface { return $this->nationality; } - + /** * @return string */ @@ -863,7 +875,53 @@ class Person implements HasCenterInterface { return $this->mobilenumber; } - + + /** + * @return Collection + */ + public function getOtherPhoneNumbers(): Collection + { + return $this->otherPhoneNumbers; + } + + /** + * @param Collection $otherPhoneNumbers + * @return $this + */ + public function setOtherPhoneNumbers(Collection $otherPhoneNumbers) + { + $this->otherPhoneNumbers = $otherPhoneNumbers; + + return $this; + } + + /** + * @param PersonPhone $otherPhoneNumber + * @return $this + */ + public function addOtherPhoneNumber(PersonPhone $otherPhoneNumber) + { + if (false === $this->otherPhoneNumbers->contains($otherPhoneNumber)) { + $otherPhoneNumber->setPerson($this); + $this->otherPhoneNumbers->add($otherPhoneNumber); + } + + return $this; + } + + /** + * @param PersonPhone $otherPhoneNumber + * @return $this + */ + public function removeOtherPhoneNumber(PersonPhone $otherPhoneNumber) + { + if ($this->otherPhoneNumbers->contains($otherPhoneNumber)) { + $this->otherPhoneNumbers->removeElement($otherPhoneNumber); + } + + return $this; + } + /** * @return string */ @@ -894,7 +952,7 @@ class Person implements HasCenterInterface { return $this->spokenLanguages; } - + /** * @param Address $address * @return $this @@ -905,7 +963,7 @@ class Person implements HasCenterInterface return $this; } - + /** * @param Address $address */ @@ -924,7 +982,7 @@ class Person implements HasCenterInterface { return $this->addresses; } - + /** * @param \DateTime|null $date * @return null diff --git a/src/Bundle/ChillPersonBundle/Entity/PersonPhone.php b/src/Bundle/ChillPersonBundle/Entity/PersonPhone.php new file mode 100644 index 000000000..f413cc0c0 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Entity/PersonPhone.php @@ -0,0 +1,133 @@ +date = new \DateTime(); + } + + /** + * @return int + */ + public function getId(): int + { + return $this->id; + } + + /** + * @return \Chill\PersonBundle\Entity\Person + */ + public function getPerson(): Person + { + return $this->person; + } + + /** + * @param \Chill\PersonBundle\Entity\Person $person + */ + public function setPerson(Person $person): void + { + $this->person = $person; + } + + /** + * @return string + */ + public function getPhonenumber(): string + { + return $this->phonenumber; + } + + /** + * @param string $phonenumber + */ + public function setPhonenumber(string $phonenumber): void + { + $this->phonenumber = $phonenumber; + } + + /** + * @return string + */ + public function getDescription(): string + { + return $this->description; + } + + /** + * @param string $description + */ + public function setDescription(string $description): void + { + $this->description = $description; + } + + /** + * @return \DateTime + */ + public function getDate(): \DateTime + { + return $this->date; + } + + /** + * @param \DateTime $date + */ + public function setDate(\DateTime $date): void + { + $this->date = $date; + } +} diff --git a/src/Bundle/ChillPersonBundle/Form/PersonType.php b/src/Bundle/ChillPersonBundle/Form/PersonType.php index be1be8124..0e4830f04 100644 --- a/src/Bundle/ChillPersonBundle/Form/PersonType.php +++ b/src/Bundle/ChillPersonBundle/Form/PersonType.php @@ -21,21 +21,23 @@ namespace Chill\PersonBundle\Form; +use Chill\CustomFieldsBundle\Form\Type\CustomFieldType; +use Chill\MainBundle\Form\Type\ChillCollectionType; +use Chill\MainBundle\Form\Type\ChillTextareaType; +use Chill\MainBundle\Form\Type\Select2CountryType; +use Chill\MainBundle\Form\Type\Select2LanguageType; +use Chill\PersonBundle\Config\ConfigPersonAltNamesHelper; +use Chill\PersonBundle\Form\Type\GenderType; +use Chill\PersonBundle\Form\Type\PersonAltNameType; +use Chill\PersonBundle\Form\Type\PersonPhoneType; +use Chill\PersonBundle\Form\Type\Select2MaritalStatusType; use Symfony\Component\Form\AbstractType; -use Symfony\Component\Form\FormBuilderInterface; -use Symfony\Component\OptionsResolver\OptionsResolver; -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 Symfony\Component\Form\Extension\Core\Type\TelType; -use Chill\PersonBundle\Form\Type\GenderType; -use Chill\MainBundle\Form\Type\Select2CountryType; -use Chill\MainBundle\Form\Type\Select2LanguageType; -use Chill\CustomFieldsBundle\Form\Type\CustomFieldType; -use Chill\PersonBundle\Form\Type\Select2MaritalStatusType; -use Chill\PersonBundle\Config\ConfigPersonAltNamesHelper; -use Chill\PersonBundle\Form\Type\PersonAltNameType; -use Chill\MainBundle\Form\Type\ChillTextareaType; +use Symfony\Component\Form\Extension\Core\Type\TextType; +use Symfony\Component\Form\FormBuilderInterface; +use Symfony\Component\OptionsResolver\OptionsResolver; class PersonType extends AbstractType { @@ -48,7 +50,7 @@ class PersonType extends AbstractType * @var string[] */ protected $config = array(); - + /** * * @var ConfigPersonAltNamesHelper @@ -80,13 +82,13 @@ class PersonType extends AbstractType ->add('gender', GenderType::class, array( 'required' => true )); - + if ($this->configAltNamesHelper->hasAltNames()) { $builder->add('altNames', PersonAltNameType::class, [ 'by_reference' => false ]); } - + if ($this->config['memo'] === 'visible') { $builder ->add('memo', ChillTextareaType::class, array('required' => false)) @@ -104,11 +106,22 @@ class PersonType extends AbstractType if ($this->config['phonenumber'] === 'visible') { $builder->add('phonenumber', TelType::class, array('required' => false)); } - + if ($this->config['mobilenumber'] === 'visible') { $builder->add('mobilenumber', TelType::class, array('required' => false)); } + $builder->add('otherPhoneNumbers', ChillCollectionType::class, [ + 'entry_type' => PersonPhoneType::class, + 'button_add_label' => 'Add new phone', + 'button_remove_label' => 'Remove phone', + 'required' => false, + 'allow_add' => true, + 'allow_delete' => true, + 'by_reference' => false, + 'label' => false, + ]); + if ($this->config['email'] === 'visible') { $builder->add('email', EmailType::class, array('required' => false)); } @@ -153,7 +166,7 @@ class PersonType extends AbstractType { $resolver->setDefaults(array( 'data_class' => 'Chill\PersonBundle\Entity\Person', - 'validation_groups' => array('general', 'creation') + 'validation_groups' => array('general', 'creation'), )); $resolver->setRequired(array( diff --git a/src/Bundle/ChillPersonBundle/Form/Type/PersonPhoneType.php b/src/Bundle/ChillPersonBundle/Form/Type/PersonPhoneType.php new file mode 100644 index 000000000..3d3f44813 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/Form/Type/PersonPhoneType.php @@ -0,0 +1,44 @@ +add('phonenumber', TelType::class, [ + 'label' => 'Other phonenumber', + 'required' => true, + ]); + + $builder->add('description', TextType::class, [ + 'required' => false, + ]); + } + + public function configureOptions(OptionsResolver $resolver) + { + /* + $resolver + ->setDefault('data_class', PersonPhone::class) + ->setDefault('validation_groups', ['general', 'creation']) + ; + */ + + $resolver + ->setDefaults([ + 'data_class' => PersonPhone::class, + 'validation_groups' => ['general', 'creation'], + ]) + ; + } +} diff --git a/src/Bundle/ChillPersonBundle/Resources/views/Person/edit.html.twig b/src/Bundle/ChillPersonBundle/Resources/views/Person/edit.html.twig index 62cc0c259..b95dbedd9 100644 --- a/src/Bundle/ChillPersonBundle/Resources/views/Person/edit.html.twig +++ b/src/Bundle/ChillPersonBundle/Resources/views/Person/edit.html.twig @@ -83,6 +83,9 @@ {%- if form.mobilenumber is defined -%} {{ form_row(form.mobilenumber, {'label': 'Mobilenumber'}) }} {%- endif -%} + {%- if form.otherPhoneNumbers is defined -%} + {{ form_row(form.otherPhoneNumbers) }} + {%- endif -%} {%- if form.contactInfo is defined -%} {{ form_row(form.contactInfo, {'label': 'Notes on contact information'}) }} {%- endif -%} diff --git a/src/Bundle/ChillPersonBundle/config/validation.yaml b/src/Bundle/ChillPersonBundle/config/validation.yaml index 814db0ee4..5bc37d710 100644 --- a/src/Bundle/ChillPersonBundle/config/validation.yaml +++ b/src/Bundle/ChillPersonBundle/config/validation.yaml @@ -51,8 +51,9 @@ Chill\PersonBundle\Entity\Person: - Chill\MainBundle\Validation\Constraint\PhonenumberConstraint: type: mobile groups: [ general, creation ] - - + otherPhoneNumbers: + - Valid: + traverse: true constraints: - Callback: callback: isAccompanyingPeriodValid @@ -77,3 +78,14 @@ Chill\PersonBundle\Entity\AccompanyingPeriod: constraints: - Callback: callback: isDateConsistent + +Chill\PersonBundle\Entity\PersonPhone: + properties: + phonenumber: + - Regex: + pattern: '/^([\+{1}])([0-9\s*]{4,20})$/' + groups: [ general, creation ] + message: 'Invalid phone number: it should begin with the international prefix starting with "+", hold only digits and be smaller than 20 characters. Ex: +33123456789' + - Chill\MainBundle\Validation\Constraint\PhonenumberConstraint: + type: any + groups: [ general, creation ] diff --git a/src/Bundle/ChillPersonBundle/migrations/Version20210318095831.php b/src/Bundle/ChillPersonBundle/migrations/Version20210318095831.php new file mode 100644 index 000000000..3258bd351 --- /dev/null +++ b/src/Bundle/ChillPersonBundle/migrations/Version20210318095831.php @@ -0,0 +1,36 @@ +addSql('CREATE SEQUENCE chill_person_phone_id_seq INCREMENT BY 1 MINVALUE 1 START 1'); + $this->addSql('CREATE TABLE chill_person_phone (id INT NOT NULL, person_id INT DEFAULT NULL, phonenumber TEXT NOT NULL, description TEXT DEFAULT NULL, date TIMESTAMP(0) WITHOUT TIME ZONE NOT NULL, PRIMARY KEY(id))'); + $this->addSql('CREATE INDEX IDX_72C1F87217BBB47 ON chill_person_phone (person_id)'); + $this->addSql('ALTER TABLE chill_person_phone ADD CONSTRAINT FK_72C1F87217BBB47 FOREIGN KEY (person_id) REFERENCES chill_person_person (id) NOT DEFERRABLE INITIALLY IMMEDIATE'); + } + + public function down(Schema $schema) : void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('DROP SEQUENCE chill_person_phone_id_seq CASCADE'); + $this->addSql('DROP TABLE chill_person_phone'); + } +}