xref: /webtrees/resources/views/lists/individuals-table.phtml (revision 4843b94f29110804308615f0385ce3541ab9c49c)
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-bordered"
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        <tfoot>
294            <tr>
295                <th colspan="16">
296                    <div class="btn-group m-1">
297                        <button id="btn-toggle-parents" class="btn btn-secondary" data-toggle="button" data-persist="show-parents">
298                            <?= I18N::translate('Show parents') ?>
299                        </button>
300                        <button id="btn-toggle-statistics" class="btn btn-secondary" data-toggle="button" data-persist="show-statistics">
301                            <?= I18N::translate('Show statistics charts') ?>
302                        </button>
303                    </div>
304                </th>
305            </tr>
306        </tfoot>
307
308        <tbody>
309            <?php foreach ($individuals as $key => $individual) : ?>
310            <tr class="<?= $individual->isPendingDeletion() ? 'wt-old' : ($individual->isPendingAddition() ? 'wt-new' : '') ?>">
311                <td colspan="2" data-sort="<?= e(str_replace([',', '@P.N.', '@N.N.'], 'AAAA', implode(',', array_reverse(explode(',', $individual->sortName()))))) ?>">
312                    <?php foreach ($individual->getAllNames() as $num => $name) : ?>
313                        <a title="<?= $name['type'] === 'NAME' ? '' : GedcomTag::getLabel($name['type'], $individual) ?>" href="<?= e($individual->url()) ?>" class="<?= $num === $individual->getPrimaryName() ? 'name2' : '' ?>">
314                            <?= $name['full'] ?>
315                        </a>
316                        <?php if ($num === $individual->getPrimaryName()) : ?>
317                            <small><?= view('icons/sex', ['sex' => $individual->sex()]) ?></small>
318                        <?php endif ?>
319                        <br>
320                    <?php endforeach ?>
321                    <?= view('lists/individual-table-parents', ['individual' => $individual]) ?>
322                </td>
323
324                <td hidden data-sort="<?= e(str_replace([',', '@P.N.', '@N.N.'], 'AAAA', $individual->sortName())) ?>"></td>
325
326                <td class="text-center" data-sort="<?= $key ?>">
327                    <?php if ($sosa) : ?>
328                        <?php if ($module instanceof RelationshipsChartModule) : ?>
329                            <a href="<?= e($module->chartUrl($individuals[1], ['xref2' => $individual->xref()])) ?>" rel="nofollow" title="<?= I18N::translate('Relationships') ?>" rel="nofollow">
330                                <?= I18N::number($key) ?>
331                            </a>
332                        <?php else : ?>
333                            <?= I18N::number($key) ?>
334                        <?php endif ?>
335                    <?php endif ?>
336                </td>
337
338                <!-- Birth date -->
339                <td data-sort="<?= $individual->getEstimatedBirthDate()->julianDay() ?>">
340                    <?php $birth_dates = $individual->getAllBirthDates(); ?>
341
342                    <?php foreach ($birth_dates as $n => $birth_date) : ?>
343                        <?= $birth_date->display(true) ?>
344                        <br>
345                    <?php endforeach ?>
346
347                    <?php if (empty($birth_dates) && $show_estimated_dates) : ?>
348                        <?= $individual->getEstimatedBirthDate()->display(true) ?>
349                    <?php endif ?>
350                </td>
351
352                <!-- Birth anniversary -->
353                <td class="text-center" data-sort="<?= - $individual->getEstimatedBirthDate()->julianDay() ?>">
354                    <?php if (isset($birth_dates[0]) && $birth_dates[0]->gregorianYear() >= 1550 && $birth_dates[0]->gregorianYear() < 2030 && !isset($unique_indis[$individual->xref()])) : ?>
355                        <?php
356                            ++$birt_by_decade[(int) ($birth_dates[0]->gregorianYear() / 10) * 10][$individual->sex()];
357                        ?>
358                        <?= Date::getAge($birth_dates[0]) ?>
359                    <?php endif ?>
360                </td>
361
362                <!-- Birth place -->
363                <td>
364                    <?php foreach ($individual->getAllBirthPlaces() as $n => $birth_place) : ?>
365                        <?= $birth_place->shortName(true) ?>
366                        <br>
367                    <?php endforeach ?>
368                </td>
369
370                <!-- Number of children -->
371                <td class="text-center" data-sort="<?= $individual->numberOfChildren() ?>">
372                    <?= I18N::number($individual->numberOfChildren()) ?>
373                </td>
374
375                <!--    Death date -->
376                <?php $death_dates = $individual->getAllDeathDates() ?>
377                <td data-sort="<?= $individual->getEstimatedDeathDate()->julianDay() ?>">
378                    <?php foreach ($death_dates as $num => $death_date) : ?>
379                        <?= $death_date->display(true) ?>
380                    <br>
381                    <?php endforeach ?>
382
383                    <?php if (empty($death_dates) && $show_estimated_dates && $individual->getEstimatedDeathDate()->minimumDate()->minimumJulianDay() < $today_jd) : ?>
384                        <?= $individual->getEstimatedDeathDate()->display(true) ?>
385                    <?php endif ?>
386                </td>
387
388                <!-- Death anniversary -->
389                <td class="text-center" data-sort="<?= - $individual->getEstimatedDeathDate()->julianDay() ?>">
390                    <?php if (isset($death_dates[0]) && $death_dates[0]->gregorianYear() >= 1550 && $death_dates[0]->gregorianYear() < 2030 && !isset($unique_indis[$individual->xref()])) : ?>
391                        <?php
392                            ++$deat_by_decade[(int) ($death_dates[0]->gregorianYear() / 10) * 10][$individual->sex()];
393                        ?>
394                        <?= Date::getAge($death_dates[0]) ?>
395                    <?php endif ?>
396                </td>
397
398                <!-- Age at death -->
399                <?php if (isset($birth_dates[0], $death_dates[0])) : ?>
400                    <?php $age_at_death_years = Date::getAgeYears($birth_dates[0], $death_dates[0]); ?>
401                    <?php $age_at_death_sort = Date::getAge($birth_dates[0], $death_dates[0]); ?>
402                    <?php $age_at_death_display = I18N::number($age_at_death_years); ?>
403                    <?php if (!isset($unique_indis[$individual->xref()]) && $age_at_death_years >= 0 && $age_at_death_years <= $max_age) : ?>
404                        <?php
405                            ++$deat_by_age[$age_at_death_years][$individual->sex()];
406                        ?>
407                    <?php endif ?>
408                <?php else : ?>
409                    <?php $age_at_death_display = ''; ?>
410                    <?php $age_at_death_sort = PHP_INT_MAX; ?>
411                <?php endif ?>
412                <td class="text-center" data-sort="<?= e($age_at_death_sort) ?>">
413                    <?= e($age_at_death_display) ?>
414                </td>
415
416                <!-- Death place -->
417                <td>
418                    <?php foreach ($individual->getAllDeathPlaces() as $n => $death_place) : ?>
419                        <?= $death_place->shortName(true) ?>
420                        <br>
421                    <?php endforeach ?>
422                </td>
423
424                <!-- Last change -->
425                <td data-sort="<?= $individual->lastChangeTimestamp()->unix() ?>">
426                    <?= view('components/datetime', ['timestamp' => $individual->lastChangeTimestamp()]) ?>
427                </td>
428
429                <!-- Filter by sex -->
430                <td hidden>
431                    <?= $individual->sex() ?>
432                </td>
433
434                <!-- Filter by birth date -->
435                <td hidden>
436                    <?php if (!$individual->canShow() || $individual->getEstimatedBirthDate()->maximumJulianDay() > $hundred_years_ago) : ?>
437                        Y100
438                    <?php else : ?>
439                        YES
440                    <?php endif ?>
441                </td>
442
443                <!-- Filter by death date -->
444                <td hidden>
445                    <?php if ($individual->getEstimatedDeathDate()->maximumJulianDay() > $hundred_years_ago) : ?>
446                        Y100
447                    <?php elseif ($individual->isDead()) : ?>
448                        YES
449                    <?php else : ?>
450                        N
451                    <?php endif ?>
452                </td>
453
454                <!-- Filter by roots/leaves -->
455                <td hidden>
456                    <?php if ($individual->childFamilies()->isEmpty()) : ?>
457                        R
458                    <?php elseif (!$individual->isDead() && $individual->numberOfChildren() < 1) : ?>
459                        L
460                    <?php endif ?>
461                </td>
462            </tr>
463
464                <?php $unique_indis[$individual->xref()] = true ?>
465            <?php endforeach ?>
466        </tbody>
467    </table>
468</div>
469
470<div id="individual-charts-<?= e($table_id) ?>" style="display: none;">
471    <div class="mb-3">
472        <div class="card-deck">
473            <div class="col-lg-12 col-md-12 mb-3">
474                <div class="card m-0">
475                    <div class="card-header">
476                        <?= I18N::translate('Decade of birth') ?>
477                    </div>
478                    <div class="card-body">
479                        <?php
480                        foreach ($birt_by_decade as $century => $values) {
481                            if (($values['M'] + $values['F']) > 0) {
482                                $birthData[] = [
483                                    [
484                                        'v' => 'Date(' . $century . ', 0, 1)',
485                                        'f' => $century,
486                                    ],
487                                    $values['M'],
488                                    $values['F'],
489                                ];
490                            }
491                        }
492                        ?>
493                        <?= view('lists/chart-by-decade', ['data' => $birthData, 'title' => I18N::translate('Decade of birth')]) ?>
494                    </div>
495                </div>
496            </div>
497        </div>
498        <div class="card-deck">
499            <div class="col-lg-12 col-md-12 mb-3">
500                <div class="card m-0">
501                    <div class="card-header">
502                        <?= I18N::translate('Decade of death') ?>
503                    </div>
504                    <div class="card-body">
505                        <?php
506                        foreach ($deat_by_decade as $century => $values) {
507                            if (($values['M'] + $values['F']) > 0) {
508                                $deathData[] = [
509                                    [
510                                        'v' => 'Date(' . $century . ', 0, 1)',
511                                        'f' => $century,
512                                    ],
513                                    $values['M'],
514                                    $values['F'],
515                                ];
516                            }
517                        }
518                        ?>
519                        <?= view('lists/chart-by-decade', ['data' => $deathData, 'title' => I18N::translate('Decade of death')]) ?>
520                    </div>
521                </div>
522            </div>
523        </div>
524        <div class="card-deck">
525            <div class="col-lg-12 col-md-12 mb-3">
526                <div class="card m-0">
527                    <div class="card-header">
528                        <?= I18N::translate('Age related to death year') ?>
529                    </div>
530                    <div class="card-body">
531                        <?php
532                            $totalAge = 0;
533                            $totalSum = 0;
534                            $max      = 0;
535
536                        foreach ($deat_by_age as $age => $values) {
537                            if (($values['M'] + $values['F']) > 0) {
538                                if (($values['M'] + $values['F']) > $max) {
539                                    $max = $values['M'] + $values['F'];
540                                }
541
542                                $totalAge += $age * ($values['M'] + $values['F']);
543                                $totalSum += $values['M'] + $values['F'];
544
545                                $deathAgeData[] = [
546                                    $age,
547                                    $values['M'],
548                                    $values['F'],
549                                    null,
550                                ];
551                            }
552                        }
553
554                        if ($totalSum > 0) {
555                            $deathAgeData[] = [
556                                round($totalAge / $totalSum, 1),
557                                null,
558                                null,
559                                0,
560                            ];
561
562                            $deathAgeData[] = [
563                                round($totalAge / $totalSum, 1),
564                                null,
565                                null,
566                                $max,
567                            ];
568                        }
569                        ?>
570                        <?= view('lists/chart-by-age', ['data' => $deathAgeData, 'title' => I18N::translate('Age related to death year')]) ?>
571                    </div>
572                </div>
573            </div>
574        </div>
575    </div>
576</div>
577