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