xref: /webtrees/resources/views/lists/individuals-table.phtml (revision e380f8990cb1cd8831d92d4267bce2ed573b9856)
1<?php
2
3declare(strict_types=1);
4
5use Fisharebest\Webtrees\Auth;
6use Fisharebest\Webtrees\Carbon;
7use Fisharebest\Webtrees\Date;
8use Fisharebest\Webtrees\GedcomTag;
9use Fisharebest\Webtrees\I18N;
10use Fisharebest\Webtrees\Module\ModuleChartInterface;
11use Fisharebest\Webtrees\Module\ModuleInterface;
12use Fisharebest\Webtrees\Module\RelationshipsChartModule;
13use Fisharebest\Webtrees\Services\ModuleService;
14use Fisharebest\Webtrees\Tree;
15use Fisharebest\Webtrees\View;
16use Ramsey\Uuid\Uuid;
17
18/**
19 * @var Tree $tree
20 */
21
22// lists requires a unique ID in case there are multiple lists per page
23$table_id = 'table-indi-' . Uuid::uuid4()->toString();
24
25$today_jd          = Carbon::now()->julianDay();
26$hundred_years_ago = Carbon::now()->subYears(100)->julianDay();
27
28$unique_indis = []; // Don't double-count indis with multiple names.
29
30$show_estimated_dates = (bool) $tree->getPreference('SHOW_EST_LIST_DATES');
31
32$module = app(ModuleService::class)
33    ->findByComponent(ModuleChartInterface::class, $tree, Auth::user())
34    ->first(static function (ModuleInterface $module) {
35        return $module instanceof RelationshipsChartModule;
36    });
37?>
38
39<?php View::push('javascript') ?>
40<script>
41$("#<?= e($table_id) ?> > .wt-table-individual").dataTable({
42    processing: true,
43    retrieve: true,
44    columns: [
45        /* Given names  */ { type: "text" },
46        /* Surnames     */ { type: "text" },
47        /* SOSA number  */ { type: "num", visible: <?= json_encode($sosa) ?> },
48        /* Birth date   */ { type: "num" },
49        /* Anniversary  */ { type: "num" },
50        /* Birthplace   */ { type: "text" },
51        /* Children     */ { type: "num" },
52        /* Deate date   */ { type: "num" },
53        /* Anniversary  */ { type: "num" },
54        /* Age          */ { type: "num" },
55        /* Death place  */ { type: "text" },
56        /* Last change  */ { visible: <?= json_encode($tree->getPreference('SHOW_LAST_CHANGE')) ?> },
57        /* Filter sex   */ { sortable: false },
58        /* Filter birth */ { sortable: false },
59        /* Filter death */ { sortable: false },
60        /* Filter tree  */ { sortable: false }
61    ],
62    sorting: <?= json_encode($sosa ? [[4, 'asc']] : [[1, 'asc']]) ?>
63});
64
65$("#<?= e($table_id) ?>")
66    /* Hide/show parents */
67    .on("click", "#btn-toggle-parents", function() {
68        $(".wt-individual-list-parents").slideToggle();
69    })
70    /* Hide/show statistics */
71    .on("click", "#btn-toggle-statistics", function() {
72        $("#individual-charts-<?= e($table_id) ?>").slideToggle({
73            complete: function () {
74                // Trigger resize to redraw the chart
75                $('div[id^="google-chart-"]').resize();
76            }
77        });
78    })
79    /* Filter buttons in table header */
80    .on("click", "input[data-filter-column]", function() {
81        let checkbox = $(this);
82        let siblings = checkbox.parent().siblings();
83
84        // Deselect other options
85        siblings.children().prop("checked", false).removeAttr("checked");
86        siblings.removeClass('active');
87
88        // Apply (or clear) this filter
89        let checked = checkbox.prop("checked");
90        let filter  = checked ? checkbox.data("filter-value") : "";
91        let column  = $("#<?= e($table_id) ?> .wt-table-individual").DataTable().column(checkbox.data("filter-column"));
92        column.search(filter).draw();
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 id="<?= e($table_id) ?>">
160    <table class="table table-bordered table-sm wt-table-individual"
161        <?= view('lists/datatables-attributes') ?>
162    >
163        <thead>
164            <tr>
165                <th colspan="16">
166                    <div class="btn-toolbar d-flex justify-content-between mb-2" role="toolbar">
167                        <div class="btn-group btn-group-toggle btn-group-sm" data-toggle="buttons">
168                            <label class="btn btn-outline-secondary" title="<?= I18N::translate('Show only males.') ?>">
169                                <input type="checkbox" data-filter-column="12" data-filter-value="M">
170                                <?= view('icons/sex', ['sex' => 'M']) ?>
171                            </label>
172                            <label class="btn btn-outline-secondary" title="<?= I18N::translate('Show only females.') ?>">
173                                <input type="checkbox" data-filter-column="12" data-filter-value="F">
174                                <?= view('icons/sex', ['sex' => 'F']) ?>
175                            </label>
176                            <label class="btn btn-outline-secondary" title="<?= I18N::translate('Show only individuals for whom the gender is not known.') ?>">
177                                <input type="checkbox" data-filter-column="12" data-filter-value="U">
178                                <?= view('icons/sex', ['sex' => 'U']) ?>
179                            </label>
180                        </div>
181
182                        <div class="btn-group btn-group-toggle btn-group-sm" data-toggle="buttons">
183                            <label class="btn btn-outline-secondary" title="<?= I18N::translate('Show individuals who are alive or couples where both partners are alive.') ?>">
184                                <input type="checkbox" data-filter-column="14" data-filter-value="N">
185                                <?= I18N::translate('Alive') ?>
186                            </label>
187                            <label class="btn btn-outline-secondary" title="<?= I18N::translate('Show individuals who are dead or couples where both partners are dead.') ?>">
188                                <input type="checkbox" data-filter-column="14" data-filter-value="Y">
189                                <?= I18N::translate('Dead') ?>
190                            </label>
191                            <label class="btn btn-outline-secondary" title="<?= I18N::translate('Show individuals who died more than 100 years ago.') ?>">
192                                <input type="checkbox" data-filter-column="14" data-filter-value="YES">
193                                <?= I18N::translate('Death') ?>&gt;100
194                            </label>
195                            <label class="btn btn-outline-secondary" title="<?= I18N::translate('Show individuals who died within the last 100 years.') ?>">
196                                <input type="checkbox" data-filter-column="14" data-filter-value="Y100">
197                                <?= I18N::translate('Death') ?>&lt;=100
198                            </label>
199                        </div>
200
201                        <div class="btn-group btn-group-toggle btn-group-sm" data-toggle="buttons">
202                            <label class="btn btn-outline-secondary" title="<?= I18N::translate('Show individuals born more than 100 years ago.') ?>">
203                                <input type="checkbox" data-filter-column="13" data-filter-value="YES">
204                                <?= I18N::translate('Birth') ?>&gt;100
205                            </label>
206                            <label class="btn btn-outline-secondary" title="<?= I18N::translate('Show individuals born within the last 100 years.') ?>">
207                                <input type="checkbox" data-filter-column="13" data-filter-value="Y100">
208                                <?= I18N::translate('Birth') ?>&lt;=100
209                            </label>
210                        </div>
211
212                        <div class="btn-group btn-group-toggle btn-group-sm" data-toggle="buttons">
213                            <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.') ?>">
214                                <input type="checkbox" data-filter-column="15" data-filter-value="R">
215                                <?= I18N::translate('Roots') ?>
216                            </label>
217                            <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.') ?>">
218                                <input type="checkbox" data-filter-column="15" data-filter-value="L">
219                                <?= I18N::translate('Leaves') ?>
220                            </label>
221                        </div>
222                    </div>
223                </th>
224            </tr>
225            <tr>
226                <th><?= I18N::translate('Given names') ?></th>
227                <th><?= I18N::translate('Surname') ?></th>
228                <th><?= /* I18N: Abbreviation for “Sosa-Stradonitz number”. This is an individual’s surname, so may need transliterating into non-latin alphabets. */
229                    I18N::translate('Sosa') ?></th>
230                <th><?= I18N::translate('Birth') ?></th>
231                <th>
232                    <span title="<?= I18N::translate('Anniversary') ?>">
233                        <?= view('icons/anniversary') ?>
234                    </span>
235                </th>
236                <th><?= I18N::translate('Place') ?></th>
237                <th>
238                    <i class="icon-children" title="<?= I18N::translate('Children') ?>"></i>
239                </th>
240                <th><?= I18N::translate('Death') ?></th>
241                <th>
242                    <span title="<?= I18N::translate('Anniversary') ?>">
243                        <?= view('icons/anniversary') ?>
244                    </span>
245                </th>
246                <th><?= I18N::translate('Age') ?></th>
247                <th><?= I18N::translate('Place') ?></th>
248                <th><?= I18N::translate('Last change') ?></th>
249                <th hidden></th>
250                <th hidden></th>
251                <th hidden></th>
252                <th hidden></th>
253            </tr>
254        </thead>
255
256        <tbody>
257            <?php foreach ($individuals as $key => $individual) : ?>
258            <tr class="<?= $individual->isPendingDeletion() ? 'wt-old' : ($individual->isPendingAddition() ? 'wt-new' : '') ?>">
259                <td colspan="2" data-sort="<?= e(str_replace([',', '@P.N.', '@N.N.'], 'AAAA', implode(',', array_reverse(explode(',', $individual->sortName()))))) ?>">
260                    <?php foreach ($individual->getAllNames() as $num => $name) : ?>
261                        <a title="<?= $name['type'] === 'NAME' ? '' : strip_tags(GedcomTag::getLabel($name['type'], $individual)) ?>" href="<?= e($individual->url()) ?>" class="<?= $num === $individual->getPrimaryName() ? 'name2' : '' ?>">
262                            <?= $name['full'] ?>
263                        </a>
264                        <?php if ($num === $individual->getPrimaryName()) : ?>
265                            <small><?= view('icons/sex', ['sex' => $individual->sex()]) ?></small>
266                        <?php endif ?>
267                        <br>
268                    <?php endforeach ?>
269                    <?= view('lists/individual-table-parents', ['individual' => $individual]) ?>
270                </td>
271
272                <td hidden data-sort="<?= e(str_replace([',', '@P.N.', '@N.N.'], 'AAAA', $individual->sortName())) ?>"></td>
273
274                <td class="text-center" data-sort="<?= $key ?>">
275                    <?php if ($sosa) : ?>
276                        <?php if ($module instanceof RelationshipsChartModule) : ?>
277                            <a href="<?= e($module->chartUrl($individuals[1], ['xref2' => $individual->xref()])) ?>" rel="nofollow" title="<?= I18N::translate('Relationships') ?>" rel="nofollow">
278                                <?= I18N::number($key) ?>
279                            </a>
280                        <?php else : ?>
281                            <?= I18N::number($key) ?>
282                        <?php endif ?>
283                    <?php endif ?>
284                </td>
285
286                <!-- Birth date -->
287                <td data-sort="<?= $individual->getEstimatedBirthDate()->julianDay() ?>">
288                    <?php $birth_dates = $individual->getAllBirthDates(); ?>
289
290                    <?php foreach ($birth_dates as $n => $birth_date) : ?>
291                        <?= $birth_date->display(true) ?>
292                        <br>
293                    <?php endforeach ?>
294
295                    <?php if (empty($birth_dates) && $show_estimated_dates) : ?>
296                        <?= $individual->getEstimatedBirthDate()->display(true) ?>
297                    <?php endif ?>
298                </td>
299
300                <!-- Birth anniversary -->
301                <td class="text-center" data-sort="<?= - $individual->getEstimatedBirthDate()->julianDay() ?>">
302                    <?php if (isset($birth_dates[0]) && $birth_dates[0]->gregorianYear() >= 1550 && $birth_dates[0]->gregorianYear() < 2030 && !isset($unique_indis[$individual->xref()])) : ?>
303                        <?php
304                            ++$birt_by_decade[(int) ($birth_dates[0]->gregorianYear() / 10) * 10][$individual->sex()];
305                        ?>
306                        <?= Date::getAge($birth_dates[0]) ?>
307                    <?php endif ?>
308                </td>
309
310                <!-- Birth place -->
311                <td>
312                    <?php foreach ($individual->getAllBirthPlaces() as $n => $birth_place) : ?>
313                        <?= $birth_place->shortName(true) ?>
314                        <br>
315                    <?php endforeach ?>
316                </td>
317
318                <!-- Number of children -->
319                <td class="text-center" data-sort="<?= $individual->numberOfChildren() ?>">
320                    <?= I18N::number($individual->numberOfChildren()) ?>
321                </td>
322
323                <!--    Death date -->
324                <?php $death_dates = $individual->getAllDeathDates() ?>
325                <td data-sort="<?= $individual->getEstimatedDeathDate()->julianDay() ?>">
326                    <?php foreach ($death_dates as $num => $death_date) : ?>
327                        <?= $death_date->display(true) ?>
328                    <br>
329                    <?php endforeach ?>
330
331                    <?php if (empty($death_dates) && $show_estimated_dates && $individual->getEstimatedDeathDate()->minimumDate()->minimumJulianDay() < $today_jd) : ?>
332                        <?= $individual->getEstimatedDeathDate()->display(true) ?>
333                    <?php endif ?>
334                </td>
335
336                <!-- Death anniversary -->
337                <td class="text-center" data-sort="<?= - $individual->getEstimatedDeathDate()->julianDay() ?>">
338                    <?php if (isset($death_dates[0]) && $death_dates[0]->gregorianYear() >= 1550 && $death_dates[0]->gregorianYear() < 2030 && !isset($unique_indis[$individual->xref()])) : ?>
339                        <?php
340                            ++$deat_by_decade[(int) ($death_dates[0]->gregorianYear() / 10) * 10][$individual->sex()];
341                        ?>
342                        <?= Date::getAge($death_dates[0]) ?>
343                    <?php endif ?>
344                </td>
345
346                <!-- Age at death -->
347                <?php if (isset($birth_dates[0], $death_dates[0])) : ?>
348                    <?php $age_at_death_years = Date::getAgeYears($birth_dates[0], $death_dates[0]); ?>
349                    <?php $age_at_death_sort = Date::getAge($birth_dates[0], $death_dates[0]); ?>
350                    <?php $age_at_death_display = I18N::number($age_at_death_years); ?>
351                    <?php if (!isset($unique_indis[$individual->xref()]) && $age_at_death_years >= 0 && $age_at_death_years <= $max_age) : ?>
352                        <?php
353                            ++$deat_by_age[$age_at_death_years][$individual->sex()];
354                        ?>
355                    <?php endif ?>
356                <?php else : ?>
357                    <?php $age_at_death_display = ''; ?>
358                    <?php $age_at_death_sort = PHP_INT_MAX; ?>
359                <?php endif ?>
360                <td class="text-center" data-sort="<?= e($age_at_death_sort) ?>">
361                    <?= e($age_at_death_display) ?>
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 ($individual->getEstimatedBirthDate()->maximumJulianDay() > $hundred_years_ago && $individual->getEstimatedBirthDate()->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