xref: /webtrees/app/Http/RequestHandlers/AutoCompleteCitation.php (revision 12bfb5e8200c79ce6feb788daa47fabf0b6186b0)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use Fisharebest\Webtrees\Auth;
23use Fisharebest\Webtrees\DB;
24use Fisharebest\Webtrees\GedcomRecord;
25use Fisharebest\Webtrees\Registry;
26use Fisharebest\Webtrees\Validator;
27use Illuminate\Database\Query\JoinClause;
28use Illuminate\Support\Collection;
29use Psr\Http\Message\ServerRequestInterface;
30
31use function preg_match_all;
32use function preg_quote;
33
34/**
35 * Autocomplete handler for source citations
36 */
37class AutoCompleteCitation extends AbstractAutocompleteHandler
38{
39    /**
40     * @param ServerRequestInterface $request
41     *
42     * @return Collection<int,string>
43     */
44    protected function search(ServerRequestInterface $request): Collection
45    {
46        $tree   = Validator::attributes($request)->tree();
47        $query  = Validator::queryParams($request)->string('query');
48        $xref   = Validator::queryParams($request)->string('extra', '');
49        $source = Registry::sourceFactory()->make($xref, $tree);
50        $source = Auth::checkSourceAccess($source);
51
52        $regex_query = strtr(preg_quote($query, '/'), [' ' => '.+']);
53
54        // Fetch all records with a link to this source
55        $individuals = DB::table('individuals')
56            ->join('link', static function (JoinClause $join): void {
57                $join
58                    ->on('l_file', '=', 'i_file')
59                    ->on('l_from', '=', 'i_id');
60            })
61            ->where('i_file', '=', $tree->id())
62            ->where('l_to', '=', $source->xref())
63            ->where('l_type', '=', 'SOUR')
64            ->distinct()
65            ->select(['individuals.*'])
66            ->get()
67            ->map(Registry::individualFactory()->mapper($tree))
68            ->filter(GedcomRecord::accessFilter());
69
70        $families = DB::table('families')
71            ->join('link', static function (JoinClause $join): void {
72                $join
73                    ->on('l_file', '=', 'f_file')
74                    ->on('l_from', '=', 'f_id')
75                    ->where('l_type', '=', 'SOUR');
76            })
77            ->where('f_file', '=', $tree->id())
78            ->where('l_to', '=', $source->xref())
79            ->where('l_type', '=', 'SOUR')
80            ->distinct()
81            ->select(['families.*'])
82            ->get()
83            ->map(Registry::familyFactory()->mapper($tree))
84            ->filter(GedcomRecord::accessFilter());
85
86        $pages = new Collection();
87
88        foreach ($individuals->merge($families) as $record) {
89            if (preg_match_all('/\n1 SOUR @' . $source->xref() . '@(?:\n[2-9].*)*\n2 PAGE (.*' . $regex_query . '.*)/i', $record->gedcom(), $matches)) {
90                $pages = $pages->concat($matches[1]);
91            }
92
93            if (preg_match_all('/\n2 SOUR @' . $source->xref() . '@(?:\n[3-9].*)*\n3 PAGE (.*' . $regex_query . '.*)/i', $record->gedcom(), $matches)) {
94                $pages = $pages->concat($matches[1]);
95            }
96        }
97
98        return $pages->uniqueStrict();
99    }
100}
101