From 7d6def733c91ca298fd1418943ce01dc1cc5edfe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Fastr=C3=A9?= Date: Mon, 23 Aug 2021 19:15:00 +0200 Subject: [PATCH] [person bloc] handle null values in date + improve layout on dead person and age --- .../ChillPersonBundle/Entity/Person.php | 15 +++++++++-- .../Resources/views/Entity/person.html.twig | 25 +++++++++++++------ .../views/Person/list_with_period.html.twig | 1 + 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/src/Bundle/ChillPersonBundle/Entity/Person.php b/src/Bundle/ChillPersonBundle/Entity/Person.php index 084a24ca3..35db2ef83 100644 --- a/src/Bundle/ChillPersonBundle/Entity/Person.php +++ b/src/Bundle/ChillPersonBundle/Entity/Person.php @@ -737,10 +737,21 @@ class Person implements HasCenterInterface, TrackCreationInterface, TrackUpdateI return $this->birthdate; } - public function getAge(): ?int + /** + * Return the age of a person, calculated at the date 'now'. + * + * If the person has a deathdate, calculate the age at the deathdate. + * + * @param string $at a valid string to create a DateTime + * @return int|null + */ + public function getAge($at = 'now'): ?int { if ($this->birthdate instanceof \DateTimeInterface) { - return date_diff($this->birthdate, date_create('now'))->format("%y"); + if ($this->deathdate instanceof \DateTimeInterface) { + return date_diff($this->birthdate, $this->deathdate)->format("%y"); + } + return date_diff($this->birthdate, date_create($at))->format("%y"); } return null; diff --git a/src/Bundle/ChillPersonBundle/Resources/views/Entity/person.html.twig b/src/Bundle/ChillPersonBundle/Resources/views/Entity/person.html.twig index 391fd164b..ba7f17723 100644 --- a/src/Bundle/ChillPersonBundle/Resources/views/Entity/person.html.twig +++ b/src/Bundle/ChillPersonBundle/Resources/views/Entity/person.html.twig @@ -60,23 +60,32 @@ {%- if person.deathdate is not null -%} - + {%- if person.birthdate is not null -%} + + - + {%- else -%} + {{ 'Date of death'|trans }}: + {%- endif -%} - - - {%- else -%} + {% if options['addAge'] %} + + ({{ 'years_old'|trans({ 'age': person.age }) }}) + + {% endif %} + {%- elseif person.birthdate is not null -%} {%- if options['addAge'] -%} - {{ 'years_old'|trans({ 'age': person.age }) }} + ({{ 'years_old'|trans({ 'age': person.age }) }}) {%- endif -%} - {%- endif -%}

{%- endif -%} @@ -141,7 +150,7 @@ {{ options['customButtons']['replace'] }} {%- elseif is_granted('CHILL_PERSON_SEE', person) -%}
  • -
  • {%- else -%} diff --git a/src/Bundle/ChillPersonBundle/Resources/views/Person/list_with_period.html.twig b/src/Bundle/ChillPersonBundle/Resources/views/Person/list_with_period.html.twig index 9db2c8377..f586d7ac0 100644 --- a/src/Bundle/ChillPersonBundle/Resources/views/Person/list_with_period.html.twig +++ b/src/Bundle/ChillPersonBundle/Resources/views/Person/list_with_period.html.twig @@ -52,6 +52,7 @@ 'render': 'bloc', 'addLink': true, 'addInfo': true, + 'addAge': true, 'addAltNames': false, 'addCenter': true, 'address_multiline': false,