xref: /webtrees/resources/views/lists/families-table.phtml (revision d4ec5ec0fe611c85b1d77527db3e9becde7e473d)
1<?php
2
3declare(strict_types=1);
4
5use Fisharebest\Webtrees\Age;
6use Fisharebest\Webtrees\Carbon;
7use Fisharebest\Webtrees\Date;
8use Fisharebest\Webtrees\Individual;
9use Fisharebest\Webtrees\Registry;
10use Fisharebest\Webtrees\Family;
11use Fisharebest\Webtrees\I18N;
12use Fisharebest\Webtrees\Tree;
13use Fisharebest\Webtrees\View;
14use Illuminate\Support\Collection;
15use Ramsey\Uuid\Uuid;
16
17$table_id = 'table-fam-' . Uuid::uuid4()->toString(); // lists requires a unique ID in case there are multiple lists per page
18
19$today             = new Date(strtoupper(date('d M Y')));
20$today_jd          = Carbon::now()->julianDay();
21$hundred_years_ago = Carbon::now()->subYears(100)->julianDay();
22
23/**
24 * @var Tree               $tree
25 * @var Collection<Family> $families
26 */
27
28?>
29
30<?php View::push('javascript') ?>
31<script>
32$("#<?= e($table_id) ?> > .wt-table-family").dataTable({
33    processing: true,
34    retrieve: true,
35    columns: [
36        /* Given names       */ { type: "text" },
37        /* Surnames          */ { type: "text" },
38        /* Age               */ { type: "num" },
39        /* Given names       */ { type: "text" },
40        /* Surnames          */ { type: "text" },
41        /* Age               */ { type: "num" },
42        /* Marriage date     */ { type: "num" },
43        /* Anniversary       */ { type: "num" },
44        /* Marriage place    */ { type: "text" },
45        /* Children          */ { type: "num" },
46        /* Last change       */ { visible: <?= json_encode((bool) $tree->getPreference('SHOW_LAST_CHANGE'), JSON_THROW_ON_ERROR) ?> },
47        /* Filter marriage   */ { sortable: false },
48        /* Filter alive/dead */ { sortable: false },
49        /* Filter tree       */ { sortable: false }
50    ],
51    sorting: [
52        [1, "asc"]
53    ]
54});
55
56$("#<?= e($table_id) ?>")
57    /* Hide/show parents */
58    .on("click", "#btn-toggle-parents", function() {
59        $(".wt-individual-list-parents").slideToggle();
60    })
61    /* Hide/show statistics */
62    .on("click", "#btn-toggle-statistics", function() {
63        $("#family-charts-<?= e($table_id) ?>").slideToggle({
64            complete: function () {
65                // Trigger resize to redraw the chart
66                $('div[id^="google-chart-"]').resize();
67            }
68        });
69    })
70    /* Filter buttons in table header */
71    .on("click", "input[data-filter-column]", function() {
72        let checkbox = $(this);
73
74        // Deselect other options
75        let siblings = checkbox.siblings("input[type='checkbox']");
76        siblings.prop("checked", false).removeAttr("checked");
77
78        // Apply (or clear) this filter
79        let checked = checkbox.prop("checked");
80        let filter  = checked ? checkbox.data("filter-value") : "";
81        let column  = $("#<?= e($table_id) ?> .wt-table-family").DataTable().column(checkbox.data("filter-column"));
82        column.search(filter).draw();
83    });
84</script>
85<?php View::endpush() ?>
86
87<?php
88$max_age = (int) $tree->getPreference('MAX_ALIVE_AGE');
89
90// init chart data
91$marr_by_age = [];
92for ($age = 0; $age <= $max_age; $age++) {
93    $marr_by_age[$age]['M'] = 0;
94    $marr_by_age[$age]['F'] = 0;
95    $marr_by_age[$age]['U'] = 0;
96}
97$birt_by_decade = [];
98$marr_by_decade = [];
99for ($year = 1400; $year < 2050; $year += 10) {
100    $birt_by_decade[$year]['M'] = 0;
101    $birt_by_decade[$year]['F'] = 0;
102    $birt_by_decade[$year]['U'] = 0;
103    $marr_by_decade[$year]['M'] = 0;
104    $marr_by_decade[$year]['F'] = 0;
105    $marr_by_decade[$year]['U'] = 0;
106}
107
108$birthData = [
109    [
110        [
111            'label' => I18N::translate('Century'),
112            'type'  => 'date',
113        ], [
114            'label' => I18N::translate('Males'),
115            'type'  => 'number',
116        ], [
117            'label' => I18N::translate('Females'),
118            'type'  => 'number',
119        ],
120    ]
121];
122
123$marriageData = [
124    [
125        [
126            'label' => I18N::translate('Century'),
127            'type'  => 'date',
128        ], [
129            'label' => I18N::translate('Males'),
130            'type'  => 'number',
131        ], [
132            'label' => I18N::translate('Females'),
133            'type'  => 'number',
134        ],
135    ]
136];
137
138$marriageAgeData = [
139    [
140        I18N::translate('Age'),
141        I18N::translate('Males'),
142        I18N::translate('Females'),
143        I18N::translate('Average age'),
144    ]
145];
146
147?>
148
149<div id="<?= e($table_id) ?>">
150    <table class="table table-bordered table-sm wt-table-family"
151        <?= view('lists/datatables-attributes') ?>
152    >
153        <thead>
154            <tr>
155                <th colspan="14">
156                    <div class="btn-toolbar d-flex justify-content-between mb-2">
157                        <div class="btn-group btn-group-sm" role="group">
158                            <input id="<?= e($table_id) ?>-bg-dead-N" class="btn-check" type="checkbox" data-filter-column="12" data-filter-value="N" autocomplete="off">
159                            <label for="<?= e($table_id) ?>-bg-dead-N" class="btn btn-outline-secondary btn-sm" title="' . I18N::translate('Show individuals who are alive or couples where both partners are alive.') ?>">
160                                <?= I18N::translate('Both alive') ?>
161                            </label>
162
163                            <input id="<?= e($table_id) ?>-bg-dead-W" class="btn-check" type="checkbox" data-filter-column="12" data-filter-value="W" autocomplete="off">
164                            <label for="<?= e($table_id) ?>-bg-dead-W" class="btn btn-outline-secondary" title="<?= I18N::translate('Show couples where only the female partner is dead.') ?>">
165                                <?= I18N::translate('Widower') ?>
166                            </label>
167
168                            <input id="<?= e($table_id) ?>-bg-dead-H" class="btn-check" type="checkbox" data-filter-column="12" data-filter-value="H" autocomplete="off">
169                            <label for="<?= e($table_id) ?>-bg-dead-H" class="btn btn-outline-secondary" title="<?= I18N::translate('Show couples where only the male partner is dead.') ?>">
170                                <?= I18N::translate('Widow') ?>
171                            </label>
172
173                            <input id="<?= e($table_id) ?>-bg-dead-Y" class="btn-check" type="checkbox" data-filter-column="12" data-filter-value="Y" autocomplete="off">
174                            <label for="<?= e($table_id) ?>-bg-dead-Y" class="btn btn-outline-secondary" title="<?= I18N::translate('Show individuals who are dead or couples where both partners are dead.') ?>">
175                                <?= I18N::translate('Both dead') ?>
176                            </label>
177                        </div>
178
179                        <div class="btn-group btn-group-sm" role="group">
180                            <input id="<?= e($table_id) ?>-bg-roots-R" class="btn-check" type="checkbox" data-filter-column="13" data-filter-value="R" autocomplete="off">
181                            <label for="<?= e($table_id) ?>-bg-roots-R" 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.') ?>">
182                                <?= I18N::translate('Roots') ?>
183                            </label>
184
185                            <input id="<?= e($table_id) ?>-bg-roots-L" class="btn-check" type="checkbox" data-filter-column="13" data-filter-value="L" autocomplete="off">
186                            <label for="<?= e($table_id) ?>-bg-roots-L" 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.') ?>">
187                                <?= I18N::translate('Leaves') ?>
188                            </label>
189                        </div>
190
191                        <div class="btn-group btn-group-sm" role="group">
192                            <input id="<?= e($table_id) ?>-bg-marr-U" class="btn-check" type="checkbox" data-filter-column="11" data-filter-value="U" autocomplete="off">
193                            <label for="<?= e($table_id) ?>-bg-marr-U" class="btn btn-outline-secondary" title="<?= I18N::translate('Show couples with an unknown marriage date.') ?>">
194                                <?= I18N::translate('Not married') ?>
195                            </label>
196
197                            <input id="<?= e($table_id) ?>-bg-marr-YES" class="btn-check" type="checkbox" data-filter-column="11" data-filter-value="YES" autocomplete="off">
198                            <label for="<?= e($table_id) ?>-bg-marr-YES" class="btn btn-outline-secondary" title="<?= I18N::translate('Show couples who married more than 100 years ago.') ?>">
199                                <?= I18N::translate('Marriage') ?>&gt;100
200                            </label>
201
202                            <input id="<?= e($table_id) ?>-bg-marr-Y100" class="btn-check" type="checkbox" data-filter-column="11" data-filter-value="Y100" autocomplete="off">
203                            <label for="<?= e($table_id) ?>-bg-marr-Y100" class="btn btn-outline-secondary" title="<?= I18N::translate('Show couples who married within the last 100 years.') ?>">
204                                <?= I18N::translate('Marriage') ?>&lt;=100
205                            </label>
206
207                            <input id="<?= e($table_id) ?>-bg-marr-D" class="btn-check" type="checkbox" data-filter-column="11" data-filter-value="D" autocomplete="off">
208                            <label for="<?= e($table_id) ?>-bg-marr-D" class="btn btn-outline-secondary" title="<?= I18N::translate('Show divorced couples.') ?>">
209                                <?= I18N::translate('Divorce') ?>
210                            </label>
211
212                            <input id="<?= e($table_id) ?>-bg-marr-M" class="btn-check" type="checkbox" data-filter-column="11" data-filter-value="M" autocomplete="off">
213                            <label for="<?= e($table_id) ?>-bg-marr-M" class="btn btn-outline-secondary" title="<?= I18N::translate('Show couples where either partner married more than once.') ?>">
214                                <?= I18N::translate('Multiple marriages') ?>
215                            </label>
216                        </div>
217                    </div>
218                </th>
219            </tr>
220            <tr>
221                <th><?= I18N::translate('Given names') ?></th>
222                <th><?= I18N::translate('Surname') ?></th>
223                <th><?= I18N::translate('Age') ?></th>
224                <th><?= I18N::translate('Given names') ?></th>
225                <th><?= I18N::translate('Surname') ?></th>
226                <th><?= I18N::translate('Age') ?></th>
227                <th><?= I18N::translate('Marriage') ?></th>
228                <th>
229                    <span title="<?= I18N::translate('Anniversary') ?>">
230                        <?= view('icons/anniversary') ?>
231                    </span>
232                </th>
233                <th><?= I18N::translate('Place') ?></th>
234                <th><i class="icon-children" title="<?= I18N::translate('Children') ?>"></i></th>
235                <th><?= I18N::translate('Last change') ?></th>
236                <th hidden></th>
237                <th hidden></th>
238                <th hidden></th>
239            </tr>
240        </thead>
241
242        <tbody>
243        <?php foreach ($families as $family) : ?>
244            <?php $husb = $family->husband() ?? Registry::individualFactory()->new('H', '0 @H@ INDI', null, $family->tree()) ?>
245            <?php $wife = $family->wife() ?? Registry::individualFactory()->new('W', '0 @W@ INDI', null, $family->tree()) ?>
246
247            <tr class="<?= $family->isPendingAddition() ? 'wt-new' : '' ?> <?= $family->isPendingDeletion() ? 'wt-old' : '' ?>">
248                <!-- Husband name -->
249                <td colspan="2" data-sort="<?= e(str_replace([',', Individual::PRAENOMEN_NESCIO, Individual::NOMEN_NESCIO], 'AAAA', implode(',', array_reverse(explode(',', $husb->sortName()))))) ?>">
250                    <?php foreach ($husb->getAllNames() as $num => $name) : ?>
251                        <?php if ($name['type'] !== '_MARNM' || $num == $husb->getPrimaryName()) : ?>
252                        <a title="<?= $name['type'] === '_MARNM' ? I18N::translate('Married name') :  '' ?>" href="<?= e($family->url()) ?>" class="<?= $num === $husb->getPrimaryName() ? '' : 'text-muted' ?>">
253                            <?= $name['full'] ?>
254                        </a>
255                            <?php if ($num === $husb->getPrimaryName()) : ?>
256                                <small><?= view('icons/sex', ['sex' => $husb->sex()]) ?></small>
257                            <?php endif ?>
258                        <br>
259                        <?php endif ?>
260                    <?php endforeach ?>
261                    <?= view('lists/individual-table-parents', ['individual' => $husb]) ?>
262                </td>
263
264                <td hidden data-sort="<?= e(str_replace([',', Individual::PRAENOMEN_NESCIO, Individual::NOMEN_NESCIO], 'AAAA', $husb->sortName())) ?>"></td>
265
266                <!-- Husband age -->
267                <?php
268                $age  = new Age($husb->getBirthDate(), $family->getMarriageDate());
269                $year = $wife->getBirthDate()->gregorianYear();
270
271                if ($year >= 1550 && $year < 2030) {
272                    ++$birt_by_decade[(int) ($year / 10) * 10][$husb->sex()];
273                }
274
275                if ($age->ageYears() >= 0 && $age->ageYears() <= $max_age) {
276                    ++$marr_by_age[$age->ageYears()][$husb->sex()];
277                }
278                ?>
279                <td class="text-center" data-sort="<?= $age->ageDays() ?>">
280                    <?= $age->ageYearsString() ?>
281                </td>
282
283                <!-- Wife name -->
284                <td colspan="2" data-sort="<?= e(str_replace([',', Individual::PRAENOMEN_NESCIO, Individual::NOMEN_NESCIO], 'AAAA', implode(',', array_reverse(explode(',', $wife->sortName()))))) ?>">
285                    <?php foreach ($wife->getAllNames() as $num => $name) : ?>
286                        <?php if ($name['type'] !== '_MARNM' || $num == $wife->getPrimaryName()) : ?>
287                            <a title="<?= $name['type'] === '_MARNM' ? I18N::translate('Married name') :  '' ?>" href="<?= e($family->url()) ?>" class="<?= $num === $wife->getPrimaryName() ? '' : 'text-muted' ?>">
288                                <?= $name['full'] ?>
289                            </a>
290                            <?php if ($num === $wife->getPrimaryName()) : ?>
291                                <small><?= view('icons/sex', ['sex' => $wife->sex()]) ?></small>
292                            <?php endif ?>
293                            <br>
294                        <?php endif ?>
295                    <?php endforeach ?>
296                    <?= view('lists/individual-table-parents', ['individual' => $wife]) ?>
297                </td>
298
299                <td hidden data-sort="<?= e(str_replace([',', Individual::PRAENOMEN_NESCIO, Individual::NOMEN_NESCIO], 'AAAA', $wife->sortName())) ?>"></td>
300
301                <!-- Wife age -->
302                <?php
303                $age  = new Age($wife->getBirthDate(), $family->getMarriageDate());
304                $year = $wife->getBirthDate()->gregorianYear();
305
306                if ($year >= 1550 && $year < 2030) {
307                    ++$birt_by_decade[(int) ($year / 10) * 10][$wife->sex()];
308                }
309
310                if ($age->ageYears() >= 0 && $age->ageYears() <= $max_age) {
311                    ++$marr_by_age[$age->ageYears()][$wife->sex()];
312                }
313                ?>
314                <td class="text-center" data-sort="<?= $age->ageDays() ?>">
315                    <?= $age->ageYearsString() ?>
316                </td>
317
318                <!-- Marriage date -->
319                <td data-sort="<?= $family->getMarriageDate()->julianDay() ?>">
320                    <?php if ($marriage_dates = $family->getAllMarriageDates()) : ?>
321                        <?php foreach ($marriage_dates as $n => $marriage_date) : ?>
322                            <div><?= $marriage_date->display($tree, null, true) ?></div>
323                        <?php endforeach ?>
324                        <?php if ($marriage_dates[0]->gregorianYear() >= 1550 && $marriage_dates[0]->gregorianYear() < 2030) : ?>
325                            <?php
326                                ++$marr_by_decade[(int) ($marriage_dates[0]->gregorianYear() / 10) * 10][$husb->sex()];
327                                ++$marr_by_decade[(int) ($marriage_dates[0]->gregorianYear() / 10) * 10][$wife->sex()];
328                            ?>
329                        <?php endif ?>
330                    <?php elseif ($family->facts(['_NMR'])->isNotEmpty()) : ?>
331                        <?= I18N::translate('no') ?>
332                    <?php elseif ($family->facts(['MARR'])->isNotEmpty()) : ?>
333                            <?= I18N::translate('yes') ?>
334                    <?php endif ?>
335                </td>
336
337                <!-- Marriage anniversary -->
338                <?php $age = new Age($family->getMarriageDate(), $today) ?>
339                <td class="text-center" data-sort="<?= $age->ageDays() ?>">
340                    <?= $age->ageYearsString() ?>
341                </td>
342
343                <!-- Marriage place -->
344                <td data-sort="<?= e($family->getMarriagePlace()->gedcomName()) ?>">
345                    <?php foreach ($family->getAllMarriagePlaces() as $n => $marriage_place) : ?>
346                        <?= $marriage_place->shortName(true) ?>
347                        <br>
348                    <?php endforeach ?>
349                </td>
350
351                <!-- Number of children -->
352                <td class="text-center" data-sort="<?= $family->numberOfChildren() ?>">
353                    <?= I18N::number($family->numberOfChildren()) ?>
354                </td>
355
356                <!-- Last change -->
357                <td data-sort="<?= $family->lastChangeTimestamp()->unix() ?>">
358                    <?= view('components/datetime', ['timestamp' => $family->lastChangeTimestamp()]) ?>
359                </td>
360
361                <!-- Filter by marriage date -->
362                <td hidden>
363                    <?php if ($family->getMarriageDate()->maximumJulianDay() > $hundred_years_ago && $family->getMarriageDate()->maximumJulianDay() <= $today_jd) : ?>
364                        Y100
365                    <?php elseif ($family->facts(['MARR'])->isNotEmpty()) : ?>
366                        YES
367                    <?php else : ?>
368                        U
369                    <?php endif ?>
370                    <?php if ($family->facts(['DIV'])->isNotEmpty()) : ?>
371                        D
372                    <?php endif ?>
373                    <?php if (count($husb->spouseFamilies()) > 1 || count($wife->spouseFamilies()) > 1) : ?>
374                        M
375                    <?php endif ?>
376                </td>
377
378                <!-- Filter by alive/dead -->
379                <td hidden>
380                    <?php if ($husb->isDead() && $wife->isDead()) : ?>
381                        Y
382                    <?php endif ?>
383                    <?php if ($husb->isDead() && !$wife->isDead()) : ?>
384                        <?php if ($wife->sex() === 'F') : ?>
385                            H
386                        <?php endif ?>
387                        <?php if ($wife->sex() === 'M') : ?>
388                            W
389                        <?php endif ?>
390                    <?php endif ?>
391                    <?php if (!$husb->isDead() && $wife->isDead()) : ?>
392                        <?php if ($husb->sex() === 'M') : ?>
393                            W
394                        <?php endif ?>
395                        <?php if ($husb->sex() === 'F') : ?>
396                            H
397                        <?php endif ?>
398                    <?php endif ?>
399                    <?php if (!$husb->isDead() && !$wife->isDead()) : ?>
400                        N
401                    <?php endif ?>
402                </td>
403
404                <!-- Filter by roots/leaves -->
405                <td hidden>
406                    <?php if ($husb->childFamilies()->isEmpty() && $wife->childFamilies()->isEmpty()) : ?>
407                        R
408                    <?php elseif (!$husb->isDead() && !$wife->isDead() && $family->numberOfChildren() === 0) : ?>
409                        L
410                    <?php endif ?>
411                </td>
412            </tr>
413        <?php endforeach ?>
414        </tbody>
415
416        <tfoot>
417            <tr>
418                <th colspan="14">
419                    <div class="btn-group btn-group-sm">
420                        <input type="checkbox" class="btn-check" id="btn-toggle-parents" autocomplete="off" data-wt-persist="families-parents">
421                        <label class="btn btn-secondary" for="btn-toggle-parents">
422                            <?= I18N::translate('Show parents') ?>
423                        </label>
424                        <input type="checkbox" class="btn-check" id="btn-toggle-statistics" autocomplete="off" data-wt-persist="families-statistics">
425                        <label class="btn btn-secondary" for="btn-toggle-statistics">
426                            <?= I18N::translate('Show statistics charts') ?>
427                        </label>
428                    </div>
429                </th>
430            </tr>
431        </tfoot>
432    </table>
433</div>
434
435<div id="family-charts-<?= e($table_id) ?>" style="display: none;">
436    <div class="d-grid gap-3 my-3">
437        <div class="card">
438            <div class="card-header">
439                <?= I18N::translate('Decade of birth') ?>
440            </div><div class="card-body">
441                <?php
442                foreach ($birt_by_decade as $century => $values) {
443                    if ($values['M'] + $values['F'] > 0) {
444                        $birthData[] = [
445                            [
446                                'v' => 'Date(' . $century . ', 0, 1)',
447                                'f' => $century,
448                            ],
449                            $values['M'],
450                            $values['F'],
451                        ];
452                    }
453                }
454                ?>
455                <?= view('lists/chart-by-decade', ['data' => $birthData, 'title' => I18N::translate('Decade of birth')]) ?>
456            </div>
457        </div>
458        <div class="card">
459            <div class="card-header">
460                <?= I18N::translate('Decade of marriage') ?>
461            </div><div class="card-body">
462                <?php
463                foreach ($marr_by_decade as $century => $values) {
464                    if ($values['M'] + $values['F'] > 0) {
465                        $marriageData[] = [
466                            [
467                                'v' => 'Date(' . $century . ', 0, 1)',
468                                'f' => $century,
469                            ],
470                            $values['M'],
471                            $values['F'],
472                        ];
473                    }
474                }
475                ?>
476                <?= view('lists/chart-by-decade', ['data' => $marriageData, 'title' => I18N::translate('Decade of marriage')]) ?>
477            </div>
478        </div>
479        <div class="card">
480            <div class="card-header">
481                <?= I18N::translate('Age in year of marriage') ?>
482            </div>
483            <div class="card-body">
484                <?php
485                    $totalAge = 0;
486                    $totalSum = 0;
487                    $max      = 0;
488
489                foreach ($marr_by_age as $age => $values) {
490                    if ($values['M'] + $values['F'] > 0) {
491                        if ($values['M'] + $values['F'] > $max) {
492                            $max = $values['M'] + $values['F'];
493                        }
494
495                        $totalAge += $age * ($values['M'] + $values['F']);
496                        $totalSum += $values['M'] + $values['F'];
497
498                        $marriageAgeData[] = [
499                            $age,
500                            $values['M'],
501                            $values['F'],
502                            null,
503                        ];
504                    }
505                }
506
507                if ($totalSum > 0) {
508                    $marriageAgeData[] = [
509                        round($totalAge / $totalSum, 1),
510                        null,
511                        null,
512                        0,
513                    ];
514
515                    $marriageAgeData[] = [
516                        round($totalAge / $totalSum, 1),
517                        null,
518                        null,
519                        $max,
520                    ];
521                }
522                ?>
523                <?= view('lists/chart-by-age', ['data' => $marriageAgeData, 'title' => I18N::translate('Age in year of marriage')]) ?>
524            </div>
525        </div>
526    </div>
527</div>
528