From 320598e9056ec45251879dceeebe438b98c47c28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Fri, 1 May 2020 15:51:30 +0200 Subject: [PATCH] add homeless to person addresses --- Controller/PersonAddressController.php | 6 +- DependencyInjection/ChillPersonExtension.php | 7 +- Doctrine/DQL/AddressPart.php | 2 +- .../AddressPart/AddressPartIsNoAddress.php | 33 +++++ Export/Export/ListPerson.php | 13 +- .../migrations/Version20200422125935.php | 138 ++++++++++++++++++ Resources/views/Address/edit.html.twig | 1 + Resources/views/Address/list.html.twig | 2 +- Resources/views/Address/new.html.twig | 1 + Resources/views/Person/view.html.twig | 2 +- 10 files changed, 197 insertions(+), 8 deletions(-) create mode 100644 Doctrine/DQL/AddressPart/AddressPartIsNoAddress.php create mode 100644 Resources/migrations/Version20200422125935.php diff --git a/Controller/PersonAddressController.php b/Controller/PersonAddressController.php index 633a43940..677d1fd3a 100644 --- a/Controller/PersonAddressController.php +++ b/Controller/PersonAddressController.php @@ -237,7 +237,8 @@ class PersonAddressController extends Controller 'action' => $this->generateUrl('chill_person_address_update', array( 'person_id' => $person->getId(), 'address_id' => $address->getId() - )) + )), + 'has_no_address' => true )); $form->add('submit', SubmitType::class, array( @@ -259,7 +260,8 @@ class PersonAddressController extends Controller 'method' => 'POST', 'action' => $this->generateUrl('chill_person_address_create', array( 'person_id' => $person->getId() - )) + )), + 'has_no_address' => true )); $form->add('submit', SubmitType::class, array( diff --git a/DependencyInjection/ChillPersonExtension.php b/DependencyInjection/ChillPersonExtension.php index 0a0ed3827..bd20b795b 100644 --- a/DependencyInjection/ChillPersonExtension.php +++ b/DependencyInjection/ChillPersonExtension.php @@ -235,8 +235,11 @@ class ChillPersonExtension extends Extension implements PrependExtensionInterfac 'GET_PERSON_ADDRESS_POSTCODE_ID' => AddressPart\AddressPartPostCodeId::class, 'GET_PERSON_ADDRESS_COUNTRY_NAME' => AddressPart\AddressPartCountryName::class, 'GET_PERSON_ADDRESS_COUNTRY_CODE' => AddressPart\AddressPartCountryCode::class, - 'GET_PERSON_ADDRESS_COUNTRY_ID' => AddressPart\AddressPartCountryId::class - ) + 'GET_PERSON_ADDRESS_COUNTRY_ID' => AddressPart\AddressPartCountryId::class, + ), + 'numeric_functions' => [ + 'GET_PERSON_ADDRESS_ISNOADDRESS' => AddressPart\AddressPartIsNoAddress::class, + ] ) ) )); diff --git a/Doctrine/DQL/AddressPart.php b/Doctrine/DQL/AddressPart.php index baa3863d1..98e362dfb 100644 --- a/Doctrine/DQL/AddressPart.php +++ b/Doctrine/DQL/AddressPart.php @@ -73,7 +73,7 @@ abstract class AddressPart extends FunctionNode * Should be one value of the "public" amongst * 'address_id', 'streetaddress1', * 'streetaddress2', 'validfrom', 'postcode_label', 'postcode_code', - * 'postcode_id', 'country_name', 'country_code', 'country_id' + * 'postcode_id', 'country_name', 'country_code', 'country_id', 'isnoaddress' * * @return string */ diff --git a/Doctrine/DQL/AddressPart/AddressPartIsNoAddress.php b/Doctrine/DQL/AddressPart/AddressPartIsNoAddress.php new file mode 100644 index 000000000..bd08bb4df --- /dev/null +++ b/Doctrine/DQL/AddressPart/AddressPartIsNoAddress.php @@ -0,0 +1,33 @@ + + * + * 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 . + */ +namespace Chill\PersonBundle\Doctrine\DQL\AddressPart; + +use Chill\PersonBundle\Doctrine\DQL\AddressPart; + +/** + * + * + * @author Julien Fastré + */ +class AddressPartIsNoAddress extends AddressPart +{ + public function getPart() + { + return 'isnoaddress'; + } +} diff --git a/Export/Export/ListPerson.php b/Export/Export/ListPerson.php index 58359d939..54571e4c0 100644 --- a/Export/Export/ListPerson.php +++ b/Export/Export/ListPerson.php @@ -60,7 +60,7 @@ class ListPerson implements ListInterface, ExportElementValidatedInterface 'mobilenumber', 'contactInfo', 'countryOfBirth', 'nationality', 'address_street_address_1', 'address_street_address_2', 'address_valid_from', 'address_postcode_label', 'address_postcode_code', - 'address_country_name', 'address_country_code' + 'address_country_name', 'address_country_code', 'address_isnoaddress' ); private $slugs = []; @@ -257,6 +257,16 @@ class ListPerson implements ListInterface, ExportElementValidatedInterface return $this->translatableStringHelper->localize(json_decode($value, true)); }; + case 'address_isnoaddress': + return function($value) use ($key) { + if ($value === '_header') { return 'address.address_homeless'; } + + if ($value) { + return 'X'; + } else { + return ''; + } + }; default: // for fields which are associated with person if (in_array($key, $this->fields)) { @@ -429,6 +439,7 @@ class ListPerson implements ListInterface, ExportElementValidatedInterface case 'address_postcode_code': case 'address_country_name': case 'address_country_code': + case 'address_isnoaddress': $qb->addSelect(sprintf( 'GET_PERSON_ADDRESS_%s(person.id, :address_date) AS %s', diff --git a/Resources/migrations/Version20200422125935.php b/Resources/migrations/Version20200422125935.php new file mode 100644 index 000000000..dd38c6487 --- /dev/null +++ b/Resources/migrations/Version20200422125935.php @@ -0,0 +1,138 @@ +addSql('DROP FUNCTION get_last_address(integer, date)'); + $this->addSql(<<<'SQL' +CREATE OR REPLACE FUNCTION public.get_last_address ( + pid integer, + before_date date) +RETURNS TABLE( + person_id integer, + address_id integer, + streetaddress1 varchar(255), + streetaddress2 varchar(255), + validfrom date, + postcode_label varchar(255), + postcode_code varchar(100), + postcode_id integer, + isnoaddress boolean, + country_name json, + country_code varchar(3), + country_id integer) + AS +$BODY$ +SELECT + pid AS person_id, + chill_main_address.id AS address_id, + chill_main_address.streetaddress1, + chill_main_address.streetaddress2, + chill_main_address.validfrom, + chill_main_postal_code.label, + chill_main_postal_code.code, + chill_main_postal_code.id AS postal_code_id, + chill_main_address.isnoaddress AS isnoaddress, + country.name, + country.countrycode, + country.id AS country_id +FROM chill_main_address +JOIN ( + SELECT + chill_main_address.id AS address_id, + validfrom, + rank() OVER (PARTITION BY person_id ORDER BY validfrom DESC) as pos + FROM chill_person_persons_to_addresses + JOIN chill_main_address ON chill_person_persons_to_addresses.address_id = chill_main_address.id + WHERE person_id = pid + AND chill_main_address.validfrom <= before_date +) AS ranking ON ranking.address_id = chill_main_address.id +JOIN chill_main_postal_code ON chill_main_address.postcode_id = chill_main_postal_code.id +JOIN country ON chill_main_postal_code.country_id = country.id +WHERE ranking.pos = 1 +$BODY$ +LANGUAGE sql VOLATILE +COST 100; +SQL + ); + + $this->addSql(<<<'SQL' +CREATE OR REPLACE FUNCTION get_last_address_isnoaddress ( + pid integer, + before_date date) +RETURNS BOOL AS +$BODY$ +SELECT isnoaddress FROM get_last_address(pid, before_date) +$BODY$ +LANGUAGE sql volatile +COST 100; +SQL + ); + + } + + public function down(Schema $schema) : void + { + $this->addSql('DROP FUNCTION public.get_last_address_isnoaddress(integer, date);'); + $this->addSql('DROP FUNCTION get_last_address(integer, date)'); + $this->addSql(<<<'SQL' +CREATE OR REPLACE FUNCTION public.get_last_address ( + pid integer, + before_date date) +RETURNS TABLE( + person_id integer, + address_id integer, + streetaddress1 varchar(255), + streetaddress2 varchar(255), + validfrom date, + postcode_label varchar(255), + postcode_code varchar(100), + postcode_id integer, + country_name json, + country_code varchar(3), + country_id integer) + AS +$BODY$ +SELECT + pid AS person_id, + chill_main_address.id AS address_id, + chill_main_address.streetaddress1, + chill_main_address.streetaddress2, + chill_main_address.validfrom, + chill_main_postal_code.label, + chill_main_postal_code.code, + chill_main_postal_code.id AS postal_code_id, + country.name, + country.countrycode, + country.id AS country_id +FROM chill_main_address +JOIN ( + SELECT + chill_main_address.id AS address_id, + validfrom, + rank() OVER (PARTITION BY person_id ORDER BY validfrom DESC) as pos + FROM chill_person_persons_to_addresses + JOIN chill_main_address ON chill_person_persons_to_addresses.address_id = chill_main_address.id + WHERE person_id = pid + AND chill_main_address.validfrom <= before_date +) AS ranking ON ranking.address_id = chill_main_address.id +JOIN chill_main_postal_code ON chill_main_address.postcode_id = chill_main_postal_code.id +JOIN country ON chill_main_postal_code.country_id = country.id +WHERE ranking.pos = 1 +$BODY$ +LANGUAGE sql VOLATILE +COST 100; +SQL + ); + + } +} diff --git a/Resources/views/Address/edit.html.twig b/Resources/views/Address/edit.html.twig index 897ba8cc5..55f07efcf 100644 --- a/Resources/views/Address/edit.html.twig +++ b/Resources/views/Address/edit.html.twig @@ -26,6 +26,7 @@ {{ form_start(form) }} + {{ form_row(form.isNoAddress) }} {{ form_row(form.streetAddress1) }} {{ form_row(form.streetAddress2) }} {{ form_row(form.postCode) }} diff --git a/Resources/views/Address/list.html.twig b/Resources/views/Address/list.html.twig index 382e87036..87e8924e3 100644 --- a/Resources/views/Address/list.html.twig +++ b/Resources/views/Address/list.html.twig @@ -50,7 +50,7 @@ {{ 'Since %date%'|trans( { '%date%' : address.validFrom|localizeddate('long', 'none') } ) }} - {{ address_macros._render(address, { 'with_valid_from' : false } ) }} + {{ address_macros._render(address, { 'with_valid_from' : false, 'has_no_address': true } ) }} diff --git a/Resources/views/Address/new.html.twig b/Resources/views/Address/new.html.twig index 971e04b17..2513a001c 100644 --- a/Resources/views/Address/new.html.twig +++ b/Resources/views/Address/new.html.twig @@ -26,6 +26,7 @@ {{ form_start(form) }} + {{ form_row(form.isNoAddress) }} {{ form_row(form.streetAddress1) }} {{ form_errors(form.streetAddress1) }} {{ form_row(form.streetAddress2) }} diff --git a/Resources/views/Person/view.html.twig b/Resources/views/Person/view.html.twig index ac93b1d2b..1d71109c8 100644 --- a/Resources/views/Person/view.html.twig +++ b/Resources/views/Person/view.html.twig @@ -178,7 +178,7 @@ This view should receive those arguments:
{{ 'Address'|trans }}
{%- if person.lastAddress is not empty -%} - {{ address._render(person.lastAddress) }} + {{ address._render(person.lastAddress, {'has_no_address': true}) }}