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