xref: /webtrees/resources/views/lists/individuals-table.phtml (revision 086626571285467c4ca32c151d6d4cbc78cf91d2)
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 = I18N::number((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 if (!isset($unique_indis[$individual->xref()]) && $age_at_death >= 0 && $age_at_death <= $max_age) : ?>
408                        <?php
409                            ++$deat_by_age[$age_at_death][$individual->sex()];
410                        ?>
411                    <?php endif ?>
412                <?php else : ?>
413                    <?php $age_at_death = ''; ?>
414                    <?php $age_at_death_sort = PHP_INT_MAX; ?>
415                <?php endif ?>
416                <td class="text-center" data-sort="<?= e($age_at_death_sort) ?>">
417                    <?= e($age_at_death) ?>
418                </td>
419
420                <!-- Death place -->
421                <td>
422                    <?php foreach ($individual->getAllDeathPlaces() as $n => $death_place) : ?>
423                        <?= $death_place->shortName(true) ?>
424                        <br>
425                    <?php endforeach ?>
426                </td>
427
428                <!-- Last change -->
429                <td data-sort="<?= $individual->lastChangeTimestamp()->unix() ?>">
430                    <?= view('components/datetime', ['timestamp' => $individual->lastChangeTimestamp()]) ?>
431                </td>
432
433                <!-- Filter by sex -->
434                <td hidden>
435                    <?= $individual->sex() ?>
436                </td>
437
438                <!-- Filter by birth date -->
439                <td hidden>
440                    <?php if (!$individual->canShow() || Date::compare($individual->getEstimatedBirthDate(), $hundred_years_ago) > 0) : ?>
441                        Y100
442                    <?php else : ?>
443                        YES
444                    <?php endif ?>
445                </td>
446
447                <!-- Filter by death date -->
448                <td hidden>
449                    <?php if (isset($death_dates[0]) && Date::compare($death_dates[0], $hundred_years_ago) > 0) : ?>
450                        Y100
451                    <?php elseif ($individual->isDead()) : ?>
452                        YES
453                    <?php else : ?>
454                        N
455                    <?php endif ?>
456                </td>
457
458                <!-- Filter by roots/leaves -->
459                <td hidden>
460                    <?php if ($individual->childFamilies()->isEmpty()) : ?>
461                        R
462                    <?php elseif (!$individual->isDead() && $individual->numberOfChildren() < 1) : ?>
463                        L
464                    <?php endif ?>
465                </td>
466            </tr>
467
468                <?php $unique_indis[$individual->xref()] = true ?>
469            <?php endforeach ?>
470        </tbody>
471    </table>
472</div>
473
474<div id="individual-charts-<?= e($table_id) ?>" style="display: none;">
475    <div class="mb-3">
476        <div class="card-deck">
477            <div class="col-lg-12 col-md-12 mb-3">
478                <div class="card m-0">
479                    <div class="card-header">
480                        <?= I18N::translate('Decade of birth') ?>
481                    </div>
482                    <div class="card-body">
483                        <?php
484                            foreach ($birt_by_decade as $century => $values) {
485                                if (($values['M'] + $values['F']) > 0) {
486                                    $birthData[] = [
487                                        [
488                                            'v' => 'Date(' . $century . ', 0, 1)',
489                                            'f' => $century,
490                                        ],
491                                        $values['M'],
492                                        $values['F'],
493                                    ];
494                                }
495                            }
496                            ?>
497                        <?= view('lists/chart-by-decade', ['data' => $birthData, 'title' => I18N::translate('Decade of birth')]) ?>
498                    </div>
499                </div>
500            </div>
501        </div>
502        <div class="card-deck">
503            <div class="col-lg-12 col-md-12 mb-3">
504                <div class="card m-0">
505                    <div class="card-header">
506                        <?= I18N::translate('Decade of death') ?>
507                    </div>
508                    <div class="card-body">
509                        <?php
510                            foreach ($deat_by_decade as $century => $values) {
511                                if (($values['M'] + $values['F']) > 0) {
512                                    $deathData[] = [
513                                        [
514                                            'v' => 'Date(' . $century . ', 0, 1)',
515                                            'f' => $century,
516                                        ],
517                                        $values['M'],
518                                        $values['F'],
519                                    ];
520                                }
521                            }
522                        ?>
523                        <?= view('lists/chart-by-decade', ['data' => $deathData, 'title' => I18N::translate('Decade of death')]) ?>
524                    </div>
525                </div>
526            </div>
527        </div>
528        <div class="card-deck">
529            <div class="col-lg-12 col-md-12 mb-3">
530                <div class="card m-0">
531                    <div class="card-header">
532                        <?= I18N::translate('Age related to death year') ?>
533                    </div>
534                    <div class="card-body">
535                        <?php
536                            $totalAge = 0;
537                            $totalSum = 0;
538                            $max      = 0;
539
540                            foreach ($deat_by_age as $age => $values) {
541                                if (($values['M'] + $values['F']) > 0) {
542                                    if (($values['M'] + $values['F']) > $max) {
543                                        $max = $values['M'] + $values['F'];
544                                    }
545
546                                    $totalAge += $age * ($values['M'] + $values['F']);
547                                    $totalSum += $values['M'] + $values['F'];
548
549                                    $deathAgeData[] = [
550                                        $age,
551                                        $values['M'],
552                                        $values['F'],
553                                        null,
554                                    ];
555                                }
556                            }
557
558                            if ($totalSum > 0) {
559                                $deathAgeData[] = [
560                                    round($totalAge / $totalSum, 1),
561                                    null,
562                                    null,
563                                    0,
564                                ];
565
566                                $deathAgeData[] = [
567                                    round($totalAge / $totalSum, 1),
568                                    null,
569                                    null,
570                                    $max,
571                                ];
572                            }
573                        ?>
574                        <?= view('lists/chart-by-age', ['data' => $deathAgeData, 'title' => I18N::translate('Age related to death year')]) ?>
575                    </div>
576                </div>
577            </div>
578        </div>
579    </div>
580</div>
581