mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-06-13 05:44:24 +00:00
Merge remote-tracking branch 'origin/master'
This commit is contained in:
commit
6942248457
@ -39,4 +39,5 @@ Master branch
|
|||||||
=============
|
=============
|
||||||
|
|
||||||
- fix long url in report download. The exports parameters are now stored in redis.
|
- fix long url in report download. The exports parameters are now stored in redis.
|
||||||
|
- add an option to allow address to be empty if street or postcode is not set. Used for embedding address in another form, when address is not required.
|
||||||
|
|
||||||
|
@ -28,6 +28,8 @@ class Address
|
|||||||
private $postcode;
|
private $postcode;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
* Indicates when the address starts validation. Used to build an history
|
||||||
|
* of address. By default, the current date.
|
||||||
*
|
*
|
||||||
* @var \DateTime
|
* @var \DateTime
|
||||||
*/
|
*/
|
||||||
|
@ -59,7 +59,11 @@ class PostalCodeChoiceLoader implements ChoiceLoaderInterface
|
|||||||
$choices = [];
|
$choices = [];
|
||||||
|
|
||||||
foreach($values as $value) {
|
foreach($values as $value) {
|
||||||
$choices[] = $this->postalCodeRepository->find($value);
|
if (empty($value)) {
|
||||||
|
$choices[] = null;
|
||||||
|
} else {
|
||||||
|
$choices[] = $this->postalCodeRepository->find($value);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $choices;
|
return $choices;
|
||||||
|
108
Form/DataMapper/AddressDataMapper.php
Normal file
108
Form/DataMapper/AddressDataMapper.php
Normal file
@ -0,0 +1,108 @@
|
|||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* Copyright (C) 2019 Champs Libres Cooperative <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/>.
|
||||||
|
*/
|
||||||
|
namespace Chill\MainBundle\Form\DataMapper;
|
||||||
|
|
||||||
|
use Symfony\Component\Form\DataMapperInterface;
|
||||||
|
use Chill\MainBundle\Entity\Address;
|
||||||
|
use Chill\MainBundle\Entity\PostalCode;
|
||||||
|
use Symfony\Component\Form\FormInterface;
|
||||||
|
use Symfony\Component\Form\Exception\UnexpectedTypeException;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Add a data mapper to Address.
|
||||||
|
*
|
||||||
|
* If the address is incomplete, the data mapper returns null
|
||||||
|
*/
|
||||||
|
class AddressDataMapper implements DataMapperInterface
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param Address $address
|
||||||
|
* @param \Iterator $forms
|
||||||
|
*/
|
||||||
|
public function mapDataToForms($address, $forms)
|
||||||
|
{
|
||||||
|
if (NULL === $address) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!$address instanceof Address) {
|
||||||
|
throw new UnexpectedTypeException($address, Address::class);
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($forms as $key => $form) {
|
||||||
|
/** @var FormInterface $form */
|
||||||
|
switch ($key) {
|
||||||
|
case 'streetAddress1':
|
||||||
|
$form->setData($address->getStreetAddress1());
|
||||||
|
break;
|
||||||
|
case 'streetAddress2':
|
||||||
|
$form->setData($address->getStreetAddress2());
|
||||||
|
break;
|
||||||
|
case 'postCode':
|
||||||
|
$form->setData($address->getPostcode());
|
||||||
|
break;
|
||||||
|
case 'validFrom':
|
||||||
|
$form->setData($address->getValidFrom());
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @param \Iterator $forms
|
||||||
|
* @param Address $address
|
||||||
|
*/
|
||||||
|
public function mapFormsToData($forms, &$address)
|
||||||
|
{
|
||||||
|
if (!$address instanceof Address) {
|
||||||
|
$address = new Address();
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach ($forms as $key => $form) {
|
||||||
|
/** @var FormInterface $form */
|
||||||
|
switch($key) {
|
||||||
|
case 'postCode':
|
||||||
|
if (!$form->getData() instanceof PostalCode) {
|
||||||
|
$address = null;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$address->setPostcode($form->getData());
|
||||||
|
break;
|
||||||
|
case 'streetAddress1':
|
||||||
|
if (empty($form->getData())) {
|
||||||
|
$address = null;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
$address->setStreetAddress1($form->getData());
|
||||||
|
break;
|
||||||
|
case 'streetAddress2':
|
||||||
|
$address->setStreetAddress2($form->getData());
|
||||||
|
break;
|
||||||
|
case 'validFrom':
|
||||||
|
$address->setValidFrom($form->getData());
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -26,12 +26,18 @@ use Symfony\Component\Form\Extension\Core\Type\TextType;
|
|||||||
use Symfony\Component\Form\Extension\Core\Type\DateType;
|
use Symfony\Component\Form\Extension\Core\Type\DateType;
|
||||||
use Chill\MainBundle\Entity\Address;
|
use Chill\MainBundle\Entity\Address;
|
||||||
use Chill\MainBundle\Form\Type\PostalCodeType;
|
use Chill\MainBundle\Form\Type\PostalCodeType;
|
||||||
|
use Chill\MainBundle\Form\DataMapper\AddressDataMapper;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A type to create/update Address entity
|
* A type to create/update Address entity
|
||||||
*
|
*
|
||||||
* @author Julien Fastré <julien.fastre@champs-libres.coop>
|
* Options:
|
||||||
* @author Champs Libres <info@champs-libres.coop>
|
*
|
||||||
|
* - `has_valid_from` (boolean): show if an entry "has valid from" must be
|
||||||
|
* shown.
|
||||||
|
* - `null_if_empty` (boolean): replace the address type by null if the street
|
||||||
|
* or the postCode is empty. This is useful when the address is not required and
|
||||||
|
* embedded in another form.
|
||||||
*/
|
*/
|
||||||
class AddressType extends AbstractType
|
class AddressType extends AbstractType
|
||||||
{
|
{
|
||||||
@ -49,17 +55,33 @@ class AddressType extends AbstractType
|
|||||||
'placeholder' => 'Choose a postal code',
|
'placeholder' => 'Choose a postal code',
|
||||||
'required' => true
|
'required' => true
|
||||||
))
|
))
|
||||||
|
;
|
||||||
|
|
||||||
|
if ($options['has_valid_from']) {
|
||||||
|
$builder
|
||||||
->add('validFrom', DateType::class, array(
|
->add('validFrom', DateType::class, array(
|
||||||
'required' => true,
|
'required' => true,
|
||||||
'widget' => 'single_text',
|
'widget' => 'single_text',
|
||||||
'format' => 'dd-MM-yyyy'
|
'format' => 'dd-MM-yyyy'
|
||||||
)
|
)
|
||||||
)
|
);
|
||||||
;
|
}
|
||||||
|
|
||||||
|
if ($options['null_if_emtpy'] === TRUE) {
|
||||||
|
$builder->setDataMapper(new AddressDataMapper());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function configureOptions(OptionsResolver $resolver)
|
public function configureOptions(OptionsResolver $resolver)
|
||||||
{
|
{
|
||||||
$resolver->setDefault('data_class', Address::class);
|
$resolver
|
||||||
|
->setDefault('data_class', Address::class)
|
||||||
|
->setDefined('has_valid_from')
|
||||||
|
->setAllowedTypes('has_valid_from', 'bool')
|
||||||
|
->setDefault('has_valid_from', true)
|
||||||
|
->setDefined('null_if_empty')
|
||||||
|
->setDefault('null_if_empty', false)
|
||||||
|
->setAllowedTypes('null_if_empty', 'bool')
|
||||||
|
;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -41,6 +41,8 @@ Chill\MainBundle\Entity\Address:
|
|||||||
- Length:
|
- Length:
|
||||||
min: 2
|
min: 2
|
||||||
max: 250
|
max: 250
|
||||||
|
- NotNull: ~
|
||||||
|
- NotBlank: ~
|
||||||
postcode:
|
postcode:
|
||||||
- NotNull: ~
|
- NotNull: ~
|
||||||
validFrom:
|
validFrom:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user