From fd9511e745c80e7e23aa25d1980dfef6280a0b1f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Fri, 1 May 2020 15:49:18 +0200 Subject: [PATCH] add homeless to addresses --- CHANGELOG.md | 6 ++ Entity/Address.php | 82 +++++++++++++++++++ Form/DataMapper/AddressDataMapper.php | 17 +++- Form/Type/AddressType.php | 22 ++++- Resources/config/doctrine/Address.orm.yml | 3 + Resources/config/validation.yml | 14 +--- .../migrations/Version20200422122715.php | 22 +++++ Resources/translations/messages.fr.yml | 4 + Resources/translations/validators.fr.yml | 5 ++ Resources/views/Address/macro.html.twig | 6 +- 10 files changed, 163 insertions(+), 18 deletions(-) create mode 100644 Resources/migrations/Version20200422122715.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 648058e67..38b3c3556 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -125,3 +125,9 @@ Version 1.5.18 - [webpack] add namespace for import sass ; - [activity] move activity.scss to own bundle ; + +Master branch +============= + +- [address] add a "homeless" characteristic to addresses ; + diff --git a/Entity/Address.php b/Entity/Address.php index 91390fc1a..76ceb8688 100644 --- a/Entity/Address.php +++ b/Entity/Address.php @@ -2,6 +2,8 @@ namespace Chill\MainBundle\Entity; +use Symfony\Component\Validator\Context\ExecutionContextInterface; + /** * Address */ @@ -35,6 +37,13 @@ class Address */ private $validFrom; + /** + * True if the address is a "no address", aka homeless person, ... + * + * @var bool + */ + private $isNoAddress = false; + public function __construct() { $this->validFrom = new \DateTime(); @@ -142,7 +151,80 @@ class Address $this->validFrom = $validFrom; return $this; } + + /** + * get "isNoAddress" + * + * Indicate true if the address is a fake address (homeless, ...) + * + * @return bool + */ + public function getIsNoAddress(): bool + { + return $this->isNoAddress; + } + + public function isNoAddress(): bool + { + return $this->getIsNoAddress(); + } + /** + * set Is No Address + * + * Indicate true if the address is a fake address (homeless, ...) + * + * @param bool $isNoAddress + * @return $this + */ + public function setIsNoAddress(bool $isNoAddress) + { + $this->isNoAddress = $isNoAddress; + return $this; + } + + /** + * Validate the address. + * + * Check that: + * + * * if the address is not home address: + * * the postal code is present + * * the valid from is not null + * * the address street 1 is greater than 2 + * + * @param ExecutionContextInterface $context + * @param array $payload + */ + public function validate(ExecutionContextInterface $context, $payload) + { + if (!$this->getValidFrom() instanceof \DateTime) { + $context + ->buildViolation("address.date-should-be-set") + ->atPath('validFrom') + ->addViolation(); + } + + if ($this->isNoAddress()) { + return; + } + + if (empty($this->getStreetAddress1())) { + $context + ->buildViolation("address.street1-should-be-set") + ->atPath('streetAddress1') + ->addViolation(); + } + + if (!$this->getPostcode() instanceof PostalCode) { + $context + ->buildViolation("address.postcode-should-be-set") + ->atPath('postCode') + ->addViolation(); + } + } + + public static function createFromAddress(Address $original) : Address { return (new Address()) diff --git a/Form/DataMapper/AddressDataMapper.php b/Form/DataMapper/AddressDataMapper.php index 657ed7636..36383f7e3 100644 --- a/Form/DataMapper/AddressDataMapper.php +++ b/Form/DataMapper/AddressDataMapper.php @@ -60,6 +60,9 @@ class AddressDataMapper implements DataMapperInterface case 'validFrom': $form->setData($address->getValidFrom()); break; + case 'isNoAddress': + $form->setData($address->isNoAddress()); + break; default: break; } @@ -77,18 +80,25 @@ class AddressDataMapper implements DataMapperInterface $address = new Address(); } + $isNoAddress = false; + foreach ($forms as $key => $form) { + if ($key === 'isNoAddress') { + $isNoAddress = $form->get('isNoAddress')->getData(); + } + } + foreach ($forms as $key => $form) { /** @var FormInterface $form */ switch($key) { case 'postCode': - if (!$form->getData() instanceof PostalCode) { + if (!$form->getData() instanceof PostalCode && !$isNoAddress) { $address = null; return; } $address->setPostcode($form->getData()); break; case 'streetAddress1': - if (empty($form->getData())) { + if (empty($form->getData()) && !$isNoAddress) { $address = null; return; } @@ -100,6 +110,9 @@ class AddressDataMapper implements DataMapperInterface case 'validFrom': $address->setValidFrom($form->getData()); break; + case 'isNoAddress': + $address->setIsNoAddress($form->getData()); + break; default: break; } diff --git a/Form/Type/AddressType.php b/Form/Type/AddressType.php index 80a29cf5b..4a0e222b8 100644 --- a/Form/Type/AddressType.php +++ b/Form/Type/AddressType.php @@ -27,6 +27,7 @@ use Symfony\Component\Form\Extension\Core\Type\DateType; use Chill\MainBundle\Entity\Address; use Chill\MainBundle\Form\Type\PostalCodeType; use Chill\MainBundle\Form\DataMapper\AddressDataMapper; +use Symfony\Component\Form\Extension\Core\Type\ChoiceType; /** * A type to create/update Address entity @@ -45,7 +46,7 @@ class AddressType extends AbstractType { $builder ->add('streetAddress1', TextType::class, array( - 'required' => true + 'required' => !$options['has_no_address'] // true if has no address is false )) ->add('streetAddress2', TextType::class, array( 'required' => false @@ -53,7 +54,7 @@ class AddressType extends AbstractType ->add('postCode', PostalCodeType::class, array( 'label' => 'Postal code', 'placeholder' => 'Choose a postal code', - 'required' => true + 'required' => !$options['has_no_address'] // true if has no address is false )) ; @@ -67,7 +68,19 @@ class AddressType extends AbstractType ); } - if ($options['null_if_emtpy'] === TRUE) { + if ($options['has_no_address']) { + $builder + ->add('isNoAddress', ChoiceType::class, [ + 'required' => true, + 'choices' => [ + 'address.consider homeless' => true, + 'address.real address' => false + ], + 'label' => 'address.address_homeless' + ]); + } + + if ($options['null_if_empty'] === TRUE) { $builder->setDataMapper(new AddressDataMapper()); } } @@ -79,6 +92,9 @@ class AddressType extends AbstractType ->setDefined('has_valid_from') ->setAllowedTypes('has_valid_from', 'bool') ->setDefault('has_valid_from', true) + ->setDefined('has_no_address') + ->setDefault('has_no_address', false) + ->setAllowedTypes('has_no_address', 'bool') ->setDefined('null_if_empty') ->setDefault('null_if_empty', false) ->setAllowedTypes('null_if_empty', 'bool') diff --git a/Resources/config/doctrine/Address.orm.yml b/Resources/config/doctrine/Address.orm.yml index 5ecc8a627..efba9070a 100644 --- a/Resources/config/doctrine/Address.orm.yml +++ b/Resources/config/doctrine/Address.orm.yml @@ -16,6 +16,9 @@ Chill\MainBundle\Entity\Address: length: 255 validFrom: type: date + isNoAddress: + type: boolean + default: false manyToOne: postcode: targetEntity: Chill\MainBundle\Entity\PostalCode diff --git a/Resources/config/validation.yml b/Resources/config/validation.yml index f592845de..dd5f8d985 100644 --- a/Resources/config/validation.yml +++ b/Resources/config/validation.yml @@ -36,18 +36,8 @@ Chill\MainBundle\Entity\Center: min: 2 Chill\MainBundle\Entity\Address: - properties: - streetAddress1: - - Length: - min: 2 - max: 250 - - NotNull: ~ - - NotBlank: ~ - postcode: - - NotNull: ~ - validFrom: - - NotNull: ~ - - Date: ~ + constraints: + - Callback: validate Chill\MainBundle\Entity\PostalCode: properties: diff --git a/Resources/migrations/Version20200422122715.php b/Resources/migrations/Version20200422122715.php new file mode 100644 index 000000000..2ce63b41a --- /dev/null +++ b/Resources/migrations/Version20200422122715.php @@ -0,0 +1,22 @@ +addSql('ALTER TABLE chill_main_address ADD isNoAddress BOOLEAN NOT NULL DEFAULT FALSE'); + } + + public function down(Schema $schema) : void + { + $this->addSql('ALTER TABLE chill_main_address DROP isNoAddress'); + } +} diff --git a/Resources/translations/messages.fr.yml b/Resources/translations/messages.fr.yml index de70316fe..dff771512 100644 --- a/Resources/translations/messages.fr.yml +++ b/Resources/translations/messages.fr.yml @@ -59,6 +59,10 @@ Street address2: Adresse ligne 2 Postal code: Code postal Valid from: Valide à partir du Choose a postal code: Choisir un code postal +address: + address_homeless: L'adresse est-elle celle d'un domicile fixe ? + real address: Adresse d'un domicile + consider homeless: N'est pas l'adresse d'un domicile (SDF) #serach Your search is empty. Please provide search terms.: La recherche est vide. Merci de fournir des termes de recherche. diff --git a/Resources/translations/validators.fr.yml b/Resources/translations/validators.fr.yml index 766bee9da..23eccd91d 100644 --- a/Resources/translations/validators.fr.yml +++ b/Resources/translations/validators.fr.yml @@ -18,3 +18,8 @@ This username or email does not exists: Cet utilisateur ou email n'est pas prés This is not a landline phonenumber: Ce numéro n'est pas une ligne fixe valide This is not a mobile phonenumber: Ce numéro n'est pas un numéro de portable valide This is not a valid phonenumber: Ce numéro de téléphone n'est pas valide + +address: + street1-should-be-set: Une ligne d'adresse doit être présente + date-should-be-set: La date de début de validité doit être présente + postcode-should-be-set: Le code postal doit être renseigné diff --git a/Resources/views/Address/macro.html.twig b/Resources/views/Address/macro.html.twig index 41cf4d573..fd74ec115 100644 --- a/Resources/views/Address/macro.html.twig +++ b/Resources/views/Address/macro.html.twig @@ -1,6 +1,10 @@ {%- macro _render(address, options) -%} - {%- set options = { 'with_valid_from' : true }|merge(options|default({})) -%} + {%- set options = { 'with_valid_from' : true }|merge(options|default({})) -%} + {%- set options = { 'has_no_address' : false }|merge(options|default({})) -%}
+ {% if options['has_no_address'] == true and address.isNoAddress == true %} +
{{ 'address.consider homeless'|trans }}
+ {% endif %}
{% if address.streetAddress1 is not empty %}

{{ address.streetAddress1 }}

{% endif %} {% if address.streetAddress2 is not empty %}

{{ address.streetAddress2 }}

{% endif %}