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