xref: /webtrees/resources/views/lists/individuals-table.phtml (revision 5497e2b29703204ef476334f6c75f032f0e1f7cc)
1<?php
2declare(strict_types=1);
3
4use Fisharebest\Webtrees\Auth;
5use Fisharebest\Webtrees\Carbon;
6use Fisharebest\Webtrees\Date;
7use Fisharebest\Webtrees\GedcomTag;
8use Fisharebest\Webtrees\I18N;
9use Fisharebest\Webtrees\Module\ModuleChartInterface;
10use Fisharebest\Webtrees\Module\ModuleInterface;
11use Fisharebest\Webtrees\Module\RelationshipsChartModule;
12use Fisharebest\Webtrees\Services\ModuleService;
13use Fisharebest\Webtrees\View;
14use Ramsey\Uuid\Uuid;
15
16// lists requires a unique ID in case there are multiple lists per page
17$table_id = 'table-indi-' . Uuid::uuid4()->toString();
18
19$hundred_years_ago = Carbon::now()->subYears(100)->julianDay();
20
21$unique_indis = []; // Don't double-count indis with multiple names.
22
23$today_jd             = unixtojd();
24$show_estimated_dates = (bool) $tree->getPreference('SHOW_EST_LIST_DATES');
25
26$module = app(ModuleService::class)
27    ->findByComponent(ModuleChartInterface::class, $tree, Auth::user())
28    ->first(static function (ModuleInterface $module) {
29        return $module instanceof RelationshipsChartModule;
30    });
31?>
32
33<?php View::push('javascript') ?>
34<script>
35
36$("#<?= e($table_id) ?>").dataTable({
37    dom: '<"H"<"filtersH_<?= e($table_id) ?>">T<"dt-clear">pf<"dt-clear">irl>t<"F"pl<"dt-clear"><"filtersF_<?= e($table_id) ?>">>',
38    autoWidth: false,
39    processing: true,
40    retrieve: true,
41    columns: [
42        /* Given names  */ { type: "text" },
43        /* Surnames     */ { type: "text" },
44        /* SOSA numnber */ { type: "num", visible: <?= json_encode($sosa) ?> },
45        /* Birth date   */ { type: "num" },
46        /* Anniversary  */ { type: "num" },
47        /* Birthplace   */ { type: "text" },
48        /* Children     */ { type: "num" },
49        /* Deate date   */ { type: "num" },
50        /* Anniversary  */ { type: "num" },
51        /* Age          */ { type: "num" },
52        /* Death place  */ { type: "text" },
53        /* Last change  */ { visible: <?= json_encode($tree->getPreference('SHOW_LAST_CHANGE')) ?> },
54        /* Filter sex   */ { sortable: false },
55        /* Filter birth */ { sortable: false },
56        /* Filter death */ { sortable: false },
57        /* Filter tree  */ { sortable: false }
58    ],
59    sorting: <?= json_encode($sosa ? [[4, 'asc']] : [[1, 'asc']]) ?>
60});
61
62$("#<?= e($table_id) ?>")
63    /* Hide/show parents */
64    .on("click", ".btn-toggle-parents", function() {
65        $(this).toggleClass("ui-state-active");
66        $(".parents", $(this).closest("table").DataTable().rows().nodes()).slideToggle();
67    })
68    /* Hide/show statistics */
69    .on("click", ".btn-toggle-statistics", function() {
70        $(this).toggleClass("ui-state-active");
71        $("#individual-charts-<?= e($table_id) ?>").slideToggle({
72            complete: function () {
73                // Trigger resize to redraw the chart
74                $('div[id^="google-chart-"]').resize();
75            }
76        });
77    })
78    /* Filter buttons in table header */
79    .on("click", "button[data-filter-column]", function() {
80        var btn = $(this);
81        // De-activate the other buttons in this button group
82        btn.siblings().removeClass("active");
83        // Apply (or clear) this filter
84        var col = $("#<?= e($table_id) ?>").DataTable().column(btn.data("filter-column"));
85        if (btn.hasClass("active")) {
86            col.search("").draw();
87        } else {
88            col.search(btn.data("filter-value")).draw();
89        }
90    });
91
92</script>
93<?php View::endpush() ?>
94
95<?php
96$max_age = (int) $tree->getPreference('MAX_ALIVE_AGE');
97
98// Inititialise chart data
99$deat_by_age = [];
100for ($age = 0; $age <= $max_age; $age++) {
101    $deat_by_age[$age]['M'] = 0;
102    $deat_by_age[$age]['F'] = 0;
103    $deat_by_age[$age]['U'] = 0;
104}
105$birt_by_decade = [];
106$deat_by_decade = [];
107for ($year = 1400; $year < 2050; $year += 10) {
108    $birt_by_decade[$year]['M'] = 0;
109    $birt_by_decade[$year]['F'] = 0;
110    $birt_by_decade[$year]['U'] = 0;
111    $deat_by_decade[$year]['M'] = 0;
112    $deat_by_decade[$year]['F'] = 0;
113    $deat_by_decade[$year]['U'] = 0;
114}
115
116$birthData = [
117    [
118        [
119            'label' => I18N::translate('Century'),
120            'type'  => 'date',
121        ], [
122            'label' => I18N::translate('Males'),
123            'type'  => 'number',
124        ], [
125            'label' => I18N::translate('Females'),
126            'type'  => 'number',
127        ],
128    ]
129];
130
131$deathData = [
132    [
133        [
134            'label' => I18N::translate('Century'),
135            'type'  => 'date',
136        ], [
137            'label' => I18N::translate('Males'),
138            'type'  => 'number',
139        ], [
140            'label' => I18N::translate('Females'),
141            'type'  => 'number',
142        ],
143    ]
144];
145
146$deathAgeData = [
147    [
148        I18N::translate('Age'),
149        I18N::translate('Males'),
150        I18N::translate('Females'),
151        I18N::translate('Average age'),
152    ]
153];
154
155?>
156
157<div class="indi-list">
158    <table id="<?= e($table_id) ?>" class="table-bordered"
159        <?= view('lists/datatables-attributes') ?>
160    >
161        <thead>
162            <tr>
163                <th colspan="16">
164                    <div class="btn-toolbar d-flex justify-content-between mb-2" role="toolbar">
165                        <div class="btn-group" data-toggle="buttons">
166                            <button
167                                class="btn btn-secondary"
168                                data-filter-column="12"
169                                data-filter-value="M"
170                                title="<?= I18N::translate('Show only males.') ?>"
171                            >
172                                <?= view('icons/sex-M') ?>
173                            </button>
174                            <button
175                                class="btn btn-secondary"
176                                data-filter-column="12"
177                                data-filter-value="F"
178                                title="<?= I18N::translate('Show only females.') ?>"
179                            >
180                                <?= view('icons/sex-F') ?>
181                            </button>
182                            <button
183                                class="btn btn-secondary"
184                                data-filter-column="12"
185                                data-filter-value="U"
186                                title="<?= I18N::translate('Show only individuals for whom the gender is not known.') ?>"
187                            >
188                                <?= view('icons/sex-U') ?>
189                            </button>
190                        </div>
191                        <div class="btn-group" data-toggle="buttons">
192                            <button
193                                class="btn btn-secondary"
194                                data-filter-column="14"
195                                data-filter-value="N"
196                                title="<?= I18N::translate('Show individuals who are alive or couples where both partners are alive.') ?>"
197                            >
198                                <?= I18N::translate('Alive') ?>
199                            </button>
200                            <button
201                                class="btn btn-secondary"
202                                data-filter-column="14"
203                                data-filter-value="Y"
204                                title="<?= I18N::translate('Show individuals who are dead or couples where both partners are dead.') ?>"
205                            >
206                                <?= I18N::translate('Dead') ?>
207                            </button>
208                            <button
209                                class="btn btn-secondary"
210                                data-filter-column="14"
211                                data-filter-value="YES"
212                                title="<?= I18N::translate('Show individuals who died more than 100 years ago.') ?>"
213                            >
214                                <?= I18N::translate('Death') ?>&gt;100
215                            </button>
216                            <button
217                                class="btn btn-secondary"
218                                data-filter-column="14"
219                                data-filter-value="Y100"
220                                title="<?= I18N::translate('Show individuals who died within the last 100 years.') ?>"
221                            >
222                                <?= I18N::translate('Death') ?>&lt;=100
223                            </button>
224                        </div>
225                        <div class="btn-group" data-toggle="buttons">
226                            <button
227                                class="btn btn-secondary"
228                                data-filter-column="13"
229                                data-filter-value="YES"
230                                title="<?= I18N::translate('Show individuals born more than 100 years ago.') ?>"
231                            >
232                                <?= I18N::translate('Birth') ?>&gt;100
233                            </button>
234                            <button
235                                class="btn btn-secondary"
236                                data-filter-column="13"
237                                data-filter-value="Y100"
238                                title="<?= I18N::translate('Show individuals born within the last 100 years.') ?>"
239                            >
240                                <?= I18N::translate('Birth') ?>&lt;=100
241                            </button>
242                        </div>
243                        <div class="btn-group" data-toggle="buttons">
244                            <button
245                                class="btn btn-secondary"
246                                data-filter-column="15"
247                                data-filter-value="R"
248                                title="<?= I18N::translate('Show “roots” couples or individuals. These individuals may also be called “patriarchs”. They are individuals who have no parents recorded in the database.') ?>"
249                            >
250                                <?= I18N::translate('Roots') ?>
251                            </button>
252                            <button
253                                class="btn btn-secondary"
254                                data-filter-column="15"
255                                data-filter-value="L"
256                                title="<?= I18N::translate('Show “leaves” couples or individuals. These are individuals who are alive but have no children recorded in the database.') ?>"
257                            >
258                                <?= I18N::translate('Leaves') ?>
259                            </button>
260                        </div>
261                    </div>
262                </th>
263            </tr>
264            <tr>
265                <th><?= I18N::translate('Given names') ?></th>
266                <th><?= I18N::translate('Surname') ?></th>
267                <th><?= /* I18N: Abbreviation for “Sosa-Stradonitz number”. This is an individual’s surname, so may need transliterating into non-latin alphabets. */
268                    I18N::translate('Sosa') ?></th>
269                <th><?= I18N::translate('Birth') ?></th>
270                <th>
271                    <span title="<?= I18N::translate('Anniversary') ?>">
272                        <?= view('icons/anniversary') ?>
273                    </span>
274                </th>
275                <th><?= I18N::translate('Place') ?></th>
276                <th>
277                    <i class="icon-children" title="<?= I18N::translate('Children') ?>"></i>
278                </th>
279                <th><?= I18N::translate('Death') ?></th>
280                <th>
281                    <span title="<?= I18N::translate('Anniversary') ?>">
282                        <?= view('icons/anniversary') ?>
283                    </span>
284                </th>
285                <th><?= I18N::translate('Age') ?></th>
286                <th><?= I18N::translate('Place') ?></th>
287                <th><?= I18N::translate('Last change') ?></th>
288                <th hidden></th>
289                <th hidden></th>
290                <th hidden></th>
291                <th hidden></th>
292            </tr>
293        </thead>
294        <tfoot>
295            <tr>
296                <th colspan="16">
297                    <div class="btn-toolbar">
298                        <div class="btn-group">
299                            <button class="ui-state-default btn-toggle-parents">
300                                <?= I18N::translate('Show parents') ?>
301                            </button>
302                            <button class="ui-state-default btn-toggle-statistics">
303                                <?= I18N::translate('Show statistics charts') ?>
304                            </button>
305                        </div>
306                    </div>
307                </th>
308            </tr>
309        </tfoot>
310
311        <tbody>
312            <?php foreach ($individuals as $key => $individual) : ?>
313            <tr class="<?= $individual->isPendingDeletion() ? 'wt-old' : ($individual->isPendingAddition() ? 'wt-new' : '') ?>">
314                <td colspan="2" data-sort="<?= e(str_replace([',', '@P.N.', '@N.N.'], 'AAAA', implode(',', array_reverse(explode(',', $individual->sortName()))))) ?>">
315                    <?php foreach ($individual->getAllNames() as $num => $name) : ?>
316                        <a title="<?= $name['type'] === 'NAME' ? '' : GedcomTag::getLabel($name['type'], $individual) ?>" href="<?= e($individual->url()) ?>" class="<?= $num === $individual->getPrimaryName() ? 'name2' : '' ?>">
317                            <?= $name['full'] ?>
318                        </a>
319                        <?php if ($num === $individual->getPrimaryName()) : ?>
320                            <small><?= view('icons/sex-' . $individual->sex()) ?></small>
321                        <?php endif ?>
322                        <br>
323                    <?php endforeach ?>
324                    <?= $individual->getPrimaryParentsNames('parents details1', 'none') ?>
325                </td>
326
327                <td hidden data-sort="<?= e(str_replace([',', '@P.N.', '@N.N.'], 'AAAA', $individual->sortName())) ?>"></td>
328
329                <td class="text-center" data-sort="<?= $key ?>">
330                    <?php if ($sosa) : ?>
331                        <?php if ($module instanceof RelationshipsChartModule) : ?>
332                            <a href="<?= e($module->chartUrl($individuals[1], ['xref2' => $individual->xref()])) ?>" rel="nofollow" title="<?= I18N::translate('Relationships') ?>" rel="nofollow">
333                                <?= I18N::number($key) ?>
334                            </a>
335                        <?php else : ?>
336                            <?= I18N::number($key) ?>
337                        <?php endif ?>
338                    <?php endif ?>
339                </td>
340
341                <!-- Birth date -->
342                <td data-sort="<?= $individual->getEstimatedBirthDate()->julianDay() ?>">
343                    <?php $birth_dates = $individual->getAllBirthDates(); ?>
344
345                    <?php foreach ($birth_dates as $n => $birth_date) : ?>
346                        <?= $birth_date->display(true) ?>
347                        <br>
348                    <?php endforeach ?>
349
350                    <?php if (empty($birth_dates) && $show_estimated_dates): ?>
351                        <?= $individual->getEstimatedBirthDate()->display(true) ?>
352                    <?php endif ?>
353                </td>
354
355                <!-- Birth anniversary -->
356                <td class="text-center" data-sort="<?= -$individual->getEstimatedBirthDate()->julianDay() ?>">
357                    <?php if (isset($birth_dates[0]) && $birth_dates[0]->gregorianYear() >= 1550 && $birth_dates[0]->gregorianYear() < 2030 && !isset($unique_indis[$individual->xref()])) : ?>
358                        <?php
359                            ++$birt_by_decade[(int) ($birth_dates[0]->gregorianYear() / 10) * 10][$individual->sex()];
360                        ?>
361                        <?= Date::getAge($birth_dates[0]) ?>
362                    <?php endif ?>
363                </td>
364
365                <!-- Birth place -->
366                <td>
367                    <?php foreach ($individual->getAllBirthPlaces() as $n => $birth_place) : ?>
368                        <?= $birth_place->shortName(true) ?>
369                        <br>
370                    <?php endforeach ?>
371                </td>
372
373                <!-- Number of children -->
374                <td class="text-center" data-sort="<?= $individual->numberOfChildren() ?>">
375                    <?= I18N::number($individual->numberOfChildren()) ?>
376                </td>
377
378                <!--    Death date -->
379                <?php $death_dates = $individual->getAllDeathDates() ?>
380                <td data-sort="<?= $individual->getEstimatedDeathDate()->julianDay() ?>">
381                    <?php foreach ($death_dates as $num => $death_date) : ?>
382                        <?= $death_date->display(true) ?>
383                    <br>
384                    <?php endforeach ?>
385
386                    <?php if (empty($death_dates) && $show_estimated_dates && $individual->getEstimatedDeathDate()->minimumDate()->minimumJulianDay() < $today_jd): ?>
387                        <?= $individual->getEstimatedDeathDate()->display(true) ?>
388                    <?php endif ?>
389                </td>
390
391                <!-- Death anniversary -->
392                <td class="text-center" data-sort="<?= -$individual->getEstimatedDeathDate()->julianDay() ?>">
393                    <?php if (isset($death_dates[0]) && $death_dates[0]->gregorianYear() >= 1550 && $death_dates[0]->gregorianYear() < 2030 && !isset($unique_indis[$individual->xref()])) : ?>
394                        <?php
395                            ++$deat_by_decade[(int) ($death_dates[0]->gregorianYear() / 10) * 10][$individual->sex()];
396                        ?>
397                        <?= Date::getAge($death_dates[0]) ?>
398                    <?php endif ?>
399                </td>
400
401                <!-- Age at death -->
402                <?php if (isset($birth_dates[0], $death_dates[0])) : ?>
403                    <?php $age_at_death_years = Date::getAgeYears($birth_dates[0], $death_dates[0]); ?>
404                    <?php $age_at_death_sort = Date::getAge($birth_dates[0], $death_dates[0]); ?>
405                    <?php $age_at_death_display = I18N::number($age_at_death_years); ?>
406                    <?php if (!isset($unique_indis[$individual->xref()]) && $age_at_death_years >= 0 && $age_at_death_years <= $max_age) : ?>
407                        <?php
408                            ++$deat_by_age[$age_at_death_years][$individual->sex()];
409                        ?>
410                    <?php endif ?>
411                <?php else : ?>
412                    <?php $age_at_death_display = ''; ?>
413                    <?php $age_at_death_sort = PHP_INT_MAX; ?>
414                <?php endif ?>
415                <td class="text-center" data-sort="<?= e($age_at_death_sort) ?>">
416                    <?= e($age_at_death_display) ?>
417                </td>
418
419                <!-- Death place -->
420                <td>
421                    <?php foreach ($individual->getAllDeathPlaces() as $n => $death_place) : ?>
422                        <?= $death_place->shortName(true) ?>
423                        <br>
424                    <?php endforeach ?>
425                </td>
426
427                <!-- Last change -->
428                <td data-sort="<?= $individual->lastChangeTimestamp()->unix() ?>">
429                    <?= view('components/datetime', ['timestamp' => $individual->lastChangeTimestamp()]) ?>
430                </td>
431
432                <!-- Filter by sex -->
433                <td hidden>
434                    <?= $individual->sex() ?>
435                </td>
436
437                <!-- Filter by birth date -->
438                <td hidden>
439                    <?php if (!$individual->canShow() || $individual->getEstimatedBirthDate()->maximumJulianDay() > $hundred_years_ago) : ?>
440                        Y100
441                    <?php else : ?>
442                        YES
443                    <?php endif ?>
444                </td>
445
446                <!-- Filter by death date -->
447                <td hidden>
448                    <?php if ($individual->getEstimatedDeathDate()->maximumJulianDay() > $hundred_years_ago) : ?>
449                        Y100
450                    <?php elseif ($individual->isDead()) : ?>
451                        YES
452                    <?php else : ?>
453                        N
454                    <?php endif ?>
455                </td>
456
457                <!-- Filter by roots/leaves -->
458                <td hidden>
459                    <?php if ($individual->childFamilies()->isEmpty()) : ?>
460                        R
461                    <?php elseif (!$individual->isDead() && $individual->numberOfChildren() < 1) : ?>
462                        L
463                    <?php endif ?>
464                </td>
465            </tr>
466
467                <?php $unique_indis[$individual->xref()] = true ?>
468            <?php endforeach ?>
469        </tbody>
470    </table>
471</div>
472
473<div id="individual-charts-<?= e($table_id) ?>" style="display: none;">
474    <div class="mb-3">
475        <div class="card-deck">
476            <div class="col-lg-12 col-md-12 mb-3">
477                <div class="card m-0">
478                    <div class="card-header">
479                        <?= I18N::translate('Decade of birth') ?>
480                    </div>
481                    <div class="card-body">
482                        <?php
483                            foreach ($birt_by_decade as $century => $values) {
484                                if (($values['M'] + $values['F']) > 0) {
485                                    $birthData[] = [
486                                        [
487                                            'v' => 'Date(' . $century . ', 0, 1)',
488                                            'f' => $century,
489                                        ],
490                                        $values['M'],
491                                        $values['F'],
492                                    ];
493                                }
494                            }
495                            ?>
496                        <?= view('lists/chart-by-decade', ['data' => $birthData, 'title' => I18N::translate('Decade of birth')]) ?>
497                    </div>
498                </div>
499            </div>
500        </div>
501        <div class="card-deck">
502            <div class="col-lg-12 col-md-12 mb-3">
503                <div class="card m-0">
504                    <div class="card-header">
505                        <?= I18N::translate('Decade of death') ?>
506                    </div>
507                    <div class="card-body">
508                        <?php
509                            foreach ($deat_by_decade as $century => $values) {
510                                if (($values['M'] + $values['F']) > 0) {
511                                    $deathData[] = [
512                                        [
513                                            'v' => 'Date(' . $century . ', 0, 1)',
514                                            'f' => $century,
515                                        ],
516                                        $values['M'],
517                                        $values['F'],
518                                    ];
519                                }
520                            }
521                        ?>
522                        <?= view('lists/chart-by-decade', ['data' => $deathData, 'title' => I18N::translate('Decade of death')]) ?>
523                    </div>
524                </div>
525            </div>
526        </div>
527        <div class="card-deck">
528            <div class="col-lg-12 col-md-12 mb-3">
529                <div class="card m-0">
530                    <div class="card-header">
531                        <?= I18N::translate('Age related to death year') ?>
532                    </div>
533                    <div class="card-body">
534                        <?php
535                            $totalAge = 0;
536                            $totalSum = 0;
537                            $max      = 0;
538
539                            foreach ($deat_by_age as $age => $values) {
540                                if (($values['M'] + $values['F']) > 0) {
541                                    if (($values['M'] + $values['F']) > $max) {
542                                        $max = $values['M'] + $values['F'];
543                                    }
544
545                                    $totalAge += $age * ($values['M'] + $values['F']);
546                                    $totalSum += $values['M'] + $values['F'];
547
548                                    $deathAgeData[] = [
549                                        $age,
550                                        $values['M'],
551                                        $values['F'],
552                                        null,
553                                    ];
554                                }
555                            }
556
557                            if ($totalSum > 0) {
558                                $deathAgeData[] = [
559                                    round($totalAge / $totalSum, 1),
560                                    null,
561                                    null,
562                                    0,
563                                ];
564
565                                $deathAgeData[] = [
566                                    round($totalAge / $totalSum, 1),
567                                    null,
568                                    null,
569                                    $max,
570                                ];
571                            }
572                        ?>
573                        <?= view('lists/chart-by-age', ['data' => $deathAgeData, 'title' => I18N::translate('Age related to death year')]) ?>
574                    </div>
575                </div>
576            </div>
577        </div>
578    </div>
579</div>
580