xref: /webtrees/app/Module/IndividualListModule.php (revision 80dcfb364a7797c77baca388219f53d615c9bab0)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Module;
21
22use Fig\Http\Message\StatusCodeInterface;
23use Fisharebest\Webtrees\Auth;
24use Fisharebest\Webtrees\DB;
25use Fisharebest\Webtrees\Family;
26use Fisharebest\Webtrees\I18N;
27use Fisharebest\Webtrees\Individual;
28use Fisharebest\Webtrees\Registry;
29use Fisharebest\Webtrees\Session;
30use Fisharebest\Webtrees\Tree;
31use Fisharebest\Webtrees\Validator;
32use Illuminate\Database\Query\Builder;
33use Illuminate\Database\Query\Expression;
34use Illuminate\Database\Query\JoinClause;
35use Illuminate\Support\Collection;
36use Psr\Http\Message\ResponseInterface;
37use Psr\Http\Message\ServerRequestInterface;
38use Psr\Http\Server\RequestHandlerInterface;
39
40use function array_filter;
41use function array_keys;
42use function array_map;
43use function array_merge;
44use function array_sum;
45use function array_values;
46use function assert;
47use function e;
48use function implode;
49use function ob_get_clean;
50use function ob_start;
51use function route;
52use function uksort;
53use function usort;
54use function view;
55
56use const ARRAY_FILTER_USE_KEY;
57
58/**
59 * Class IndividualListModule
60 */
61class IndividualListModule extends AbstractModule implements ModuleListInterface, RequestHandlerInterface
62{
63    use ModuleListTrait;
64
65    protected const ROUTE_URL = '/tree/{tree}/individual-list';
66
67    // The individual list and family list use the same code/logic.
68    // They just display different lists.
69    protected bool $families = false;
70
71    /**
72     * Initialization.
73     *
74     * @return void
75     */
76    public function boot(): void
77    {
78        Registry::routeFactory()->routeMap()
79            ->get(static::class, static::ROUTE_URL, $this);
80    }
81
82    /**
83     * How should this module be identified in the control panel, etc.?
84     *
85     * @return string
86     */
87    public function title(): string
88    {
89        /* I18N: Name of a module/list */
90        return I18N::translate('Individuals');
91    }
92
93    /**
94     * A sentence describing what this module does.
95     *
96     * @return string
97     */
98    public function description(): string
99    {
100        /* I18N: Description of the “Individuals” module */
101        return I18N::translate('A list of individuals.');
102    }
103
104    /**
105     * CSS class for the URL.
106     *
107     * @return string
108     */
109    public function listMenuClass(): string
110    {
111        return 'menu-list-indi';
112    }
113
114    /**
115     * @param Tree                                      $tree
116     * @param array<bool|int|string|array<string>|null> $parameters
117     *
118     * @return string
119     */
120    public function listUrl(Tree $tree, array $parameters = []): string
121    {
122        $request = Registry::container()->get(ServerRequestInterface::class);
123        $xref    = Validator::attributes($request)->isXref()->string('xref', '');
124
125        if ($xref !== '') {
126            $individual = Registry::individualFactory()->make($xref, $tree);
127
128            if ($individual instanceof Individual && $individual->canShow()) {
129                $primary_name = $individual->getPrimaryName();
130
131                $parameters['surname'] ??= $individual->getAllNames()[$primary_name]['surn'] ?? null;
132            }
133        }
134
135        $parameters['tree'] = $tree->name();
136
137        return route(static::class, $parameters);
138    }
139
140    /**
141     * @return array<string>
142     */
143    public function listUrlAttributes(): array
144    {
145        return [];
146    }
147
148    /**
149     * @param ServerRequestInterface $request
150     *
151     * @return ResponseInterface
152     */
153    public function handle(ServerRequestInterface $request): ResponseInterface
154    {
155        $tree = Validator::attributes($request)->tree();
156        $user = Validator::attributes($request)->user();
157
158        Auth::checkComponentAccess($this, ModuleListInterface::class, $tree, $user);
159
160        // All individuals with this surname
161        $surname_param = Validator::queryParams($request)->string('surname', '');
162        $surname       = I18N::strtoupper(I18N::language()->normalize($surname_param));
163
164        // All surnames beginning with this letter, where "@" is unknown and "," is none
165        $alpha = Validator::queryParams($request)->string('alpha', '');
166
167        // All first names beginning with this letter where "@" is unknown
168        $falpha = Validator::queryParams($request)->string('falpha', '');
169
170        // What type of list to display, if any
171        $show = Validator::queryParams($request)->string('show', 'surn');
172
173        // All individuals
174        $show_all = Validator::queryParams($request)->string('show_all', '');
175
176        // Include/exclude married names
177        $show_marnm = Validator::queryParams($request)->string('show_marnm', '');
178
179        // Break long lists down by given name
180        $show_all_firstnames = Validator::queryParams($request)->string('show_all_firstnames', '');
181
182        $params = [
183            'alpha'               => $alpha,
184            'falpha'              => $falpha,
185            'show'                => $show,
186            'show_all'            => $show_all,
187            'show_all_firstnames' => $show_all_firstnames,
188            'show_marnm'          => $show_marnm,
189            'surname'             => $surname,
190        ];
191
192        if ($surname_param !== $surname) {
193            return Registry::responseFactory()
194                ->redirectUrl($this->listUrl($tree, $params), StatusCodeInterface::STATUS_MOVED_PERMANENTLY);
195        }
196
197
198        // Make sure parameters are consistent with each other.
199        if ($show_all_firstnames === 'yes') {
200            $falpha = '';
201        }
202
203        if ($show_all === 'yes') {
204            $alpha   = '';
205            $surname = '';
206        }
207
208        if ($surname !== '') {
209            $alpha = I18N::language()->initialLetter($surname);
210        }
211
212        $surname_data     = $this->surnameData($tree, $show_marnm === 'yes', $this->families);
213        $all_surns        = $this->allSurns($surname_data);
214        $all_surnames     = $this->allSurnames($surname_data);
215        $surname_initials = $this->surnameInitials($surname_data);
216
217        // Make sure selections are consistent.
218        // i.e. can’t specify show_all and surname at the same time.
219        if ($show_all === 'yes') {
220            if ($show_all_firstnames === 'yes') {
221                $legend = I18N::translate('All');
222                $params = ['tree' => $tree->name(), 'show_all' => 'yes', 'show_marnm' => $show_marnm];
223                $show   = 'indi';
224            } elseif ($falpha !== '') {
225                $legend = I18N::translate('All') . ', ' . e($falpha) . '…';
226                $params = ['tree' => $tree->name(), 'show_all' => 'yes', 'show_marnm' => $show_marnm];
227                $show   = 'indi';
228            } else {
229                $legend = I18N::translate('All');
230                $params = ['tree' => $tree->name(), 'show_all' => 'yes', 'show_marnm' => $show_marnm];
231            }
232        } elseif ($surname !== '') {
233            $show_all = 'no';
234            if ($surname === Individual::NOMEN_NESCIO) {
235                $legend = I18N::translateContext('Unknown surname', '…');
236                $show   = 'indi'; // The surname list makes no sense with only one surname.
237            } else {
238                // The surname parameter is a root/canonical form. Display the actual surnames found.
239                $variants = array_keys($all_surnames[$surname] ?? [$surname => $surname]);
240                usort($variants, I18N::comparator());
241                $variants = array_map(static fn (string $x): string => $x === '' ? I18N::translate('No surname') : $x, $variants);
242                $legend   = implode('/', $variants);
243                $show     = 'indi'; // The surname list makes no sense with only one surname.
244            }
245            $params = ['tree' => $tree->name(), 'surname' => $surname, 'falpha' => $falpha, 'show_marnm' => $show_marnm];
246            switch ($falpha) {
247                case '':
248                    break;
249                case '@':
250                    $legend .= ', ' . I18N::translateContext('Unknown given name', '…');
251                    break;
252                default:
253                    $legend .= ', ' . e($falpha) . '…';
254                    break;
255            }
256        } elseif ($alpha === '@') {
257            $show_all = 'no';
258            $legend   = I18N::translateContext('Unknown surname', '…');
259            $params   = ['alpha' => $alpha, 'tree' => $tree->name(), 'show_marnm' => $show_marnm];
260            $surname  = Individual::NOMEN_NESCIO;
261            $show     = 'indi'; // SURN list makes no sense here
262        } elseif ($alpha === ',') {
263            $show_all = 'no';
264            $legend   = I18N::translate('No surname');
265            $params   = ['alpha' => $alpha, 'tree' => $tree->name(), 'show_marnm' => $show_marnm];
266            $show     = 'indi'; // SURN list makes no sense here
267        } elseif ($alpha !== '') {
268            $show_all = 'no';
269            $legend   = e($alpha) . '…';
270            $params   = ['alpha' => $alpha, 'tree' => $tree->name(), 'show_marnm' => $show_marnm];
271        } else {
272            $show_all = 'no';
273            $legend   = '…';
274            $params   = ['tree' => $tree->name(), 'show_marnm' => $show_marnm];
275            $show     = 'none'; // Don't show lists until something is chosen
276        }
277        $legend = '<bdi>' . $legend . '</bdi>';
278
279        if ($this->families) {
280            $title = I18N::translate('Families') . ' — ' . $legend;
281        } else {
282            $title = I18N::translate('Individuals') . ' — ' . $legend;
283        }
284
285        ob_start(); ?>
286        <div class="d-flex flex-column wt-page-options wt-page-options-individual-list d-print-none">
287            <ul class="d-flex flex-wrap list-unstyled justify-content-center wt-initials-list wt-initials-list-surname">
288
289                <?php foreach ($surname_initials as $letter => $count) : ?>
290                    <li class="wt-initials-list-item d-flex">
291                        <?php if ($count > 0) : ?>
292                            <a href="<?= e($this->listUrl($tree, ['alpha' => $letter, 'show_marnm' => $show_marnm, 'tree' => $tree->name()])) ?>" class="wt-initial px-1<?= $letter === $alpha ? ' active' : '' ?> '" title="<?= I18N::number($count) ?>"><?= $this->displaySurnameInitial((string) $letter) ?></a>
293                        <?php else : ?>
294                            <span class="wt-initial px-1 text-muted"><?= $this->displaySurnameInitial((string) $letter) ?></span>
295
296                        <?php endif ?>
297                    </li>
298                <?php endforeach ?>
299
300                <?php if (Session::has('initiated')) : ?>
301                    <!-- Search spiders don't get the "show all" option as the other links give them everything. -->
302                    <li class="wt-initials-list-item d-flex">
303                        <a class="wt-initial px-1<?= $show_all === 'yes' ? ' active' : '' ?>" href="<?= e($this->listUrl($tree, ['show_all' => 'yes'] + $params)) ?>"><?= I18N::translate('All') ?></a>
304                    </li>
305                <?php endif ?>
306            </ul>
307
308            <!-- Search spiders don't get an option to show/hide the surname sublists, nor does it make sense on the all/unknown/surname views -->
309            <?php if ($show !== 'none' && Session::has('initiated')) : ?>
310                <?php if ($show_marnm === 'yes') : ?>
311                    <p>
312                        <a href="<?= e($this->listUrl($tree, ['show' => $show, 'show_marnm' => 'no'] + $params)) ?>">
313                            <?= I18N::translate('Exclude individuals with “%s” as a married name', $legend) ?>
314                        </a>
315                    </p>
316                <?php else : ?>
317                    <p>
318                        <a href="<?= e($this->listUrl($tree, ['show' => $show, 'show_marnm' => 'yes'] + $params)) ?>">
319                            <?= I18N::translate('Include individuals with “%s” as a married name', $legend) ?>
320                        </a>
321                    </p>
322                <?php endif ?>
323
324                <?php if ($alpha !== '@' && $alpha !== ',' && $surname === '') : ?>
325                    <?php if ($show === 'surn') : ?>
326                        <p>
327                            <a href="<?= e($this->listUrl($tree, ['show' => 'indi'] + $params)) ?>">
328                                <?= I18N::translate('Show the list of individuals') ?>
329                            </a>
330                        </p>
331                    <?php else : ?>
332                        <p>
333                            <a href="<?= e($this->listUrl($tree, ['show' => 'surn'] + $params)) ?>">
334                                <?= I18N::translate('Show the list of surnames') ?>
335                            </a>
336                        </p>
337                    <?php endif ?>
338                <?php endif ?>
339            <?php endif ?>
340        </div>
341
342        <div class="wt-page-content">
343            <?php
344            if ($show === 'indi' || $show === 'surn') {
345                switch ($alpha) {
346                    case '@':
347                        $filter = static fn(string $x): bool => $x === Individual::NOMEN_NESCIO;
348                        break;
349                    case ',':
350                        $filter = static fn(string $x): bool => $x === '';
351                        break;
352                    case '':
353                        if ($show_all === 'yes') {
354                            $filter = static fn(string $x): bool => $x !== '' && $x !== Individual::NOMEN_NESCIO;
355                        } else {
356                            $filter = static fn(string $x): bool => $x === $surname;
357                        }
358                        break;
359                    default:
360                        if ($surname === '') {
361                            $filter = static fn(string $x): bool => I18N::language()->initialLetter($x) === $alpha;
362                        } else {
363                            $filter = static fn(string $x): bool => $x === $surname;
364                        }
365                        break;
366                }
367
368                $all_surnames = array_filter($all_surnames, $filter, ARRAY_FILTER_USE_KEY);
369
370                if ($show === 'surn') {
371                    // Show the surname list
372                    switch ($tree->getPreference('SURNAME_LIST_STYLE')) {
373                        case 'style1':
374                            echo view('lists/surnames-column-list', [
375                                'module'   => $this,
376                                'params'   => ['show' => 'indi', 'show_all' => null] + $params,
377                                'surnames' => $all_surnames,
378                                'totals'   => true,
379                                'tree'     => $tree,
380                            ]);
381                            break;
382                        case 'style3':
383                            echo view('lists/surnames-tag-cloud', [
384                                'module'   => $this,
385                                'params'   => ['show' => 'indi', 'show_all' => null] + $params,
386                                'surnames' => $all_surnames,
387                                'totals'   => true,
388                                'tree'     => $tree,
389                            ]);
390                            break;
391                        case 'style2':
392                        default:
393                            echo view('lists/surnames-table', [
394                                'families' => $this->families,
395                                'module'   => $this,
396                                'order'    => [[0, 'asc']],
397                                'params'   => ['show' => 'indi', 'show_all' => null] + $params,
398                                'surnames' => $all_surnames,
399                                'tree'     => $tree,
400                            ]);
401                            break;
402                    }
403                } else {
404                    // Show the list
405                    $count = array_sum(array_map(static fn(array $x): int => array_sum($x), $all_surnames));
406
407                    // Don't sublist short lists.
408                    $sublist_threshold = (int) $tree->getPreference('SUBLIST_TRIGGER_I');
409                    if ($sublist_threshold === 0 || $count < $sublist_threshold) {
410                        $falpha = '';
411                    } else {
412                        // Break long lists by initial letter of given name
413                        $all_surnames  = array_values(array_map(static fn($x): array => array_keys($x), $all_surnames));
414                        $all_surnames  = array_merge(...$all_surnames);
415                        $givn_initials = $this->givenNameInitials($tree, $all_surnames, $show_marnm === 'yes', $this->families);
416
417                        if ($surname !== '' || $show_all === 'yes') {
418                            if ($show_all !== 'yes') {
419                                echo '<h2 class="wt-page-title">', I18N::translate('Individuals with surname %s', $legend), '</h2>';
420                            }
421                            // Don't show the list until we have some filter criteria
422                            $show = $falpha !== '' || $show_all_firstnames === 'yes' ? 'indi' : 'none';
423                            echo '<ul class="d-flex flex-wrap list-unstyled justify-content-center wt-initials-list wt-initials-list-given-names">';
424                            foreach ($givn_initials as $givn_initial => $given_count) {
425                                echo '<li class="wt-initials-list-item d-flex">';
426                                if ($given_count > 0) {
427                                    if ($show === 'indi' && $givn_initial === $falpha && $show_all_firstnames !== 'yes') {
428                                        echo '<a class="wt-initial px-1 active" href="' . e($this->listUrl($tree, ['falpha' => $givn_initial] + $params)) . '" title="' . I18N::number($given_count) . '">' . $this->displayGivenNameInitial((string) $givn_initial) . '</a>';
429                                    } else {
430                                        echo '<a class="wt-initial px-1" href="' . e($this->listUrl($tree, ['falpha' => $givn_initial] + $params)) . '" title="' . I18N::number($given_count) . '">' . $this->displayGivenNameInitial((string) $givn_initial) . '</a>';
431                                    }
432                                } else {
433                                    echo '<span class="wt-initial px-1 text-muted">' . $this->displayGivenNameInitial((string) $givn_initial) . '</span>';
434                                }
435                                echo '</li>';
436                            }
437                            // Search spiders don't get the "show all" option as the other links give them everything.
438                            if (Session::has('initiated')) {
439                                echo '<li class="wt-initials-list-item d-flex">';
440                                if ($show_all_firstnames === 'yes') {
441                                    echo '<span class="wt-initial px-1 active">' . I18N::translate('All') . '</span>';
442                                } else {
443                                    echo '<a class="wt-initial px-1" href="' . e($this->listUrl($tree, ['show_all_firstnames' => 'yes'] + $params)) . '" title="' . I18N::number($count) . '">' . I18N::translate('All') . '</a>';
444                                }
445                                echo '</li>';
446                            }
447                            echo '</ul>';
448                        }
449                    }
450                    if ($show === 'indi') {
451                        if ($alpha === '@') {
452                            $surns_to_show = ['@N.N.'];
453                        } elseif ($alpha === ',') {
454                            $surns_to_show = [''];
455                        } elseif ($surname !== '') {
456                            $surns_to_show = $all_surns[$surname];
457                        } elseif ($alpha !== '') {
458                            $tmp = array_filter(
459                                $all_surns,
460                                static fn (string $x): bool => I18N::language()->initialLetter($x) === $alpha,
461                                ARRAY_FILTER_USE_KEY
462                            );
463
464                            $surns_to_show = array_merge(...array_values($tmp));
465                        } else {
466                            $surns_to_show = [];
467                        }
468
469                        if ($this->families) {
470                            echo view('lists/families-table', [
471                                'families' => $this->families($tree, $surns_to_show, $falpha, $show_marnm === 'yes'),
472                                'tree'     => $tree,
473                            ]);
474                        } else {
475                            echo view('lists/individuals-table', [
476                                'individuals' => $this->individuals($tree, $surns_to_show, $falpha, $show_marnm === 'yes', false),
477                                'sosa'        => false,
478                                'tree'        => $tree,
479                            ]);
480                        }
481                    }
482                }
483            } ?>
484        </div>
485        <?php
486
487        $html = ob_get_clean();
488
489        return $this->viewResponse('modules/individual-list/page', [
490            'content' => $html,
491            'title'   => $title,
492            'tree'    => $tree,
493        ]);
494    }
495
496    /**
497     * Some initial letters have a special meaning
498     *
499     * @param string $initial
500     *
501     * @return string
502     */
503    protected function displayGivenNameInitial(string $initial): string
504    {
505        if ($initial === '@') {
506            return I18N::translateContext('Unknown given name', '…');
507        }
508
509        return e($initial);
510    }
511
512    /**
513     * Some initial letters have a special meaning
514     *
515     * @param string $initial
516     *
517     * @return string
518     */
519    protected function displaySurnameInitial(string $initial): string
520    {
521        if ($initial === '@') {
522            return I18N::translateContext('Unknown surname', '…');
523        }
524
525        if ($initial === ',') {
526            return I18N::translate('No surname');
527        }
528
529        return e($initial);
530    }
531
532    /**
533     * Restrict a query to individuals that are a spouse in a family record.
534     *
535     * @param bool    $fams
536     * @param Builder $query
537     */
538    protected function whereFamily(bool $fams, Builder $query): void
539    {
540        if ($fams) {
541            $query->join('link', static function (JoinClause $join): void {
542                $join
543                    ->on('l_from', '=', 'n_id')
544                    ->on('l_file', '=', 'n_file')
545                    ->where('l_type', '=', 'FAMS');
546            });
547        }
548    }
549
550    /**
551     * Restrict a query to include/exclude married names.
552     *
553     * @param bool    $marnm
554     * @param Builder $query
555     */
556    protected function whereMarriedName(bool $marnm, Builder $query): void
557    {
558        if (!$marnm) {
559            $query->where('n_type', '<>', '_MARNM');
560        }
561    }
562
563    /**
564     * Get a count of individuals with each initial letter
565     *
566     * @param Tree          $tree
567     * @param array<string> $surns if set, only consider people with this surname
568     * @param bool          $marnm if set, include married names
569     * @param bool          $fams  if set, only consider individuals with FAMS records
570     *
571     * @return array<int>
572     */
573    public function givenNameInitials(Tree $tree, array $surns, bool $marnm, bool $fams): array
574    {
575        $initials = [];
576
577        // Ensure our own language comes before others.
578        foreach (I18N::language()->alphabet() as $initial) {
579            $initials[$initial] = 0;
580        }
581
582        $query = DB::table('name')
583            ->where('n_file', '=', $tree->id());
584
585        $this->whereFamily($fams, $query);
586        $this->whereMarriedName($marnm, $query);
587
588        if ($surns !== []) {
589            $query->whereIn('n_surn', $surns);
590        }
591
592        $query
593            ->select([$this->binaryColumn('n_givn', 'n_givn'), new Expression('COUNT(*) AS count')])
594            ->groupBy([$this->binaryColumn('n_givn')]);
595
596        foreach ($query->get() as $row) {
597            $initial            = I18N::strtoupper(I18N::language()->initialLetter($row->n_givn));
598            $initials[$initial] ??= 0;
599            $initials[$initial] += (int) $row->count;
600        }
601
602        $count_unknown = $initials['@'] ?? 0;
603
604        if ($count_unknown > 0) {
605            unset($initials['@']);
606            $initials['@'] = $count_unknown;
607        }
608
609        return $initials;
610    }
611
612    /**
613     * @param Tree $tree
614     * @param bool $marnm
615     * @param bool $fams
616     *
617     * @return array<object{n_surn:string,n_surname:string,total:int}>
618     */
619    private function surnameData(Tree $tree, bool $marnm, bool $fams): array
620    {
621        $query = DB::table('name')
622            ->where('n_file', '=', $tree->id())
623            ->whereNotNull('n_surn') // Filters old records for sources, repositories, etc.
624            ->whereNotNull('n_surname')
625            ->select([
626                $this->binaryColumn('n_surn', 'n_surn'),
627                $this->binaryColumn('n_surname', 'n_surname'),
628                new Expression('COUNT(*) AS total'),
629            ]);
630
631        $this->whereFamily($fams, $query);
632        $this->whereMarriedName($marnm, $query);
633
634        $query->groupBy([
635            $this->binaryColumn('n_surn'),
636            $this->binaryColumn('n_surname'),
637        ]);
638
639        return $query
640            ->get()
641            ->map(static fn(object $x): object => (object) ['n_surn' => $x->n_surn, 'n_surname' => $x->n_surname, 'total' => (int) $x->total])
642            ->all();
643    }
644
645    /**
646     * Group n_surn values, based on collation rules for the current language.
647     * We need them to find the individuals with this n_surn.
648     *
649     * @param array<object{n_surn:string,n_surname:string,total:int}> $surname_data
650     *
651     * @return array<array<int,string>>
652     */
653    protected function allSurns(array $surname_data): array
654    {
655        $list = [];
656
657        foreach ($surname_data as $row) {
658            $normalized = I18N::strtoupper(I18N::language()->normalize($row->n_surn));
659            $list[$normalized][] = $row->n_surn;
660        }
661
662        uksort($list, I18N::comparator());
663
664        return $list;
665    }
666
667    /**
668     * Group n_surname values, based on collation rules for each n_surn.
669     * We need them to show counts of individuals with each surname.
670     *
671     * @param array<object{n_surn:string,n_surname:string,total:int}> $surname_data
672     *
673     * @return array<array<int>>
674     */
675    protected function allSurnames(array $surname_data): array
676    {
677        $list = [];
678
679        foreach ($surname_data as $row) {
680            $n_surn = $row->n_surn === '' ? $row->n_surname : $row->n_surn;
681            $n_surn = I18N::strtoupper(I18N::language()->normalize($n_surn));
682
683            $list[$n_surn][$row->n_surname] ??= 0;
684            $list[$n_surn][$row->n_surname] += $row->total;
685        }
686
687        uksort($list, I18N::comparator());
688
689        return $list;
690    }
691
692    /**
693     * Extract initial letters and counts for all surnames.
694     *
695     * @param array<object{n_surn:string,n_surname:string,total:int}> $surname_data
696     *
697     * @return array<int>
698     */
699    protected function surnameInitials(array $surname_data): array
700    {
701        $initials = [];
702
703        // Ensure our own language comes before others.
704        foreach (I18N::language()->alphabet() as $initial) {
705            $initials[$initial] = 0;
706        }
707
708        foreach ($surname_data as $row) {
709            $initial = I18N::language()->initialLetter(I18N::strtoupper($row->n_surn));
710            $initial = I18N::language()->normalize($initial);
711
712            $initials[$initial] ??= 0;
713            $initials[$initial] += $row->total;
714        }
715
716        // Move specials to the end
717        $count_none = $initials[''] ?? 0;
718
719        if ($count_none > 0) {
720            unset($initials['']);
721            $initials[','] = $count_none;
722        }
723
724        $count_unknown = $initials['@'] ?? 0;
725
726        if ($count_unknown > 0) {
727            unset($initials['@']);
728            $initials['@'] = $count_unknown;
729        }
730
731        return $initials;
732    }
733
734    /**
735     * Fetch a list of individuals with specified names
736     * To search for unknown names, use $surn="@N.N.", $salpha="@" or $galpha="@"
737     * To search for names with no surnames, use $salpha=","
738     *
739     * @param Tree          $tree
740     * @param array<string> $surns_to_show if set, only fetch people with this n_surn
741     * @param string        $galpha        if set, only fetch given names starting with this letter
742     * @param bool          $marnm         if set, include married names
743     * @param bool          $fams          if set, only fetch individuals with FAMS records
744     *
745     * @return Collection<int,Individual>
746     */
747    protected function individuals(Tree $tree, array $surns_to_show, string $galpha, bool $marnm, bool $fams): Collection
748    {
749        $query = DB::table('individuals')
750            ->join('name', static function (JoinClause $join): void {
751                $join
752                    ->on('n_id', '=', 'i_id')
753                    ->on('n_file', '=', 'i_file');
754            })
755            ->where('i_file', '=', $tree->id())
756            ->select(['i_id AS xref', 'i_gedcom AS gedcom', 'n_givn', 'n_surn']);
757
758        $this->whereFamily($fams, $query);
759        $this->whereMarriedName($marnm, $query);
760
761        if ($surns_to_show === []) {
762            $query->whereNotIn('n_surn', ['', '@N.N.']);
763        } else {
764            $query->whereIn($this->binaryColumn('n_surn'), $surns_to_show);
765        }
766
767        $individuals = new Collection();
768
769        foreach ($query->get() as $row) {
770            $individual = Registry::individualFactory()->make($row->xref, $tree, $row->gedcom);
771            assert($individual instanceof Individual);
772
773            // The name from the database may be private - check the filtered list...
774            foreach ($individual->getAllNames() as $n => $name) {
775                if ($name['givn'] === $row->n_givn && $name['surn'] === $row->n_surn) {
776                    if ($galpha === '' || I18N::strtoupper(I18N::language()->initialLetter($row->n_givn)) === $galpha) {
777                        $individual->setPrimaryName($n);
778                        // We need to clone $individual, as we may have multiple references to the
779                        // same individual in this list, and the "primary name" would otherwise
780                        // be shared amongst all of them.
781                        $individuals->push(clone $individual);
782                        break;
783                    }
784                }
785            }
786        }
787
788        return $individuals;
789    }
790
791    /**
792     * Fetch a list of families with specified names
793     * To search for unknown names, use $surn="@N.N.", $salpha="@" or $galpha="@"
794     * To search for names with no surnames, use $salpha=","
795     *
796     * @param Tree          $tree
797     * @param array<string> $surnames if set, only fetch people with this n_surname
798     * @param string        $galpha   if set, only fetch given names starting with this letter
799     * @param bool          $marnm    if set, include married names
800     *
801     * @return Collection<int,Family>
802     */
803    protected function families(Tree $tree, array $surnames, string $galpha, bool $marnm): Collection
804    {
805        $families = new Collection();
806
807        foreach ($this->individuals($tree, $surnames, $galpha, $marnm, true) as $indi) {
808            foreach ($indi->spouseFamilies() as $family) {
809                $families->push($family);
810            }
811        }
812
813        return $families->unique();
814    }
815
816    /**
817     * This module assumes the database will use binary collation on the name columns.
818     * Until we convert MySQL databases to use utf8_bin, we need to do this at run-time.
819     *
820     * @param string      $column
821     * @param string|null $alias
822     *
823     * @return Expression
824     */
825    private function binaryColumn(string $column, string $alias = null): Expression
826    {
827        if (DB::connection()->getDriverName() === 'mysql') {
828            $sql = 'CAST(' . $column . ' AS binary)';
829        } else {
830            $sql = $column;
831        }
832
833        if ($alias !== null) {
834            $sql .= ' AS ' . $alias;
835        }
836
837        return new Expression($sql);
838    }
839}
840