xref: /webtrees/resources/views/lists/individuals-table.phtml (revision 9f9acdbc09170c04c1e150c36ee57d49027e314a)
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// Initialise 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" autocomplete="off">
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" autocomplete="off">
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" autocomplete="off">
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" autocomplete="off">
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" autocomplete="off">
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" autocomplete="off">
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" autocomplete="off">
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" autocomplete="off">
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" autocomplete="off">
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" autocomplete="off">
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" autocomplete="off">
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'])) ?>" 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                <?php if (isset($birth_dates[0]) && $birth_dates[0]->gregorianYear() >= 1550 && $birth_dates[0]->gregorianYear() < 2030 && !isset($unique_indis[$individual->xref()])) : ?>
311                    <?php
312                    ++$birt_by_decade[(int) ($birth_dates[0]->gregorianYear() / 10) * 10][$individual->sex()];
313                    ?>
314                <?php endif ?>
315                <td class="text-center" data-sort="<?= - $estimated_birth_date->julianDay() ?>">
316                    <?= (new Age($birth_dates[0] ?? new Date(''), $today))->ageYearsString() ?>
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                <?php if (isset($death_dates[0]) && $death_dates[0]->gregorianYear() >= 1550 && $death_dates[0]->gregorianYear() < 2030 && !isset($unique_indis[$individual->xref()])) : ?>
347                    <?php
348                    ++$deat_by_decade[(int) ($death_dates[0]->gregorianYear() / 10) * 10][$individual->sex()];
349                    ?>
350                <?php endif ?>
351                <td class="text-center" data-sort="<?= - $individual->getEstimatedDeathDate()->julianDay() ?>">
352                    <?= (new Age($death_dates[0] ?? new Date(''), $today))->ageYearsString() ?>
353                </td>
354
355                <!-- Age at death -->
356                <?php $age = new Age($birth_dates[0] ?? new Date(''), $death_dates[0] ?? new Date('')) ?>
357                <?php if (!isset($unique_indis[$individual->xref()]) && $age->ageYears() >= 0 && $age->ageYears() <= $max_age) : ?>
358                    <?php ++$deat_by_age[$age->ageYears()][$individual->sex()] ?>
359                <?php endif ?>
360                <td class="text-center" data-sort="<?= $age->ageDays() ?>">
361                    <?= $age->ageYearsString() ?>
362                </td>
363
364                <!-- Death place -->
365                <td>
366                    <?php foreach ($individual->getAllDeathPlaces() as $n => $death_place) : ?>
367                        <?= $death_place->shortName(true) ?>
368                        <br>
369                    <?php endforeach ?>
370                </td>
371
372                <!-- Last change -->
373                <td data-sort="<?= $individual->lastChangeTimestamp()->unix() ?>">
374                    <?= view('components/datetime', ['timestamp' => $individual->lastChangeTimestamp()]) ?>
375                </td>
376
377                <!-- Filter by sex -->
378                <td hidden>
379                    <?= $individual->sex() ?>
380                </td>
381
382                <!-- Filter by birth date -->
383                <td hidden>
384                    <?php if ($estimated_birth_date->maximumJulianDay() > $hundred_years_ago && $estimated_birth_date->maximumJulianDay() <= $today_jd) : ?>
385                        Y100
386                    <?php else : ?>
387                        YES
388                    <?php endif ?>
389                </td>
390
391                <!-- Filter by death date -->
392                <td hidden>
393                    <?php if ($individual->getEstimatedDeathDate()->maximumJulianDay() > $hundred_years_ago && $individual->getEstimatedDeathDate()->maximumJulianDay() <= $today_jd) : ?>
394                        Y100
395                    <?php elseif ($individual->isDead()) : ?>
396                        YES
397                    <?php else : ?>
398                        N
399                    <?php endif ?>
400                </td>
401
402                <!-- Filter by roots/leaves -->
403                <td hidden>
404                    <?php if ($individual->childFamilies()->isEmpty()) : ?>
405                        R
406                    <?php elseif (!$individual->isDead() && $individual->numberOfChildren() < 1) : ?>
407                        L
408                    <?php endif ?>
409                </td>
410            </tr>
411
412                <?php $unique_indis[$individual->xref()] = true ?>
413            <?php endforeach ?>
414        </tbody>
415
416        <tfoot>
417            <tr>
418                <th colspan="16">
419                    <div class="btn-group btn-group-sm">
420                        <button id="btn-toggle-parents" class="btn btn-outline-secondary" data-toggle="button" data-persist="show-parents">
421                            <?= I18N::translate('Show parents') ?>
422                        </button>
423                        <button id="btn-toggle-statistics" class="btn btn-outline-secondary" data-toggle="button" data-persist="show-statistics">
424                            <?= I18N::translate('Show statistics charts') ?>
425                        </button>
426                    </div>
427                </th>
428            </tr>
429        </tfoot>
430    </table>
431</div>
432
433<div id="individual-charts-<?= e($table_id) ?>" style="display: none;">
434    <div class="mb-3">
435        <div class="card-deck">
436            <div class="col-lg-12 col-md-12 mb-3">
437                <div class="card m-0">
438                    <div class="card-header">
439                        <?= I18N::translate('Decade of birth') ?>
440                    </div>
441                    <div class="card-body">
442                        <?php
443                        foreach ($birt_by_decade as $century => $values) {
444                            if (($values['M'] + $values['F']) > 0) {
445                                $birthData[] = [
446                                    [
447                                        'v' => 'Date(' . $century . ', 0, 1)',
448                                        'f' => $century,
449                                    ],
450                                    $values['M'],
451                                    $values['F'],
452                                ];
453                            }
454                        }
455                        ?>
456                        <?= view('lists/chart-by-decade', ['data' => $birthData, 'title' => I18N::translate('Decade of birth')]) ?>
457                    </div>
458                </div>
459            </div>
460        </div>
461        <div class="card-deck">
462            <div class="col-lg-12 col-md-12 mb-3">
463                <div class="card m-0">
464                    <div class="card-header">
465                        <?= I18N::translate('Decade of death') ?>
466                    </div>
467                    <div class="card-body">
468                        <?php
469                        foreach ($deat_by_decade as $century => $values) {
470                            if (($values['M'] + $values['F']) > 0) {
471                                $deathData[] = [
472                                    [
473                                        'v' => 'Date(' . $century . ', 0, 1)',
474                                        'f' => $century,
475                                    ],
476                                    $values['M'],
477                                    $values['F'],
478                                ];
479                            }
480                        }
481                        ?>
482                        <?= view('lists/chart-by-decade', ['data' => $deathData, 'title' => I18N::translate('Decade of death')]) ?>
483                    </div>
484                </div>
485            </div>
486        </div>
487        <div class="card-deck">
488            <div class="col-lg-12 col-md-12 mb-3">
489                <div class="card m-0">
490                    <div class="card-header">
491                        <?= I18N::translate('Age related to death year') ?>
492                    </div>
493                    <div class="card-body">
494                        <?php
495                            $totalAge = 0;
496                            $totalSum = 0;
497                            $max      = 0;
498
499                        foreach ($deat_by_age as $age => $values) {
500                            if (($values['M'] + $values['F']) > 0) {
501                                if (($values['M'] + $values['F']) > $max) {
502                                    $max = $values['M'] + $values['F'];
503                                }
504
505                                $totalAge += $age * ($values['M'] + $values['F']);
506                                $totalSum += $values['M'] + $values['F'];
507
508                                $deathAgeData[] = [
509                                    $age,
510                                    $values['M'],
511                                    $values['F'],
512                                    null,
513                                ];
514                            }
515                        }
516
517                        if ($totalSum > 0) {
518                            $deathAgeData[] = [
519                                round($totalAge / $totalSum, 1),
520                                null,
521                                null,
522                                0,
523                            ];
524
525                            $deathAgeData[] = [
526                                round($totalAge / $totalSum, 1),
527                                null,
528                                null,
529                                $max,
530                            ];
531                        }
532                        ?>
533                        <?= view('lists/chart-by-age', ['data' => $deathAgeData, 'title' => I18N::translate('Age related to death year')]) ?>
534                    </div>
535                </div>
536            </div>
537        </div>
538    </div>
539</div>
540