xref: /webtrees/app/Module/IndividualListModule.php (revision 6b9cb339562c8aa4681c7eff2bf3bdf401cc9edd)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2020 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 <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Module;
21
22use Aura\Router\RouterContainer;
23use Fisharebest\Localization\Locale\LocaleInterface;
24use Fisharebest\Webtrees\Auth;
25use Fisharebest\Webtrees\Contracts\UserInterface;
26use Fisharebest\Webtrees\Registry;
27use Fisharebest\Webtrees\Family;
28use Fisharebest\Webtrees\Functions\FunctionsPrintLists;
29use Fisharebest\Webtrees\GedcomRecord;
30use Fisharebest\Webtrees\I18N;
31use Fisharebest\Webtrees\Individual;
32use Fisharebest\Webtrees\Services\LocalizationService;
33use Fisharebest\Webtrees\Session;
34use Fisharebest\Webtrees\Tree;
35use Illuminate\Database\Capsule\Manager as DB;
36use Illuminate\Database\Query\Builder;
37use Illuminate\Database\Query\Expression;
38use Illuminate\Database\Query\JoinClause;
39use Psr\Http\Message\ResponseInterface;
40use Psr\Http\Message\ServerRequestInterface;
41use Psr\Http\Server\RequestHandlerInterface;
42
43use function app;
44use function array_keys;
45use function assert;
46use function e;
47use function implode;
48use function in_array;
49use function ob_get_clean;
50use function ob_start;
51use function redirect;
52use function route;
53use function usort;
54use function view;
55
56/**
57 * Class IndividualListModule
58 */
59class IndividualListModule extends AbstractModule implements ModuleListInterface, RequestHandlerInterface
60{
61    use ModuleListTrait;
62
63    protected const ROUTE_URL = '/tree/{tree}/individual-list';
64
65    /** @var LocalizationService */
66    private $localization_service;
67
68    /**
69     * IndividualListModule constructor.
70     *
71     * @param LocalizationService $localization_service
72     */
73    public function __construct(LocalizationService $localization_service)
74    {
75        $this->localization_service = $localization_service;
76    }
77
78    /**
79     * Initialization.
80     *
81     * @return void
82     */
83    public function boot(): void
84    {
85        $router_container = app(RouterContainer::class);
86        assert($router_container instanceof RouterContainer);
87
88        $router_container->getMap()
89            ->get(static::class, static::ROUTE_URL, $this);
90    }
91
92    /**
93     * How should this module be identified in the control panel, etc.?
94     *
95     * @return string
96     */
97    public function title(): string
98    {
99        /* I18N: Name of a module/list */
100        return I18N::translate('Individuals');
101    }
102
103    /**
104     * A sentence describing what this module does.
105     *
106     * @return string
107     */
108    public function description(): string
109    {
110        /* I18N: Description of the “Individuals” module */
111        return I18N::translate('A list of individuals.');
112    }
113
114    /**
115     * CSS class for the URL.
116     *
117     * @return string
118     */
119    public function listMenuClass(): string
120    {
121        return 'menu-list-indi';
122    }
123
124    /**
125     * @param Tree    $tree
126     * @param mixed[] $parameters
127     *
128     * @return string
129     */
130    public function listUrl(Tree $tree, array $parameters = []): string
131    {
132        $xref = app(ServerRequestInterface::class)->getAttribute('xref', '');
133
134        if ($xref !== '') {
135            $individual = Registry::individualFactory()->make($xref, $tree);
136
137            if ($individual instanceof Individual && $individual->canShow()) {
138                $primary_name = $individual->getPrimaryName();
139
140                $parameters['surname'] = $parameters['surname'] ?? $individual->getAllNames()[$primary_name]['surn'] ?? null;
141            }
142        }
143
144        $parameters['tree'] = $tree->name();
145
146        return route(static::class, $parameters);
147    }
148
149    /**
150     * @return string[]
151     */
152    public function listUrlAttributes(): array
153    {
154        return [];
155    }
156
157    /**
158     * Handle URLs generated by older versions of webtrees
159     *
160     * @param ServerRequestInterface $request
161     *
162     * @return ResponseInterface
163     */
164    public function getListAction(ServerRequestInterface $request): ResponseInterface
165    {
166        return redirect($this->listUrl($request->getAttribute('tree'), $request->getQueryParams()));
167    }
168
169    /**
170     * @param ServerRequestInterface $request
171     *
172     * @return ResponseInterface
173     */
174    public function handle(ServerRequestInterface $request): ResponseInterface
175    {
176        $tree = $request->getAttribute('tree');
177        assert($tree instanceof Tree);
178
179        $user = $request->getAttribute('user');
180        assert($user instanceof UserInterface);
181
182        Auth::checkComponentAccess($this, ModuleListInterface::class, $tree, $user);
183
184        return $this->createResponse($tree, $user, $request->getQueryParams(), false);
185    }
186
187    /**
188     * @param Tree          $tree
189     * @param UserInterface $user
190     * @param array<string> $params
191     * @param bool          $families
192     *
193     * @return ResponseInterface
194     */
195    protected function createResponse(Tree $tree, UserInterface $user, array $params, bool $families): ResponseInterface
196    {
197        ob_start();
198
199        // We show three different lists: initials, surnames and individuals
200
201        // All surnames beginning with this letter where "@"=unknown and ","=none
202        $alpha = $params['alpha'] ?? '';
203
204        // All individuals with this surname
205        $surname = $params['surname'] ?? '';
206
207        // All individuals
208        $show_all = $params['show_all'] ?? 'no';
209
210        // Long lists can be broken down by given name
211        $show_all_firstnames = $params['show_all_firstnames'] ?? 'no';
212        if ($show_all_firstnames === 'yes') {
213            $falpha = '';
214        } else {
215            // All first names beginning with this letter
216            $falpha = $params['falpha'] ?? '';
217        }
218
219        $show_marnm = $params['show_marnm'] ?? '';
220        switch ($show_marnm) {
221            case 'no':
222            case 'yes':
223                $user->setPreference($families ? 'family-list-marnm' : 'individual-list-marnm', $show_marnm);
224                break;
225            default:
226                $show_marnm = $user->getPreference($families ? 'family-list-marnm' : 'individual-list-marnm');
227        }
228
229        // Make sure selections are consistent.
230        // i.e. can’t specify show_all and surname at the same time.
231        if ($show_all === 'yes') {
232            if ($show_all_firstnames === 'yes') {
233                $alpha   = '';
234                $surname = '';
235                $legend  = I18N::translate('All');
236                $params  = [
237                    'tree'     => $tree->name(),
238                    'show_all' => 'yes',
239                ];
240                $show    = 'indi';
241            } elseif ($falpha !== '') {
242                $alpha   = '';
243                $surname = '';
244                $legend  = I18N::translate('All') . ', ' . e($falpha) . '…';
245                $params  = [
246                    'tree'     => $tree->name(),
247                    'show_all' => 'yes',
248                ];
249                $show    = 'indi';
250            } else {
251                $alpha   = '';
252                $surname = '';
253                $legend  = I18N::translate('All');
254                $show    = $params['show'] ?? 'surn';
255                $params  = [
256                    'tree'     => $tree->name(),
257                    'show_all' => 'yes',
258                ];
259            }
260        } elseif ($surname !== '') {
261            $alpha    = $this->localization_service->initialLetter($surname, I18N::locale()); // so we can highlight the initial letter
262            $show_all = 'no';
263            if ($surname === '@N.N.') {
264                $legend = I18N::translateContext('Unknown surname', '…');
265            } else {
266                // The surname parameter is a root/canonical form.
267                // Display it as the actual surname
268                $legend = implode('/', array_keys($this->surnames($tree, $surname, $alpha, $show_marnm === 'yes', $families, I18N::locale())));
269            }
270            $params = [
271                'tree'    => $tree->name(),
272                'surname' => $surname,
273                'falpha'  => $falpha,
274            ];
275            switch ($falpha) {
276                case '':
277                    break;
278                case '@':
279                    $legend .= ', ' . I18N::translateContext('Unknown given name', '…');
280                    break;
281                default:
282                    $legend .= ', ' . e($falpha) . '…';
283                    break;
284            }
285            $show = 'indi'; // SURN list makes no sense here
286        } elseif ($alpha === '@') {
287            $show_all = 'no';
288            $legend   = I18N::translateContext('Unknown surname', '…');
289            $params   = [
290                'alpha' => $alpha,
291                'tree'  => $tree->name(),
292            ];
293            $show     = 'indi'; // SURN list makes no sense here
294        } elseif ($alpha === ',') {
295            $show_all = 'no';
296            $legend   = I18N::translate('None');
297            $params   = [
298                'alpha' => $alpha,
299                'tree'  => $tree->name(),
300            ];
301            $show     = 'indi'; // SURN list makes no sense here
302        } elseif ($alpha !== '') {
303            $show_all = 'no';
304            $legend   = e($alpha) . '…';
305            $show     = $params['show'] ?? 'surn';
306            $params   = [
307                'alpha' => $alpha,
308                'tree'  => $tree->name(),
309            ];
310        } else {
311            $show_all = 'no';
312            $legend   = '…';
313            $params   = [
314                'tree' => $tree->name(),
315            ];
316            $show     = 'none'; // Don't show lists until something is chosen
317        }
318        $legend = '<span dir="auto">' . $legend . '</span>';
319
320        if ($families) {
321            $title = I18N::translate('Families') . ' — ' . $legend;
322        } else {
323            $title = I18N::translate('Individuals') . ' — ' . $legend;
324        } ?>
325        <div class="d-flex flex-column wt-page-options wt-page-options-individual-list d-print-none">
326            <ul class="d-flex flex-wrap list-unstyled justify-content-center wt-initials-list wt-initials-list-surname">
327
328                <?php foreach ($this->surnameAlpha($tree, $show_marnm === 'yes', $families, I18N::locale()) as $letter => $count) : ?>
329                    <li class="wt-initials-list-item d-flex">
330                        <?php if ($count > 0) : ?>
331                            <a href="<?= e($this->listUrl($tree, ['alpha' => $letter, 'tree' => $tree->name()])) ?>" class="wt-initial px-1<?= $letter === $alpha ? ' active' : '' ?> '" title="<?= I18N::number($count) ?>"><?= $this->surnameInitial((string) $letter) ?></a>
332                        <?php else : ?>
333                            <span class="wt-initial px-1 text-muted"><?= $this->surnameInitial((string) $letter) ?></span>
334
335                        <?php endif ?>
336                    </li>
337                <?php endforeach ?>
338
339                <?php if (Session::has('initiated')) : ?>
340                    <!-- Search spiders don't get the "show all" option as the other links give them everything. -->
341                    <li class="wt-initials-list-item d-flex">
342                        <a class="wt-initial px-1<?= $show_all === 'yes' ? ' active' : '' ?>" href="<?= e($this->listUrl($tree, ['show_all' => 'yes'] + $params)) ?>"><?= I18N::translate('All') ?></a>
343                    </li>
344                <?php endif ?>
345            </ul>
346
347            <!-- Search spiders don't get an option to show/hide the surname sublists, nor does it make sense on the all/unknown/surname views -->
348            <?php if ($show !== 'none' && Session::has('initiated')) : ?>
349                <?php if ($show_marnm === 'yes') : ?>
350                    <p>
351                        <a href="<?= e($this->listUrl($tree, ['show' => $show, 'show_marnm' => 'no'] + $params)) ?>">
352                            <?= I18N::translate('Exclude individuals with “%s” as a married name', $legend) ?>
353                        </a>
354                    </p>
355                <?php else : ?>
356                    <p>
357                        <a href="<?= e($this->listUrl($tree, ['show' => $show, 'show_marnm' => 'yes'] + $params)) ?>">
358                            <?= I18N::translate('Include individuals with “%s” as a married name', $legend) ?>
359                        </a>
360                    </p>
361                <?php endif ?>
362
363                <?php if ($alpha !== '@' && $alpha !== ',' && $surname === '') : ?>
364                    <?php if ($show === 'surn') : ?>
365                        <p>
366                            <a href="<?= e($this->listUrl($tree, ['show' => 'indi', 'show_marnm' => 'no'] + $params)) ?>">
367                                <?= I18N::translate('Show the list of individuals') ?>
368                            </a>
369                        </p>
370                    <?php else : ?>
371                        <p>
372                            <a href="<?= e($this->listUrl($tree, ['show' => 'surn', 'show_marnm' => 'no'] + $params)) ?>">
373                                <?= I18N::translate('Show the list of surnames') ?>
374                            </a>
375                        </p>
376                    <?php endif ?>
377                <?php endif ?>
378            <?php endif ?>
379        </div>
380
381        <div class="wt-page-content">
382            <?php
383
384            if ($show === 'indi' || $show === 'surn') {
385                $surns = $this->surnames($tree, $surname, $alpha, $show_marnm === 'yes', $families, I18N::locale());
386                if ($show === 'surn') {
387                    // Show the surname list
388                    switch ($tree->getPreference('SURNAME_LIST_STYLE')) {
389                        case 'style1':
390                            echo FunctionsPrintLists::surnameList($surns, 3, true, $this, $tree);
391                            break;
392                        case 'style3':
393                            echo FunctionsPrintLists::surnameTagCloud($surns, $this, true, $tree);
394                            break;
395                        case 'style2':
396                        default:
397                            echo view('lists/surnames-table', [
398                                'surnames' => $surns,
399                                'families' => $families,
400                                'module'   => $this,
401                                'tree'     => $tree,
402                            ]);
403                            break;
404                    }
405                } else {
406                    // Show the list
407                    $count = 0;
408                    foreach ($surns as $surnames) {
409                        foreach ($surnames as $total) {
410                            $count += $total;
411                        }
412                    }
413                    // Don't sublist short lists.
414                    if ($count < $tree->getPreference('SUBLIST_TRIGGER_I')) {
415                        $falpha = '';
416                    } else {
417                        $givn_initials = $this->givenAlpha($tree, $surname, $alpha, $show_marnm === 'yes', $families, I18N::locale());
418                        // Break long lists by initial letter of given name
419                        if ($surname !== '' || $show_all === 'yes') {
420                            if ($show_all === 'no') {
421                                echo '<h2 class="wt-page-title">', I18N::translate('Individuals with surname %s', $legend), '</h2>';
422                            }
423                            // Don't show the list until we have some filter criteria
424                            $show = $falpha !== '' || $show_all_firstnames === 'yes' ? 'indi' : 'none';
425                            $list = [];
426                            echo '<ul class="d-flex flex-wrap list-unstyled justify-content-center wt-initials-list wt-initials-list-given-names">';
427                            foreach ($givn_initials as $givn_initial => $given_count) {
428                                echo '<li class="wt-initials-list-item d-flex">';
429                                if ($given_count > 0) {
430                                    if ($show === 'indi' && $givn_initial === $falpha && $show_all_firstnames === 'no') {
431                                        echo '<a class="wt-initial px-1 active" href="' . e($this->listUrl($tree, ['falpha' => $givn_initial] + $params)) . '" title="' . I18N::number($given_count) . '">' . $this->givenNameInitial((string) $givn_initial) . '</a>';
432                                    } else {
433                                        echo '<a class="wt-initial px-1" href="' . e($this->listUrl($tree, ['falpha' => $givn_initial] + $params)) . '" title="' . I18N::number($given_count) . '">' . $this->givenNameInitial((string) $givn_initial) . '</a>';
434                                    }
435                                } else {
436                                    echo '<span class="wt-initial px-1 text-muted">' . $this->givenNameInitial((string) $givn_initial) . '</span>';
437                                }
438                                echo '</li>';
439                            }
440                            // Search spiders don't get the "show all" option as the other links give them everything.
441                            if (Session::has('initiated')) {
442                                echo '<li class="wt-initials-list-item d-flex">';
443                                if ($show_all_firstnames === 'yes') {
444                                    echo '<span class="wt-initial px-1 warning">' . I18N::translate('All') . '</span>';
445                                } else {
446                                    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>';
447                                }
448                                echo '</li>';
449                            }
450                            echo '</ul>';
451                            echo '<p class="text-center alpha_index">', implode(' | ', $list), '</p>';
452                        }
453                    }
454                    if ($show === 'indi') {
455                        if (!$families) {
456                            echo view('lists/individuals-table', [
457                                'individuals' => $this->individuals($tree, $surname, $alpha, $falpha, $show_marnm === 'yes', false, I18N::locale()),
458                                'sosa'        => false,
459                                'tree'        => $tree,
460                            ]);
461                        } else {
462                            echo view('lists/families-table', [
463                                'families' => $this->families($tree, $surname, $alpha, $falpha, $show_marnm === 'yes', I18N::locale()),
464                                'tree'     => $tree,
465                            ]);
466                        }
467                    }
468                }
469            } ?>
470        </div>
471        <?php
472
473        $html = ob_get_clean();
474
475        return $this->viewResponse('modules/individual-list/page', [
476            'content' => $html,
477            'title'   => $title,
478            'tree'    => $tree,
479        ]);
480    }
481
482    /**
483     * Some initial letters have a special meaning
484     *
485     * @param string $initial
486     *
487     * @return string
488     */
489    protected function givenNameInitial(string $initial): string
490    {
491        if ($initial === '@') {
492            return I18N::translateContext('Unknown given name', '…');
493        }
494
495        return e($initial);
496    }
497
498    /**
499     * Some initial letters have a special meaning
500     *
501     * @param string $initial
502     *
503     * @return string
504     */
505    protected function surnameInitial(string $initial): string
506    {
507        if ($initial === '@') {
508            return I18N::translateContext('Unknown surname', '…');
509        }
510
511        if ($initial === ',') {
512            return I18N::translate('None');
513        }
514
515        return e($initial);
516    }
517
518    /**
519     * Restrict a query to individuals that are a spouse in a family record.
520     *
521     * @param bool    $fams
522     * @param Builder $query
523     */
524    protected function whereFamily(bool $fams, Builder $query): void
525    {
526        if ($fams) {
527            $query->join('link', static function (JoinClause $join): void {
528                $join
529                    ->on('l_from', '=', 'n_id')
530                    ->on('l_file', '=', 'n_file')
531                    ->where('l_type', '=', 'FAMS');
532            });
533        }
534    }
535
536    /**
537     * Restrict a query to include/exclude married names.
538     *
539     * @param bool    $marnm
540     * @param Builder $query
541     */
542    protected function whereMarriedName(bool $marnm, Builder $query): void
543    {
544        if (!$marnm) {
545            $query->where('n_type', '<>', '_MARNM');
546        }
547    }
548
549    /**
550     * Get a list of initial surname letters.
551     *
552     * @param Tree            $tree
553     * @param bool            $marnm if set, include married names
554     * @param bool            $fams  if set, only consider individuals with FAMS records
555     * @param LocaleInterface $locale
556     *
557     * @return int[]
558     */
559    public function surnameAlpha(Tree $tree, bool $marnm, bool $fams, LocaleInterface $locale): array
560    {
561        $collation = $this->localization_service->collation($locale);
562
563        $n_surn = $this->fieldWithCollation('n_surn', $collation);
564        $alphas = [];
565
566        $query = DB::table('name')->where('n_file', '=', $tree->id());
567
568        $this->whereFamily($fams, $query);
569        $this->whereMarriedName($marnm, $query);
570
571        // Fetch all the letters in our alphabet, whether or not there
572        // are any names beginning with that letter. It looks better to
573        // show the full alphabet, rather than omitting rare letters such as X.
574        foreach ($this->localization_service->alphabet($locale) as $letter) {
575            $query2 = clone $query;
576
577            $this->whereInitial($query2, 'n_surn', $letter, $locale);
578
579            $alphas[$letter] = $query2->count();
580        }
581
582        // Now fetch initial letters that are not in our alphabet,
583        // including "@" (for "@N.N.") and "" for no surname.
584        foreach ($this->localization_service->alphabet($locale) as $n => $letter) {
585            $query->where($n_surn, 'NOT LIKE', $letter . '%');
586        }
587
588        $rows = $query
589            ->groupBy(['initial'])
590            ->orderBy('initial')
591            ->pluck(new Expression('COUNT(*) AS aggregate'), new Expression('SUBSTR(n_surn, 1, 1) AS initial'));
592
593        $specials = ['@', ''];
594
595        foreach ($rows as $alpha => $count) {
596            if (!in_array($alpha, $specials, true)) {
597                $alphas[$alpha] = (int) $count;
598            }
599        }
600
601        // Empty surnames have a special code ',' - as we search for SURN.GIVN
602        foreach ($specials as $special) {
603            if ($rows->has($special)) {
604                $alphas[$special ?: ','] = (int) $rows[$special];
605            }
606        }
607
608        return $alphas;
609    }
610
611    /**
612     * Get a list of initial given name letters for indilist.php and famlist.php
613     *
614     * @param Tree            $tree
615     * @param string          $surn   if set, only consider people with this surname
616     * @param string          $salpha if set, only consider surnames starting with this letter
617     * @param bool            $marnm  if set, include married names
618     * @param bool            $fams   if set, only consider individuals with FAMS records
619     * @param LocaleInterface $locale
620     *
621     * @return int[]
622     */
623    public function givenAlpha(Tree $tree, string $surn, string $salpha, bool $marnm, bool $fams, LocaleInterface $locale): array
624    {
625        $collation = $this->localization_service->collation($locale);
626
627        $alphas = [];
628
629        $query = DB::table('name')
630            ->where('n_file', '=', $tree->id());
631
632        $this->whereFamily($fams, $query);
633        $this->whereMarriedName($marnm, $query);
634
635        if ($surn !== '') {
636            $n_surn = $this->fieldWithCollation('n_surn', $collation);
637            $query->where($n_surn, '=', $surn);
638        } elseif ($salpha === ',') {
639            $query->where('n_surn', '=', '');
640        } elseif ($salpha === '@') {
641            $query->where('n_surn', '=', '@N.N.');
642        } elseif ($salpha !== '') {
643            $this->whereInitial($query, 'n_surn', $salpha, $locale);
644        } else {
645            // All surnames
646            $query->whereNotIn('n_surn', ['', '@N.N.']);
647        }
648
649        // Fetch all the letters in our alphabet, whether or not there
650        // are any names beginning with that letter. It looks better to
651        // show the full alphabet, rather than omitting rare letters such as X
652        foreach ($this->localization_service->alphabet($locale) as $letter) {
653            $query2 = clone $query;
654
655            $this->whereInitial($query2, 'n_givn', $letter, $locale);
656
657            $alphas[$letter] = $query2->distinct()->count('n_id');
658        }
659
660        $rows = $query
661            ->groupBy(['initial'])
662            ->orderBy('initial')
663            ->pluck(new Expression('COUNT(*) AS aggregate'), new Expression('UPPER(SUBSTR(n_givn, 1, 1)) AS initial'));
664
665        $specials = ['@'];
666
667        foreach ($rows as $alpha => $count) {
668            if ($alpha !== '@') {
669                $alphas[$alpha] = (int) $count;
670            }
671        }
672
673        foreach ($specials as $special) {
674            if ($rows->has('@')) {
675                $alphas['@'] = (int) $rows['@'];
676            }
677        }
678
679
680        return $alphas;
681    }
682
683    /**
684     * Get a count of actual surnames and variants, based on a "root" surname.
685     *
686     * @param Tree            $tree
687     * @param string          $surn   if set, only count people with this surname
688     * @param string          $salpha if set, only consider surnames starting with this letter
689     * @param bool            $marnm  if set, include married names
690     * @param bool            $fams   if set, only consider individuals with FAMS records
691     * @param LocaleInterface $locale
692     *
693     * @return int[][]
694     */
695    public function surnames(
696        Tree $tree,
697        string $surn,
698        string $salpha,
699        bool $marnm,
700        bool $fams,
701        LocaleInterface $locale
702    ): array {
703        $collation = $this->localization_service->collation($locale);
704
705        $query = DB::table('name')
706            ->where('n_file', '=', $tree->id())
707            ->select([
708                new Expression('UPPER(n_surn /*! COLLATE ' . $collation . ' */) AS n_surn'),
709                new Expression('n_surname /*! COLLATE utf8_bin */ AS n_surname'),
710                new Expression('COUNT(*) AS total'),
711            ]);
712
713        $this->whereFamily($fams, $query);
714        $this->whereMarriedName($marnm, $query);
715
716        if ($surn !== '') {
717            $query->where('n_surn', '=', $surn);
718        } elseif ($salpha === ',') {
719            $query->where('n_surn', '=', '');
720        } elseif ($salpha === '@') {
721            $query->where('n_surn', '=', '@N.N.');
722        } elseif ($salpha !== '') {
723            $this->whereInitial($query, 'n_surn', $salpha, $locale);
724        } else {
725            // All surnames
726            $query->whereNotIn('n_surn', ['', '@N.N.']);
727        }
728        $query
729            ->groupBy(['n_surn'])
730            ->groupBy(['n_surname'])
731            ->orderBy('n_surname');
732
733        $list = [];
734
735        foreach ($query->get() as $row) {
736            $list[$row->n_surn][$row->n_surname] = (int) $row->total;
737        }
738
739        return $list;
740    }
741
742    /**
743     * Fetch a list of individuals with specified names
744     * To search for unknown names, use $surn="@N.N.", $salpha="@" or $galpha="@"
745     * To search for names with no surnames, use $salpha=","
746     *
747     * @param Tree            $tree
748     * @param string          $surn   if set, only fetch people with this surname
749     * @param string          $salpha if set, only fetch surnames starting with this letter
750     * @param string          $galpha if set, only fetch given names starting with this letter
751     * @param bool            $marnm  if set, include married names
752     * @param bool            $fams   if set, only fetch individuals with FAMS records
753     * @param LocaleInterface $locale
754     *
755     * @return Individual[]
756     */
757    public function individuals(
758        Tree $tree,
759        string $surn,
760        string $salpha,
761        string $galpha,
762        bool $marnm,
763        bool $fams,
764        LocaleInterface $locale
765    ): array {
766        $collation = $this->localization_service->collation($locale);
767
768        // Use specific collation for name fields.
769        $n_givn = $this->fieldWithCollation('n_givn', $collation);
770        $n_surn = $this->fieldWithCollation('n_surn', $collation);
771
772        $query = DB::table('individuals')
773            ->join('name', static function (JoinClause $join): void {
774                $join
775                    ->on('n_id', '=', 'i_id')
776                    ->on('n_file', '=', 'i_file');
777            })
778            ->where('i_file', '=', $tree->id())
779            ->select(['i_id AS xref', 'i_gedcom AS gedcom', 'n_givn', 'n_surn']);
780
781        $this->whereFamily($fams, $query);
782        $this->whereMarriedName($marnm, $query);
783
784        if ($surn) {
785            $query->where($n_surn, '=', $surn);
786        } elseif ($salpha === ',') {
787            $query->where($n_surn, '=', '');
788        } elseif ($salpha === '@') {
789            $query->where($n_surn, '=', '@N.N.');
790        } elseif ($salpha) {
791            $this->whereInitial($query, 'n_surn', $salpha, $locale);
792        } else {
793            // All surnames
794            $query->whereNotIn($n_surn, ['', '@N.N.']);
795        }
796        if ($galpha) {
797            $this->whereInitial($query, 'n_givn', $galpha, $locale);
798        }
799
800        $query
801            ->orderBy(new Expression("CASE n_surn WHEN '@N.N.' THEN 1 ELSE 0 END"))
802            ->orderBy($n_surn)
803            ->orderBy(new Expression("CASE n_givn WHEN '@N.N.' THEN 1 ELSE 0 END"))
804            ->orderBy($n_givn);
805
806        $list = [];
807        $rows = $query->get();
808
809        foreach ($rows as $row) {
810            $individual = Registry::individualFactory()->make($row->xref, $tree, $row->gedcom);
811            assert($individual instanceof Individual);
812
813            // The name from the database may be private - check the filtered list...
814            foreach ($individual->getAllNames() as $n => $name) {
815                if ($name['givn'] === $row->n_givn && $name['surn'] === $row->n_surn) {
816                    $individual->setPrimaryName($n);
817                    // We need to clone $individual, as we may have multiple references to the
818                    // same individual in this list, and the "primary name" would otherwise
819                    // be shared amongst all of them.
820                    $list[] = clone $individual;
821                    break;
822                }
823            }
824        }
825
826        return $list;
827    }
828
829    /**
830     * Fetch a list of families with specified names
831     * To search for unknown names, use $surn="@N.N.", $salpha="@" or $galpha="@"
832     * To search for names with no surnames, use $salpha=","
833     *
834     * @param Tree            $tree
835     * @param string          $surn   if set, only fetch people with this surname
836     * @param string          $salpha if set, only fetch surnames starting with this letter
837     * @param string          $galpha if set, only fetch given names starting with this letter
838     * @param bool            $marnm  if set, include married names
839     * @param LocaleInterface $locale
840     *
841     * @return Family[]
842     */
843    public function families(Tree $tree, $surn, $salpha, $galpha, $marnm, LocaleInterface $locale): array
844    {
845        $list = [];
846        foreach ($this->individuals($tree, $surn, $salpha, $galpha, $marnm, true, $locale) as $indi) {
847            foreach ($indi->spouseFamilies() as $family) {
848                $list[$family->xref()] = $family;
849            }
850        }
851        usort($list, GedcomRecord::nameComparator());
852
853        return $list;
854    }
855
856    /**
857     * Use MySQL-specific comments so we can run these queries on other RDBMS.
858     *
859     * @param string $field
860     * @param string $collation
861     *
862     * @return Expression
863     */
864    protected function fieldWithCollation(string $field, string $collation): Expression
865    {
866        return new Expression($field . ' /*! COLLATE ' . $collation . ' */');
867    }
868
869    /**
870     * Modify a query to restrict a field to a given initial letter.
871     * Take account of digraphs, equialent letters, etc.
872     *
873     * @param Builder         $query
874     * @param string          $field
875     * @param string          $letter
876     * @param LocaleInterface $locale
877     *
878     * @return void
879     */
880    protected function whereInitial(
881        Builder $query,
882        string $field,
883        string $letter,
884        LocaleInterface $locale
885    ): void {
886        $collation = $this->localization_service->collation($locale);
887
888        // Use MySQL-specific comments so we can run these queries on other RDBMS.
889        $field_with_collation = $this->fieldWithCollation($field, $collation);
890
891        switch ($locale->languageTag()) {
892            case 'cs':
893                $this->whereInitialCzech($query, $field_with_collation, $letter);
894                break;
895
896            case 'da':
897            case 'nb':
898            case 'nn':
899                $this->whereInitialNorwegian($query, $field_with_collation, $letter);
900                break;
901
902            case 'sv':
903            case 'fi':
904                $this->whereInitialSwedish($query, $field_with_collation, $letter);
905                break;
906
907            case 'hu':
908                $this->whereInitialHungarian($query, $field_with_collation, $letter);
909                break;
910
911            case 'nl':
912                $this->whereInitialDutch($query, $field_with_collation, $letter);
913                break;
914
915            default:
916                $query->where($field_with_collation, 'LIKE', '\\' . $letter . '%');
917        }
918    }
919
920    /**
921     * @param Builder    $query
922     * @param Expression $field
923     * @param string     $letter
924     */
925    protected function whereInitialCzech(Builder $query, Expression $field, string $letter): void
926    {
927        if ($letter === 'C') {
928            $query->where($field, 'LIKE', 'C%')->where($field, 'NOT LIKE', 'CH%');
929        } else {
930            $query->where($field, 'LIKE', '\\' . $letter . '%');
931        }
932    }
933
934    /**
935     * @param Builder    $query
936     * @param Expression $field
937     * @param string     $letter
938     */
939    protected function whereInitialDutch(Builder $query, Expression $field, string $letter): void
940    {
941        if ($letter === 'I') {
942            $query->where($field, 'LIKE', 'I%')->where($field, 'NOT LIKE', 'IJ%');
943        } else {
944            $query->where($field, 'LIKE', '\\' . $letter . '%');
945        }
946    }
947
948    /**
949     * Hungarian has many digraphs and trigraphs, so exclude these from prefixes.
950     *
951     * @param Builder    $query
952     * @param Expression $field
953     * @param string     $letter
954     */
955    protected function whereInitialHungarian(Builder $query, Expression $field, string $letter): void
956    {
957        switch ($letter) {
958            case 'C':
959                $query->where($field, 'LIKE', 'C%')->where($field, 'NOT LIKE', 'CS%');
960                break;
961
962            case 'D':
963                $query->where($field, 'LIKE', 'D%')->where($field, 'NOT LIKE', 'DZ%');
964                break;
965
966            case 'DZ':
967                $query->where($field, 'LIKE', 'DZ%')->where($field, 'NOT LIKE', 'DZS%');
968                break;
969
970            case 'G':
971                $query->where($field, 'LIKE', 'G%')->where($field, 'NOT LIKE', 'GY%');
972                break;
973
974            case 'L':
975                $query->where($field, 'LIKE', 'L%')->where($field, 'NOT LIKE', 'LY%');
976                break;
977
978            case 'N':
979                $query->where($field, 'LIKE', 'N%')->where($field, 'NOT LIKE', 'NY%');
980                break;
981
982            case 'S':
983                $query->where($field, 'LIKE', 'S%')->where($field, 'NOT LIKE', 'SZ%');
984                break;
985
986            case 'T':
987                $query->where($field, 'LIKE', 'T%')->where($field, 'NOT LIKE', 'TY%');
988                break;
989
990            case 'Z':
991                $query->where($field, 'LIKE', 'Z%')->where($field, 'NOT LIKE', 'ZS%');
992                break;
993
994            default:
995                $query->where($field, 'LIKE', '\\' . $letter . '%');
996                break;
997        }
998    }
999
1000    /**
1001     * In Norwegian and Danish, AA gets listed under Å, NOT A
1002     *
1003     * @param Builder    $query
1004     * @param Expression $field
1005     * @param string     $letter
1006     */
1007    protected function whereInitialNorwegian(Builder $query, Expression $field, string $letter): void
1008    {
1009        switch ($letter) {
1010            case 'A':
1011                $query->where($field, 'LIKE', 'A%')->where($field, 'NOT LIKE', 'AA%');
1012                break;
1013
1014            case 'Å':
1015                $query->where(static function (Builder $query) use ($field): void {
1016                    $query
1017                        ->where($field, 'LIKE', 'Å%')
1018                        ->orWhere($field, 'LIKE', 'AA%');
1019                });
1020                break;
1021
1022            default:
1023                $query->where($field, 'LIKE', '\\' . $letter . '%');
1024                break;
1025        }
1026    }
1027
1028    /**
1029     * In Swedish and Finnish, AA gets listed under A, NOT Å (even though Swedish collation says they should).
1030     *
1031     * @param Builder    $query
1032     * @param Expression $field
1033     * @param string     $letter
1034     */
1035    protected function whereInitialSwedish(Builder $query, Expression $field, string $letter): void
1036    {
1037        if ($letter === 'Å') {
1038            $query->where($field, 'LIKE', 'Å%')->where($field, 'NOT LIKE', 'AA%');
1039        } else {
1040            $query->where($field, 'LIKE', '\\' . $letter . '%');
1041        }
1042    }
1043}
1044