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