xref: /webtrees/app/Module/FamilyTreeStatisticsModule.php (revision 6f4ec3cadc983f0a7294108c634bef48846b4311)
18c2e8227SGreg Roach<?php
23976b470SGreg Roach
38c2e8227SGreg Roach/**
48c2e8227SGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
68c2e8227SGreg Roach * This program is free software: you can redistribute it and/or modify
78c2e8227SGreg Roach * it under the terms of the GNU General Public License as published by
88c2e8227SGreg Roach * the Free Software Foundation, either version 3 of the License, or
98c2e8227SGreg Roach * (at your option) any later version.
108c2e8227SGreg Roach * This program is distributed in the hope that it will be useful,
118c2e8227SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
128c2e8227SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
138c2e8227SGreg Roach * GNU General Public License for more details.
148c2e8227SGreg Roach * 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/>.
168c2e8227SGreg Roach */
17fcfa147eSGreg Roach
18e7f56f2aSGreg Roachdeclare(strict_types=1);
19e7f56f2aSGreg Roach
2076692c8bSGreg Roachnamespace Fisharebest\Webtrees\Module;
2176692c8bSGreg Roach
220e62c4b8SGreg Roachuse Fisharebest\Webtrees\Auth;
23*6f4ec3caSGreg Roachuse Fisharebest\Webtrees\DB;
240e62c4b8SGreg Roachuse Fisharebest\Webtrees\I18N;
258fb4e87cSGreg Roachuse Fisharebest\Webtrees\Individual;
26d35568b4SGreg Roachuse Fisharebest\Webtrees\Registry;
2767992b6aSRichard Cisseeuse Fisharebest\Webtrees\Services\ModuleService;
289219296aSGreg Roachuse Fisharebest\Webtrees\Statistics;
29e490cd80SGreg Roachuse Fisharebest\Webtrees\Tree;
30748dbe15SGreg Roachuse Fisharebest\Webtrees\Validator;
31a69f5655SGreg Roachuse Illuminate\Database\Query\Expression;
321e7a7a28SGreg Roachuse Illuminate\Support\Str;
336ccdf4f0SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
348c2e8227SGreg Roach
35b86c018aSGreg Roachuse function array_slice;
36cd1ec0d0SGreg Roachuse function extract;
37cd1ec0d0SGreg Roachuse function view;
38cd1ec0d0SGreg Roach
39cd1ec0d0SGreg Roachuse const EXTR_OVERWRITE;
40cd1ec0d0SGreg Roach
418c2e8227SGreg Roach/**
428c2e8227SGreg Roach * Class FamilyTreeStatisticsModule
438c2e8227SGreg Roach */
4437eb8894SGreg Roachclass FamilyTreeStatisticsModule extends AbstractModule implements ModuleBlockInterface
45c1010edaSGreg Roach{
4649a243cbSGreg Roach    use ModuleBlockTrait;
4749a243cbSGreg Roach
48f36626dbSGreg Roach    /** Show this number of surnames by default */
4916d6367aSGreg Roach    private const DEFAULT_NUMBER_OF_SURNAMES = '10';
50f36626dbSGreg Roach
51b55cbc6bSGreg Roach    private ModuleService $module_service;
52b55cbc6bSGreg Roach
53b55cbc6bSGreg Roach    /**
54b55cbc6bSGreg Roach     * @param ModuleService $module_service
55b55cbc6bSGreg Roach     */
56b55cbc6bSGreg Roach    public function __construct(ModuleService $module_service)
57b55cbc6bSGreg Roach    {
58b55cbc6bSGreg Roach        $this->module_service = $module_service;
59b55cbc6bSGreg Roach    }
60b55cbc6bSGreg Roach
61961ec755SGreg Roach    /**
620cfd6963SGreg Roach     * How should this module be identified in the control panel, etc.?
63961ec755SGreg Roach     *
64961ec755SGreg Roach     * @return string
65961ec755SGreg Roach     */
6649a243cbSGreg Roach    public function title(): string
67c1010edaSGreg Roach    {
68bbb76c12SGreg Roach        /* I18N: Name of a module */
69bbb76c12SGreg Roach        return I18N::translate('Statistics');
708c2e8227SGreg Roach    }
718c2e8227SGreg Roach
72961ec755SGreg Roach    /**
73961ec755SGreg Roach     * A sentence describing what this module does.
74961ec755SGreg Roach     *
75961ec755SGreg Roach     * @return string
76961ec755SGreg Roach     */
7749a243cbSGreg Roach    public function description(): string
78c1010edaSGreg Roach    {
79bbb76c12SGreg Roach        /* I18N: Description of “Statistics” module */
80bbb76c12SGreg Roach        return I18N::translate('The size of the family tree, earliest and latest events, common names, etc.');
818c2e8227SGreg Roach    }
828c2e8227SGreg Roach
8376692c8bSGreg Roach    /**
8476692c8bSGreg Roach     * Generate the HTML content of this block.
8576692c8bSGreg Roach     *
86e490cd80SGreg Roach     * @param Tree                 $tree
8776692c8bSGreg Roach     * @param int                  $block_id
883caaa4d2SGreg Roach     * @param string               $context
8976d39c55SGreg Roach     * @param array<string,string> $config
9076692c8bSGreg Roach     *
9176692c8bSGreg Roach     * @return string
9276692c8bSGreg Roach     */
933caaa4d2SGreg Roach    public function getBlock(Tree $tree, int $block_id, string $context, array $config = []): string
94c1010edaSGreg Roach    {
95d35568b4SGreg Roach        $statistics = Registry::container()->get(Statistics::class);
96bf57b580SGreg Roach
97e2a378d3SGreg Roach        $show_last_update     = $this->getBlockSetting($block_id, 'show_last_update', '1');
98e2a378d3SGreg Roach        $show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1');
9980536c2dSGreg Roach        $number_of_surnames   = (int) $this->getBlockSetting($block_id, 'number_of_surnames', self::DEFAULT_NUMBER_OF_SURNAMES);
100e2a378d3SGreg Roach        $stat_indi            = $this->getBlockSetting($block_id, 'stat_indi', '1');
101e2a378d3SGreg Roach        $stat_fam             = $this->getBlockSetting($block_id, 'stat_fam', '1');
102e2a378d3SGreg Roach        $stat_sour            = $this->getBlockSetting($block_id, 'stat_sour', '1');
103e2a378d3SGreg Roach        $stat_media           = $this->getBlockSetting($block_id, 'stat_media', '1');
104e2a378d3SGreg Roach        $stat_repo            = $this->getBlockSetting($block_id, 'stat_repo', '1');
105e2a378d3SGreg Roach        $stat_surname         = $this->getBlockSetting($block_id, 'stat_surname', '1');
106e2a378d3SGreg Roach        $stat_events          = $this->getBlockSetting($block_id, 'stat_events', '1');
107e2a378d3SGreg Roach        $stat_users           = $this->getBlockSetting($block_id, 'stat_users', '1');
108e2a378d3SGreg Roach        $stat_first_birth     = $this->getBlockSetting($block_id, 'stat_first_birth', '1');
109e2a378d3SGreg Roach        $stat_last_birth      = $this->getBlockSetting($block_id, 'stat_last_birth', '1');
110e2a378d3SGreg Roach        $stat_first_death     = $this->getBlockSetting($block_id, 'stat_first_death', '1');
111e2a378d3SGreg Roach        $stat_last_death      = $this->getBlockSetting($block_id, 'stat_last_death', '1');
112e2a378d3SGreg Roach        $stat_long_life       = $this->getBlockSetting($block_id, 'stat_long_life', '1');
113e2a378d3SGreg Roach        $stat_avg_life        = $this->getBlockSetting($block_id, 'stat_avg_life', '1');
114e2a378d3SGreg Roach        $stat_most_chil       = $this->getBlockSetting($block_id, 'stat_most_chil', '1');
115e2a378d3SGreg Roach        $stat_avg_chil        = $this->getBlockSetting($block_id, 'stat_avg_chil', '1');
1168c2e8227SGreg Roach
1173caaa4d2SGreg Roach        extract($config, EXTR_OVERWRITE);
1188c2e8227SGreg Roach
1198c2e8227SGreg Roach        if ($show_common_surnames) {
120b86c018aSGreg Roach            $query = DB::table('name')
1211ae92905SGreg Roach                ->where('n_file', '=', $tree->id())
1221ae92905SGreg Roach                ->where('n_type', '<>', '_MARNM')
123b86c018aSGreg Roach                ->where('n_surn', '<>', '')
124b86c018aSGreg Roach                ->where('n_surn', '<>', Individual::NOMEN_NESCIO)
125b86c018aSGreg Roach                ->select([
126b86c018aSGreg Roach                    $this->binaryColumn('n_surn', 'n_surn'),
127b86c018aSGreg Roach                    $this->binaryColumn('n_surname', 'n_surname'),
128b86c018aSGreg Roach                    new Expression('COUNT(*) AS total'),
129b86c018aSGreg Roach                ])
130b86c018aSGreg Roach                ->groupBy([
131b86c018aSGreg Roach                    $this->binaryColumn('n_surn'),
132b86c018aSGreg Roach                    $this->binaryColumn('n_surname'),
133b86c018aSGreg Roach                ]);
134e0275e5bSGreg Roach
135b86c018aSGreg Roach            /** @var array<array<int>> $top_surnames */
136b86c018aSGreg Roach            $top_surnames = [];
1371ae92905SGreg Roach
138b86c018aSGreg Roach            foreach ($query->get() as $row) {
139b86c018aSGreg Roach                $row->n_surn = $row->n_surn === '' ? $row->n_surname : $row->n_surn;
140b86c018aSGreg Roach                $row->n_surn = I18N::strtoupper(I18N::language()->normalize($row->n_surn));
14180536c2dSGreg Roach
142b86c018aSGreg Roach                $top_surnames[$row->n_surn][$row->n_surname] ??= 0;
143b86c018aSGreg Roach                $top_surnames[$row->n_surn][$row->n_surname] += (int) $row->total;
144f36626dbSGreg Roach            }
14580536c2dSGreg Roach
146b86c018aSGreg Roach            uasort($top_surnames, static fn (array $x, array $y): int => array_sum($y) <=> array_sum($x));
147b86c018aSGreg Roach
148b86c018aSGreg Roach            $top_surnames = array_slice($top_surnames, 0, $number_of_surnames, true);
1491d3c0c1aSGreg Roach
150b55cbc6bSGreg Roach            // Find a module providing individual lists
151b55cbc6bSGreg Roach            $module = $this->module_service
152b55cbc6bSGreg Roach                ->findByComponent(ModuleListInterface::class, $tree, Auth::user())
153b55cbc6bSGreg Roach                ->first(static fn (ModuleInterface $module): bool => $module instanceof IndividualListModule);
15467992b6aSRichard Cissee
155cd1ec0d0SGreg Roach            $surnames = view('lists/surnames-compact-list', [
156cd1ec0d0SGreg Roach                'module'   => $module,
157cd1ec0d0SGreg Roach                'totals'   => false,
158b86c018aSGreg Roach                'surnames' => $top_surnames,
159cd1ec0d0SGreg Roach                'tree'     => $tree,
160cd1ec0d0SGreg Roach            ]);
1611d3c0c1aSGreg Roach        } else {
1621d3c0c1aSGreg Roach            $surnames = '';
1638c2e8227SGreg Roach        }
1641d3c0c1aSGreg Roach
165147e99aaSGreg Roach        $content = view('modules/gedcom_stats/statistics', [
1661d3c0c1aSGreg Roach            'show_last_update'     => $show_last_update,
1671d3c0c1aSGreg Roach            'show_common_surnames' => $show_common_surnames,
1681d3c0c1aSGreg Roach            'number_of_surnames'   => $number_of_surnames,
1691d3c0c1aSGreg Roach            'stat_indi'            => $stat_indi,
1701d3c0c1aSGreg Roach            'stat_fam'             => $stat_fam,
1711d3c0c1aSGreg Roach            'stat_sour'            => $stat_sour,
1721d3c0c1aSGreg Roach            'stat_media'           => $stat_media,
1731d3c0c1aSGreg Roach            'stat_repo'            => $stat_repo,
1741d3c0c1aSGreg Roach            'stat_surname'         => $stat_surname,
1751d3c0c1aSGreg Roach            'stat_events'          => $stat_events,
1761d3c0c1aSGreg Roach            'stat_users'           => $stat_users,
1771d3c0c1aSGreg Roach            'stat_first_birth'     => $stat_first_birth,
1781d3c0c1aSGreg Roach            'stat_last_birth'      => $stat_last_birth,
1791d3c0c1aSGreg Roach            'stat_first_death'     => $stat_first_death,
1801d3c0c1aSGreg Roach            'stat_last_death'      => $stat_last_death,
1811d3c0c1aSGreg Roach            'stat_long_life'       => $stat_long_life,
1821d3c0c1aSGreg Roach            'stat_avg_life'        => $stat_avg_life,
1831d3c0c1aSGreg Roach            'stat_most_chil'       => $stat_most_chil,
1841d3c0c1aSGreg Roach            'stat_avg_chil'        => $stat_avg_chil,
1851d3c0c1aSGreg Roach            'surnames'             => $surnames,
1861d3c0c1aSGreg Roach        ]);
18728941e3cSGreg Roach
188bd055353SGreg Roach        $content = $statistics->embedTags($content);
189bd055353SGreg Roach
1903caaa4d2SGreg Roach        if ($context !== self::CONTEXT_EMBED) {
191147e99aaSGreg Roach            return view('modules/block-template', [
1921e7a7a28SGreg Roach                'block'      => Str::kebab($this->name()),
1939c6524dcSGreg Roach                'id'         => $block_id,
1943caaa4d2SGreg Roach                'config_url' => $this->configUrl($tree, $context, $block_id),
19549a243cbSGreg Roach                'title'      => $this->title(),
1969c6524dcSGreg Roach                'content'    => $content,
1979c6524dcSGreg Roach            ]);
1988c2e8227SGreg Roach        }
199b2ce94c6SRico Sonntag
200b2ce94c6SRico Sonntag        return $content;
2018c2e8227SGreg Roach    }
2028c2e8227SGreg Roach
2033caaa4d2SGreg Roach    /**
2043caaa4d2SGreg Roach     * Should this block load asynchronously using AJAX?
2053caaa4d2SGreg Roach     *
2063caaa4d2SGreg Roach     * Simple blocks are faster in-line, more complex ones can be loaded later.
2073caaa4d2SGreg Roach     *
2083caaa4d2SGreg Roach     * @return bool
2093caaa4d2SGreg Roach     */
210c1010edaSGreg Roach    public function loadAjax(): bool
211c1010edaSGreg Roach    {
2128c2e8227SGreg Roach        return true;
2138c2e8227SGreg Roach    }
2148c2e8227SGreg Roach
2153caaa4d2SGreg Roach    /**
2163caaa4d2SGreg Roach     * Can this block be shown on the user’s home page?
2173caaa4d2SGreg Roach     *
2183caaa4d2SGreg Roach     * @return bool
2193caaa4d2SGreg Roach     */
220c1010edaSGreg Roach    public function isUserBlock(): bool
221c1010edaSGreg Roach    {
2228c2e8227SGreg Roach        return true;
2238c2e8227SGreg Roach    }
2248c2e8227SGreg Roach
2253caaa4d2SGreg Roach    /**
2263caaa4d2SGreg Roach     * Can this block be shown on the tree’s home page?
2273caaa4d2SGreg Roach     *
2283caaa4d2SGreg Roach     * @return bool
2293caaa4d2SGreg Roach     */
23063276d8fSGreg Roach    public function isTreeBlock(): bool
231c1010edaSGreg Roach    {
2328c2e8227SGreg Roach        return true;
2338c2e8227SGreg Roach    }
2348c2e8227SGreg Roach
23576692c8bSGreg Roach    /**
236a45f9889SGreg Roach     * Update the configuration for a block.
237a45f9889SGreg Roach     *
2386ccdf4f0SGreg Roach     * @param ServerRequestInterface $request
239a45f9889SGreg Roach     * @param int     $block_id
240a45f9889SGreg Roach     *
241a45f9889SGreg Roach     * @return void
242a45f9889SGreg Roach     */
2436ccdf4f0SGreg Roach    public function saveBlockConfiguration(ServerRequestInterface $request, int $block_id): void
244a45f9889SGreg Roach    {
245748dbe15SGreg Roach        $show_last_update     = Validator::parsedBody($request)->boolean('show_last_update', false);
246748dbe15SGreg Roach        $show_common_surnames = Validator::parsedBody($request)->boolean('show_common_surnames', false);
247748dbe15SGreg Roach        $number_of_surnames   = Validator::parsedBody($request)->integer('number_of_surnames');
248748dbe15SGreg Roach        $stat_indi            = Validator::parsedBody($request)->boolean('stat_indi', false);
249748dbe15SGreg Roach        $stat_fam             = Validator::parsedBody($request)->boolean('stat_fam', false);
250748dbe15SGreg Roach        $stat_sour            = Validator::parsedBody($request)->boolean('stat_sour', false);
251748dbe15SGreg Roach        $stat_other           = Validator::parsedBody($request)->boolean('stat_other', false);
252748dbe15SGreg Roach        $stat_media           = Validator::parsedBody($request)->boolean('stat_media', false);
253748dbe15SGreg Roach        $stat_repo            = Validator::parsedBody($request)->boolean('stat_repo', false);
254748dbe15SGreg Roach        $stat_surname         = Validator::parsedBody($request)->boolean('stat_surname', false);
255748dbe15SGreg Roach        $stat_events          = Validator::parsedBody($request)->boolean('stat_events', false);
256748dbe15SGreg Roach        $stat_users           = Validator::parsedBody($request)->boolean('stat_users', false);
257748dbe15SGreg Roach        $stat_first_birth     = Validator::parsedBody($request)->boolean('stat_first_birth', false);
258748dbe15SGreg Roach        $stat_last_birth      = Validator::parsedBody($request)->boolean('stat_last_birth', false);
259748dbe15SGreg Roach        $stat_first_death     = Validator::parsedBody($request)->boolean('stat_first_death', false);
260748dbe15SGreg Roach        $stat_last_death      = Validator::parsedBody($request)->boolean('stat_last_death', false);
261748dbe15SGreg Roach        $stat_long_life       = Validator::parsedBody($request)->boolean('stat_long_life', false);
262748dbe15SGreg Roach        $stat_avg_life        = Validator::parsedBody($request)->boolean('stat_avg_life', false);
263748dbe15SGreg Roach        $stat_most_chil       = Validator::parsedBody($request)->boolean('stat_most_chil', false);
264748dbe15SGreg Roach        $stat_avg_chil        = Validator::parsedBody($request)->boolean('stat_avg_chil', false);
265ac5f8ed1SGreg Roach
266748dbe15SGreg Roach        $this->setBlockSetting($block_id, 'show_last_update', (string) $show_last_update);
267748dbe15SGreg Roach        $this->setBlockSetting($block_id, 'show_common_surnames', (string) $show_common_surnames);
268748dbe15SGreg Roach        $this->setBlockSetting($block_id, 'number_of_surnames', (string) $number_of_surnames);
269748dbe15SGreg Roach        $this->setBlockSetting($block_id, 'stat_indi', (string) $stat_indi);
270748dbe15SGreg Roach        $this->setBlockSetting($block_id, 'stat_fam', (string) $stat_fam);
271748dbe15SGreg Roach        $this->setBlockSetting($block_id, 'stat_sour', (string) $stat_sour);
272748dbe15SGreg Roach        $this->setBlockSetting($block_id, 'stat_other', (string) $stat_other);
273748dbe15SGreg Roach        $this->setBlockSetting($block_id, 'stat_media', (string) $stat_media);
274748dbe15SGreg Roach        $this->setBlockSetting($block_id, 'stat_repo', (string) $stat_repo);
275748dbe15SGreg Roach        $this->setBlockSetting($block_id, 'stat_surname', (string) $stat_surname);
276748dbe15SGreg Roach        $this->setBlockSetting($block_id, 'stat_events', (string) $stat_events);
277748dbe15SGreg Roach        $this->setBlockSetting($block_id, 'stat_users', (string) $stat_users);
278748dbe15SGreg Roach        $this->setBlockSetting($block_id, 'stat_first_birth', (string) $stat_first_birth);
279748dbe15SGreg Roach        $this->setBlockSetting($block_id, 'stat_last_birth', (string) $stat_last_birth);
280748dbe15SGreg Roach        $this->setBlockSetting($block_id, 'stat_first_death', (string) $stat_first_death);
281748dbe15SGreg Roach        $this->setBlockSetting($block_id, 'stat_last_death', (string) $stat_last_death);
282748dbe15SGreg Roach        $this->setBlockSetting($block_id, 'stat_long_life', (string) $stat_long_life);
283748dbe15SGreg Roach        $this->setBlockSetting($block_id, 'stat_avg_life', (string) $stat_avg_life);
284748dbe15SGreg Roach        $this->setBlockSetting($block_id, 'stat_most_chil', (string) $stat_most_chil);
285748dbe15SGreg Roach        $this->setBlockSetting($block_id, 'stat_avg_chil', (string) $stat_avg_chil);
286a45f9889SGreg Roach    }
287a45f9889SGreg Roach
288a45f9889SGreg Roach    /**
28976692c8bSGreg Roach     * An HTML form to edit block settings
29076692c8bSGreg Roach     *
291e490cd80SGreg Roach     * @param Tree $tree
29276692c8bSGreg Roach     * @param int  $block_id
293a9430be8SGreg Roach     *
2943caaa4d2SGreg Roach     * @return string
29576692c8bSGreg Roach     */
2963caaa4d2SGreg Roach    public function editBlockConfiguration(Tree $tree, int $block_id): string
297c1010edaSGreg Roach    {
298e2a378d3SGreg Roach        $show_last_update     = $this->getBlockSetting($block_id, 'show_last_update', '1');
299e2a378d3SGreg Roach        $show_common_surnames = $this->getBlockSetting($block_id, 'show_common_surnames', '1');
300f36626dbSGreg Roach        $number_of_surnames   = $this->getBlockSetting($block_id, 'number_of_surnames', self::DEFAULT_NUMBER_OF_SURNAMES);
301e2a378d3SGreg Roach        $stat_indi            = $this->getBlockSetting($block_id, 'stat_indi', '1');
302e2a378d3SGreg Roach        $stat_fam             = $this->getBlockSetting($block_id, 'stat_fam', '1');
303e2a378d3SGreg Roach        $stat_sour            = $this->getBlockSetting($block_id, 'stat_sour', '1');
304e2a378d3SGreg Roach        $stat_media           = $this->getBlockSetting($block_id, 'stat_media', '1');
305e2a378d3SGreg Roach        $stat_repo            = $this->getBlockSetting($block_id, 'stat_repo', '1');
306e2a378d3SGreg Roach        $stat_surname         = $this->getBlockSetting($block_id, 'stat_surname', '1');
307e2a378d3SGreg Roach        $stat_events          = $this->getBlockSetting($block_id, 'stat_events', '1');
308e2a378d3SGreg Roach        $stat_users           = $this->getBlockSetting($block_id, 'stat_users', '1');
309e2a378d3SGreg Roach        $stat_first_birth     = $this->getBlockSetting($block_id, 'stat_first_birth', '1');
310e2a378d3SGreg Roach        $stat_last_birth      = $this->getBlockSetting($block_id, 'stat_last_birth', '1');
311e2a378d3SGreg Roach        $stat_first_death     = $this->getBlockSetting($block_id, 'stat_first_death', '1');
312e2a378d3SGreg Roach        $stat_last_death      = $this->getBlockSetting($block_id, 'stat_last_death', '1');
313e2a378d3SGreg Roach        $stat_long_life       = $this->getBlockSetting($block_id, 'stat_long_life', '1');
314e2a378d3SGreg Roach        $stat_avg_life        = $this->getBlockSetting($block_id, 'stat_avg_life', '1');
315e2a378d3SGreg Roach        $stat_most_chil       = $this->getBlockSetting($block_id, 'stat_most_chil', '1');
316e2a378d3SGreg Roach        $stat_avg_chil        = $this->getBlockSetting($block_id, 'stat_avg_chil', '1');
3178c2e8227SGreg Roach
3183caaa4d2SGreg Roach        return view('modules/gedcom_stats/config', [
319c385536dSGreg Roach            'show_last_update'     => $show_last_update,
320c385536dSGreg Roach            'show_common_surnames' => $show_common_surnames,
321c385536dSGreg Roach            'number_of_surnames'   => $number_of_surnames,
322c385536dSGreg Roach            'stat_indi'            => $stat_indi,
323c385536dSGreg Roach            'stat_fam'             => $stat_fam,
324c385536dSGreg Roach            'stat_sour'            => $stat_sour,
325c385536dSGreg Roach            'stat_media'           => $stat_media,
326c385536dSGreg Roach            'stat_repo'            => $stat_repo,
327c385536dSGreg Roach            'stat_surname'         => $stat_surname,
328c385536dSGreg Roach            'stat_events'          => $stat_events,
329c385536dSGreg Roach            'stat_users'           => $stat_users,
330c385536dSGreg Roach            'stat_first_birth'     => $stat_first_birth,
331c385536dSGreg Roach            'stat_last_birth'      => $stat_last_birth,
332c385536dSGreg Roach            'stat_first_death'     => $stat_first_death,
333c385536dSGreg Roach            'stat_last_death'      => $stat_last_death,
334c385536dSGreg Roach            'stat_long_life'       => $stat_long_life,
335c385536dSGreg Roach            'stat_avg_life'        => $stat_avg_life,
336c385536dSGreg Roach            'stat_most_chil'       => $stat_most_chil,
337c385536dSGreg Roach            'stat_avg_chil'        => $stat_avg_chil,
338c385536dSGreg Roach        ]);
3398c2e8227SGreg Roach    }
340b86c018aSGreg Roach
341b86c018aSGreg Roach    /**
342b86c018aSGreg Roach     * This module assumes the database will use binary collation on the name columns.
343b86c018aSGreg Roach     * Until we convert MySQL databases to use utf8_bin, we need to do this at run-time.
344b86c018aSGreg Roach     *
345b86c018aSGreg Roach     * @param string      $column
346b86c018aSGreg Roach     * @param string|null $alias
347b86c018aSGreg Roach     *
348b86c018aSGreg Roach     * @return Expression
349b86c018aSGreg Roach     */
350b86c018aSGreg Roach    private function binaryColumn(string $column, string $alias = null): Expression
351b86c018aSGreg Roach    {
352b86c018aSGreg Roach        if (DB::connection()->getDriverName() === 'mysql') {
353b86c018aSGreg Roach            $sql = 'CAST(' . $column . ' AS binary)';
354b86c018aSGreg Roach        } else {
355b86c018aSGreg Roach            $sql = $column;
356b86c018aSGreg Roach        }
357b86c018aSGreg Roach
358b86c018aSGreg Roach        if ($alias !== null) {
359b86c018aSGreg Roach            $sql .= ' AS ' . $alias;
360b86c018aSGreg Roach        }
361b86c018aSGreg Roach
362b86c018aSGreg Roach        return new Expression($sql);
363b86c018aSGreg Roach    }
3648c2e8227SGreg Roach}
365