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