[address] allow to add custom data on addresses

This commit is contained in:
Julien Fastré 2021-03-08 14:11:39 +01:00
parent 14d32aa763
commit 5bf9bbc22e
9 changed files with 172 additions and 17 deletions

View File

@ -146,3 +146,7 @@ Version 1.5.21
- [CRUD] Forward query parameters when pushing button "save and new" in "create" page;
- [Show/hide] Take selects input into account;
Version 1.5.23
==============
- [address] allow to add custom fields to addresses

View File

@ -44,6 +44,13 @@ class Address
*/
private $isNoAddress = false;
/**
* A list of metadata, added by customizable fields
*
* @var array
*/
private $customs = [];
public function __construct()
{
$this->validFrom = new \DateTime();
@ -183,6 +190,29 @@ class Address
return $this;
}
/**
* Get customs informations in the address
*
* @return array
*/
public function getCustoms(): array
{
return $this->customs;
}
/**
* Store custom informations in the address
*
* @param array $customs
* @return $this
*/
public function setCustoms(array $customs): self
{
$this->customs = $customs;
return $this;
}
/**
* Validate the address.
*

View File

@ -39,6 +39,8 @@ use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
* - `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.
* - `customize_data` (callable): allow to add custom field to the form, which
* will be store in the `customs` property
*/
class AddressType extends AbstractType
{
@ -80,9 +82,19 @@ class AddressType extends AbstractType
]);
}
if ($options['customize_data'] !== NULL && is_callable($options['customize_data'])) {
$customsBuilder = $builder->create('customs', NULL, [
'compound' => true,
'label' => false
]);
\call_user_func($options['customize_data'], $customsBuilder);
$builder->add($customsBuilder);
}
if ($options['null_if_empty'] === TRUE) {
$builder->setDataMapper(new AddressDataMapper());
}
}
public function configureOptions(OptionsResolver $resolver)
@ -98,6 +110,8 @@ class AddressType extends AbstractType
->setDefined('null_if_empty')
->setDefault('null_if_empty', false)
->setAllowedTypes('null_if_empty', 'bool')
->setDefined('customize_data')
->setAllowedTypes('customize_data', 'callable')
;
}
}

View File

@ -19,6 +19,9 @@ Chill\MainBundle\Entity\Address:
isNoAddress:
type: boolean
default: false
customs:
type: json
default: []
manyToOne:
postcode:
targetEntity: Chill\MainBundle\Entity\PostalCode

View File

@ -29,4 +29,10 @@ services:
Chill\MainBundle\Templating\Entity\ChillEntityRenderExtension:
tags:
- { name: twig.extension }
- { name: twig.extension }
Chill\MainBundle\Templating\Entity\AddressRendering:
arguments:
$twig: '@templating.engine.twig'
tags:
- { name: 'chill.render_entity' }

View File

@ -1,20 +1,5 @@
{%- macro _render(address, options) -%}
{{ address|chill_entity_render_box( options|default({}) ) }}
{%- set options = { 'with_valid_from' : true }|merge(options|default({})) -%}
{%- set options = { 'has_no_address' : false }|merge(options|default({})) -%}
<div class="chill_address">
{% if options['has_no_address'] == true and address.isNoAddress == true %}
<div class="chill_address_is_noaddress">{{ 'address.consider homeless'|trans }}</div>
{% endif %}
<div class="chill_address_address">
{% if address.streetAddress1 is not empty %}<p class="street street1">{{ address.streetAddress1 }}</p>{% endif %}
{% if address.streetAddress2 is not empty %}<p class="street street2">{{ address.streetAddress2 }}</p>{% endif %}
{% if address.postCode is not empty %}
<p class="postalCode"><span class="code">{{ address.postCode.code }}</span> <span class="name">{{ address.postCode.name }}</span></p>
<p class="country">{{ address.postCode.country.name|localize_translatable_string }}</p>
{% endif %}
</div>
{%- if options['with_valid_from'] == true -%}
<span class="address_since">{{ 'Since %date%'|trans( { '%date%' : address.validFrom|localizeddate('long', 'none') } ) }}</span>
{%- endif -%}
</div>
{%- endmacro -%}

View File

@ -0,0 +1,19 @@
<div class="chill_address">
{% if options['has_no_address'] == true and address.isNoAddress == true %}
<div class="chill_address_is_noaddress">{{ 'address.consider homeless'|trans }}</div>
{% endif %}
<div class="chill_address_address">
{% if address.streetAddress1 is not empty %}<p class="street street1">{{ address.streetAddress1 }}</p>{% endif %}
{% if address.streetAddress2 is not empty %}<p class="street street2">{{ address.streetAddress2 }}</p>{% endif %}
{% if address.postCode is not empty %}
<p class="postalCode"><span class="code">{{ address.postCode.code }}</span> <span class="name">{{ address.postCode.name }}</span></p>
<p class="country">{{ address.postCode.country.name|localize_translatable_string }}</p>
{% endif %}
</div>
{%- if options['with_valid_from'] == true -%}
<span class="address_since">{{ 'Since %date%'|trans( { '%date%' : address.validFrom|localizeddate('long', 'none') } ) }}</span>
{%- endif -%}
{% for k,v in address.customs %}
<strong>{{ ('address.customs.' ~ k)|trans }}&nbsp;:</strong> {{ v }}
{% endfor %}
</div>

View File

@ -182,4 +182,14 @@
<button class="chill-collection__button--add sc-button" data-collection-add-target="{{ form.vars.name|escape('html_attr') }}" data-form-prototype="{{ ('<div>' ~ form_widget(form.vars.prototype) ~ '</div>')|escape('html_attr') }}" >{{ form.vars.button_add_label|trans }}</button>
</div>
{% endif %}
{% endblock %}
{% block address_widget %}
{% for entry in form %}
{% if entry.vars.name != 'customs' %}
{{ form_row(entry) }}
{% else %}
{{ form_widget(entry) }}
{% endif %}
{% endfor %}
{% endblock %}

View File

@ -0,0 +1,84 @@
<?php
/*
* Chill is a software for social workers
*
* Copyright (C) 2014-2021, Champs Libres Cooperative SCRLFS,
* <http://www.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\Templating\Entity;
use Chill\MainBundle\Entity\Address;
use Symfony\Bundle\TwigBundle\TwigEngine;
/**
* Render entity `Address`
*
* options:
*
* `with_valid_from`: add validity date to the rendering
* `has_no_address`: add information if the address "is no address" homeless
*
*/
class AddressRendering implements ChillEntityRenderInterface
{
/**
*
* @var TwigEngine
*/
protected $twig;
public function __construct(TwigEngine $twig)
{
$this->twig = $twig;
}
public function renderBox($entity, array $options): string
{
$defaultOptions = [
'has_no_address' => false,
'with_valid_from' => false
];
return $this->twig->render('@ChillMain/Address/render_box_address.html.twig',
[
'address' => $entity,
'options' => \array_merge($defaultOptions, $options)
]);
}
/**
*
* @param Address $entity
* @param array $options
* @return string
*/
public function renderString($entity, array $options): string
{
return \htmlspecialchars(
$entity->getStreetAddress1().', '.
$entity->getStreetAddress2().', '.
$entity->getPostcode()->getCode().' '.
$entity->getPostcode()->getName()
);
}
public function supports($entity, array $options): bool
{
return $entity instanceof Address;
}
}