[person bloc] handle null values in date + improve layout on dead person

and age
This commit is contained in:
Julien Fastré 2021-08-23 19:15:00 +02:00
parent bfed062910
commit 7d6def733c
3 changed files with 31 additions and 10 deletions

View File

@ -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;

View File

@ -60,23 +60,32 @@
<i class="fa fa-fw {{ gender }}" title="{{ genderTitle|trans }}"></i>
{%- if person.deathdate is not null -%}
<time datetime="{{ person.deathdate|date('Y-m-d') }}" title="{{ 'Deathdate'|trans }}">
{{ 'Date of death'|trans({'deathdate': person.deathdate|format_date("medium") }) }}
{%- if person.birthdate is not null -%}
<time datetime="{{ person.birthdate|date('Y-m-d') }}" title="{{ 'birthdate'|trans|e('html_attr') }}">
{{ person.birthdate|format_date("medium") }}
</time>
&#x002D;
{%- else -%}
{{ 'Date of death'|trans }}:
{%- endif -%}
<time datetime="{{ person.deathdate|date('Y-m-d') }}" title="{{ 'Deathdate'|trans }}">
{{ person.deathdate|format_date("medium") }}
</time>
{% if options['addAge'] %}
<span class="age">
({{ 'years_old'|trans({ 'age': person.age }) }})
</span>
{% endif %}
{%- elseif person.birthdate is not null -%}
<time datetime="{{ person.birthdate|date('Y-m-d') }}" title="{{ 'Birthdate'|trans }}">
{{ 'Born the date'|trans({'gender': person.gender,
'birthdate': person.birthdate|format_date("medium") }) }}
</time>
{%- if options['addAge'] -%}
<span class="age">
{{ 'years_old'|trans({ 'age': person.age }) }}
({{ 'years_old'|trans({ 'age': person.age }) }})
</span>
{%- endif -%}
{%- endif -%}
</p>
{%- endif -%}
@ -141,7 +150,7 @@
{{ options['customButtons']['replace'] }}
{%- elseif is_granted('CHILL_PERSON_SEE', person) -%}
<li>
<a class="btn btn-sm btn-show" target="_blank" title="{{ 'Show person'|trans }}"
<a class="btn btn-sm btn-show" title="{{ 'Show person'|trans }}"
href="{{ path('chill_person_view', { person_id: person.id }) }}"></a>
</li>
{%- else -%}

View File

@ -52,6 +52,7 @@
'render': 'bloc',
'addLink': true,
'addInfo': true,
'addAge': true,
'addAltNames': false,
'addCenter': true,
'address_multiline': false,