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