xref: /webtrees/app/Statistics/Repository/FamilyRepository.php (revision d7c2f5cc56f17801156c069987440ca1da2c5057)
18add1155SRico Sonntag<?php
23976b470SGreg Roach
38add1155SRico Sonntag/**
48add1155SRico Sonntag * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
68add1155SRico Sonntag * This program is free software: you can redistribute it and/or modify
78add1155SRico Sonntag * it under the terms of the GNU General Public License as published by
88add1155SRico Sonntag * the Free Software Foundation, either version 3 of the License, or
98add1155SRico Sonntag * (at your option) any later version.
108add1155SRico Sonntag * This program is distributed in the hope that it will be useful,
118add1155SRico Sonntag * but WITHOUT ANY WARRANTY; without even the implied warranty of
128add1155SRico Sonntag * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
138add1155SRico Sonntag * GNU General Public License for more details.
148add1155SRico Sonntag * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
168add1155SRico Sonntag */
17fcfa147eSGreg Roach
188add1155SRico Sonntagdeclare(strict_types=1);
198add1155SRico Sonntag
208add1155SRico Sonntagnamespace Fisharebest\Webtrees\Statistics\Repository;
218add1155SRico Sonntag
226ccdf4f0SGreg Roachuse Exception;
236f4ec3caSGreg Roachuse Fisharebest\Webtrees\DB;
248add1155SRico Sonntaguse Fisharebest\Webtrees\Family;
25d1a467e4SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
268add1155SRico Sonntaguse Fisharebest\Webtrees\I18N;
27f78da678SGreg Roachuse Fisharebest\Webtrees\Individual;
28f78da678SGreg Roachuse Fisharebest\Webtrees\Registry;
298add1155SRico Sonntaguse Fisharebest\Webtrees\Statistics\Google\ChartChildren;
308add1155SRico Sonntaguse Fisharebest\Webtrees\Statistics\Google\ChartDivorce;
318add1155SRico Sonntaguse Fisharebest\Webtrees\Statistics\Google\ChartFamilyLargest;
328add1155SRico Sonntaguse Fisharebest\Webtrees\Statistics\Google\ChartMarriage;
338add1155SRico Sonntaguse Fisharebest\Webtrees\Statistics\Google\ChartMarriageAge;
348add1155SRico Sonntaguse Fisharebest\Webtrees\Statistics\Google\ChartNoChildrenFamilies;
35f78da678SGreg Roachuse Fisharebest\Webtrees\Statistics\Service\CenturyService;
36f78da678SGreg Roachuse Fisharebest\Webtrees\Statistics\Service\ColorService;
378add1155SRico Sonntaguse Fisharebest\Webtrees\Tree;
382d7289dcSGreg Roachuse Illuminate\Database\Query\Builder;
39a69f5655SGreg Roachuse Illuminate\Database\Query\Expression;
40d1a467e4SGreg Roachuse Illuminate\Database\Query\JoinClause;
413976b470SGreg Roach
424c78e066SGreg Roachuse function arsort;
434c78e066SGreg Roachuse function asort;
444c78e066SGreg Roachuse function e;
454c78e066SGreg Roachuse function floor;
464c78e066SGreg Roachuse function implode;
476ccdf4f0SGreg Roachuse function in_array;
484c78e066SGreg Roachuse function str_replace;
494c78e066SGreg Roachuse function view;
503976b470SGreg Roach
518add1155SRico Sonntag/**
524c78e066SGreg Roach * A repository providing methods for family related statistics.
538add1155SRico Sonntag */
548add1155SRico Sonntagclass FamilyRepository
558add1155SRico Sonntag{
56f78da678SGreg Roach    private CenturyService $century_service;
57f78da678SGreg Roach
58f78da678SGreg Roach    private ColorService $color_service;
59f78da678SGreg Roach
604c78e066SGreg Roach    private Tree $tree;
618add1155SRico Sonntag
628add1155SRico Sonntag    /**
63f78da678SGreg Roach     * @param CenturyService $century_service
64f78da678SGreg Roach     * @param ColorService   $color_service
658add1155SRico Sonntag     * @param Tree           $tree
668add1155SRico Sonntag     */
67f78da678SGreg Roach    public function __construct(CenturyService $century_service, ColorService $color_service, Tree $tree)
688add1155SRico Sonntag    {
69f78da678SGreg Roach        $this->century_service = $century_service;
70f78da678SGreg Roach        $this->color_service   = $color_service;
718add1155SRico Sonntag        $this->tree            = $tree;
728add1155SRico Sonntag    }
738add1155SRico Sonntag
748add1155SRico Sonntag    /**
758add1155SRico Sonntag     * General query on family.
768add1155SRico Sonntag     *
778add1155SRico Sonntag     * @param string $type
788add1155SRico Sonntag     *
798add1155SRico Sonntag     * @return string
808add1155SRico Sonntag     */
818add1155SRico Sonntag    private function familyQuery(string $type): string
828add1155SRico Sonntag    {
8344cdc21eSGreg Roach        $row = DB::table('families')
8444cdc21eSGreg Roach            ->where('f_file', '=', $this->tree->id())
8544cdc21eSGreg Roach            ->orderBy('f_numchil', 'desc')
8644cdc21eSGreg Roach            ->first();
878add1155SRico Sonntag
8844cdc21eSGreg Roach        if ($row === null) {
898add1155SRico Sonntag            return '';
908add1155SRico Sonntag        }
918add1155SRico Sonntag
926b9cb339SGreg Roach        $family = Registry::familyFactory()->mapper($this->tree)($row);
938add1155SRico Sonntag
9444cdc21eSGreg Roach        if (!$family->canShow()) {
9544cdc21eSGreg Roach            return I18N::translate('This information is private and cannot be shown.');
968add1155SRico Sonntag        }
978add1155SRico Sonntag
988add1155SRico Sonntag        switch ($type) {
998add1155SRico Sonntag            default:
1008add1155SRico Sonntag            case 'full':
10144cdc21eSGreg Roach                return $family->formatList();
1028add1155SRico Sonntag
10344cdc21eSGreg Roach            case 'size':
10444cdc21eSGreg Roach                return I18N::number((int) $row->f_numchil);
10544cdc21eSGreg Roach
10644cdc21eSGreg Roach            case 'name':
10739ca88baSGreg Roach                return '<a href="' . e($family->url()) . '">' . $family->fullName() . '</a>';
10844cdc21eSGreg Roach        }
1098add1155SRico Sonntag    }
1108add1155SRico Sonntag
1118add1155SRico Sonntag    /**
1128add1155SRico Sonntag     * Find the family with the most children.
1138add1155SRico Sonntag     *
1148add1155SRico Sonntag     * @return string
1158add1155SRico Sonntag     */
1168add1155SRico Sonntag    public function largestFamily(): string
1178add1155SRico Sonntag    {
1188add1155SRico Sonntag        return $this->familyQuery('full');
1198add1155SRico Sonntag    }
1208add1155SRico Sonntag
1218add1155SRico Sonntag    /**
1228add1155SRico Sonntag     * Find the number of children in the largest family.
1238add1155SRico Sonntag     *
1248add1155SRico Sonntag     * @return string
1258add1155SRico Sonntag     */
1268add1155SRico Sonntag    public function largestFamilySize(): string
1278add1155SRico Sonntag    {
1288add1155SRico Sonntag        return $this->familyQuery('size');
1298add1155SRico Sonntag    }
1308add1155SRico Sonntag
1318add1155SRico Sonntag    /**
1328add1155SRico Sonntag     * Find the family with the most children.
1338add1155SRico Sonntag     *
1348add1155SRico Sonntag     * @return string
1358add1155SRico Sonntag     */
1368add1155SRico Sonntag    public function largestFamilyName(): string
1378add1155SRico Sonntag    {
1388add1155SRico Sonntag        return $this->familyQuery('name');
1398add1155SRico Sonntag    }
1408add1155SRico Sonntag
1418add1155SRico Sonntag    /**
1428add1155SRico Sonntag     * Find the couple with the most grandchildren.
1438add1155SRico Sonntag     *
1448add1155SRico Sonntag     * @param int $total
1458add1155SRico Sonntag     *
14676d39c55SGreg Roach     * @return array<array<string,int|Family>>
1478add1155SRico Sonntag     */
1488add1155SRico Sonntag    private function topTenGrandFamilyQuery(int $total): array
1498add1155SRico Sonntag    {
1502d7289dcSGreg Roach        return DB::table('families')
1510b5fd0a6SGreg Roach            ->join('link AS children', static function (JoinClause $join): void {
1522d7289dcSGreg Roach                $join
1532d7289dcSGreg Roach                    ->on('children.l_from', '=', 'f_id')
1542d7289dcSGreg Roach                    ->on('children.l_file', '=', 'f_file')
1552d7289dcSGreg Roach                    ->where('children.l_type', '=', 'CHIL');
1560b5fd0a6SGreg Roach            })->join('link AS mchildren', static function (JoinClause $join): void {
1572d7289dcSGreg Roach                $join
1582d7289dcSGreg Roach                    ->on('mchildren.l_file', '=', 'children.l_file')
1592d7289dcSGreg Roach                    ->on('mchildren.l_from', '=', 'children.l_to')
1602d7289dcSGreg Roach                    ->where('mchildren.l_type', '=', 'FAMS');
1610b5fd0a6SGreg Roach            })->join('link AS gchildren', static function (JoinClause $join): void {
1622d7289dcSGreg Roach                $join
1632d7289dcSGreg Roach                    ->on('gchildren.l_file', '=', 'mchildren.l_file')
1642d7289dcSGreg Roach                    ->on('gchildren.l_from', '=', 'mchildren.l_to')
1652d7289dcSGreg Roach                    ->where('gchildren.l_type', '=', 'CHIL');
1662d7289dcSGreg Roach            })
1672d7289dcSGreg Roach            ->where('f_file', '=', $this->tree->id())
1682d7289dcSGreg Roach            ->groupBy(['f_id', 'f_file'])
169a69f5655SGreg Roach            ->orderBy(new Expression('COUNT(*)'), 'DESC')
17047256fc5SGreg Roach            ->select(['families.*'])
1712d7289dcSGreg Roach            ->limit($total)
1722d7289dcSGreg Roach            ->get()
1736b9cb339SGreg Roach            ->map(Registry::familyFactory()->mapper($this->tree))
1742d7289dcSGreg Roach            ->filter(GedcomRecord::accessFilter())
1750b5fd0a6SGreg Roach            ->map(static function (Family $family): array {
1762d7289dcSGreg Roach                $count = 0;
1772d7289dcSGreg Roach                foreach ($family->children() as $child) {
1782d7289dcSGreg Roach                    foreach ($child->spouseFamilies() as $spouse_family) {
1792d7289dcSGreg Roach                        $count += $spouse_family->children()->count();
1802d7289dcSGreg Roach                    }
1818add1155SRico Sonntag                }
1828add1155SRico Sonntag
1832d7289dcSGreg Roach                return [
1848add1155SRico Sonntag                    'family' => $family,
1852d7289dcSGreg Roach                    'count'  => $count,
1868add1155SRico Sonntag                ];
1872d7289dcSGreg Roach            })
1882d7289dcSGreg Roach            ->all();
1898add1155SRico Sonntag    }
1908add1155SRico Sonntag
1918add1155SRico Sonntag    /**
1928add1155SRico Sonntag     * Find the couple with the most grandchildren.
1938add1155SRico Sonntag     *
1948add1155SRico Sonntag     * @param int $total
1958add1155SRico Sonntag     *
1968add1155SRico Sonntag     * @return string
1978add1155SRico Sonntag     */
1988add1155SRico Sonntag    public function topTenLargestGrandFamily(int $total = 10): string
1998add1155SRico Sonntag    {
2002d7289dcSGreg Roach        return view('statistics/families/top10-nolist-grand', [
2012d7289dcSGreg Roach            'records' => $this->topTenGrandFamilyQuery($total),
2022d7289dcSGreg Roach        ]);
2038add1155SRico Sonntag    }
2048add1155SRico Sonntag
2058add1155SRico Sonntag    /**
2068add1155SRico Sonntag     * Find the couple with the most grandchildren.
2078add1155SRico Sonntag     *
2088add1155SRico Sonntag     * @param int $total
2098add1155SRico Sonntag     *
2108add1155SRico Sonntag     * @return string
2118add1155SRico Sonntag     */
2128add1155SRico Sonntag    public function topTenLargestGrandFamilyList(int $total = 10): string
2138add1155SRico Sonntag    {
2142d7289dcSGreg Roach        return view('statistics/families/top10-list-grand', [
2152d7289dcSGreg Roach            'records' => $this->topTenGrandFamilyQuery($total),
2162d7289dcSGreg Roach        ]);
2178add1155SRico Sonntag    }
2188add1155SRico Sonntag
2198add1155SRico Sonntag    /**
2208add1155SRico Sonntag     * Find the families with no children.
2218add1155SRico Sonntag     *
2228add1155SRico Sonntag     * @return int
2238add1155SRico Sonntag     */
2248add1155SRico Sonntag    private function noChildrenFamiliesQuery(): int
2258add1155SRico Sonntag    {
2262d7289dcSGreg Roach        return DB::table('families')
2272d7289dcSGreg Roach            ->where('f_file', '=', $this->tree->id())
2282d7289dcSGreg Roach            ->where('f_numchil', '=', 0)
2292d7289dcSGreg Roach            ->count();
2308add1155SRico Sonntag    }
2318add1155SRico Sonntag
2328add1155SRico Sonntag    /**
2338add1155SRico Sonntag     * Find the families with no children.
2348add1155SRico Sonntag     *
2358add1155SRico Sonntag     * @return string
2368add1155SRico Sonntag     */
2378add1155SRico Sonntag    public function noChildrenFamilies(): string
2388add1155SRico Sonntag    {
2398add1155SRico Sonntag        return I18N::number($this->noChildrenFamiliesQuery());
2408add1155SRico Sonntag    }
2418add1155SRico Sonntag
2428add1155SRico Sonntag    /**
2438add1155SRico Sonntag     * Find the families with no children.
2448add1155SRico Sonntag     *
2458add1155SRico Sonntag     * @param string $type
2468add1155SRico Sonntag     *
2478add1155SRico Sonntag     * @return string
2488add1155SRico Sonntag     */
24973d58381SGreg Roach    public function noChildrenFamiliesList(string $type = 'list'): string
2508add1155SRico Sonntag    {
2512c928961SGreg Roach        $families = DB::table('families')
2522c928961SGreg Roach            ->where('f_file', '=', $this->tree->id())
2532c928961SGreg Roach            ->where('f_numchil', '=', 0)
2542c928961SGreg Roach            ->get()
2556b9cb339SGreg Roach            ->map(Registry::familyFactory()->mapper($this->tree))
2562c928961SGreg Roach            ->filter(GedcomRecord::accessFilter());
2578add1155SRico Sonntag
2588add1155SRico Sonntag        $top10 = [];
2592c928961SGreg Roach
2602c928961SGreg Roach        foreach ($families as $family) {
2618add1155SRico Sonntag            if ($type === 'list') {
26239ca88baSGreg Roach                $top10[] = '<li><a href="' . e($family->url()) . '">' . $family->fullName() . '</a></li>';
2638add1155SRico Sonntag            } else {
26439ca88baSGreg Roach                $top10[] = '<a href="' . e($family->url()) . '">' . $family->fullName() . '</a>';
2658add1155SRico Sonntag            }
2668add1155SRico Sonntag        }
2678add1155SRico Sonntag
2688add1155SRico Sonntag        if ($type === 'list') {
2698add1155SRico Sonntag            $top10 = implode('', $top10);
2708add1155SRico Sonntag        } else {
2718add1155SRico Sonntag            $top10 = implode('; ', $top10);
2728add1155SRico Sonntag        }
2738add1155SRico Sonntag
2748add1155SRico Sonntag        if ($type === 'list') {
2758add1155SRico Sonntag            return '<ul>' . $top10 . '</ul>';
2768add1155SRico Sonntag        }
2778add1155SRico Sonntag
2788add1155SRico Sonntag        return $top10;
2798add1155SRico Sonntag    }
2808add1155SRico Sonntag
2818add1155SRico Sonntag    /**
2828add1155SRico Sonntag     * Create a chart of children with no families.
2838add1155SRico Sonntag     *
2848add1155SRico Sonntag     * @param int $year1
2858add1155SRico Sonntag     * @param int $year2
2868add1155SRico Sonntag     *
2878add1155SRico Sonntag     * @return string
2888add1155SRico Sonntag     */
28988de55fdSRico Sonntag    public function chartNoChildrenFamilies(int $year1 = -1, int $year2 = -1): string
2908add1155SRico Sonntag    {
2918add1155SRico Sonntag        $no_child_fam = $this->noChildrenFamiliesQuery();
2928add1155SRico Sonntag
293f78da678SGreg Roach        return (new ChartNoChildrenFamilies($this->century_service, $this->tree))
29488de55fdSRico Sonntag            ->chartNoChildrenFamilies($no_child_fam, $year1, $year2);
2958add1155SRico Sonntag    }
2968add1155SRico Sonntag
2978add1155SRico Sonntag    /**
2988add1155SRico Sonntag     * Returns the ages between siblings.
2998add1155SRico Sonntag     *
3008add1155SRico Sonntag     * @param int $total The total number of records to query
3018add1155SRico Sonntag     *
302f70bcff5SGreg Roach     * @return array<object>
3038add1155SRico Sonntag     */
3048add1155SRico Sonntag    private function ageBetweenSiblingsQuery(int $total): array
3058add1155SRico Sonntag    {
306b1126ab4SGreg Roach        return DB::table('link AS link1')
3070b5fd0a6SGreg Roach            ->join('link AS link2', static function (JoinClause $join): void {
308b1126ab4SGreg Roach                $join
309b1126ab4SGreg Roach                    ->on('link2.l_from', '=', 'link1.l_from')
310b1126ab4SGreg Roach                    ->on('link2.l_type', '=', 'link1.l_type')
311b1126ab4SGreg Roach                    ->on('link2.l_file', '=', 'link1.l_file');
312b1126ab4SGreg Roach            })
3130b5fd0a6SGreg Roach            ->join('dates AS child1', static function (JoinClause $join): void {
314b1126ab4SGreg Roach                $join
315b1126ab4SGreg Roach                    ->on('child1.d_gid', '=', 'link1.l_to')
316b1126ab4SGreg Roach                    ->on('child1.d_file', '=', 'link1.l_file')
317b1126ab4SGreg Roach                    ->where('child1.d_fact', '=', 'BIRT')
318b1126ab4SGreg Roach                    ->where('child1.d_julianday1', '<>', 0);
319b1126ab4SGreg Roach            })
3200b5fd0a6SGreg Roach            ->join('dates AS child2', static function (JoinClause $join): void {
321b1126ab4SGreg Roach                $join
322b1126ab4SGreg Roach                    ->on('child2.d_gid', '=', 'link2.l_to')
323b1126ab4SGreg Roach                    ->on('child2.d_file', '=', 'link2.l_file')
324b1126ab4SGreg Roach                    ->where('child2.d_fact', '=', 'BIRT')
325b1126ab4SGreg Roach                    ->whereColumn('child2.d_julianday2', '>', 'child1.d_julianday1');
326b1126ab4SGreg Roach            })
327b1126ab4SGreg Roach            ->where('link1.l_type', '=', 'CHIL')
328b1126ab4SGreg Roach            ->where('link1.l_file', '=', $this->tree->id())
329b1126ab4SGreg Roach            ->distinct()
330211018abSGreg Roach            ->select(['link1.l_from AS family', 'link1.l_to AS ch1', 'link2.l_to AS ch2', new Expression(DB::prefix('child2.d_julianday2') . ' - ' . DB::prefix('child1.d_julianday1') . ' AS age')])
331b1126ab4SGreg Roach            ->orderBy('age', 'DESC')
332b1126ab4SGreg Roach            ->take($total)
333b1126ab4SGreg Roach            ->get()
334b1126ab4SGreg Roach            ->all();
3358add1155SRico Sonntag    }
3368add1155SRico Sonntag
3378add1155SRico Sonntag    /**
3388add1155SRico Sonntag     * Returns the calculated age the time of event.
3398add1155SRico Sonntag     *
3408add1155SRico Sonntag     * @param int $age The age from the database record
3418add1155SRico Sonntag     *
3428add1155SRico Sonntag     * @return string
3438add1155SRico Sonntag     */
3448add1155SRico Sonntag    private function calculateAge(int $age): string
3458add1155SRico Sonntag    {
346054771e9SGreg Roach        if ($age < 31) {
347054771e9SGreg Roach            return I18N::plural('%s day', '%s days', $age, I18N::number($age));
3488add1155SRico Sonntag        }
3498add1155SRico Sonntag
350054771e9SGreg Roach        if ($age < 365) {
351054771e9SGreg Roach            $months = (int) ($age / 30.5);
352054771e9SGreg Roach
3531061e22bSGreg Roach            return I18N::plural('%s month', '%s months', $months, I18N::number($months));
354054771e9SGreg Roach        }
355054771e9SGreg Roach
356054771e9SGreg Roach        $years = (int) ($age / 365.25);
357054771e9SGreg Roach
358054771e9SGreg Roach        return I18N::plural('%s year', '%s years', $years, I18N::number($years));
3598add1155SRico Sonntag    }
3608add1155SRico Sonntag
3618add1155SRico Sonntag    /**
3628add1155SRico Sonntag     * Find the ages between siblings.
3638add1155SRico Sonntag     *
3648add1155SRico Sonntag     * @param int $total The total number of records to query
3658add1155SRico Sonntag     *
366f78da678SGreg Roach     * @return array<string,Individual|Family|string>
3676ccdf4f0SGreg Roach     * @throws Exception
3688add1155SRico Sonntag     */
3698add1155SRico Sonntag    private function ageBetweenSiblingsNoList(int $total): array
3708add1155SRico Sonntag    {
3718add1155SRico Sonntag        $rows = $this->ageBetweenSiblingsQuery($total);
3728add1155SRico Sonntag
3738add1155SRico Sonntag        foreach ($rows as $fam) {
3746b9cb339SGreg Roach            $family = Registry::familyFactory()->make($fam->family, $this->tree);
3756b9cb339SGreg Roach            $child1 = Registry::individualFactory()->make($fam->ch1, $this->tree);
3766b9cb339SGreg Roach            $child2 = Registry::individualFactory()->make($fam->ch2, $this->tree);
3778add1155SRico Sonntag
378f78da678SGreg Roach            if ($family !== null && $child1 !== null && $child2 !== null && $child1->canShow() && $child2->canShow()) {
3798add1155SRico Sonntag                // ! Single array (no list)
3808add1155SRico Sonntag                return [
3818add1155SRico Sonntag                    'child1' => $child1,
3828add1155SRico Sonntag                    'child2' => $child2,
3838add1155SRico Sonntag                    'family' => $family,
3848add1155SRico Sonntag                    'age'    => $this->calculateAge((int) $fam->age),
3858add1155SRico Sonntag                ];
3868add1155SRico Sonntag            }
3878add1155SRico Sonntag        }
3888add1155SRico Sonntag
3898add1155SRico Sonntag        return [];
3908add1155SRico Sonntag    }
3918add1155SRico Sonntag
3928add1155SRico Sonntag    /**
3938add1155SRico Sonntag     * Find the ages between siblings.
3948add1155SRico Sonntag     *
3958add1155SRico Sonntag     * @param int  $total The total number of records to query
3968add1155SRico Sonntag     * @param bool $one   Include each family only once if true
3978add1155SRico Sonntag     *
398f78da678SGreg Roach     * @return array<int,array<string,Individual|Family|string>>
3996ccdf4f0SGreg Roach     * @throws Exception
4008add1155SRico Sonntag     */
4018add1155SRico Sonntag    private function ageBetweenSiblingsList(int $total, bool $one): array
4028add1155SRico Sonntag    {
4038add1155SRico Sonntag        $rows  = $this->ageBetweenSiblingsQuery($total);
4048add1155SRico Sonntag        $top10 = [];
4058add1155SRico Sonntag        $dist  = [];
4068add1155SRico Sonntag
4078add1155SRico Sonntag        foreach ($rows as $fam) {
4086b9cb339SGreg Roach            $family = Registry::familyFactory()->make($fam->family, $this->tree);
4096b9cb339SGreg Roach            $child1 = Registry::individualFactory()->make($fam->ch1, $this->tree);
4106b9cb339SGreg Roach            $child2 = Registry::individualFactory()->make($fam->ch2, $this->tree);
4118add1155SRico Sonntag
4128add1155SRico Sonntag            $age = $this->calculateAge((int) $fam->age);
4138add1155SRico Sonntag
4146ccdf4f0SGreg Roach            if ($one && !in_array($fam->family, $dist, true)) {
415f78da678SGreg Roach                if ($family !== null && $child1 !== null && $child2 !== null && $child1->canShow() && $child2->canShow()) {
4168add1155SRico Sonntag                    $top10[] = [
4178add1155SRico Sonntag                        'child1' => $child1,
4188add1155SRico Sonntag                        'child2' => $child2,
4198add1155SRico Sonntag                        'family' => $family,
4208add1155SRico Sonntag                        'age'    => $age,
4218add1155SRico Sonntag                    ];
4228add1155SRico Sonntag
4238add1155SRico Sonntag                    $dist[] = $fam->family;
4248add1155SRico Sonntag                }
425f78da678SGreg Roach            } elseif (!$one && $family !== null && $child1 !== null && $child2 !== null && $child1->canShow() && $child2->canShow()) {
4268add1155SRico Sonntag                $top10[] = [
4278add1155SRico Sonntag                    'child1' => $child1,
4288add1155SRico Sonntag                    'child2' => $child2,
4298add1155SRico Sonntag                    'family' => $family,
4308add1155SRico Sonntag                    'age'    => $age,
4318add1155SRico Sonntag                ];
4328add1155SRico Sonntag            }
4338add1155SRico Sonntag        }
4348add1155SRico Sonntag
4358add1155SRico Sonntag        return $top10;
4368add1155SRico Sonntag    }
4378add1155SRico Sonntag
4388add1155SRico Sonntag    /**
4398add1155SRico Sonntag     * Find the ages between siblings.
4408add1155SRico Sonntag     *
4418add1155SRico Sonntag     * @param int $total The total number of records to query
4428add1155SRico Sonntag     *
4438add1155SRico Sonntag     * @return string
4448add1155SRico Sonntag     */
4458add1155SRico Sonntag    private function ageBetweenSiblingsAge(int $total): string
4468add1155SRico Sonntag    {
4478add1155SRico Sonntag        $rows = $this->ageBetweenSiblingsQuery($total);
4488add1155SRico Sonntag
4498add1155SRico Sonntag        foreach ($rows as $fam) {
4508add1155SRico Sonntag            return $this->calculateAge((int) $fam->age);
4518add1155SRico Sonntag        }
4528add1155SRico Sonntag
4538add1155SRico Sonntag        return '';
4548add1155SRico Sonntag    }
4558add1155SRico Sonntag
4568add1155SRico Sonntag    /**
4578add1155SRico Sonntag     * Find the ages between siblings.
4588add1155SRico Sonntag     *
4598add1155SRico Sonntag     * @param int $total The total number of records to query
4608add1155SRico Sonntag     *
4618add1155SRico Sonntag     * @return string
4626ccdf4f0SGreg Roach     * @throws Exception
4638add1155SRico Sonntag     */
4648add1155SRico Sonntag    private function ageBetweenSiblingsName(int $total): string
4658add1155SRico Sonntag    {
4668add1155SRico Sonntag        $rows = $this->ageBetweenSiblingsQuery($total);
4678add1155SRico Sonntag
4688add1155SRico Sonntag        foreach ($rows as $fam) {
4696b9cb339SGreg Roach            $family = Registry::familyFactory()->make($fam->family, $this->tree);
4706b9cb339SGreg Roach            $child1 = Registry::individualFactory()->make($fam->ch1, $this->tree);
4716b9cb339SGreg Roach            $child2 = Registry::individualFactory()->make($fam->ch2, $this->tree);
4728add1155SRico Sonntag
473f78da678SGreg Roach            if ($family !== null && $child1 !== null && $child2 !== null && $child1->canShow() && $child2->canShow()) {
47439ca88baSGreg Roach                $return = '<a href="' . e($child2->url()) . '">' . $child2->fullName() . '</a> ';
4758add1155SRico Sonntag                $return .= I18N::translate('and') . ' ';
47639ca88baSGreg Roach                $return .= '<a href="' . e($child1->url()) . '">' . $child1->fullName() . '</a>';
4778add1155SRico Sonntag                $return .= ' <a href="' . e($family->url()) . '">[' . I18N::translate('View this family') . ']</a>';
4788add1155SRico Sonntag            } else {
4798add1155SRico Sonntag                $return = I18N::translate('This information is private and cannot be shown.');
4808add1155SRico Sonntag            }
4818add1155SRico Sonntag
4828add1155SRico Sonntag            return $return;
4838add1155SRico Sonntag        }
4848add1155SRico Sonntag
4858add1155SRico Sonntag        return '';
4868add1155SRico Sonntag    }
4878add1155SRico Sonntag
4888add1155SRico Sonntag    /**
4898add1155SRico Sonntag     * Find the names of siblings with the widest age gap.
4908add1155SRico Sonntag     *
4918add1155SRico Sonntag     * @param int $total
4928add1155SRico Sonntag     *
4938add1155SRico Sonntag     * @return string
4948add1155SRico Sonntag     */
4958add1155SRico Sonntag    public function topAgeBetweenSiblingsName(int $total = 10): string
4968add1155SRico Sonntag    {
4978add1155SRico Sonntag        return $this->ageBetweenSiblingsName($total);
4988add1155SRico Sonntag    }
4998add1155SRico Sonntag
5008add1155SRico Sonntag    /**
5018add1155SRico Sonntag     * Find the widest age gap between siblings.
5028add1155SRico Sonntag     *
5038add1155SRico Sonntag     * @param int $total
5048add1155SRico Sonntag     *
5058add1155SRico Sonntag     * @return string
5068add1155SRico Sonntag     */
5078add1155SRico Sonntag    public function topAgeBetweenSiblings(int $total = 10): string
5088add1155SRico Sonntag    {
5098add1155SRico Sonntag        return $this->ageBetweenSiblingsAge($total);
5108add1155SRico Sonntag    }
5118add1155SRico Sonntag
5128add1155SRico Sonntag    /**
5138add1155SRico Sonntag     * Find the name of siblings with the widest age gap.
5148add1155SRico Sonntag     *
5158add1155SRico Sonntag     * @param int $total
5168add1155SRico Sonntag     *
5178add1155SRico Sonntag     * @return string
5188add1155SRico Sonntag     */
5198add1155SRico Sonntag    public function topAgeBetweenSiblingsFullName(int $total = 10): string
5208add1155SRico Sonntag    {
5218add1155SRico Sonntag        $record = $this->ageBetweenSiblingsNoList($total);
5228add1155SRico Sonntag
523b2448a1bSGreg Roach        if ($record === []) {
524dd7dd2a1SRico Sonntag            return I18N::translate('This information is not available.');
525cb2263dcSGreg Roach        }
526cb2263dcSGreg Roach
527cb2263dcSGreg Roach        return view('statistics/families/top10-nolist-age', [
5288add1155SRico Sonntag            'record' => $record,
529cb2263dcSGreg Roach        ]);
5308add1155SRico Sonntag    }
5318add1155SRico Sonntag
5328add1155SRico Sonntag    /**
5338add1155SRico Sonntag     * Find the siblings with the widest age gaps.
5348add1155SRico Sonntag     *
5358add1155SRico Sonntag     * @param int    $total
5368add1155SRico Sonntag     * @param string $one
5378add1155SRico Sonntag     *
5388add1155SRico Sonntag     * @return string
5398add1155SRico Sonntag     */
5408add1155SRico Sonntag    public function topAgeBetweenSiblingsList(int $total = 10, string $one = ''): string
5418add1155SRico Sonntag    {
5428add1155SRico Sonntag        $records = $this->ageBetweenSiblingsList($total, (bool) $one);
5438add1155SRico Sonntag
544cb2263dcSGreg Roach        return view('statistics/families/top10-list-age', [
5458add1155SRico Sonntag            'records' => $records,
546cb2263dcSGreg Roach        ]);
5478add1155SRico Sonntag    }
5488add1155SRico Sonntag
5498add1155SRico Sonntag    /**
55052f124b0SAlejandro Criado-Pérez     * General query on families/children.
5518add1155SRico Sonntag     *
5528add1155SRico Sonntag     * @param int    $year1
5538add1155SRico Sonntag     * @param int    $year2
5548add1155SRico Sonntag     *
555*d7c2f5ccSGreg Roach     * @return array<object>
5568add1155SRico Sonntag     */
557b1126ab4SGreg Roach    public function statsChildrenQuery(int $year1 = -1, int $year2 = -1): array
5588add1155SRico Sonntag    {
559b1126ab4SGreg Roach        $query = DB::table('families')
560b1126ab4SGreg Roach            ->where('f_file', '=', $this->tree->id())
5617f5c2944SGreg Roach            ->groupBy(['f_numchil'])
562a69f5655SGreg Roach            ->select(['f_numchil', new Expression('COUNT(*) AS total')]);
5638add1155SRico Sonntag
5648add1155SRico Sonntag        if ($year1 >= 0 && $year2 >= 0) {
565b1126ab4SGreg Roach            $query
5660b5fd0a6SGreg Roach                ->join('dates', static function (JoinClause $join): void {
567b1126ab4SGreg Roach                    $join
568b1126ab4SGreg Roach                        ->on('d_file', '=', 'f_file')
569b1126ab4SGreg Roach                        ->on('d_gid', '=', 'f_id');
570b1126ab4SGreg Roach                })
571b1126ab4SGreg Roach                ->where('d_fact', '=', 'MARR')
572b1126ab4SGreg Roach                ->whereIn('d_type', ['@#DGREGORIAN@', '@#DJULIAN@'])
573b1126ab4SGreg Roach                ->whereBetween('d_year', [$year1, $year2]);
5748add1155SRico Sonntag        }
5758add1155SRico Sonntag
576b1126ab4SGreg Roach        return $query->get()->all();
5778add1155SRico Sonntag    }
5788add1155SRico Sonntag
5798add1155SRico Sonntag    /**
5808add1155SRico Sonntag     * Genearl query on families/children.
5818add1155SRico Sonntag     *
5828add1155SRico Sonntag     * @return string
5838add1155SRico Sonntag     */
58488de55fdSRico Sonntag    public function statsChildren(): string
5858add1155SRico Sonntag    {
586f78da678SGreg Roach        return (new ChartChildren($this->century_service, $this->tree))
58788de55fdSRico Sonntag            ->chartChildren();
5888add1155SRico Sonntag    }
5898add1155SRico Sonntag
5908add1155SRico Sonntag    /**
5918add1155SRico Sonntag     * Count the total children.
5928add1155SRico Sonntag     *
5938add1155SRico Sonntag     * @return string
5948add1155SRico Sonntag     */
5958add1155SRico Sonntag    public function totalChildren(): string
5968add1155SRico Sonntag    {
597d1a467e4SGreg Roach        $total = (int) DB::table('families')
598d1a467e4SGreg Roach            ->where('f_file', '=', $this->tree->id())
599d1a467e4SGreg Roach            ->sum('f_numchil');
6008add1155SRico Sonntag
6018add1155SRico Sonntag        return I18N::number($total);
6028add1155SRico Sonntag    }
6038add1155SRico Sonntag
6048add1155SRico Sonntag    /**
6058add1155SRico Sonntag     * Find the average number of children in families.
6068add1155SRico Sonntag     *
6078add1155SRico Sonntag     * @return string
6088add1155SRico Sonntag     */
6098add1155SRico Sonntag    public function averageChildren(): string
6108add1155SRico Sonntag    {
611d1a467e4SGreg Roach        $average = (float) DB::table('families')
612d1a467e4SGreg Roach            ->where('f_file', '=', $this->tree->id())
613d1a467e4SGreg Roach            ->avg('f_numchil');
6148add1155SRico Sonntag
6158add1155SRico Sonntag        return I18N::number($average, 2);
6168add1155SRico Sonntag    }
6178add1155SRico Sonntag
6188add1155SRico Sonntag    /**
6198add1155SRico Sonntag     * General query on families.
6208add1155SRico Sonntag     *
6218add1155SRico Sonntag     * @param int $total
6228add1155SRico Sonntag     *
62391c84b80SGreg Roach     * @return array<array<string,mixed>>
6248add1155SRico Sonntag     */
6258add1155SRico Sonntag    private function topTenFamilyQuery(int $total): array
6268add1155SRico Sonntag    {
6272d7289dcSGreg Roach        return DB::table('families')
6282d7289dcSGreg Roach            ->where('f_file', '=', $this->tree->id())
6292d7289dcSGreg Roach            ->orderBy('f_numchil', 'DESC')
6302d7289dcSGreg Roach            ->limit($total)
6312d7289dcSGreg Roach            ->get()
6326b9cb339SGreg Roach            ->map(Registry::familyFactory()->mapper($this->tree))
6332d7289dcSGreg Roach            ->filter(GedcomRecord::accessFilter())
634f25fc0f9SGreg Roach            ->map(static fn (Family $family): array => [
6358add1155SRico Sonntag                'family' => $family,
6362d7289dcSGreg Roach                'count'  => $family->numberOfChildren(),
637f25fc0f9SGreg Roach            ])
6382d7289dcSGreg Roach            ->all();
6398add1155SRico Sonntag    }
6408add1155SRico Sonntag
6418add1155SRico Sonntag    /**
6428add1155SRico Sonntag     * The the families with the most children.
6438add1155SRico Sonntag     *
6448add1155SRico Sonntag     * @param int $total
6458add1155SRico Sonntag     *
6468add1155SRico Sonntag     * @return string
6478add1155SRico Sonntag     */
6488add1155SRico Sonntag    public function topTenLargestFamily(int $total = 10): string
6498add1155SRico Sonntag    {
6508add1155SRico Sonntag        $records = $this->topTenFamilyQuery($total);
6518add1155SRico Sonntag
652c0112ce8SGreg Roach        return view('statistics/families/top10-nolist', [
6538add1155SRico Sonntag            'records' => $records,
654c0112ce8SGreg Roach        ]);
6558add1155SRico Sonntag    }
6568add1155SRico Sonntag
6578add1155SRico Sonntag    /**
6588add1155SRico Sonntag     * Find the families with the most children.
6598add1155SRico Sonntag     *
6608add1155SRico Sonntag     * @param int $total
6618add1155SRico Sonntag     *
6628add1155SRico Sonntag     * @return string
6638add1155SRico Sonntag     */
6648add1155SRico Sonntag    public function topTenLargestFamilyList(int $total = 10): string
6658add1155SRico Sonntag    {
6668add1155SRico Sonntag        $records = $this->topTenFamilyQuery($total);
6678add1155SRico Sonntag
668c0112ce8SGreg Roach        return view('statistics/families/top10-list', [
6698add1155SRico Sonntag            'records' => $records,
670c0112ce8SGreg Roach        ]);
6718add1155SRico Sonntag    }
6728add1155SRico Sonntag
6738add1155SRico Sonntag    /**
6748add1155SRico Sonntag     * Create a chart of the largest families.
6758add1155SRico Sonntag     *
6768add1155SRico Sonntag     * @param string|null $color_from
6778add1155SRico Sonntag     * @param string|null $color_to
6788add1155SRico Sonntag     * @param int         $total
6798add1155SRico Sonntag     *
6808add1155SRico Sonntag     * @return string
6818add1155SRico Sonntag     */
6828add1155SRico Sonntag    public function chartLargestFamilies(
6832c6f1bd5SGreg Roach        string|null $color_from = null,
6842c6f1bd5SGreg Roach        string|null $color_to = null,
6858add1155SRico Sonntag        int $total = 10
686e2cbf57aSGreg Roach    ): string {
687f78da678SGreg Roach        return (new ChartFamilyLargest($this->color_service, $this->tree))
68888de55fdSRico Sonntag            ->chartLargestFamilies($color_from, $color_to, $total);
6898add1155SRico Sonntag    }
6908add1155SRico Sonntag
6918add1155SRico Sonntag    /**
6928add1155SRico Sonntag     * Find the month in the year of the birth of the first child.
6938add1155SRico Sonntag     *
694999da590SGreg Roach     * @param int $year1
695999da590SGreg Roach     * @param int $year2
6968add1155SRico Sonntag     *
697999da590SGreg Roach     * @return Builder
6988add1155SRico Sonntag     */
699999da590SGreg Roach    public function monthFirstChildQuery(int $year1 = -1, int $year2 = -1): Builder
7008add1155SRico Sonntag    {
701b1126ab4SGreg Roach        $first_child_subquery = DB::table('link')
7020b5fd0a6SGreg Roach            ->join('dates', static function (JoinClause $join): void {
703b1126ab4SGreg Roach                $join
704b1126ab4SGreg Roach                    ->on('d_gid', '=', 'l_to')
705b1126ab4SGreg Roach                    ->on('d_file', '=', 'l_file')
706b1126ab4SGreg Roach                    ->where('d_julianday1', '<>', 0)
707b1126ab4SGreg Roach                    ->whereIn('d_month', ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC']);
708b1126ab4SGreg Roach            })
709b1126ab4SGreg Roach            ->where('l_file', '=', $this->tree->id())
710b1126ab4SGreg Roach            ->where('l_type', '=', 'CHIL')
711a69f5655SGreg Roach            ->select(['l_from AS family_id', new Expression('MIN(d_julianday1) AS min_birth_jd')])
7127f5c2944SGreg Roach            ->groupBy(['family_id']);
713b1126ab4SGreg Roach
714b1126ab4SGreg Roach        $query = DB::table('link')
7150b5fd0a6SGreg Roach            ->join('dates', static function (JoinClause $join): void {
716b1126ab4SGreg Roach                $join
717b1126ab4SGreg Roach                    ->on('d_gid', '=', 'l_to')
718b1126ab4SGreg Roach                    ->on('d_file', '=', 'l_file');
719b1126ab4SGreg Roach            })
7200b5fd0a6SGreg Roach            ->joinSub($first_child_subquery, 'subquery', static function (JoinClause $join): void {
721b1126ab4SGreg Roach                $join
722b1126ab4SGreg Roach                    ->on('family_id', '=', 'l_from')
723b1126ab4SGreg Roach                    ->on('min_birth_jd', '=', 'd_julianday1');
724b1126ab4SGreg Roach            })
725b1126ab4SGreg Roach            ->where('link.l_file', '=', $this->tree->id())
726999da590SGreg Roach            ->where('link.l_type', '=', 'CHIL')
727a69f5655SGreg Roach            ->select(['d_month', new Expression('COUNT(*) AS total')])
728999da590SGreg Roach            ->groupBy(['d_month']);
729b1126ab4SGreg Roach
730999da590SGreg Roach        if ($year1 >= 0 && $year2 >= 0) {
731999da590SGreg Roach            $query->whereBetween('d_year', [$year1, $year2]);
732999da590SGreg Roach        }
733b1126ab4SGreg Roach
734999da590SGreg Roach        return $query;
735999da590SGreg Roach    }
736999da590SGreg Roach
737999da590SGreg Roach    /**
738999da590SGreg Roach     * Find the month in the year of the birth of the first child.
739999da590SGreg Roach     *
740999da590SGreg Roach     * @param int $year1
741999da590SGreg Roach     * @param int $year2
742999da590SGreg Roach     *
743999da590SGreg Roach     * @return Builder
744999da590SGreg Roach     */
745999da590SGreg Roach    public function monthFirstChildBySexQuery(int $year1 = -1, int $year2 = -1): Builder
746999da590SGreg Roach    {
747999da590SGreg Roach        return $this->monthFirstChildQuery($year1, $year2)
7480b5fd0a6SGreg Roach            ->join('individuals', static function (JoinClause $join): void {
749b1126ab4SGreg Roach                $join
750b1126ab4SGreg Roach                    ->on('i_file', '=', 'l_file')
751b1126ab4SGreg Roach                    ->on('i_id', '=', 'l_to');
752b1126ab4SGreg Roach            })
753a69f5655SGreg Roach            ->select(['d_month', 'i_sex', new Expression('COUNT(*) AS total')])
754b1126ab4SGreg Roach            ->groupBy(['d_month', 'i_sex']);
7558add1155SRico Sonntag    }
7568add1155SRico Sonntag
7578add1155SRico Sonntag    /**
7588add1155SRico Sonntag     * Number of husbands.
7598add1155SRico Sonntag     *
7608add1155SRico Sonntag     * @return string
7618add1155SRico Sonntag     */
7628add1155SRico Sonntag    public function totalMarriedMales(): string
7638add1155SRico Sonntag    {
764320f6a24SGreg Roach        $n = DB::table('families')
765d1a467e4SGreg Roach            ->where('f_file', '=', $this->tree->id())
766d1a467e4SGreg Roach            ->where('f_gedcom', 'LIKE', "%\n1 MARR%")
767d1a467e4SGreg Roach            ->distinct()
768d1a467e4SGreg Roach            ->count('f_husb');
7698add1155SRico Sonntag
7708add1155SRico Sonntag        return I18N::number($n);
7718add1155SRico Sonntag    }
7728add1155SRico Sonntag
7738add1155SRico Sonntag    /**
7748add1155SRico Sonntag     * Number of wives.
7758add1155SRico Sonntag     *
7768add1155SRico Sonntag     * @return string
7778add1155SRico Sonntag     */
7788add1155SRico Sonntag    public function totalMarriedFemales(): string
7798add1155SRico Sonntag    {
780320f6a24SGreg Roach        $n = DB::table('families')
781d1a467e4SGreg Roach            ->where('f_file', '=', $this->tree->id())
782d1a467e4SGreg Roach            ->where('f_gedcom', 'LIKE', "%\n1 MARR%")
783d1a467e4SGreg Roach            ->distinct()
784d1a467e4SGreg Roach            ->count('f_wife');
7858add1155SRico Sonntag
7868add1155SRico Sonntag        return I18N::number($n);
7878add1155SRico Sonntag    }
7888add1155SRico Sonntag
7898add1155SRico Sonntag    /**
7908add1155SRico Sonntag     * General query on parents.
7918add1155SRico Sonntag     *
7928add1155SRico Sonntag     * @param string $type
7938add1155SRico Sonntag     * @param string $age_dir
7948add1155SRico Sonntag     * @param string $sex
7958add1155SRico Sonntag     * @param bool   $show_years
7968add1155SRico Sonntag     *
7978add1155SRico Sonntag     * @return string
7988add1155SRico Sonntag     */
7998add1155SRico Sonntag    private function parentsQuery(string $type, string $age_dir, string $sex, bool $show_years): string
8008add1155SRico Sonntag    {
8018add1155SRico Sonntag        if ($sex === 'F') {
8028add1155SRico Sonntag            $sex_field = 'WIFE';
8038add1155SRico Sonntag        } else {
8048add1155SRico Sonntag            $sex_field = 'HUSB';
8058add1155SRico Sonntag        }
8068add1155SRico Sonntag
8078add1155SRico Sonntag        if ($age_dir !== 'ASC') {
8088add1155SRico Sonntag            $age_dir = 'DESC';
8098add1155SRico Sonntag        }
8108add1155SRico Sonntag
8112c928961SGreg Roach        $row = DB::table('link AS parentfamily')
8120b5fd0a6SGreg Roach            ->join('link AS childfamily', static function (JoinClause $join): void {
8132c928961SGreg Roach                $join
8142c928961SGreg Roach                    ->on('childfamily.l_file', '=', 'parentfamily.l_file')
8152c928961SGreg Roach                    ->on('childfamily.l_from', '=', 'parentfamily.l_from')
8162c928961SGreg Roach                    ->where('childfamily.l_type', '=', 'CHIL');
8172c928961SGreg Roach            })
8180b5fd0a6SGreg Roach            ->join('dates AS birth', static function (JoinClause $join): void {
8192c928961SGreg Roach                $join
8202c928961SGreg Roach                    ->on('birth.d_file', '=', 'parentfamily.l_file')
8212c928961SGreg Roach                    ->on('birth.d_gid', '=', 'parentfamily.l_to')
8222c928961SGreg Roach                    ->where('birth.d_fact', '=', 'BIRT')
8232c928961SGreg Roach                    ->where('birth.d_julianday1', '<>', 0);
8242c928961SGreg Roach            })
8250b5fd0a6SGreg Roach            ->join('dates AS childbirth', static function (JoinClause $join): void {
8262c928961SGreg Roach                $join
8272c928961SGreg Roach                    ->on('childbirth.d_file', '=', 'parentfamily.l_file')
8283413ec75SRico Sonntag                    ->on('childbirth.d_gid', '=', 'childfamily.l_to')
8293413ec75SRico Sonntag                    ->where('childbirth.d_fact', '=', 'BIRT');
8302c928961SGreg Roach            })
8312c928961SGreg Roach            ->where('childfamily.l_file', '=', $this->tree->id())
8322c928961SGreg Roach            ->where('parentfamily.l_type', '=', $sex_field)
833211018abSGreg Roach            ->where('childbirth.d_julianday2', '>', new Expression(DB::prefix('birth.d_julianday1')))
834211018abSGreg Roach            ->select(['parentfamily.l_to AS id', new Expression(DB::prefix('childbirth.d_julianday2') . ' - ' . DB::prefix('birth.d_julianday1') . ' AS age')])
8352c928961SGreg Roach            ->take(1)
8362c928961SGreg Roach            ->orderBy('age', $age_dir)
8372c928961SGreg Roach            ->get()
8382c928961SGreg Roach            ->first();
8392c928961SGreg Roach
8402c928961SGreg Roach        if ($row === null) {
841bd055353SGreg Roach            return I18N::translate('This information is not available.');
8428add1155SRico Sonntag        }
8438add1155SRico Sonntag
8446b9cb339SGreg Roach        $person = Registry::individualFactory()->make($row->id, $this->tree);
8458add1155SRico Sonntag
8468add1155SRico Sonntag        switch ($type) {
8478add1155SRico Sonntag            default:
8488add1155SRico Sonntag            case 'full':
849f78da678SGreg Roach                if ($person !== null && $person->canShow()) {
8508add1155SRico Sonntag                    $result = $person->formatList();
8518add1155SRico Sonntag                } else {
8528add1155SRico Sonntag                    $result = I18N::translate('This information is private and cannot be shown.');
8538add1155SRico Sonntag                }
8548add1155SRico Sonntag                break;
8558add1155SRico Sonntag
8568add1155SRico Sonntag            case 'name':
85739ca88baSGreg Roach                $result = '<a href="' . e($person->url()) . '">' . $person->fullName() . '</a>';
8588add1155SRico Sonntag                break;
8598add1155SRico Sonntag
8608add1155SRico Sonntag            case 'age':
8618add1155SRico Sonntag                $age = $row->age;
8628add1155SRico Sonntag
8638add1155SRico Sonntag                if ($show_years) {
8648add1155SRico Sonntag                    $result = $this->calculateAge((int) $row->age);
8658add1155SRico Sonntag                } else {
8668add1155SRico Sonntag                    $result = (string) floor($age / 365.25);
8678add1155SRico Sonntag                }
8688add1155SRico Sonntag
8698add1155SRico Sonntag                break;
8708add1155SRico Sonntag        }
8718add1155SRico Sonntag
8728add1155SRico Sonntag        return $result;
8738add1155SRico Sonntag    }
8748add1155SRico Sonntag
8758add1155SRico Sonntag    /**
8768add1155SRico Sonntag     * Find the youngest mother
8778add1155SRico Sonntag     *
8788add1155SRico Sonntag     * @return string
8798add1155SRico Sonntag     */
8808add1155SRico Sonntag    public function youngestMother(): string
8818add1155SRico Sonntag    {
8828add1155SRico Sonntag        return $this->parentsQuery('full', 'ASC', 'F', false);
8838add1155SRico Sonntag    }
8848add1155SRico Sonntag
8858add1155SRico Sonntag    /**
8868add1155SRico Sonntag     * Find the name of the youngest mother.
8878add1155SRico Sonntag     *
8888add1155SRico Sonntag     * @return string
8898add1155SRico Sonntag     */
8908add1155SRico Sonntag    public function youngestMotherName(): string
8918add1155SRico Sonntag    {
8928add1155SRico Sonntag        return $this->parentsQuery('name', 'ASC', 'F', false);
8938add1155SRico Sonntag    }
8948add1155SRico Sonntag
8958add1155SRico Sonntag    /**
8968add1155SRico Sonntag     * Find the age of the youngest mother.
8978add1155SRico Sonntag     *
8988add1155SRico Sonntag     * @param string $show_years
8998add1155SRico Sonntag     *
9008add1155SRico Sonntag     * @return string
9018add1155SRico Sonntag     */
9028add1155SRico Sonntag    public function youngestMotherAge(string $show_years = ''): string
9038add1155SRico Sonntag    {
9048add1155SRico Sonntag        return $this->parentsQuery('age', 'ASC', 'F', (bool) $show_years);
9058add1155SRico Sonntag    }
9068add1155SRico Sonntag
9078add1155SRico Sonntag    /**
9088add1155SRico Sonntag     * Find the oldest mother.
9098add1155SRico Sonntag     *
9108add1155SRico Sonntag     * @return string
9118add1155SRico Sonntag     */
9128add1155SRico Sonntag    public function oldestMother(): string
9138add1155SRico Sonntag    {
9148add1155SRico Sonntag        return $this->parentsQuery('full', 'DESC', 'F', false);
9158add1155SRico Sonntag    }
9168add1155SRico Sonntag
9178add1155SRico Sonntag    /**
9188add1155SRico Sonntag     * Find the name of the oldest mother.
9198add1155SRico Sonntag     *
9208add1155SRico Sonntag     * @return string
9218add1155SRico Sonntag     */
9228add1155SRico Sonntag    public function oldestMotherName(): string
9238add1155SRico Sonntag    {
9248add1155SRico Sonntag        return $this->parentsQuery('name', 'DESC', 'F', false);
9258add1155SRico Sonntag    }
9268add1155SRico Sonntag
9278add1155SRico Sonntag    /**
9288add1155SRico Sonntag     * Find the age of the oldest mother.
9298add1155SRico Sonntag     *
9308add1155SRico Sonntag     * @param string $show_years
9318add1155SRico Sonntag     *
9328add1155SRico Sonntag     * @return string
9338add1155SRico Sonntag     */
9348add1155SRico Sonntag    public function oldestMotherAge(string $show_years = ''): string
9358add1155SRico Sonntag    {
9368add1155SRico Sonntag        return $this->parentsQuery('age', 'DESC', 'F', (bool) $show_years);
9378add1155SRico Sonntag    }
9388add1155SRico Sonntag
9398add1155SRico Sonntag    /**
9408add1155SRico Sonntag     * Find the youngest father.
9418add1155SRico Sonntag     *
9428add1155SRico Sonntag     * @return string
9438add1155SRico Sonntag     */
9448add1155SRico Sonntag    public function youngestFather(): string
9458add1155SRico Sonntag    {
9468add1155SRico Sonntag        return $this->parentsQuery('full', 'ASC', 'M', false);
9478add1155SRico Sonntag    }
9488add1155SRico Sonntag
9498add1155SRico Sonntag    /**
9508add1155SRico Sonntag     * Find the name of the youngest father.
9518add1155SRico Sonntag     *
9528add1155SRico Sonntag     * @return string
9538add1155SRico Sonntag     */
9548add1155SRico Sonntag    public function youngestFatherName(): string
9558add1155SRico Sonntag    {
9568add1155SRico Sonntag        return $this->parentsQuery('name', 'ASC', 'M', false);
9578add1155SRico Sonntag    }
9588add1155SRico Sonntag
9598add1155SRico Sonntag    /**
9608add1155SRico Sonntag     * Find the age of the youngest father.
9618add1155SRico Sonntag     *
9628add1155SRico Sonntag     * @param string $show_years
9638add1155SRico Sonntag     *
9648add1155SRico Sonntag     * @return string
9658add1155SRico Sonntag     */
9668add1155SRico Sonntag    public function youngestFatherAge(string $show_years = ''): string
9678add1155SRico Sonntag    {
9688add1155SRico Sonntag        return $this->parentsQuery('age', 'ASC', 'M', (bool) $show_years);
9698add1155SRico Sonntag    }
9708add1155SRico Sonntag
9718add1155SRico Sonntag    /**
9728add1155SRico Sonntag     * Find the oldest father.
9738add1155SRico Sonntag     *
9748add1155SRico Sonntag     * @return string
9758add1155SRico Sonntag     */
9768add1155SRico Sonntag    public function oldestFather(): string
9778add1155SRico Sonntag    {
9788add1155SRico Sonntag        return $this->parentsQuery('full', 'DESC', 'M', false);
9798add1155SRico Sonntag    }
9808add1155SRico Sonntag
9818add1155SRico Sonntag    /**
9828add1155SRico Sonntag     * Find the name of the oldest father.
9838add1155SRico Sonntag     *
9848add1155SRico Sonntag     * @return string
9858add1155SRico Sonntag     */
9868add1155SRico Sonntag    public function oldestFatherName(): string
9878add1155SRico Sonntag    {
9888add1155SRico Sonntag        return $this->parentsQuery('name', 'DESC', 'M', false);
9898add1155SRico Sonntag    }
9908add1155SRico Sonntag
9918add1155SRico Sonntag    /**
9928add1155SRico Sonntag     * Find the age of the oldest father.
9938add1155SRico Sonntag     *
9948add1155SRico Sonntag     * @param string $show_years
9958add1155SRico Sonntag     *
9968add1155SRico Sonntag     * @return string
9978add1155SRico Sonntag     */
9988add1155SRico Sonntag    public function oldestFatherAge(string $show_years = ''): string
9998add1155SRico Sonntag    {
10008add1155SRico Sonntag        return $this->parentsQuery('age', 'DESC', 'M', (bool) $show_years);
10018add1155SRico Sonntag    }
10028add1155SRico Sonntag
10038add1155SRico Sonntag    /**
10048add1155SRico Sonntag     * General query on age at marriage.
10058add1155SRico Sonntag     *
10068add1155SRico Sonntag     * @param string $type
10075ad3f7b6SGreg Roach     * @param string $age_dir "ASC" or "DESC"
10088add1155SRico Sonntag     * @param int    $total
10098add1155SRico Sonntag     *
10108add1155SRico Sonntag     * @return string
10118add1155SRico Sonntag     */
10128add1155SRico Sonntag    private function ageOfMarriageQuery(string $type, string $age_dir, int $total): string
10138add1155SRico Sonntag    {
10145ad3f7b6SGreg Roach        $hrows = DB::table('families')
10155ad3f7b6SGreg Roach            ->where('f_file', '=', $this->tree->id())
10160b5fd0a6SGreg Roach            ->join('dates AS married', static function (JoinClause $join): void {
10175ad3f7b6SGreg Roach                $join
10185ad3f7b6SGreg Roach                    ->on('married.d_file', '=', 'f_file')
10195ad3f7b6SGreg Roach                    ->on('married.d_gid', '=', 'f_id')
10205ad3f7b6SGreg Roach                    ->where('married.d_fact', '=', 'MARR')
10215ad3f7b6SGreg Roach                    ->where('married.d_julianday1', '<>', 0);
10225ad3f7b6SGreg Roach            })
10230b5fd0a6SGreg Roach            ->join('dates AS husbdeath', static function (JoinClause $join): void {
10245ad3f7b6SGreg Roach                $join
10255ad3f7b6SGreg Roach                    ->on('husbdeath.d_gid', '=', 'f_husb')
10265ad3f7b6SGreg Roach                    ->on('husbdeath.d_file', '=', 'f_file')
10275ad3f7b6SGreg Roach                    ->where('husbdeath.d_fact', '=', 'DEAT');
10285ad3f7b6SGreg Roach            })
10295ad3f7b6SGreg Roach            ->whereColumn('married.d_julianday1', '<', 'husbdeath.d_julianday2')
10307f5c2944SGreg Roach            ->groupBy(['f_id'])
1031211018abSGreg Roach            ->select(['f_id AS family', new Expression('MIN(' . DB::prefix('husbdeath.d_julianday2') . ' - ' . DB::prefix('married.d_julianday1') . ') AS age')])
10325ad3f7b6SGreg Roach            ->get()
10335ad3f7b6SGreg Roach            ->all();
10348add1155SRico Sonntag
10355ad3f7b6SGreg Roach        $wrows = DB::table('families')
10365ad3f7b6SGreg Roach            ->where('f_file', '=', $this->tree->id())
10370b5fd0a6SGreg Roach            ->join('dates AS married', static function (JoinClause $join): void {
10385ad3f7b6SGreg Roach                $join
10395ad3f7b6SGreg Roach                    ->on('married.d_file', '=', 'f_file')
10405ad3f7b6SGreg Roach                    ->on('married.d_gid', '=', 'f_id')
10415ad3f7b6SGreg Roach                    ->where('married.d_fact', '=', 'MARR')
10425ad3f7b6SGreg Roach                    ->where('married.d_julianday1', '<>', 0);
10435ad3f7b6SGreg Roach            })
10440b5fd0a6SGreg Roach            ->join('dates AS wifedeath', static function (JoinClause $join): void {
10455ad3f7b6SGreg Roach                $join
10465ad3f7b6SGreg Roach                    ->on('wifedeath.d_gid', '=', 'f_wife')
10475ad3f7b6SGreg Roach                    ->on('wifedeath.d_file', '=', 'f_file')
10485ad3f7b6SGreg Roach                    ->where('wifedeath.d_fact', '=', 'DEAT');
10495ad3f7b6SGreg Roach            })
10505ad3f7b6SGreg Roach            ->whereColumn('married.d_julianday1', '<', 'wifedeath.d_julianday2')
10517f5c2944SGreg Roach            ->groupBy(['f_id'])
1052211018abSGreg Roach            ->select(['f_id AS family', new Expression('MIN(' . DB::prefix('wifedeath.d_julianday2') . ' - ' . DB::prefix('married.d_julianday1') . ') AS age')])
10535ad3f7b6SGreg Roach            ->get()
10545ad3f7b6SGreg Roach            ->all();
10558add1155SRico Sonntag
10565ad3f7b6SGreg Roach        $drows = DB::table('families')
10575ad3f7b6SGreg Roach            ->where('f_file', '=', $this->tree->id())
10580b5fd0a6SGreg Roach            ->join('dates AS married', static function (JoinClause $join): void {
10595ad3f7b6SGreg Roach                $join
10605ad3f7b6SGreg Roach                    ->on('married.d_file', '=', 'f_file')
10615ad3f7b6SGreg Roach                    ->on('married.d_gid', '=', 'f_id')
10625ad3f7b6SGreg Roach                    ->where('married.d_fact', '=', 'MARR')
10635ad3f7b6SGreg Roach                    ->where('married.d_julianday1', '<>', 0);
10645ad3f7b6SGreg Roach            })
10650b5fd0a6SGreg Roach            ->join('dates AS divorced', static function (JoinClause $join): void {
10665ad3f7b6SGreg Roach                $join
10675ad3f7b6SGreg Roach                    ->on('divorced.d_gid', '=', 'f_id')
10685ad3f7b6SGreg Roach                    ->on('divorced.d_file', '=', 'f_file')
10695ad3f7b6SGreg Roach                    ->whereIn('divorced.d_fact', ['DIV', 'ANUL', '_SEPR']);
10705ad3f7b6SGreg Roach            })
10715ad3f7b6SGreg Roach            ->whereColumn('married.d_julianday1', '<', 'divorced.d_julianday2')
10727f5c2944SGreg Roach            ->groupBy(['f_id'])
1073211018abSGreg Roach            ->select(['f_id AS family', new Expression('MIN(' . DB::prefix('divorced.d_julianday2') . ' - ' . DB::prefix('married.d_julianday1') . ') AS age')])
10745ad3f7b6SGreg Roach            ->get()
10755ad3f7b6SGreg Roach            ->all();
10768add1155SRico Sonntag
10778add1155SRico Sonntag        $rows = [];
10788add1155SRico Sonntag        foreach ($drows as $family) {
10798add1155SRico Sonntag            $rows[$family->family] = $family->age;
10808add1155SRico Sonntag        }
10818add1155SRico Sonntag
10828add1155SRico Sonntag        foreach ($hrows as $family) {
10838add1155SRico Sonntag            if (!isset($rows[$family->family])) {
10848add1155SRico Sonntag                $rows[$family->family] = $family->age;
10858add1155SRico Sonntag            }
10868add1155SRico Sonntag        }
10878add1155SRico Sonntag
10888add1155SRico Sonntag        foreach ($wrows as $family) {
10898add1155SRico Sonntag            if (!isset($rows[$family->family])) {
10908add1155SRico Sonntag                $rows[$family->family] = $family->age;
10918add1155SRico Sonntag            } elseif ($rows[$family->family] > $family->age) {
10928add1155SRico Sonntag                $rows[$family->family] = $family->age;
10938add1155SRico Sonntag            }
10948add1155SRico Sonntag        }
10958add1155SRico Sonntag
10968add1155SRico Sonntag        if ($age_dir === 'DESC') {
10978add1155SRico Sonntag            arsort($rows);
10988add1155SRico Sonntag        } else {
10998add1155SRico Sonntag            asort($rows);
11008add1155SRico Sonntag        }
11018add1155SRico Sonntag
11028add1155SRico Sonntag        $top10 = [];
11038add1155SRico Sonntag        $i     = 0;
1104b092a991SGreg Roach        foreach ($rows as $xref => $age) {
11056b9cb339SGreg Roach            $family = Registry::familyFactory()->make((string) $xref, $this->tree);
11068add1155SRico Sonntag            if ($type === 'name') {
11078add1155SRico Sonntag                return $family->formatList();
11088add1155SRico Sonntag            }
11098add1155SRico Sonntag
11108add1155SRico Sonntag            $age = $this->calculateAge((int) $age);
11118add1155SRico Sonntag
11128add1155SRico Sonntag            if ($type === 'age') {
11138add1155SRico Sonntag                return $age;
11148add1155SRico Sonntag            }
11158add1155SRico Sonntag
111639ca88baSGreg Roach            $husb = $family->husband();
111739ca88baSGreg Roach            $wife = $family->wife();
11188add1155SRico Sonntag
1119ef475b14SGreg Roach            if (
1120ef475b14SGreg Roach                $husb instanceof Individual &&
1121ef475b14SGreg Roach                $wife instanceof Individual &&
1122ef475b14SGreg Roach                ($husb->getAllDeathDates() || !$husb->isDead()) &&
1123ef475b14SGreg Roach                ($wife->getAllDeathDates() || !$wife->isDead())
1124ef475b14SGreg Roach            ) {
112541acc92bSGreg Roach                if ($family->canShow()) {
11268add1155SRico Sonntag                    if ($type === 'list') {
112739ca88baSGreg Roach                        $top10[] = '<li><a href="' . e($family->url()) . '">' . $family->fullName() . '</a> (' . $age . ')' . '</li>';
11288add1155SRico Sonntag                    } else {
112939ca88baSGreg Roach                        $top10[] = '<a href="' . e($family->url()) . '">' . $family->fullName() . '</a> (' . $age . ')';
11308add1155SRico Sonntag                    }
11318add1155SRico Sonntag                }
11328add1155SRico Sonntag                if (++$i === $total) {
11338add1155SRico Sonntag                    break;
11348add1155SRico Sonntag                }
11358add1155SRico Sonntag            }
11368add1155SRico Sonntag        }
11378add1155SRico Sonntag
11388add1155SRico Sonntag        if ($type === 'list') {
11398add1155SRico Sonntag            $top10 = implode('', $top10);
11408add1155SRico Sonntag        } else {
11418add1155SRico Sonntag            $top10 = implode('; ', $top10);
11428add1155SRico Sonntag        }
11438add1155SRico Sonntag
11448add1155SRico Sonntag        if (I18N::direction() === 'rtl') {
11458add1155SRico Sonntag            $top10 = str_replace([
11468add1155SRico Sonntag                '[',
11478add1155SRico Sonntag                ']',
11488add1155SRico Sonntag                '(',
11498add1155SRico Sonntag                ')',
11508add1155SRico Sonntag                '+',
11518add1155SRico Sonntag            ], [
11528add1155SRico Sonntag                '&rlm;[',
11538add1155SRico Sonntag                '&rlm;]',
11548add1155SRico Sonntag                '&rlm;(',
11558add1155SRico Sonntag                '&rlm;)',
11568add1155SRico Sonntag                '&rlm;+',
11578add1155SRico Sonntag            ], $top10);
11588add1155SRico Sonntag        }
11598add1155SRico Sonntag
11608add1155SRico Sonntag        if ($type === 'list') {
11618add1155SRico Sonntag            return '<ul>' . $top10 . '</ul>';
11628add1155SRico Sonntag        }
11638add1155SRico Sonntag
11648add1155SRico Sonntag        return $top10;
11658add1155SRico Sonntag    }
11668add1155SRico Sonntag
11678add1155SRico Sonntag    /**
11688add1155SRico Sonntag     * General query on marriage ages.
11698add1155SRico Sonntag     *
11708add1155SRico Sonntag     * @return string
11718add1155SRico Sonntag     */
11728add1155SRico Sonntag    public function topAgeOfMarriageFamily(): string
11738add1155SRico Sonntag    {
11748add1155SRico Sonntag        return $this->ageOfMarriageQuery('name', 'DESC', 1);
11758add1155SRico Sonntag    }
11768add1155SRico Sonntag
11778add1155SRico Sonntag    /**
11788add1155SRico Sonntag     * General query on marriage ages.
11798add1155SRico Sonntag     *
11808add1155SRico Sonntag     * @return string
11818add1155SRico Sonntag     */
11828add1155SRico Sonntag    public function topAgeOfMarriage(): string
11838add1155SRico Sonntag    {
11848add1155SRico Sonntag        return $this->ageOfMarriageQuery('age', 'DESC', 1);
11858add1155SRico Sonntag    }
11868add1155SRico Sonntag
11878add1155SRico Sonntag    /**
11888add1155SRico Sonntag     * General query on marriage ages.
11898add1155SRico Sonntag     *
11908add1155SRico Sonntag     * @param int $total
11918add1155SRico Sonntag     *
11928add1155SRico Sonntag     * @return string
11938add1155SRico Sonntag     */
11948add1155SRico Sonntag    public function topAgeOfMarriageFamilies(int $total = 10): string
11958add1155SRico Sonntag    {
11968add1155SRico Sonntag        return $this->ageOfMarriageQuery('nolist', 'DESC', $total);
11978add1155SRico Sonntag    }
11988add1155SRico Sonntag
11998add1155SRico Sonntag    /**
12008add1155SRico Sonntag     * General query on marriage ages.
12018add1155SRico Sonntag     *
12028add1155SRico Sonntag     * @param int $total
12038add1155SRico Sonntag     *
12048add1155SRico Sonntag     * @return string
12058add1155SRico Sonntag     */
12068add1155SRico Sonntag    public function topAgeOfMarriageFamiliesList(int $total = 10): string
12078add1155SRico Sonntag    {
12088add1155SRico Sonntag        return $this->ageOfMarriageQuery('list', 'DESC', $total);
12098add1155SRico Sonntag    }
12108add1155SRico Sonntag
12118add1155SRico Sonntag    /**
12128add1155SRico Sonntag     * General query on marriage ages.
12138add1155SRico Sonntag     *
12148add1155SRico Sonntag     * @return string
12158add1155SRico Sonntag     */
12168add1155SRico Sonntag    public function minAgeOfMarriageFamily(): string
12178add1155SRico Sonntag    {
12188add1155SRico Sonntag        return $this->ageOfMarriageQuery('name', 'ASC', 1);
12198add1155SRico Sonntag    }
12208add1155SRico Sonntag
12218add1155SRico Sonntag    /**
12228add1155SRico Sonntag     * General query on marriage ages.
12238add1155SRico Sonntag     *
12248add1155SRico Sonntag     * @return string
12258add1155SRico Sonntag     */
12268add1155SRico Sonntag    public function minAgeOfMarriage(): string
12278add1155SRico Sonntag    {
12288add1155SRico Sonntag        return $this->ageOfMarriageQuery('age', 'ASC', 1);
12298add1155SRico Sonntag    }
12308add1155SRico Sonntag
12318add1155SRico Sonntag    /**
12328add1155SRico Sonntag     * General query on marriage ages.
12338add1155SRico Sonntag     *
12348add1155SRico Sonntag     * @param int $total
12358add1155SRico Sonntag     *
12368add1155SRico Sonntag     * @return string
12378add1155SRico Sonntag     */
12388add1155SRico Sonntag    public function minAgeOfMarriageFamilies(int $total = 10): string
12398add1155SRico Sonntag    {
12408add1155SRico Sonntag        return $this->ageOfMarriageQuery('nolist', 'ASC', $total);
12418add1155SRico Sonntag    }
12428add1155SRico Sonntag
12438add1155SRico Sonntag    /**
12448add1155SRico Sonntag     * General query on marriage ages.
12458add1155SRico Sonntag     *
12468add1155SRico Sonntag     * @param int $total
12478add1155SRico Sonntag     *
12488add1155SRico Sonntag     * @return string
12498add1155SRico Sonntag     */
12508add1155SRico Sonntag    public function minAgeOfMarriageFamiliesList(int $total = 10): string
12518add1155SRico Sonntag    {
12528add1155SRico Sonntag        return $this->ageOfMarriageQuery('list', 'ASC', $total);
12538add1155SRico Sonntag    }
12548add1155SRico Sonntag
12558add1155SRico Sonntag    /**
12568add1155SRico Sonntag     * Find the ages between spouses.
12578add1155SRico Sonntag     *
12588add1155SRico Sonntag     * @param string $age_dir
12598add1155SRico Sonntag     * @param int    $total
12608add1155SRico Sonntag     *
126191c84b80SGreg Roach     * @return array<array<string,mixed>>
12628add1155SRico Sonntag     */
12638add1155SRico Sonntag    private function ageBetweenSpousesQuery(string $age_dir, int $total): array
12648add1155SRico Sonntag    {
1265d1a467e4SGreg Roach        $query = DB::table('families')
1266d1a467e4SGreg Roach            ->where('f_file', '=', $this->tree->id())
12670b5fd0a6SGreg Roach            ->join('dates AS wife', static function (JoinClause $join): void {
1268d1a467e4SGreg Roach                $join
1269d1a467e4SGreg Roach                    ->on('wife.d_gid', '=', 'f_wife')
1270d1a467e4SGreg Roach                    ->on('wife.d_file', '=', 'f_file')
1271d1a467e4SGreg Roach                    ->where('wife.d_fact', '=', 'BIRT')
1272d1a467e4SGreg Roach                    ->where('wife.d_julianday1', '<>', 0);
1273d1a467e4SGreg Roach            })
12740b5fd0a6SGreg Roach            ->join('dates AS husb', static function (JoinClause $join): void {
1275d1a467e4SGreg Roach                $join
1276d1a467e4SGreg Roach                    ->on('husb.d_gid', '=', 'f_husb')
1277d1a467e4SGreg Roach                    ->on('husb.d_file', '=', 'f_file')
1278d1a467e4SGreg Roach                    ->where('husb.d_fact', '=', 'BIRT')
1279d1a467e4SGreg Roach                    ->where('husb.d_julianday1', '<>', 0);
1280d1a467e4SGreg Roach            });
1281d1a467e4SGreg Roach
12828add1155SRico Sonntag        if ($age_dir === 'DESC') {
1283d1a467e4SGreg Roach            $query
1284d1a467e4SGreg Roach                ->whereColumn('wife.d_julianday1', '>=', 'husb.d_julianday1')
1285211018abSGreg Roach                ->orderBy(new Expression('MIN(' . DB::prefix('wife.d_julianday1') . ') - MIN(' . DB::prefix('husb.d_julianday1') . ')'), 'DESC');
12868add1155SRico Sonntag        } else {
1287d1a467e4SGreg Roach            $query
1288d1a467e4SGreg Roach                ->whereColumn('husb.d_julianday1', '>=', 'wife.d_julianday1')
1289211018abSGreg Roach                ->orderBy(new Expression('MIN(' . DB::prefix('husb.d_julianday1') . ') - MIN(' . DB::prefix('wife.d_julianday1') . ')'), 'DESC');
12908add1155SRico Sonntag        }
12918add1155SRico Sonntag
1292c0112ce8SGreg Roach        return $query
1293d1a467e4SGreg Roach            ->groupBy(['f_id', 'f_file'])
129447256fc5SGreg Roach            ->select(['families.*'])
1295d1a467e4SGreg Roach            ->take($total)
1296d1a467e4SGreg Roach            ->get()
12976b9cb339SGreg Roach            ->map(Registry::familyFactory()->mapper($this->tree))
1298c0112ce8SGreg Roach            ->filter(GedcomRecord::accessFilter())
1299c0112ce8SGreg Roach            ->map(function (Family $family) use ($age_dir): array {
1300d1a467e4SGreg Roach                $husb_birt_jd = $family->husband()->getBirthDate()->minimumJulianDay();
1301d1a467e4SGreg Roach                $wife_birt_jd = $family->wife()->getBirthDate()->minimumJulianDay();
13028add1155SRico Sonntag
1303d1a467e4SGreg Roach                if ($age_dir === 'DESC') {
1304d1a467e4SGreg Roach                    $diff = $wife_birt_jd - $husb_birt_jd;
1305d1a467e4SGreg Roach                } else {
1306d1a467e4SGreg Roach                    $diff = $husb_birt_jd - $wife_birt_jd;
13078add1155SRico Sonntag                }
13088add1155SRico Sonntag
1309c0112ce8SGreg Roach                return [
13108add1155SRico Sonntag                    'family' => $family,
131193ccd686SRico Sonntag                    'age'    => $this->calculateAge($diff),
13128add1155SRico Sonntag                ];
1313c0112ce8SGreg Roach            })
1314c0112ce8SGreg Roach            ->all();
13158add1155SRico Sonntag    }
13168add1155SRico Sonntag
13178add1155SRico Sonntag    /**
13188add1155SRico Sonntag     * Find the age between husband and wife.
13198add1155SRico Sonntag     *
13208add1155SRico Sonntag     * @param int $total
13218add1155SRico Sonntag     *
13228add1155SRico Sonntag     * @return string
13238add1155SRico Sonntag     */
13248add1155SRico Sonntag    public function ageBetweenSpousesMF(int $total = 10): string
13258add1155SRico Sonntag    {
13268add1155SRico Sonntag        $records = $this->ageBetweenSpousesQuery('DESC', $total);
13278add1155SRico Sonntag
1328c0112ce8SGreg Roach        return view('statistics/families/top10-nolist-spouses', [
13298add1155SRico Sonntag            'records' => $records,
1330c0112ce8SGreg Roach        ]);
13318add1155SRico Sonntag    }
13328add1155SRico Sonntag
13338add1155SRico Sonntag    /**
13348add1155SRico Sonntag     * Find the age between husband and wife.
13358add1155SRico Sonntag     *
13368add1155SRico Sonntag     * @param int $total
13378add1155SRico Sonntag     *
13388add1155SRico Sonntag     * @return string
13398add1155SRico Sonntag     */
13408add1155SRico Sonntag    public function ageBetweenSpousesMFList(int $total = 10): string
13418add1155SRico Sonntag    {
13428add1155SRico Sonntag        $records = $this->ageBetweenSpousesQuery('DESC', $total);
13438add1155SRico Sonntag
1344c0112ce8SGreg Roach        return view('statistics/families/top10-list-spouses', [
13458add1155SRico Sonntag            'records' => $records,
1346c0112ce8SGreg Roach        ]);
13478add1155SRico Sonntag    }
13488add1155SRico Sonntag
13498add1155SRico Sonntag    /**
13508add1155SRico Sonntag     * Find the age between wife and husband..
13518add1155SRico Sonntag     *
13528add1155SRico Sonntag     * @param int $total
13538add1155SRico Sonntag     *
13548add1155SRico Sonntag     * @return string
13558add1155SRico Sonntag     */
13568add1155SRico Sonntag    public function ageBetweenSpousesFM(int $total = 10): string
13578add1155SRico Sonntag    {
13588add1155SRico Sonntag        $records = $this->ageBetweenSpousesQuery('ASC', $total);
13598add1155SRico Sonntag
1360c0112ce8SGreg Roach        return view('statistics/families/top10-nolist-spouses', [
13618add1155SRico Sonntag            'records' => $records,
1362c0112ce8SGreg Roach        ]);
13638add1155SRico Sonntag    }
13648add1155SRico Sonntag
13658add1155SRico Sonntag    /**
13668add1155SRico Sonntag     * Find the age between wife and husband..
13678add1155SRico Sonntag     *
13688add1155SRico Sonntag     * @param int $total
13698add1155SRico Sonntag     *
13708add1155SRico Sonntag     * @return string
13718add1155SRico Sonntag     */
13728add1155SRico Sonntag    public function ageBetweenSpousesFMList(int $total = 10): string
13738add1155SRico Sonntag    {
13748add1155SRico Sonntag        $records = $this->ageBetweenSpousesQuery('ASC', $total);
13758add1155SRico Sonntag
1376c0112ce8SGreg Roach        return view('statistics/families/top10-list-spouses', [
13778add1155SRico Sonntag            'records' => $records,
1378c0112ce8SGreg Roach        ]);
13798add1155SRico Sonntag    }
13808add1155SRico Sonntag
13818add1155SRico Sonntag    /**
13828add1155SRico Sonntag     * General query on ages at marriage.
13838add1155SRico Sonntag     *
1384afa8d404SGreg Roach     * @param string $sex "M" or "F"
13858add1155SRico Sonntag     * @param int    $year1
13868add1155SRico Sonntag     * @param int    $year2
13878add1155SRico Sonntag     *
1388*d7c2f5ccSGreg Roach     * @return array<object>
13898add1155SRico Sonntag     */
139024f2a3afSGreg Roach    public function statsMarrAgeQuery(string $sex, int $year1 = -1, int $year2 = -1): array
13918add1155SRico Sonntag    {
1392afa8d404SGreg Roach        $query = DB::table('dates AS married')
13930b5fd0a6SGreg Roach            ->join('families', static function (JoinClause $join): void {
1394afa8d404SGreg Roach                $join
1395afa8d404SGreg Roach                    ->on('f_file', '=', 'married.d_file')
1396afa8d404SGreg Roach                    ->on('f_id', '=', 'married.d_gid');
1397afa8d404SGreg Roach            })
13980b5fd0a6SGreg Roach            ->join('dates AS birth', static function (JoinClause $join) use ($sex): void {
1399afa8d404SGreg Roach                $join
1400afa8d404SGreg Roach                    ->on('birth.d_file', '=', 'married.d_file')
1401afa8d404SGreg Roach                    ->on('birth.d_gid', '=', $sex === 'M' ? 'f_husb' : 'f_wife')
1402afa8d404SGreg Roach                    ->where('birth.d_julianday1', '<>', 0)
1403afa8d404SGreg Roach                    ->where('birth.d_fact', '=', 'BIRT')
1404afa8d404SGreg Roach                    ->whereIn('birth.d_type', ['@#DGREGORIAN@', '@#DJULIAN@']);
1405afa8d404SGreg Roach            })
1406afa8d404SGreg Roach            ->where('married.d_file', '=', $this->tree->id())
1407afa8d404SGreg Roach            ->where('married.d_fact', '=', 'MARR')
1408afa8d404SGreg Roach            ->whereIn('married.d_type', ['@#DGREGORIAN@', '@#DJULIAN@'])
1409afa8d404SGreg Roach            ->whereColumn('married.d_julianday1', '>', 'birth.d_julianday1')
1410211018abSGreg Roach            ->select(['f_id', 'birth.d_gid', new Expression(DB::prefix('married.d_julianday2') . ' - ' . DB::prefix('birth.d_julianday1') . ' AS age')]);
1411afa8d404SGreg Roach
14128add1155SRico Sonntag        if ($year1 >= 0 && $year2 >= 0) {
1413afa8d404SGreg Roach            $query->whereBetween('married.d_year', [$year1, $year2]);
14148add1155SRico Sonntag        }
14158add1155SRico Sonntag
1416afa8d404SGreg Roach        return $query
1417afa8d404SGreg Roach            ->get()
1418*d7c2f5ccSGreg Roach            ->map(static function (object $row): object {
14198add1155SRico Sonntag                $row->age = (int) $row->age;
14208add1155SRico Sonntag
1421afa8d404SGreg Roach                return $row;
1422afa8d404SGreg Roach            })
1423afa8d404SGreg Roach            ->all();
14248add1155SRico Sonntag    }
14258add1155SRico Sonntag
14268add1155SRico Sonntag    /**
14278add1155SRico Sonntag     * General query on marriage ages.
14288add1155SRico Sonntag     *
14298add1155SRico Sonntag     * @return string
14308add1155SRico Sonntag     */
143188de55fdSRico Sonntag    public function statsMarrAge(): string
14328add1155SRico Sonntag    {
1433f78da678SGreg Roach        return (new ChartMarriageAge($this->century_service, $this->tree))
143488de55fdSRico Sonntag            ->chartMarriageAge();
14358add1155SRico Sonntag    }
14368add1155SRico Sonntag
14378add1155SRico Sonntag    /**
14388add1155SRico Sonntag     * Query the database for marriage tags.
14398add1155SRico Sonntag     *
14402c928961SGreg Roach     * @param string $type       "full", "name" or "age"
14412c928961SGreg Roach     * @param string $age_dir    "ASC" or "DESC"
14422c928961SGreg Roach     * @param string $sex        "F" or "M"
14438add1155SRico Sonntag     * @param bool   $show_years
14448add1155SRico Sonntag     *
14458add1155SRico Sonntag     * @return string
14468add1155SRico Sonntag     */
14478add1155SRico Sonntag    private function marriageQuery(string $type, string $age_dir, string $sex, bool $show_years): string
14488add1155SRico Sonntag    {
14498add1155SRico Sonntag        if ($sex === 'F') {
14508add1155SRico Sonntag            $sex_field = 'f_wife';
14518add1155SRico Sonntag        } else {
14528add1155SRico Sonntag            $sex_field = 'f_husb';
14538add1155SRico Sonntag        }
14548add1155SRico Sonntag
14558add1155SRico Sonntag        if ($age_dir !== 'ASC') {
14568add1155SRico Sonntag            $age_dir = 'DESC';
14578add1155SRico Sonntag        }
14588add1155SRico Sonntag
14592c928961SGreg Roach        $row = DB::table('families')
14600b5fd0a6SGreg Roach            ->join('dates AS married', static function (JoinClause $join): void {
14612c928961SGreg Roach                $join
14622c928961SGreg Roach                    ->on('married.d_file', '=', 'f_file')
14632c928961SGreg Roach                    ->on('married.d_gid', '=', 'f_id')
14642c928961SGreg Roach                    ->where('married.d_fact', '=', 'MARR');
14652c928961SGreg Roach            })
14660b5fd0a6SGreg Roach            ->join('individuals', static function (JoinClause $join) use ($sex, $sex_field): void {
14672c928961SGreg Roach                $join
14682c928961SGreg Roach                    ->on('i_file', '=', 'f_file')
14692c928961SGreg Roach                    ->on('i_id', '=', $sex_field)
14702c928961SGreg Roach                    ->where('i_sex', '=', $sex);
14712c928961SGreg Roach            })
14720b5fd0a6SGreg Roach            ->join('dates AS birth', static function (JoinClause $join): void {
14732c928961SGreg Roach                $join
14742c928961SGreg Roach                    ->on('birth.d_file', '=', 'i_file')
14752c928961SGreg Roach                    ->on('birth.d_gid', '=', 'i_id')
14762c928961SGreg Roach                    ->where('birth.d_fact', '=', 'BIRT')
14772c928961SGreg Roach                    ->where('birth.d_julianday1', '<>', 0);
14782c928961SGreg Roach            })
14792c928961SGreg Roach            ->where('f_file', '=', $this->tree->id())
1480211018abSGreg Roach            ->where('married.d_julianday2', '>', new Expression(DB::prefix('birth.d_julianday1')))
1481211018abSGreg Roach            ->orderBy(new Expression(DB::prefix('married.d_julianday2') . ' - ' . DB::prefix('birth.d_julianday1')), $age_dir)
1482211018abSGreg Roach            ->select(['f_id AS famid', $sex_field, new Expression(DB::prefix('married.d_julianday2') . ' - ' . DB::prefix('birth.d_julianday1') . ' AS age'), 'i_id'])
14832c928961SGreg Roach            ->take(1)
14842c928961SGreg Roach            ->get()
14852c928961SGreg Roach            ->first();
14862c928961SGreg Roach
14872c928961SGreg Roach        if ($row === null) {
1488bd055353SGreg Roach            return I18N::translate('This information is not available.');
14898add1155SRico Sonntag        }
14908add1155SRico Sonntag
14916b9cb339SGreg Roach        $family = Registry::familyFactory()->make($row->famid, $this->tree);
14926b9cb339SGreg Roach        $person = Registry::individualFactory()->make($row->i_id, $this->tree);
14938add1155SRico Sonntag
14948add1155SRico Sonntag        switch ($type) {
14958add1155SRico Sonntag            default:
14968add1155SRico Sonntag            case 'full':
1497f78da678SGreg Roach                if ($family !== null && $family->canShow()) {
14988add1155SRico Sonntag                    $result = $family->formatList();
14998add1155SRico Sonntag                } else {
15008add1155SRico Sonntag                    $result = I18N::translate('This information is private and cannot be shown.');
15018add1155SRico Sonntag                }
15028add1155SRico Sonntag                break;
15038add1155SRico Sonntag
15048add1155SRico Sonntag            case 'name':
150539ca88baSGreg Roach                $result = '<a href="' . e($family->url()) . '">' . $person->fullName() . '</a>';
15068add1155SRico Sonntag                break;
15078add1155SRico Sonntag
15088add1155SRico Sonntag            case 'age':
15098add1155SRico Sonntag                $age = $row->age;
15108add1155SRico Sonntag
15118add1155SRico Sonntag                if ($show_years) {
15128add1155SRico Sonntag                    $result = $this->calculateAge((int) $row->age);
15138add1155SRico Sonntag                } else {
15148add1155SRico Sonntag                    $result = I18N::number((int) ($age / 365.25));
15158add1155SRico Sonntag                }
15168add1155SRico Sonntag
15178add1155SRico Sonntag                break;
15188add1155SRico Sonntag        }
15198add1155SRico Sonntag
15208add1155SRico Sonntag        return $result;
15218add1155SRico Sonntag    }
15228add1155SRico Sonntag
15238add1155SRico Sonntag    /**
15248add1155SRico Sonntag     * Find the youngest wife.
15258add1155SRico Sonntag     *
15268add1155SRico Sonntag     * @return string
15278add1155SRico Sonntag     */
15288add1155SRico Sonntag    public function youngestMarriageFemale(): string
15298add1155SRico Sonntag    {
15308add1155SRico Sonntag        return $this->marriageQuery('full', 'ASC', 'F', false);
15318add1155SRico Sonntag    }
15328add1155SRico Sonntag
15338add1155SRico Sonntag    /**
15348add1155SRico Sonntag     * Find the name of the youngest wife.
15358add1155SRico Sonntag     *
15368add1155SRico Sonntag     * @return string
15378add1155SRico Sonntag     */
15388add1155SRico Sonntag    public function youngestMarriageFemaleName(): string
15398add1155SRico Sonntag    {
15408add1155SRico Sonntag        return $this->marriageQuery('name', 'ASC', 'F', false);
15418add1155SRico Sonntag    }
15428add1155SRico Sonntag
15438add1155SRico Sonntag    /**
15448add1155SRico Sonntag     * Find the age of the youngest wife.
15458add1155SRico Sonntag     *
15468add1155SRico Sonntag     * @param string $show_years
15478add1155SRico Sonntag     *
15488add1155SRico Sonntag     * @return string
15498add1155SRico Sonntag     */
15508add1155SRico Sonntag    public function youngestMarriageFemaleAge(string $show_years = ''): string
15518add1155SRico Sonntag    {
15528add1155SRico Sonntag        return $this->marriageQuery('age', 'ASC', 'F', (bool) $show_years);
15538add1155SRico Sonntag    }
15548add1155SRico Sonntag
15558add1155SRico Sonntag    /**
15568add1155SRico Sonntag     * Find the oldest wife.
15578add1155SRico Sonntag     *
15588add1155SRico Sonntag     * @return string
15598add1155SRico Sonntag     */
15608add1155SRico Sonntag    public function oldestMarriageFemale(): string
15618add1155SRico Sonntag    {
15628add1155SRico Sonntag        return $this->marriageQuery('full', 'DESC', 'F', false);
15638add1155SRico Sonntag    }
15648add1155SRico Sonntag
15658add1155SRico Sonntag    /**
15668add1155SRico Sonntag     * Find the name of the oldest wife.
15678add1155SRico Sonntag     *
15688add1155SRico Sonntag     * @return string
15698add1155SRico Sonntag     */
15708add1155SRico Sonntag    public function oldestMarriageFemaleName(): string
15718add1155SRico Sonntag    {
15728add1155SRico Sonntag        return $this->marriageQuery('name', 'DESC', 'F', false);
15738add1155SRico Sonntag    }
15748add1155SRico Sonntag
15758add1155SRico Sonntag    /**
15768add1155SRico Sonntag     * Find the age of the oldest wife.
15778add1155SRico Sonntag     *
15788add1155SRico Sonntag     * @param string $show_years
15798add1155SRico Sonntag     *
15808add1155SRico Sonntag     * @return string
15818add1155SRico Sonntag     */
15828add1155SRico Sonntag    public function oldestMarriageFemaleAge(string $show_years = ''): string
15838add1155SRico Sonntag    {
15848add1155SRico Sonntag        return $this->marriageQuery('age', 'DESC', 'F', (bool) $show_years);
15858add1155SRico Sonntag    }
15868add1155SRico Sonntag
15878add1155SRico Sonntag    /**
15888add1155SRico Sonntag     * Find the youngest husband.
15898add1155SRico Sonntag     *
15908add1155SRico Sonntag     * @return string
15918add1155SRico Sonntag     */
15928add1155SRico Sonntag    public function youngestMarriageMale(): string
15938add1155SRico Sonntag    {
15948add1155SRico Sonntag        return $this->marriageQuery('full', 'ASC', 'M', false);
15958add1155SRico Sonntag    }
15968add1155SRico Sonntag
15978add1155SRico Sonntag    /**
15988add1155SRico Sonntag     * Find the name of the youngest husband.
15998add1155SRico Sonntag     *
16008add1155SRico Sonntag     * @return string
16018add1155SRico Sonntag     */
16028add1155SRico Sonntag    public function youngestMarriageMaleName(): string
16038add1155SRico Sonntag    {
16048add1155SRico Sonntag        return $this->marriageQuery('name', 'ASC', 'M', false);
16058add1155SRico Sonntag    }
16068add1155SRico Sonntag
16078add1155SRico Sonntag    /**
16088add1155SRico Sonntag     * Find the age of the youngest husband.
16098add1155SRico Sonntag     *
16108add1155SRico Sonntag     * @param string $show_years
16118add1155SRico Sonntag     *
16128add1155SRico Sonntag     * @return string
16138add1155SRico Sonntag     */
16148add1155SRico Sonntag    public function youngestMarriageMaleAge(string $show_years = ''): string
16158add1155SRico Sonntag    {
16168add1155SRico Sonntag        return $this->marriageQuery('age', 'ASC', 'M', (bool) $show_years);
16178add1155SRico Sonntag    }
16188add1155SRico Sonntag
16198add1155SRico Sonntag    /**
16208add1155SRico Sonntag     * Find the oldest husband.
16218add1155SRico Sonntag     *
16228add1155SRico Sonntag     * @return string
16238add1155SRico Sonntag     */
16248add1155SRico Sonntag    public function oldestMarriageMale(): string
16258add1155SRico Sonntag    {
16268add1155SRico Sonntag        return $this->marriageQuery('full', 'DESC', 'M', false);
16278add1155SRico Sonntag    }
16288add1155SRico Sonntag
16298add1155SRico Sonntag    /**
16308add1155SRico Sonntag     * Find the name of the oldest husband.
16318add1155SRico Sonntag     *
16328add1155SRico Sonntag     * @return string
16338add1155SRico Sonntag     */
16348add1155SRico Sonntag    public function oldestMarriageMaleName(): string
16358add1155SRico Sonntag    {
16368add1155SRico Sonntag        return $this->marriageQuery('name', 'DESC', 'M', false);
16378add1155SRico Sonntag    }
16388add1155SRico Sonntag
16398add1155SRico Sonntag    /**
16408add1155SRico Sonntag     * Find the age of the oldest husband.
16418add1155SRico Sonntag     *
16428add1155SRico Sonntag     * @param string $show_years
16438add1155SRico Sonntag     *
16448add1155SRico Sonntag     * @return string
16458add1155SRico Sonntag     */
16468add1155SRico Sonntag    public function oldestMarriageMaleAge(string $show_years = ''): string
16478add1155SRico Sonntag    {
16488add1155SRico Sonntag        return $this->marriageQuery('age', 'DESC', 'M', (bool) $show_years);
16498add1155SRico Sonntag    }
16508add1155SRico Sonntag
16518add1155SRico Sonntag    /**
16528add1155SRico Sonntag     * General query on marriages.
16538add1155SRico Sonntag     *
16548add1155SRico Sonntag     * @param int  $year1
16558add1155SRico Sonntag     * @param int  $year2
16568add1155SRico Sonntag     *
1657e6f3d5e2SGreg Roach     * @return Builder
16588add1155SRico Sonntag     */
1659e6f3d5e2SGreg Roach    public function statsMarriageQuery(int $year1 = -1, int $year2 = -1): Builder
16608add1155SRico Sonntag    {
1661e6f3d5e2SGreg Roach        $query = DB::table('dates')
1662e6f3d5e2SGreg Roach            ->where('d_file', '=', $this->tree->id())
1663e6f3d5e2SGreg Roach            ->where('d_fact', '=', 'MARR')
1664a69f5655SGreg Roach            ->select(['d_month', new Expression('COUNT(*) AS total')])
16657f5c2944SGreg Roach            ->groupBy(['d_month']);
1666e6f3d5e2SGreg Roach
1667e6f3d5e2SGreg Roach        if ($year1 >= 0 && $year2 >= 0) {
1668e6f3d5e2SGreg Roach            $query->whereBetween('d_year', [$year1, $year2]);
1669e6f3d5e2SGreg Roach        }
1670e6f3d5e2SGreg Roach
1671e6f3d5e2SGreg Roach        return $query;
1672e6f3d5e2SGreg Roach    }
1673e6f3d5e2SGreg Roach
1674e6f3d5e2SGreg Roach    /**
1675e6f3d5e2SGreg Roach     * General query on marriages.
1676e6f3d5e2SGreg Roach     *
1677e6f3d5e2SGreg Roach     * @param int  $year1
1678e6f3d5e2SGreg Roach     * @param int  $year2
1679e6f3d5e2SGreg Roach     *
1680e6f3d5e2SGreg Roach     * @return Builder
1681e6f3d5e2SGreg Roach     */
1682e6f3d5e2SGreg Roach    public function statsFirstMarriageQuery(int $year1 = -1, int $year2 = -1): Builder
1683e6f3d5e2SGreg Roach    {
16842d7289dcSGreg Roach        $query = DB::table('families')
16850b5fd0a6SGreg Roach            ->join('dates', static function (JoinClause $join): void {
16862d7289dcSGreg Roach                $join
16872d7289dcSGreg Roach                    ->on('d_gid', '=', 'f_id')
16882d7289dcSGreg Roach                    ->on('d_file', '=', 'f_file')
16892d7289dcSGreg Roach                    ->where('d_fact', '=', 'MARR')
169018e9654eSGreg Roach                    ->whereIn('d_month', ['JAN', 'FEB', 'MAR', 'APR', 'MAY', 'JUN', 'JUL', 'AUG', 'SEP', 'OCT', 'NOV', 'DEC'])
16912d7289dcSGreg Roach                    ->where('d_julianday2', '<>', 0);
16922d7289dcSGreg Roach            })
1693dca2dd78SGreg Roach            ->where('f_file', '=', $this->tree->id());
16948add1155SRico Sonntag
16958add1155SRico Sonntag        if ($year1 >= 0 && $year2 >= 0) {
16962d7289dcSGreg Roach            $query->whereBetween('d_year', [$year1, $year2]);
16978add1155SRico Sonntag        }
16988add1155SRico Sonntag
16992d7289dcSGreg Roach        return $query
1700dca2dd78SGreg Roach            ->select(['f_husb', 'f_wife', 'd_month AS month'])
17018305ac84SGreg Roach            ->orderBy('d_julianday2');
17028add1155SRico Sonntag    }
17038add1155SRico Sonntag
17048add1155SRico Sonntag    /**
17058add1155SRico Sonntag     * General query on marriages.
17068add1155SRico Sonntag     *
17078add1155SRico Sonntag     * @param string|null $color_from
17088add1155SRico Sonntag     * @param string|null $color_to
17098add1155SRico Sonntag     *
17108add1155SRico Sonntag     * @return string
17118add1155SRico Sonntag     */
17122c6f1bd5SGreg Roach    public function statsMarr(string|null $color_from = null, string|null $color_to = null): string
17138add1155SRico Sonntag    {
1714f78da678SGreg Roach        return (new ChartMarriage($this->century_service, $this->color_service, $this->tree))
171588de55fdSRico Sonntag            ->chartMarriage($color_from, $color_to);
17168add1155SRico Sonntag    }
17178add1155SRico Sonntag
17188add1155SRico Sonntag    /**
17198add1155SRico Sonntag     * General divorce query.
17208add1155SRico Sonntag     *
17218add1155SRico Sonntag     * @param string|null $color_from
17228add1155SRico Sonntag     * @param string|null $color_to
17238add1155SRico Sonntag     *
17248add1155SRico Sonntag     * @return string
17258add1155SRico Sonntag     */
17262c6f1bd5SGreg Roach    public function statsDiv(string|null $color_from = null, string|null $color_to = null): string
17278add1155SRico Sonntag    {
1728f78da678SGreg Roach        return (new ChartDivorce($this->century_service, $this->color_service, $this->tree))
172988de55fdSRico Sonntag            ->chartDivorce($color_from, $color_to);
17308add1155SRico Sonntag    }
17318add1155SRico Sonntag}
1732