xref: /webtrees/app/Services/SearchService.php (revision e0458bdc6fe08e8f4a459f05e98f137641a91ad1)
132cd2800SGreg Roach<?php
232cd2800SGreg Roach/**
332cd2800SGreg Roach * webtrees: online genealogy
432cd2800SGreg Roach * Copyright (C) 2019 webtrees development team
532cd2800SGreg Roach * This program is free software: you can redistribute it and/or modify
632cd2800SGreg Roach * it under the terms of the GNU General Public License as published by
732cd2800SGreg Roach * the Free Software Foundation, either version 3 of the License, or
832cd2800SGreg Roach * (at your option) any later version.
932cd2800SGreg Roach * This program is distributed in the hope that it will be useful,
1032cd2800SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
1132cd2800SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1232cd2800SGreg Roach * GNU General Public License for more details.
1332cd2800SGreg Roach * You should have received a copy of the GNU General Public License
1432cd2800SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
1532cd2800SGreg Roach */
1632cd2800SGreg Roachdeclare(strict_types=1);
1732cd2800SGreg Roach
1832cd2800SGreg Roachnamespace Fisharebest\Webtrees\Services;
1932cd2800SGreg Roach
2032cd2800SGreg Roachuse Closure;
21dfb2cda2SGreg Roachuse Fisharebest\Webtrees\Date;
2252a8ef61SGreg Roachuse Fisharebest\Webtrees\Exceptions\InternalServerErrorException;
2332cd2800SGreg Roachuse Fisharebest\Webtrees\Family;
24a7a24840SGreg Roachuse Fisharebest\Webtrees\Gedcom;
2532cd2800SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
2652a8ef61SGreg Roachuse Fisharebest\Webtrees\I18N;
2732cd2800SGreg Roachuse Fisharebest\Webtrees\Individual;
2832cd2800SGreg Roachuse Fisharebest\Webtrees\Media;
2932cd2800SGreg Roachuse Fisharebest\Webtrees\Note;
30b68caec6SGreg Roachuse Fisharebest\Webtrees\Place;
3132cd2800SGreg Roachuse Fisharebest\Webtrees\Repository;
322d686e68SGreg Roachuse Fisharebest\Webtrees\Soundex;
3332cd2800SGreg Roachuse Fisharebest\Webtrees\Source;
3432cd2800SGreg Roachuse Fisharebest\Webtrees\Tree;
3532cd2800SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
3632cd2800SGreg Roachuse Illuminate\Database\Query\Builder;
37a7a24840SGreg Roachuse Illuminate\Database\Query\Expression;
3832cd2800SGreg Roachuse Illuminate\Database\Query\JoinClause;
3932cd2800SGreg Roachuse Illuminate\Support\Collection;
4032cd2800SGreg Roachuse stdClass;
41a7a24840SGreg Roachuse function mb_stripos;
4232cd2800SGreg Roach
4332cd2800SGreg Roach/**
4432cd2800SGreg Roach * Search trees for genealogy records.
4532cd2800SGreg Roach */
4632cd2800SGreg Roachclass SearchService
4732cd2800SGreg Roach{
48a7a24840SGreg Roach    /**
49a7a24840SGreg Roach     * @param Tree[]   $trees
50a7a24840SGreg Roach     * @param string[] $search
51a7a24840SGreg Roach     *
5254c7f8dfSGreg Roach     * @return Collection
5354c7f8dfSGreg Roach     * @return Family[]
54a7a24840SGreg Roach     */
55a7a24840SGreg Roach    public function searchFamilies(array $trees, array $search): Collection
56a7a24840SGreg Roach    {
57a7a24840SGreg Roach        $query = DB::table('families');
58a7a24840SGreg Roach
59a7a24840SGreg Roach        $this->whereTrees($query, 'f_file', $trees);
60a7a24840SGreg Roach        $this->whereSearch($query, 'f_gedcom', $search);
61a7a24840SGreg Roach
62a7a24840SGreg Roach        return $query
63a7a24840SGreg Roach            ->get()
6452a8ef61SGreg Roach            ->each($this->rowLimiter())
65a7a24840SGreg Roach            ->map(Family::rowMapper())
66a7a24840SGreg Roach            ->filter(GedcomRecord::accessFilter())
677f5fa3c2SGreg Roach            ->filter($this->rawGedcomFilter($search));
68a7a24840SGreg Roach    }
69a7a24840SGreg Roach
7032cd2800SGreg Roach    /**
7132cd2800SGreg Roach     * Search for families by name.
7232cd2800SGreg Roach     *
73a7a24840SGreg Roach     * @param Tree[]   $trees
74a7a24840SGreg Roach     * @param string[] $search
7532cd2800SGreg Roach     * @param int      $offset
7632cd2800SGreg Roach     * @param int      $limit
7732cd2800SGreg Roach     *
7854c7f8dfSGreg Roach     * @return Collection
7954c7f8dfSGreg Roach     * @return Family[]
8032cd2800SGreg Roach     */
81a7a24840SGreg Roach    public function searchFamilyNames(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection
8232cd2800SGreg Roach    {
8332cd2800SGreg Roach        $query = DB::table('families')
84a7a24840SGreg Roach            ->join('name AS husb_name', function (JoinClause $join): void {
8532cd2800SGreg Roach                $join
8632cd2800SGreg Roach                    ->on('husb_name.n_file', '=', 'families.f_file')
87a7a24840SGreg Roach                    ->on('husb_name.n_id', '=', 'families.f_husb');
8832cd2800SGreg Roach            })
89a7a24840SGreg Roach            ->join('name AS wife_name', function (JoinClause $join): void {
9032cd2800SGreg Roach                $join
9132cd2800SGreg Roach                    ->on('wife_name.n_file', '=', 'families.f_file')
92a7a24840SGreg Roach                    ->on('wife_name.n_id', '=', 'families.f_wife');
9332cd2800SGreg Roach            })
94a7a24840SGreg Roach            ->where('wife_name.n_type', '<>', '_MARNM')
95a7a24840SGreg Roach            ->where('husb_name.n_type', '<>', '_MARNM');
96a7a24840SGreg Roach
97a7a24840SGreg Roach        $prefix = DB::connection()->getTablePrefix();
98436c1c02SGreg Roach        $field  = DB::raw($prefix . 'husb_name.n_full || ' . $prefix . 'wife_name.n_full');
99a7a24840SGreg Roach
100a7a24840SGreg Roach        $this->whereTrees($query, 'f_file', $trees);
101a7a24840SGreg Roach        $this->whereSearch($query, $field, $search);
102a7a24840SGreg Roach
103a7a24840SGreg Roach        $query
10432cd2800SGreg Roach            ->orderBy('husb_name.n_sort')
10532cd2800SGreg Roach            ->orderBy('wife_name.n_sort')
106c0804649SGreg Roach            ->select(['families.*', 'husb_name.n_sort', 'wife_name.n_sort'])
10732cd2800SGreg Roach            ->distinct();
10832cd2800SGreg Roach
109a7a24840SGreg Roach        return $this->paginateQuery($query, Family::rowMapper(), GedcomRecord::accessFilter(), $offset, $limit);
110a7a24840SGreg Roach    }
111a7a24840SGreg Roach
112a7a24840SGreg Roach    /**
113a7a24840SGreg Roach     * @param Tree[]   $trees
114a7a24840SGreg Roach     * @param string[] $search
115a7a24840SGreg Roach     *
11654c7f8dfSGreg Roach     * @return Collection
11754c7f8dfSGreg Roach     * @return Individual[]
118a7a24840SGreg Roach     */
119a7a24840SGreg Roach    public function searchIndividuals(array $trees, array $search): Collection
120a7a24840SGreg Roach    {
121a7a24840SGreg Roach        $query = DB::table('individuals');
122a7a24840SGreg Roach
123a7a24840SGreg Roach        $this->whereTrees($query, 'i_file', $trees);
124a7a24840SGreg Roach        $this->whereSearch($query, 'i_gedcom', $search);
125a7a24840SGreg Roach
126a7a24840SGreg Roach        return $query
127a7a24840SGreg Roach            ->get()
12852a8ef61SGreg Roach            ->each($this->rowLimiter())
129a7a24840SGreg Roach            ->map(Individual::rowMapper())
130a7a24840SGreg Roach            ->filter(GedcomRecord::accessFilter())
1317f5fa3c2SGreg Roach            ->filter($this->rawGedcomFilter($search));
13232cd2800SGreg Roach    }
13332cd2800SGreg Roach
13432cd2800SGreg Roach    /**
13532cd2800SGreg Roach     * Search for individuals by name.
13632cd2800SGreg Roach     *
137a7a24840SGreg Roach     * @param Tree[]   $trees
138a7a24840SGreg Roach     * @param string[] $search
13932cd2800SGreg Roach     * @param int      $offset
14032cd2800SGreg Roach     * @param int      $limit
14132cd2800SGreg Roach     *
14254c7f8dfSGreg Roach     * @return Collection
14354c7f8dfSGreg Roach     * @return Individual[]
14432cd2800SGreg Roach     */
145a7a24840SGreg Roach    public function searchIndividualNames(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection
14632cd2800SGreg Roach    {
14732cd2800SGreg Roach        $query = DB::table('individuals')
148a7a24840SGreg Roach            ->join('name', function (JoinClause $join): void {
14932cd2800SGreg Roach                $join
15032cd2800SGreg Roach                    ->on('name.n_file', '=', 'individuals.i_file')
151a7a24840SGreg Roach                    ->on('name.n_id', '=', 'individuals.i_id');
15232cd2800SGreg Roach            })
153e84cf2deSGreg Roach            ->orderBy('n_sort')
154*e0458bdcSGreg Roach            ->select(['individuals.*', 'n_num']);
15532cd2800SGreg Roach
156a7a24840SGreg Roach        $this->whereTrees($query, 'i_file', $trees);
157a7a24840SGreg Roach        $this->whereSearch($query, 'n_full', $search);
158a7a24840SGreg Roach
159a7a24840SGreg Roach        return $this->paginateQuery($query, Individual::rowMapper(), GedcomRecord::accessFilter(), $offset, $limit);
16032cd2800SGreg Roach    }
16132cd2800SGreg Roach
16232cd2800SGreg Roach    /**
16332cd2800SGreg Roach     * Search for media objects.
16432cd2800SGreg Roach     *
165a7a24840SGreg Roach     * @param Tree[]   $trees
166a7a24840SGreg Roach     * @param string[] $search
16732cd2800SGreg Roach     * @param int      $offset
16832cd2800SGreg Roach     * @param int      $limit
16932cd2800SGreg Roach     *
17054c7f8dfSGreg Roach     * @return Collection
17154c7f8dfSGreg Roach     * @return Media[]
17232cd2800SGreg Roach     */
173a7a24840SGreg Roach    public function searchMedia(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection
17432cd2800SGreg Roach    {
175a7a24840SGreg Roach        $query = DB::table('media');
17632cd2800SGreg Roach
177a7a24840SGreg Roach        $this->whereTrees($query, 'media.m_file', $trees);
178a7a24840SGreg Roach        $this->whereSearch($query, 'm_gedcom', $search);
179a7a24840SGreg Roach
180a7a24840SGreg Roach        return $this->paginateQuery($query, Media::rowMapper(), GedcomRecord::accessFilter(), $offset, $limit);
18132cd2800SGreg Roach    }
18232cd2800SGreg Roach
18332cd2800SGreg Roach    /**
18432cd2800SGreg Roach     * Search for notes.
18532cd2800SGreg Roach     *
186a7a24840SGreg Roach     * @param Tree[]   $trees
187a7a24840SGreg Roach     * @param string[] $search
18832cd2800SGreg Roach     * @param int      $offset
18932cd2800SGreg Roach     * @param int      $limit
19032cd2800SGreg Roach     *
19154c7f8dfSGreg Roach     * @return Collection
19254c7f8dfSGreg Roach     * @return Note[]
19332cd2800SGreg Roach     */
194a7a24840SGreg Roach    public function searchNotes(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection
19532cd2800SGreg Roach    {
19632cd2800SGreg Roach        $query = DB::table('other')
197a7a24840SGreg Roach            ->where('o_type', '=', 'NOTE');
19832cd2800SGreg Roach
199a7a24840SGreg Roach        $this->whereTrees($query, 'o_file', $trees);
200a7a24840SGreg Roach        $this->whereSearch($query, 'o_gedcom', $search);
201a7a24840SGreg Roach
202a7a24840SGreg Roach        return $this->paginateQuery($query, Note::rowMapper(), GedcomRecord::accessFilter(), $offset, $limit);
20332cd2800SGreg Roach    }
20432cd2800SGreg Roach
20532cd2800SGreg Roach    /**
20632cd2800SGreg Roach     * Search for repositories.
20732cd2800SGreg Roach     *
208a7a24840SGreg Roach     * @param Tree[]   $trees
209a7a24840SGreg Roach     * @param string[] $search
21032cd2800SGreg Roach     * @param int      $offset
21132cd2800SGreg Roach     * @param int      $limit
21232cd2800SGreg Roach     *
21354c7f8dfSGreg Roach     * @return Collection
21454c7f8dfSGreg Roach     * @return Repository[]
21532cd2800SGreg Roach     */
216a7a24840SGreg Roach    public function searchRepositories(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection
21732cd2800SGreg Roach    {
21832cd2800SGreg Roach        $query = DB::table('other')
219a7a24840SGreg Roach            ->where('o_type', '=', 'REPO');
22032cd2800SGreg Roach
221a7a24840SGreg Roach        $this->whereTrees($query, 'o_file', $trees);
222a7a24840SGreg Roach        $this->whereSearch($query, 'o_gedcom', $search);
223a7a24840SGreg Roach
224a7a24840SGreg Roach        return $this->paginateQuery($query, Repository::rowMapper(), GedcomRecord::accessFilter(), $offset, $limit);
22532cd2800SGreg Roach    }
22632cd2800SGreg Roach
22732cd2800SGreg Roach    /**
228a7a24840SGreg Roach     * Search for sources.
22932cd2800SGreg Roach     *
230a7a24840SGreg Roach     * @param Tree[]   $trees
231a7a24840SGreg Roach     * @param string[] $search
23232cd2800SGreg Roach     * @param int      $offset
23332cd2800SGreg Roach     * @param int      $limit
23432cd2800SGreg Roach     *
23554c7f8dfSGreg Roach     * @return Collection
23654c7f8dfSGreg Roach     * @return Source[]
23732cd2800SGreg Roach     */
238a7a24840SGreg Roach    public function searchSources(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection
239a7a24840SGreg Roach    {
240a7a24840SGreg Roach        $query = DB::table('sources');
241a7a24840SGreg Roach
242a7a24840SGreg Roach        $this->whereTrees($query, 's_file', $trees);
243a7a24840SGreg Roach        $this->whereSearch($query, 's_gedcom', $search);
244a7a24840SGreg Roach
245a7a24840SGreg Roach        return $this->paginateQuery($query, Source::rowMapper(), GedcomRecord::accessFilter(), $offset, $limit);
246a7a24840SGreg Roach    }
247a7a24840SGreg Roach
248a7a24840SGreg Roach    /**
249a7a24840SGreg Roach     * Search for sources by name.
250a7a24840SGreg Roach     *
251a7a24840SGreg Roach     * @param Tree[]   $trees
252a7a24840SGreg Roach     * @param string[] $search
253a7a24840SGreg Roach     * @param int      $offset
254a7a24840SGreg Roach     * @param int      $limit
255a7a24840SGreg Roach     *
25654c7f8dfSGreg Roach     * @return Collection
25754c7f8dfSGreg Roach     * @return Source[]
258a7a24840SGreg Roach     */
259a7a24840SGreg Roach    public function searchSourcesByName(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection
26032cd2800SGreg Roach    {
26132cd2800SGreg Roach        $query = DB::table('sources')
262c0804649SGreg Roach            ->orderBy('s_name');
26332cd2800SGreg Roach
264a7a24840SGreg Roach        $this->whereTrees($query, 's_file', $trees);
265a7a24840SGreg Roach        $this->whereSearch($query, 's_name', $search);
266a7a24840SGreg Roach
267a7a24840SGreg Roach        return $this->paginateQuery($query, Source::rowMapper(), GedcomRecord::accessFilter(), $offset, $limit);
26832cd2800SGreg Roach    }
26932cd2800SGreg Roach
27032cd2800SGreg Roach    /**
27132cd2800SGreg Roach     * Search for submitters.
27232cd2800SGreg Roach     *
273a7a24840SGreg Roach     * @param Tree[]   $trees
274a7a24840SGreg Roach     * @param string[] $search
27532cd2800SGreg Roach     * @param int      $offset
27632cd2800SGreg Roach     * @param int      $limit
27732cd2800SGreg Roach     *
27854c7f8dfSGreg Roach     * @return Collection
27954c7f8dfSGreg Roach     * @return GedcomRecord[]
28032cd2800SGreg Roach     */
281a7a24840SGreg Roach    public function searchSubmitters(array $trees, array $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection
28232cd2800SGreg Roach    {
28332cd2800SGreg Roach        $query = DB::table('other')
284a7a24840SGreg Roach            ->where('o_type', '=', 'SUBM');
28532cd2800SGreg Roach
286a7a24840SGreg Roach        $this->whereTrees($query, 'o_file', $trees);
287a7a24840SGreg Roach        $this->whereSearch($query, 'o_gedcom', $search);
288a7a24840SGreg Roach
289a7a24840SGreg Roach        return $this->paginateQuery($query, GedcomRecord::rowMapper(), GedcomRecord::accessFilter(), $offset, $limit);
29032cd2800SGreg Roach    }
29132cd2800SGreg Roach
29232cd2800SGreg Roach    /**
293b68caec6SGreg Roach     * Search for places.
294b68caec6SGreg Roach     *
295b68caec6SGreg Roach     * @param Tree   $tree
296b68caec6SGreg Roach     * @param string $search
297b68caec6SGreg Roach     * @param int    $offset
298b68caec6SGreg Roach     * @param int    $limit
299b68caec6SGreg Roach     *
30054c7f8dfSGreg Roach     * @return Collection
30154c7f8dfSGreg Roach     * @return Place[]
302b68caec6SGreg Roach     */
303b68caec6SGreg Roach    public function searchPlaces(Tree $tree, string $search, int $offset = 0, int $limit = PHP_INT_MAX): Collection
304b68caec6SGreg Roach    {
305b68caec6SGreg Roach        $query = DB::table('places AS p0')
306b68caec6SGreg Roach            ->where('p0.p_file', '=', $tree->id())
307b68caec6SGreg Roach            ->leftJoin('places AS p1', 'p1.p_id', '=', 'p0.p_parent_id')
308b68caec6SGreg Roach            ->leftJoin('places AS p2', 'p2.p_id', '=', 'p1.p_parent_id')
309b68caec6SGreg Roach            ->leftJoin('places AS p3', 'p3.p_id', '=', 'p2.p_parent_id')
310b68caec6SGreg Roach            ->leftJoin('places AS p4', 'p4.p_id', '=', 'p3.p_parent_id')
311b68caec6SGreg Roach            ->leftJoin('places AS p5', 'p5.p_id', '=', 'p4.p_parent_id')
312b68caec6SGreg Roach            ->leftJoin('places AS p6', 'p6.p_id', '=', 'p5.p_parent_id')
313b68caec6SGreg Roach            ->leftJoin('places AS p7', 'p7.p_id', '=', 'p6.p_parent_id')
314b68caec6SGreg Roach            ->leftJoin('places AS p8', 'p8.p_id', '=', 'p7.p_parent_id')
315b68caec6SGreg Roach            ->orderBy('p0.p_place')
316b68caec6SGreg Roach            ->orderBy('p1.p_place')
317b68caec6SGreg Roach            ->orderBy('p2.p_place')
318b68caec6SGreg Roach            ->orderBy('p3.p_place')
319b68caec6SGreg Roach            ->orderBy('p4.p_place')
320b68caec6SGreg Roach            ->orderBy('p5.p_place')
321b68caec6SGreg Roach            ->orderBy('p6.p_place')
322b68caec6SGreg Roach            ->orderBy('p7.p_place')
323b68caec6SGreg Roach            ->orderBy('p8.p_place')
324b68caec6SGreg Roach            ->select([
325b68caec6SGreg Roach                'p0.p_place AS place0',
326b68caec6SGreg Roach                'p1.p_place AS place1',
327b68caec6SGreg Roach                'p2.p_place AS place2',
328b68caec6SGreg Roach                'p3.p_place AS place3',
329b68caec6SGreg Roach                'p4.p_place AS place4',
330b68caec6SGreg Roach                'p5.p_place AS place5',
331b68caec6SGreg Roach                'p6.p_place AS place6',
332b68caec6SGreg Roach                'p7.p_place AS place7',
333b68caec6SGreg Roach                'p8.p_place AS place8',
334b68caec6SGreg Roach            ]);
335b68caec6SGreg Roach
336b68caec6SGreg Roach        // Filter each level of the hierarchy.
337b68caec6SGreg Roach        foreach (explode(',', $search, 9) as $level => $string) {
338b68caec6SGreg Roach            $query->whereContains('p' . $level . '.p_place', $string);
339b68caec6SGreg Roach        }
340b68caec6SGreg Roach
341b68caec6SGreg Roach        $row_mapper = function (stdClass $row) use ($tree): Place {
342b68caec6SGreg Roach            $place = implode(', ', array_filter((array) $row));
343b68caec6SGreg Roach
344b68caec6SGreg Roach            return new Place($place, $tree);
345b68caec6SGreg Roach        };
346b68caec6SGreg Roach
347a7a24840SGreg Roach        $filter = function (): bool {
348a7a24840SGreg Roach            return true;
349a7a24840SGreg Roach        };
350b68caec6SGreg Roach
351a7a24840SGreg Roach        return $this->paginateQuery($query, $row_mapper, $filter, $offset, $limit);
352a7a24840SGreg Roach    }
353b68caec6SGreg Roach
354b68caec6SGreg Roach    /**
355a5dbc5b2SGreg Roach     * @param Tree[]   $trees
356dfb2cda2SGreg Roach     * @param string[] $fields
357dfb2cda2SGreg Roach     * @param string[] $modifiers
358dfb2cda2SGreg Roach     *
35954c7f8dfSGreg Roach     * @return Collection
36054c7f8dfSGreg Roach     * @return Individual[]
361dfb2cda2SGreg Roach     */
362dfb2cda2SGreg Roach    public function searchIndividualsAdvanced(array $trees, array $fields, array $modifiers): Collection
363dfb2cda2SGreg Roach    {
364dfb2cda2SGreg Roach        $fields = array_filter($fields);
365dfb2cda2SGreg Roach
366dfb2cda2SGreg Roach        $query = DB::table('individuals')
367dfb2cda2SGreg Roach            ->select(['individuals.*'])
368dfb2cda2SGreg Roach            ->distinct();
369dfb2cda2SGreg Roach
370dfb2cda2SGreg Roach        $this->whereTrees($query, 'i_file', $trees);
371dfb2cda2SGreg Roach
372dfb2cda2SGreg Roach        // Join the following tables
373dfb2cda2SGreg Roach        $father_name   = false;
374dfb2cda2SGreg Roach        $mother_name   = false;
375dfb2cda2SGreg Roach        $spouse_family = false;
376dfb2cda2SGreg Roach        $indi_name     = false;
377dfb2cda2SGreg Roach        $indi_date     = false;
378dfb2cda2SGreg Roach        $fam_date      = false;
379dfb2cda2SGreg Roach        $indi_plac     = false;
380dfb2cda2SGreg Roach        $fam_plac      = false;
381dfb2cda2SGreg Roach
382dfb2cda2SGreg Roach        foreach ($fields as $field_name => $field_value) {
383dfb2cda2SGreg Roach            if ($field_value !== '') {
384dfb2cda2SGreg Roach                if (substr($field_name, 0, 14) === 'FAMC:HUSB:NAME') {
385dfb2cda2SGreg Roach                    $father_name = true;
386dfb2cda2SGreg Roach                } elseif (substr($field_name, 0, 14) === 'FAMC:WIFE:NAME') {
387dfb2cda2SGreg Roach                    $mother_name = true;
388dfb2cda2SGreg Roach                } elseif (substr($field_name, 0, 4) === 'NAME') {
389dfb2cda2SGreg Roach                    $indi_name = true;
390dfb2cda2SGreg Roach                } elseif (strpos($field_name, ':DATE') !== false) {
391dfb2cda2SGreg Roach                    if (substr($field_name, 0, 4) === 'FAMS') {
392dfb2cda2SGreg Roach                        $fam_date      = true;
393dfb2cda2SGreg Roach                        $spouse_family = true;
394dfb2cda2SGreg Roach                    } else {
395dfb2cda2SGreg Roach                        $indi_date = true;
396dfb2cda2SGreg Roach                    }
397dfb2cda2SGreg Roach                } elseif (strpos($field_name, ':PLAC') !== false) {
398dfb2cda2SGreg Roach                    if (substr($field_name, 0, 4) === 'FAMS') {
399dfb2cda2SGreg Roach                        $fam_plac      = true;
400dfb2cda2SGreg Roach                        $spouse_family = true;
401dfb2cda2SGreg Roach                    } else {
402dfb2cda2SGreg Roach                        $indi_plac = true;
403dfb2cda2SGreg Roach                    }
404dfb2cda2SGreg Roach                } elseif ($field_name === 'FAMS:NOTE') {
405dfb2cda2SGreg Roach                    $spouse_family = true;
406dfb2cda2SGreg Roach                }
407dfb2cda2SGreg Roach            }
408dfb2cda2SGreg Roach        }
409dfb2cda2SGreg Roach
410dfb2cda2SGreg Roach        if ($father_name || $mother_name) {
411dfb2cda2SGreg Roach            $query->join('link AS l1', function (JoinClause $join): void {
412dfb2cda2SGreg Roach                $join
413dfb2cda2SGreg Roach                    ->on('l1.l_file', '=', 'individuals.i_file')
414dfb2cda2SGreg Roach                    ->on('l1.l_from', '=', 'individuals.i_id')
415dfb2cda2SGreg Roach                    ->where('l1.l_type', '=', 'FAMC');
416dfb2cda2SGreg Roach            });
417dfb2cda2SGreg Roach
418dfb2cda2SGreg Roach            if ($father_name) {
419dfb2cda2SGreg Roach                $query->join('link AS l2', function (JoinClause $join): void {
420dfb2cda2SGreg Roach                    $join
421dfb2cda2SGreg Roach                        ->on('l2.l_file', '=', 'l1.l_file')
422dfb2cda2SGreg Roach                        ->on('l2.l_from', '=', 'l1.l_to')
423dfb2cda2SGreg Roach                        ->where('l2.l_type', '=', 'HUSB');
424dfb2cda2SGreg Roach                });
425dfb2cda2SGreg Roach                $query->join('name AS father_name', function (JoinClause $join): void {
426dfb2cda2SGreg Roach                    $join
427dfb2cda2SGreg Roach                        ->on('father_name.n_file', '=', 'l2.l_file')
428dfb2cda2SGreg Roach                        ->on('father_name.n_id', '=', 'l2.l_to');
429dfb2cda2SGreg Roach                });
430dfb2cda2SGreg Roach            }
431dfb2cda2SGreg Roach
432dfb2cda2SGreg Roach            if ($mother_name) {
433dfb2cda2SGreg Roach                $query->join('link AS l3', function (JoinClause $join): void {
434dfb2cda2SGreg Roach                    $join
435dfb2cda2SGreg Roach                        ->on('l3.l_file', '=', 'l1.l_file')
436dfb2cda2SGreg Roach                        ->on('l3.l_from', '=', 'l1.l_to')
437dfb2cda2SGreg Roach                        ->where('l3.l_type', '=', 'WIFE');
438dfb2cda2SGreg Roach                });
439dfb2cda2SGreg Roach                $query->join('name AS mother_name', function (JoinClause $join): void {
440dfb2cda2SGreg Roach                    $join
441dfb2cda2SGreg Roach                        ->on('mother_name.n_file', '=', 'l3.l_file')
442dfb2cda2SGreg Roach                        ->on('mother_name.n_id', '=', 'l3.l_to');
443dfb2cda2SGreg Roach                });
444dfb2cda2SGreg Roach            }
445dfb2cda2SGreg Roach        }
446dfb2cda2SGreg Roach
447dfb2cda2SGreg Roach        if ($spouse_family) {
448dfb2cda2SGreg Roach            $query->join('link AS l4', function (JoinClause $join): void {
449dfb2cda2SGreg Roach                $join
450dfb2cda2SGreg Roach                    ->on('l4.l_file', '=', 'individuals.i_file')
451dfb2cda2SGreg Roach                    ->on('l4.l_from', '=', 'individuals.i_id')
452dfb2cda2SGreg Roach                    ->where('l4.l_type', '=', 'FAMS');
453dfb2cda2SGreg Roach            });
454dfb2cda2SGreg Roach            $query->join('families AS spouse_families', function (JoinClause $join): void {
455dfb2cda2SGreg Roach                $join
456dfb2cda2SGreg Roach                    ->on('spouse_families.f_file', '=', 'l4.l_file')
457dfb2cda2SGreg Roach                    ->on('spouse_families.f_id', '=', 'l4.l_to');
458dfb2cda2SGreg Roach            });
459dfb2cda2SGreg Roach        }
460dfb2cda2SGreg Roach
461dfb2cda2SGreg Roach        if ($indi_name) {
462dfb2cda2SGreg Roach            $query->join('name AS individual_name', function (JoinClause $join): void {
463dfb2cda2SGreg Roach                $join
464dfb2cda2SGreg Roach                    ->on('individual_name.n_file', '=', 'individuals.i_file')
465dfb2cda2SGreg Roach                    ->on('individual_name.n_id', '=', 'individuals.i_id');
466dfb2cda2SGreg Roach            });
467dfb2cda2SGreg Roach        }
468dfb2cda2SGreg Roach
469dfb2cda2SGreg Roach        if ($indi_date) {
470dfb2cda2SGreg Roach            $query->join('dates AS individual_dates', function (JoinClause $join): void {
471dfb2cda2SGreg Roach                $join
472dfb2cda2SGreg Roach                    ->on('individual_dates.d_file', '=', 'individuals.i_file')
473dfb2cda2SGreg Roach                    ->on('individual_dates.d_gid', '=', 'individuals.i_id');
474dfb2cda2SGreg Roach            });
475dfb2cda2SGreg Roach        }
476dfb2cda2SGreg Roach
477dfb2cda2SGreg Roach        if ($fam_date) {
478dfb2cda2SGreg Roach            $query->join('dates AS family_dates', function (JoinClause $join): void {
479dfb2cda2SGreg Roach                $join
480dfb2cda2SGreg Roach                    ->on('family_dates.d_file', '=', 'spouse_families.f_file')
481dfb2cda2SGreg Roach                    ->on('family_dates.d_gid', '=', 'spouse_families.f_id');
482dfb2cda2SGreg Roach            });
483dfb2cda2SGreg Roach        }
484dfb2cda2SGreg Roach
485dfb2cda2SGreg Roach        if ($indi_plac) {
486dfb2cda2SGreg Roach            $query->join('placelinks AS individual_placelinks', function (JoinClause $join): void {
487dfb2cda2SGreg Roach                $join
488dfb2cda2SGreg Roach                    ->on('individual_placelinks.pl_file', '=', 'individuals.i_file')
489dfb2cda2SGreg Roach                    ->on('individual_placelinks.pl_gid', '=', 'individuals.i_id');
490dfb2cda2SGreg Roach            });
491dfb2cda2SGreg Roach            $query->join('places AS individual_places', function (JoinClause $join): void {
492dfb2cda2SGreg Roach                $join
493dfb2cda2SGreg Roach                    ->on('individual_places.p_file', '=', 'individual_placelinks.pl_file')
494dfb2cda2SGreg Roach                    ->on('individual_places.p_id', '=', 'individual_placelinks.pl_p_id');
495dfb2cda2SGreg Roach            });
496dfb2cda2SGreg Roach        }
497dfb2cda2SGreg Roach
498dfb2cda2SGreg Roach        if ($fam_plac) {
499dfb2cda2SGreg Roach            $query->join('placelinks AS familyl_placelinks', function (JoinClause $join): void {
500dfb2cda2SGreg Roach                $join
501dfb2cda2SGreg Roach                    ->on('familyl_placelinks.pl_file', '=', 'individuals.i_file')
502dfb2cda2SGreg Roach                    ->on('familyl_placelinks.pl_gid', '=', 'individuals.i_id');
503dfb2cda2SGreg Roach            });
504dfb2cda2SGreg Roach            $query->join('places AS family_places', function (JoinClause $join): void {
505dfb2cda2SGreg Roach                $join
506dfb2cda2SGreg Roach                    ->on('family_places.p_file', '=', 'familyl_placelinks.pl_file')
507dfb2cda2SGreg Roach                    ->on('family_places.p_id', '=', 'familyl_placelinks.pl_p_id');
508dfb2cda2SGreg Roach            });
509dfb2cda2SGreg Roach        }
510dfb2cda2SGreg Roach
511dfb2cda2SGreg Roach        foreach ($fields as $field_name => $field_value) {
512dfb2cda2SGreg Roach            $parts = preg_split('/:/', $field_name . '::::');
513dfb2cda2SGreg Roach            if ($parts[0] === 'NAME') {
514dfb2cda2SGreg Roach                // NAME:*
515dfb2cda2SGreg Roach                switch ($parts[1]) {
516dfb2cda2SGreg Roach                    case 'GIVN':
517dfb2cda2SGreg Roach                        switch ($modifiers[$field_name]) {
518dfb2cda2SGreg Roach                            case 'EXACT':
519dfb2cda2SGreg Roach                                $query->where('individual_name.n_givn', '=', $field_value);
520dfb2cda2SGreg Roach                                break;
521dfb2cda2SGreg Roach                            case 'BEGINS':
522dfb2cda2SGreg Roach                                $query->where('individual_name.n_givn', 'LIKE', $field_value . '%');
523dfb2cda2SGreg Roach                                break;
524dfb2cda2SGreg Roach                            case 'CONTAINS':
525dfb2cda2SGreg Roach                                $query->where('individual_name.n_givn', 'LIKE', '%' . $field_value . '%');
526dfb2cda2SGreg Roach                                break;
527dfb2cda2SGreg Roach                            case 'SDX_STD':
528dfb2cda2SGreg Roach                                $sdx = Soundex::russell($field_value);
529dfb2cda2SGreg Roach                                if ($sdx !== '') {
530dfb2cda2SGreg Roach                                    $this->wherePhonetic($query, 'individual_name.n_soundex_givn_std', $sdx);
531dfb2cda2SGreg Roach                                } else {
532dfb2cda2SGreg Roach                                    // No phonetic content? Use a substring match
533dfb2cda2SGreg Roach                                    $query->where('individual_name.n_givn', 'LIKE', '%' . $field_value . '%');
534dfb2cda2SGreg Roach                                }
535dfb2cda2SGreg Roach                                break;
536dfb2cda2SGreg Roach                            case 'SDX': // SDX uses DM by default.
537dfb2cda2SGreg Roach                            case 'SDX_DM':
538dfb2cda2SGreg Roach                                $sdx = Soundex::daitchMokotoff($field_value);
539dfb2cda2SGreg Roach                                if ($sdx !== '') {
540dfb2cda2SGreg Roach                                    $this->wherePhonetic($query, 'individual_name.n_soundex_givn_dm', $sdx);
541dfb2cda2SGreg Roach                                } else {
542dfb2cda2SGreg Roach                                    // No phonetic content? Use a substring match
543dfb2cda2SGreg Roach                                    $query->where('individual_name.n_givn', 'LIKE', '%' . $field_value . '%');
544dfb2cda2SGreg Roach                                }
545dfb2cda2SGreg Roach                                break;
546dfb2cda2SGreg Roach                        }
547dfb2cda2SGreg Roach                        break;
548dfb2cda2SGreg Roach                    case 'SURN':
549dfb2cda2SGreg Roach                        switch ($modifiers[$field_name]) {
550dfb2cda2SGreg Roach                            case 'EXACT':
551dfb2cda2SGreg Roach                                $query->where('individual_name.n_surn', '=', $field_value);
552dfb2cda2SGreg Roach                                break;
553dfb2cda2SGreg Roach                            case 'BEGINS':
554dfb2cda2SGreg Roach                                $query->where('individual_name.n_surn', 'LIKE', $field_value . '%');
555dfb2cda2SGreg Roach                                break;
556dfb2cda2SGreg Roach                            case 'CONTAINS':
557dfb2cda2SGreg Roach                                $query->where('individual_name.n_surn', 'LIKE', '%' . $field_value . '%');
558dfb2cda2SGreg Roach                                break;
559dfb2cda2SGreg Roach                            case 'SDX_STD':
560dfb2cda2SGreg Roach                                $sdx = Soundex::russell($field_value);
561dfb2cda2SGreg Roach                                if ($sdx !== '') {
562dfb2cda2SGreg Roach                                    $this->wherePhonetic($query, 'individual_name.n_soundex_surn_std', $sdx);
563dfb2cda2SGreg Roach                                } else {
564dfb2cda2SGreg Roach                                    // No phonetic content? Use a substring match
565dfb2cda2SGreg Roach                                    $query->where('individual_name.n_surn', 'LIKE', '%' . $field_value . '%');
566dfb2cda2SGreg Roach                                }
567dfb2cda2SGreg Roach                                break;
568dfb2cda2SGreg Roach                            case 'SDX': // SDX uses DM by default.
569dfb2cda2SGreg Roach                            case 'SDX_DM':
570dfb2cda2SGreg Roach                                $sdx = Soundex::daitchMokotoff($field_value);
571dfb2cda2SGreg Roach                                if ($sdx !== '') {
572dfb2cda2SGreg Roach                                    $this->wherePhonetic($query, 'individual_name.n_soundex_surn_dm', $sdx);
573dfb2cda2SGreg Roach                                } else {
574dfb2cda2SGreg Roach                                    // No phonetic content? Use a substring match
575dfb2cda2SGreg Roach                                    $query->where('individual_name.n_surn', 'LIKE', '%' . $field_value . '%');
576dfb2cda2SGreg Roach                                }
577dfb2cda2SGreg Roach                                break;
578dfb2cda2SGreg Roach                        }
579dfb2cda2SGreg Roach                        break;
580dfb2cda2SGreg Roach                    case 'NICK':
581dfb2cda2SGreg Roach                    case '_MARNM':
582dfb2cda2SGreg Roach                    case '_HEB':
583dfb2cda2SGreg Roach                    case '_AKA':
584dfb2cda2SGreg Roach                        $query
585dfb2cda2SGreg Roach                            ->where('individual_name', '=', $parts[1])
586dfb2cda2SGreg Roach                            ->where('individual_name', 'LIKE', '%' . $field_value . '%');
587dfb2cda2SGreg Roach                        break;
588dfb2cda2SGreg Roach                }
589dfb2cda2SGreg Roach                unset($fields[$field_name]);
590dfb2cda2SGreg Roach            } elseif ($parts[1] === 'DATE') {
591dfb2cda2SGreg Roach                // *:DATE
592dfb2cda2SGreg Roach                $date = new Date($field_value);
593dfb2cda2SGreg Roach                if ($date->isOK()) {
594dfb2cda2SGreg Roach                    $delta = 365 * ($modifiers[$field_name] ?? 0);
595dfb2cda2SGreg Roach                    $query
596dfb2cda2SGreg Roach                        ->where('individual_dates.d_fact', '=', $parts[0])
597dfb2cda2SGreg Roach                        ->where('individual_dates.d_julianday1', '>=', $date->minimumJulianDay() - $delta)
598dfb2cda2SGreg Roach                        ->where('individual_dates.d_julianday2', '<=', $date->minimumJulianDay() + $delta);
599dfb2cda2SGreg Roach                }
600dfb2cda2SGreg Roach                unset($fields[$field_name]);
601dfb2cda2SGreg Roach            } elseif ($parts[0] === 'FAMS' && $parts[2] === 'DATE') {
602dfb2cda2SGreg Roach                // FAMS:*:DATE
603dfb2cda2SGreg Roach                $date = new Date($field_value);
604dfb2cda2SGreg Roach                if ($date->isOK()) {
605dfb2cda2SGreg Roach                    $delta = 365 * $modifiers[$field_name];
606dfb2cda2SGreg Roach                    $query
607dfb2cda2SGreg Roach                        ->where('family_dates.d_fact', '=', $parts[1])
608dfb2cda2SGreg Roach                        ->where('family_dates.d_julianday1', '>=', $date->minimumJulianDay() - $delta)
609dfb2cda2SGreg Roach                        ->where('family_dates.d_julianday2', '<=', $date->minimumJulianDay() + $delta);
610dfb2cda2SGreg Roach                }
611dfb2cda2SGreg Roach                unset($fields[$field_name]);
612dfb2cda2SGreg Roach            } elseif ($parts[1] === 'PLAC') {
613dfb2cda2SGreg Roach                // *:PLAC
614dfb2cda2SGreg Roach                // SQL can only link a place to a person/family, not to an event.
615dfb2cda2SGreg Roach                $query->where('individual_places.p_place', 'LIKE', '%' . $field_value . '%');
616dfb2cda2SGreg Roach            } elseif ($parts[0] === 'FAMS' && $parts[2] === 'PLAC') {
617dfb2cda2SGreg Roach                // FAMS:*:PLAC
618dfb2cda2SGreg Roach                // SQL can only link a place to a person/family, not to an event.
619dfb2cda2SGreg Roach                $query->where('family_places.p_place', 'LIKE', '%' . $field_value . '%');
620dfb2cda2SGreg Roach            } elseif ($parts[0] === 'FAMC' && $parts[2] === 'NAME') {
621dfb2cda2SGreg Roach                $table = $parts[1] === 'HUSB' ? 'father_name' : 'mother_name';
622dfb2cda2SGreg Roach                // NAME:*
623dfb2cda2SGreg Roach                switch ($parts[3]) {
624dfb2cda2SGreg Roach                    case 'GIVN':
625dfb2cda2SGreg Roach                        switch ($modifiers[$field_name]) {
626dfb2cda2SGreg Roach                            case 'EXACT':
627dfb2cda2SGreg Roach                                $query->where($table . '.n_givn', '=', $field_value);
628dfb2cda2SGreg Roach                                break;
629dfb2cda2SGreg Roach                            case 'BEGINS':
630dfb2cda2SGreg Roach                                $query->where($table . '.n_givn', 'LIKE', $field_value . '%');
631dfb2cda2SGreg Roach                                break;
632dfb2cda2SGreg Roach                            case 'CONTAINS':
633dfb2cda2SGreg Roach                                $query->where($table . '.n_givn', 'LIKE', '%' . $field_value . '%');
634dfb2cda2SGreg Roach                                break;
635dfb2cda2SGreg Roach                            case 'SDX_STD':
636dfb2cda2SGreg Roach                                $sdx = Soundex::russell($field_value);
637dfb2cda2SGreg Roach                                if ($sdx !== '') {
638dfb2cda2SGreg Roach                                    $this->wherePhonetic($query, $table . '.n_soundex_givn_std', $sdx);
639dfb2cda2SGreg Roach                                } else {
640dfb2cda2SGreg Roach                                    // No phonetic content? Use a substring match
641dfb2cda2SGreg Roach                                    $query->where($table . '.n_givn', 'LIKE', '%' . $field_value . '%');
642dfb2cda2SGreg Roach                                }
643dfb2cda2SGreg Roach                                break;
644dfb2cda2SGreg Roach                            case 'SDX': // SDX uses DM by default.
645dfb2cda2SGreg Roach                            case 'SDX_DM':
646dfb2cda2SGreg Roach                                $sdx = Soundex::daitchMokotoff($field_value);
647dfb2cda2SGreg Roach                                if ($sdx !== '') {
648dfb2cda2SGreg Roach                                    $this->wherePhonetic($query, $table . '.n_soundex_givn_dm', $sdx);
649dfb2cda2SGreg Roach                                } else {
650dfb2cda2SGreg Roach                                    // No phonetic content? Use a substring match
651dfb2cda2SGreg Roach                                    $query->where($table . '.n_givn', 'LIKE', '%' . $field_value . '%');
652dfb2cda2SGreg Roach                                }
653dfb2cda2SGreg Roach                                break;
654dfb2cda2SGreg Roach                        }
655dfb2cda2SGreg Roach                        break;
656dfb2cda2SGreg Roach                    case 'SURN':
657dfb2cda2SGreg Roach                        switch ($modifiers[$field_name]) {
658dfb2cda2SGreg Roach                            case 'EXACT':
659dfb2cda2SGreg Roach                                $query->where($table . '.n_surn', '=', $field_value);
660dfb2cda2SGreg Roach                                break;
661dfb2cda2SGreg Roach                            case 'BEGINS':
662dfb2cda2SGreg Roach                                $query->where($table . '.n_surn', 'LIKE', $field_value . '%');
663dfb2cda2SGreg Roach                                break;
664dfb2cda2SGreg Roach                            case 'CONTAINS':
665dfb2cda2SGreg Roach                                $query->where($table . '.n_surn', 'LIKE', '%' . $field_value . '%');
666dfb2cda2SGreg Roach                                break;
667dfb2cda2SGreg Roach                            case 'SDX_STD':
668dfb2cda2SGreg Roach                                $sdx = Soundex::russell($field_value);
669dfb2cda2SGreg Roach                                if ($sdx !== '') {
670dfb2cda2SGreg Roach                                    $this->wherePhonetic($query, $table . '.n_soundex_surn_std', $sdx);
671dfb2cda2SGreg Roach                                } else {
672dfb2cda2SGreg Roach                                    // No phonetic content? Use a substring match
673dfb2cda2SGreg Roach                                    $query->where($table . '.n_surn', 'LIKE', '%' . $field_value . '%');
674dfb2cda2SGreg Roach                                }
675dfb2cda2SGreg Roach                                break;
676dfb2cda2SGreg Roach                            case 'SDX': // SDX uses DM by default.
677dfb2cda2SGreg Roach                            case 'SDX_DM':
678dfb2cda2SGreg Roach                                $sdx = Soundex::daitchMokotoff($field_value);
679dfb2cda2SGreg Roach                                if ($sdx !== '') {
680dfb2cda2SGreg Roach                                    $this->wherePhonetic($query, $table . '.n_soundex_surn_dm', $sdx);
681dfb2cda2SGreg Roach                                } else {
682dfb2cda2SGreg Roach                                    // No phonetic content? Use a substring match
683dfb2cda2SGreg Roach                                    $query->where($table . '.n_surn', 'LIKE', '%' . $field_value . '%');
684dfb2cda2SGreg Roach                                }
685dfb2cda2SGreg Roach                                break;
686dfb2cda2SGreg Roach                        }
687dfb2cda2SGreg Roach                        break;
688dfb2cda2SGreg Roach                }
689dfb2cda2SGreg Roach                unset($fields[$field_name]);
690dfb2cda2SGreg Roach            } elseif ($parts[0] === 'FAMS') {
691dfb2cda2SGreg Roach                // e.g. searches for occupation, religion, note, etc.
692dfb2cda2SGreg Roach                // Initial matching only.  Need PHP to apply filter.
693dfb2cda2SGreg Roach                $query->where('families.f_gedcom', 'LIKE', "%\n1 " . $parts[1] . ' %' . $field_value . '%');
694dfb2cda2SGreg Roach            } elseif ($parts[1] === 'TYPE') {
695dfb2cda2SGreg Roach                // e.g. FACT:TYPE or EVEN:TYPE
696dfb2cda2SGreg Roach                // Initial matching only.  Need PHP to apply filter.
697dfb2cda2SGreg Roach                $query->where('individuals.i_gedcom', 'LIKE', "%\n1 " . $parts[0] . '%\n2 TYPE %' . $field_value . '%');
698dfb2cda2SGreg Roach            } else {
699dfb2cda2SGreg Roach                // e.g. searches for occupation, religion, note, etc.
700dfb2cda2SGreg Roach                // Initial matching only.  Need PHP to apply filter.
701dfb2cda2SGreg Roach                $query->where('individuals.i_gedcom', 'LIKE', "%\n1 " . $parts[0] . ' %' . $field_value . '%');
702dfb2cda2SGreg Roach            }
703dfb2cda2SGreg Roach        }
704dfb2cda2SGreg Roach
705dfb2cda2SGreg Roach        return $query
706dfb2cda2SGreg Roach            ->get()
70752a8ef61SGreg Roach            ->each($this->rowLimiter())
708dfb2cda2SGreg Roach            ->map(Individual::rowMapper())
709dfb2cda2SGreg Roach            ->filter(GedcomRecord::accessFilter())
710dfb2cda2SGreg Roach            ->filter(function (Individual $individual) use ($fields): bool {
711dfb2cda2SGreg Roach                // Check for XXXX:PLAC fields, which were only partially matched by SQL
712dfb2cda2SGreg Roach                foreach ($fields as $field_name => $field_value) {
713a41e65f0SGreg Roach                    $regex = '/' . preg_quote($field_value, '/') . '/i';
714dfb2cda2SGreg Roach
715dfb2cda2SGreg Roach                    $parts = preg_split('/:/', $field_name . '::::');
716dfb2cda2SGreg Roach
717dfb2cda2SGreg Roach                    if ($parts[1] === 'PLAC') {
718dfb2cda2SGreg Roach                        // *:PLAC
719a41e65f0SGreg Roach                        foreach ($individual->facts([$parts[0]]) as $fact) {
720392561bbSGreg Roach                            if (preg_match($regex, $fact->place()->gedcomName())) {
721a41e65f0SGreg Roach                                return true;
722dfb2cda2SGreg Roach                            }
723a41e65f0SGreg Roach                        }
724dfb2cda2SGreg Roach                    } elseif ($parts[0] === 'FAMS' && $parts[2] === 'PLAC') {
725dfb2cda2SGreg Roach                        // FAMS:*:PLAC
72639ca88baSGreg Roach                        foreach ($individual->spouseFamilies() as $family) {
727a41e65f0SGreg Roach                            foreach ($family->facts([$parts[1]]) as $fact) {
728392561bbSGreg Roach                                if (preg_match($regex, $fact->place()->gedcomName())) {
729a41e65f0SGreg Roach                                    return true;
730a41e65f0SGreg Roach                                }
731dfb2cda2SGreg Roach                            }
732dfb2cda2SGreg Roach                        }
733dfb2cda2SGreg Roach                    } elseif ($parts[0] === 'FAMS') {
734dfb2cda2SGreg Roach                        // e.g. searches for occupation, religion, note, etc.
73539ca88baSGreg Roach                        foreach ($individual->spouseFamilies() as $family) {
736a41e65f0SGreg Roach                            foreach ($family->facts([$parts[1]]) as $fact) {
737a41e65f0SGreg Roach                                if (preg_match($regex, $fact->value())) {
738a41e65f0SGreg Roach                                    return true;
739a41e65f0SGreg Roach                                }
740dfb2cda2SGreg Roach                            }
741dfb2cda2SGreg Roach                        }
742dfb2cda2SGreg Roach                    } elseif ($parts[1] === 'TYPE') {
743dfb2cda2SGreg Roach                        // e.g. FACT:TYPE or EVEN:TYPE
744a41e65f0SGreg Roach                        foreach ($individual->facts([$parts[0]]) as $fact) {
745a41e65f0SGreg Roach                            if (preg_match($regex, $fact->attribute('TYPE'))) {
746a41e65f0SGreg Roach                                return true;
747a41e65f0SGreg Roach                            }
748dfb2cda2SGreg Roach                        }
749dfb2cda2SGreg Roach                    } else {
750dfb2cda2SGreg Roach                        // e.g. searches for occupation, religion, note, etc.
751a41e65f0SGreg Roach                        foreach ($individual->facts([$parts[0]]) as $fact) {
752a41e65f0SGreg Roach                            if (preg_match($regex, $fact->value())) {
753a41e65f0SGreg Roach                                return true;
754a41e65f0SGreg Roach                            }
755dfb2cda2SGreg Roach                        }
756dfb2cda2SGreg Roach                    }
757dfb2cda2SGreg Roach
758a41e65f0SGreg Roach                    // No match
759dfb2cda2SGreg Roach                    return false;
760dfb2cda2SGreg Roach                }
761dfb2cda2SGreg Roach
762dfb2cda2SGreg Roach                return true;
763dfb2cda2SGreg Roach            });
764dfb2cda2SGreg Roach    }
765dfb2cda2SGreg Roach
766dfb2cda2SGreg Roach    /**
767dfb2cda2SGreg Roach     * @param string $soundex
768dfb2cda2SGreg Roach     * @param string $lastname
769dfb2cda2SGreg Roach     * @param string $firstname
770dfb2cda2SGreg Roach     * @param string $place
771dfb2cda2SGreg Roach     * @param Tree[] $search_trees
772dfb2cda2SGreg Roach     *
77354c7f8dfSGreg Roach     * @return Collection
77454c7f8dfSGreg Roach     * @return Individual[]
775dfb2cda2SGreg Roach     */
776dfb2cda2SGreg Roach    public function searchIndividualsPhonetic(string $soundex, string $lastname, string $firstname, string $place, array $search_trees): Collection
777dfb2cda2SGreg Roach    {
778dfb2cda2SGreg Roach        switch ($soundex) {
779dfb2cda2SGreg Roach            default:
780dfb2cda2SGreg Roach            case 'Russell':
781dfb2cda2SGreg Roach                $givn_sdx   = Soundex::russell($firstname);
782dfb2cda2SGreg Roach                $surn_sdx   = Soundex::russell($lastname);
783dfb2cda2SGreg Roach                $plac_sdx   = Soundex::russell($place);
784dfb2cda2SGreg Roach                $givn_field = 'n_soundex_givn_std';
785dfb2cda2SGreg Roach                $surn_field = 'n_soundex_surn_std';
786dfb2cda2SGreg Roach                $plac_field = 'p_std_soundex';
787dfb2cda2SGreg Roach                break;
788dfb2cda2SGreg Roach            case 'DaitchM':
789dfb2cda2SGreg Roach                $givn_sdx   = Soundex::daitchMokotoff($firstname);
790dfb2cda2SGreg Roach                $surn_sdx   = Soundex::daitchMokotoff($lastname);
791dfb2cda2SGreg Roach                $plac_sdx   = Soundex::daitchMokotoff($place);
792dfb2cda2SGreg Roach                $givn_field = 'n_soundex_givn_dm';
793dfb2cda2SGreg Roach                $surn_field = 'n_soundex_surn_dm';
794dfb2cda2SGreg Roach                $plac_field = 'p_dm_soundex';
795dfb2cda2SGreg Roach                break;
796dfb2cda2SGreg Roach        }
797dfb2cda2SGreg Roach
798dfb2cda2SGreg Roach        // Nothing to search for? Return nothing.
799dfb2cda2SGreg Roach        if ($givn_sdx === '' && $surn_sdx === '' && $plac_sdx === '') {
8003fda39a7SGreg Roach            return new Collection();
801dfb2cda2SGreg Roach        }
802dfb2cda2SGreg Roach
803dfb2cda2SGreg Roach        $query = DB::table('individuals')
804dfb2cda2SGreg Roach            ->select(['individuals.*'])
805dfb2cda2SGreg Roach            ->distinct();
806dfb2cda2SGreg Roach
807dfb2cda2SGreg Roach        $this->whereTrees($query, 'i_file', $search_trees);
808dfb2cda2SGreg Roach
809dfb2cda2SGreg Roach        if ($plac_sdx !== '') {
810dfb2cda2SGreg Roach            $query->join('placelinks', function (JoinClause $join): void {
811dfb2cda2SGreg Roach                $join
812dfb2cda2SGreg Roach                    ->on('placelinks.pl_file', '=', 'individuals.i_file')
813dfb2cda2SGreg Roach                    ->on('placelinks.pl_gid', '=', 'individuals.i_id');
814dfb2cda2SGreg Roach            });
815dfb2cda2SGreg Roach            $query->join('places', function (JoinClause $join): void {
816dfb2cda2SGreg Roach                $join
817dfb2cda2SGreg Roach                    ->on('places.p_file', '=', 'placelinks.pl_file')
818dfb2cda2SGreg Roach                    ->on('places.p_id', '=', 'placelinks.pl_p_id');
819dfb2cda2SGreg Roach            });
820dfb2cda2SGreg Roach
821dfb2cda2SGreg Roach            $this->wherePhonetic($query, $plac_field, $plac_sdx);
822dfb2cda2SGreg Roach        }
823dfb2cda2SGreg Roach
824dfb2cda2SGreg Roach        if ($givn_sdx !== '' || $surn_sdx !== '') {
825dfb2cda2SGreg Roach            $query->join('name', function (JoinClause $join): void {
826dfb2cda2SGreg Roach                $join
827dfb2cda2SGreg Roach                    ->on('name.n_file', '=', 'individuals.i_file')
828dfb2cda2SGreg Roach                    ->on('name.n_id', '=', 'individuals.i_id');
829dfb2cda2SGreg Roach            });
830dfb2cda2SGreg Roach
831dfb2cda2SGreg Roach            $this->wherePhonetic($query, $givn_field, $givn_sdx);
832dfb2cda2SGreg Roach            $this->wherePhonetic($query, $surn_field, $surn_sdx);
833dfb2cda2SGreg Roach        }
834dfb2cda2SGreg Roach
835dfb2cda2SGreg Roach        return $query
836dfb2cda2SGreg Roach            ->get()
83752a8ef61SGreg Roach            ->each($this->rowLimiter())
838dfb2cda2SGreg Roach            ->map(Individual::rowMapper())
839dfb2cda2SGreg Roach            ->filter(GedcomRecord::accessFilter());
840dfb2cda2SGreg Roach    }
841dfb2cda2SGreg Roach
842dfb2cda2SGreg Roach    /**
84332cd2800SGreg Roach     * Paginate a search query.
84432cd2800SGreg Roach     *
84532cd2800SGreg Roach     * @param Builder $query      Searches the database for the desired records.
84632cd2800SGreg Roach     * @param Closure $row_mapper Converts a row from the query into a record.
847a7a24840SGreg Roach     * @param Closure $row_filter
84832cd2800SGreg Roach     * @param int     $offset     Skip this many rows.
84932cd2800SGreg Roach     * @param int     $limit      Take this many rows.
85032cd2800SGreg Roach     *
85132cd2800SGreg Roach     * @return Collection
85232cd2800SGreg Roach     */
853a7a24840SGreg Roach    private function paginateQuery(Builder $query, Closure $row_mapper, Closure $row_filter, int $offset, int $limit): Collection
85432cd2800SGreg Roach    {
85532cd2800SGreg Roach        $collection = new Collection();
85632cd2800SGreg Roach
85732cd2800SGreg Roach        foreach ($query->cursor() as $row) {
85832cd2800SGreg Roach            $record = $row_mapper($row);
859b68caec6SGreg Roach            // If the object has a method "canShow()", then use it to filter for privacy.
860a7a24840SGreg Roach            if ($row_filter($record)) {
86132cd2800SGreg Roach                if ($offset > 0) {
86232cd2800SGreg Roach                    $offset--;
86332cd2800SGreg Roach                } else {
86432cd2800SGreg Roach                    if ($limit > 0) {
86532cd2800SGreg Roach                        $collection->push($record);
86632cd2800SGreg Roach                    }
86732cd2800SGreg Roach
86832cd2800SGreg Roach                    $limit--;
86932cd2800SGreg Roach
87032cd2800SGreg Roach                    if ($limit === 0) {
87132cd2800SGreg Roach                        break;
87232cd2800SGreg Roach                    }
87332cd2800SGreg Roach                }
87432cd2800SGreg Roach            }
87532cd2800SGreg Roach        }
87632cd2800SGreg Roach
877*e0458bdcSGreg Roach
87832cd2800SGreg Roach        return $collection;
87932cd2800SGreg Roach    }
880a7a24840SGreg Roach
881a7a24840SGreg Roach    /**
882a7a24840SGreg Roach     * Apply search filters to a SQL query column.  Apply collation rules to MySQL.
883a7a24840SGreg Roach     *
884a7a24840SGreg Roach     * @param Builder           $query
885a7a24840SGreg Roach     * @param Expression|string $field
886a7a24840SGreg Roach     * @param string[]          $search_terms
887a7a24840SGreg Roach     */
888a7a24840SGreg Roach    private function whereSearch(Builder $query, $field, array $search_terms): void
889a7a24840SGreg Roach    {
890a7a24840SGreg Roach        if ($field instanceof Expression) {
891a7a24840SGreg Roach            $field = $field->getValue();
892a7a24840SGreg Roach        }
893a7a24840SGreg Roach
894a7a24840SGreg Roach        foreach ($search_terms as $search_term) {
895436c1c02SGreg Roach            $query->whereContains(DB::raw($field), $search_term);
896a7a24840SGreg Roach        }
897a7a24840SGreg Roach    }
898a7a24840SGreg Roach
899a7a24840SGreg Roach    /**
9002d686e68SGreg Roach     * Apply soundex search filters to a SQL query column.
9012d686e68SGreg Roach     *
9022d686e68SGreg Roach     * @param Builder           $query
9032d686e68SGreg Roach     * @param Expression|string $field
9042d686e68SGreg Roach     * @param string            $soundex
9052d686e68SGreg Roach     */
9062d686e68SGreg Roach    private function wherePhonetic(Builder $query, $field, string $soundex): void
9072d686e68SGreg Roach    {
9082d686e68SGreg Roach        if ($soundex !== '') {
9092d686e68SGreg Roach            $query->where(function (Builder $query) use ($soundex, $field): void {
9102d686e68SGreg Roach                foreach (explode(':', $soundex) as $sdx) {
9112d686e68SGreg Roach                    $query->orWhere($field, 'LIKE', '%' . $sdx . '%');
9122d686e68SGreg Roach                }
9132d686e68SGreg Roach            });
9142d686e68SGreg Roach        }
9152d686e68SGreg Roach    }
9162d686e68SGreg Roach
9172d686e68SGreg Roach    /**
918a7a24840SGreg Roach     * @param Builder $query
919a7a24840SGreg Roach     * @param string  $tree_id_field
920a7a24840SGreg Roach     * @param Tree[]  $trees
921a7a24840SGreg Roach     */
922a7a24840SGreg Roach    private function whereTrees(Builder $query, string $tree_id_field, array $trees): void
923a7a24840SGreg Roach    {
92425d7fe95SGreg Roach        $tree_ids = array_map(function (Tree $tree): int {
925a7a24840SGreg Roach            return $tree->id();
926a7a24840SGreg Roach        }, $trees);
927a7a24840SGreg Roach
928a7a24840SGreg Roach        $query->whereIn($tree_id_field, $tree_ids);
929a7a24840SGreg Roach    }
930a7a24840SGreg Roach
931a7a24840SGreg Roach    /**
932a7a24840SGreg Roach     * A closure to filter records by privacy-filtered GEDCOM data.
933a7a24840SGreg Roach     *
934a7a24840SGreg Roach     * @param array $search_terms
935a7a24840SGreg Roach     *
936a7a24840SGreg Roach     * @return Closure
937a7a24840SGreg Roach     */
938a7a24840SGreg Roach    private function rawGedcomFilter(array $search_terms): Closure
939a7a24840SGreg Roach    {
940a7a24840SGreg Roach        return function (GedcomRecord $record) use ($search_terms): bool {
941a7a24840SGreg Roach            // Ignore non-genealogy fields
942a7a24840SGreg Roach            $gedcom = preg_replace('/\n\d (?:_UID) .*/', '', $record->gedcom());
943a7a24840SGreg Roach
944a7a24840SGreg Roach            // Ignore matches in links
945a7a24840SGreg Roach            $gedcom = preg_replace('/\n\d ' . Gedcom::REGEX_TAG . '( @' . Gedcom::REGEX_XREF . '@)?/', '', $gedcom);
946a7a24840SGreg Roach
947a7a24840SGreg Roach            // Re-apply the filtering
948a7a24840SGreg Roach            foreach ($search_terms as $search_term) {
949a7a24840SGreg Roach                if (mb_stripos($gedcom, $search_term) === false) {
950a7a24840SGreg Roach                    return false;
951a7a24840SGreg Roach                }
952a7a24840SGreg Roach            }
953a7a24840SGreg Roach
954a7a24840SGreg Roach            return true;
955a7a24840SGreg Roach        };
956a7a24840SGreg Roach    }
95752a8ef61SGreg Roach
95852a8ef61SGreg Roach    /**
95952a8ef61SGreg Roach     * Searching for short or common text can give more results than the system can process.
96052a8ef61SGreg Roach     *
96152a8ef61SGreg Roach     * @param int $limit
96252a8ef61SGreg Roach     *
96352a8ef61SGreg Roach     * @return Closure
96452a8ef61SGreg Roach     */
96552a8ef61SGreg Roach    private function rowLimiter(int $limit = 1000): Closure
96652a8ef61SGreg Roach    {
96725d7fe95SGreg Roach        return function () use ($limit): void {
96852a8ef61SGreg Roach            static $n = 0;
96952a8ef61SGreg Roach
97052a8ef61SGreg Roach            if (++$n > $limit) {
97152a8ef61SGreg Roach                $message = I18N::translate('The search returned too many results.');
97252a8ef61SGreg Roach
97352a8ef61SGreg Roach                throw new InternalServerErrorException($message);
97452a8ef61SGreg Roach            }
97552a8ef61SGreg Roach        };
97652a8ef61SGreg Roach    }
97732cd2800SGreg Roach}
978