xref: /webtrees/resources/views/lists/individuals-table.phtml (revision add3fa4120ca696c713a0d0ac9b9c86f751fe49a)
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(static 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    autoWidth: false,
38    processing: true,
39    retrieve: true,
40    columns: [
41        /* Given names  */ { type: "text" },
42        /* Surnames     */ { type: "text" },
43        /* SOSA numnber */ { type: "num", visible: <?= json_encode($sosa) ?> },
44        /* Birth date   */ { type: "num" },
45        /* Anniversary  */ { type: "num" },
46        /* Birthplace   */ { type: "text" },
47        /* Children     */ { type: "num" },
48        /* Deate date   */ { type: "num" },
49        /* Anniversary  */ { type: "num" },
50        /* Age          */ { type: "num" },
51        /* Death place  */ { type: "text" },
52        /* Last change  */ { visible: <?= json_encode($tree->getPreference('SHOW_LAST_CHANGE')) ?> },
53        /* Filter sex   */ { sortable: false },
54        /* Filter birth */ { sortable: false },
55        /* Filter death */ { sortable: false },
56        /* Filter tree  */ { sortable: false }
57    ],
58    sorting: <?= json_encode($sosa ? [[4, 'asc']] : [[1, 'asc']]) ?>
59});
60
61$("#<?= e($table_id) ?>")
62    /* Hide/show parents */
63    .on("click", ".btn-toggle-parents", function() {
64        $(this).toggleClass("ui-state-active");
65        $(".parents", $(this).closest("table").DataTable().rows().nodes()).slideToggle();
66    })
67    /* Hide/show statistics */
68    .on("click", ".btn-toggle-statistics", function() {
69        $(this).toggleClass("ui-state-active");
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                                <?= Individual::sexImage('M', 'large') ?>
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                                <?= Individual::sexImage('F', 'large') ?>
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                                <?= Individual::sexImage('U', 'large') ?>
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-toolbar">
297                        <div class="btn-group">
298                            <button class="ui-state-default btn-toggle-parents">
299                                <?= I18N::translate('Show parents') ?>
300                            </button>
301                            <button class="ui-state-default btn-toggle-statistics">
302                                <?= I18N::translate('Show statistics charts') ?>
303                            </button>
304                        </div>
305                    </div>
306                </th>
307            </tr>
308        </tfoot>
309
310        <tbody>
311            <?php foreach ($individuals as $key => $individual) : ?>
312            <tr class="<?= $individual->isPendingDeletion() ? 'wt-old' : ($individual->isPendingAddition() ? 'wt-new' : '') ?>">
313                <td colspan="2" data-sort="<?= e(str_replace([',', '@P.N.', '@N.N.'], 'AAAA', implode(',', array_reverse(explode(',', $individual->sortName()))))) ?>">
314                    <?php foreach ($individual->getAllNames() as $num => $name) : ?>
315                        <a title="<?= $name['type'] === 'NAME' ? '' : GedcomTag::getLabel($name['type'], $individual) ?>" href="<?= e($individual->url()) ?>" class="<?= $num === $individual->getPrimaryName() ? 'name2' : '' ?>">
316                            <?= $name['full'] ?>
317                        </a>
318                        <?php if ($num === $individual->getPrimaryName()) : ?>
319                            <?= $individual->getSexImage() ?>
320                        <?php endif ?>
321                        <br>
322                    <?php endforeach ?>
323                    <?= $individual->getPrimaryParentsNames('parents details1', 'none') ?>
324                </td>
325
326                <td hidden data-sort="<?= e(str_replace([',', '@P.N.', '@N.N.'], 'AAAA', $individual->sortName())) ?>"></td>
327
328                <td class="text-center" data-sort="<?= $key ?>">
329                    <?php if ($sosa) : ?>
330                        <?php if ($module instanceof RelationshipsChartModule) : ?>
331                            <a href="<?= e($module->chartUrl($individuals[1], ['xref2' => $individual->xref()])) ?>" rel="nofollow" title="<?= I18N::translate('Relationships') ?>" rel="nofollow">
332                                <?= I18N::number($key) ?>
333                            </a>
334                        <?php else : ?>
335                            <?= I18N::number($key) ?>
336                        <?php endif ?>
337                    <?php endif ?>
338                </td>
339
340                <!-- Birth date -->
341                <td data-sort="<?= $individual->getEstimatedBirthDate()->julianDay() ?>">
342                    <?php $birth_dates = $individual->getAllBirthDates(); ?>
343
344                    <?php foreach ($birth_dates as $n => $birth_date) : ?>
345                        <?= $birth_date->display(true) ?>
346                        <br>
347                    <?php endforeach ?>
348
349                    <?php if (empty($birth_dates)): ?>
350                        <?= $individual->getEstimatedBirthDate()->display(true) ?>
351                    <?php endif ?>
352                </td>
353
354                <!-- Birth anniversary -->
355                <td class="text-center" data-sort="<?= -$individual->getEstimatedBirthDate()->julianDay() ?>">
356                    <?php if (isset($birth_dates[0]) && $birth_dates[0]->gregorianYear() >= 1550 && $birth_dates[0]->gregorianYear() < 2030 && !isset($unique_indis[$individual->xref()])) : ?>
357                        <?php
358                            ++$birt_by_decade[(int) ($birth_dates[0]->gregorianYear() / 10) * 10][$individual->sex()];
359                        ?>
360                        <?= Date::getAge($birth_dates[0]) ?>
361                    <?php endif ?>
362                </td>
363
364                <!-- Birth place -->
365                <td>
366                    <?php foreach ($individual->getAllBirthPlaces() as $n => $birth_place) : ?>
367                        <?= $birth_place->shortName(true) ?>
368                        <br>
369                    <?php endforeach ?>
370                </td>
371
372                <!-- Number of children -->
373                <td class="text-center" data-sort="<?= $individual->numberOfChildren() ?>">
374                    <?= I18N::number($individual->numberOfChildren()) ?>
375                </td>
376
377                <!--    Death date -->
378                <?php $death_dates = $individual->getAllDeathDates() ?>
379                <td data-sort="<?= $individual->getEstimatedDeathDate()->julianDay() ?>">
380                    <?php foreach ($death_dates as $num => $death_date) : ?>
381                        <?= $death_date->display(true) ?>
382                    <br>
383                    <?php endforeach ?>
384
385                    <?php if (empty($death_dates)): ?>
386                        <?= $individual->getEstimatedDeathDate()->display(true) ?>
387                    <?php endif ?>
388                </td>
389
390                <!-- Death anniversary -->
391                <td class="text-center" data-sort="<?= -$individual->getEstimatedDeathDate()->julianDay() ?>">
392                    <?php if (isset($death_dates[0]) && $death_dates[0]->gregorianYear() >= 1550 && $death_dates[0]->gregorianYear() < 2030 && !isset($unique_indis[$individual->xref()])) : ?>
393                        <?php
394                            ++$deat_by_decade[(int) ($death_dates[0]->gregorianYear() / 10) * 10][$individual->sex()];
395                        ?>
396                        <?= Date::getAge($death_dates[0]) ?>
397                    <?php endif ?>
398                </td>
399
400                <!-- Age at death -->
401                <?php if (isset($birth_dates[0]) && isset($death_dates[0])) : ?>
402                    <?php $age_at_death = I18N::number((int) Date::getAgeYears($birth_dates[0], $death_dates[0])); ?>
403                    <?php $age_at_death_sort = Date::getAge($birth_dates[0], $death_dates[0]); ?>
404                    <?php if (!isset($unique_indis[$individual->xref()]) && $age_at_death >= 0 && $age_at_death <= $max_age) : ?>
405                        <?php
406                            ++$deat_by_age[$age_at_death][$individual->sex()];
407                        ?>
408                    <?php endif ?>
409                <?php else : ?>
410                    <?php $age_at_death = ''; ?>
411                    <?php $age_at_death_sort = PHP_INT_MAX; ?>
412                <?php endif ?>
413                <td class="text-center" data-sort="<?= e($age_at_death_sort) ?>">
414                    <?= e($age_at_death) ?>
415                </td>
416
417                <!-- Death place -->
418                <td>
419                    <?php foreach ($individual->getAllDeathPlaces() as $n => $death_place) : ?>
420                        <?= $death_place->shortName(true) ?>
421                        <br>
422                    <?php endforeach ?>
423                </td>
424
425                <!-- Last change -->
426                <td data-sort="<?= $individual->lastChangeTimestamp()->unix() ?>">
427                    <?= view('components/datetime', ['timestamp' => $individual->lastChangeTimestamp()]) ?>
428                </td>
429
430                <!-- Filter by sex -->
431                <td hidden>
432                    <?= $individual->sex() ?>
433                </td>
434
435                <!-- Filter by birth date -->
436                <td hidden>
437                    <?php if (!$individual->canShow() || Date::compare($individual->getEstimatedBirthDate(), $hundred_years_ago) > 0) : ?>
438                        Y100
439                    <?php else : ?>
440                        YES
441                    <?php endif ?>
442                </td>
443
444                <!-- Filter by death date -->
445                <td hidden>
446                    <?php if (isset($death_dates[0]) && Date::compare($death_dates[0], $hundred_years_ago) > 0) : ?>
447                        Y100
448                    <?php elseif ($individual->isDead()) : ?>
449                        YES
450                    <?php else : ?>
451                        N
452                    <?php endif ?>
453                </td>
454
455                <!-- Filter by roots/leaves -->
456                <td hidden>
457                    <?php if (!$individual->childFamilies()) : ?>
458                        R
459                    <?php elseif (!$individual->isDead() && $individual->numberOfChildren() < 1) : ?>
460                        L
461                    <?php endif ?>
462                </td>
463            </tr>
464
465                <?php $unique_indis[$individual->xref()] = true ?>
466            <?php endforeach ?>
467        </tbody>
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