xref: /webtrees/app/Http/RequestHandlers/AutoCompleteCitation.php (revision 36de22acf6348b1059dac63e3cd19589574906ac)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 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\GedcomRecord;
24use Fisharebest\Webtrees\Registry;
25use Fisharebest\Webtrees\Tree;
26use Illuminate\Database\Capsule\Manager as DB;
27use Illuminate\Database\Query\JoinClause;
28use Illuminate\Support\Collection;
29use Psr\Http\Message\ServerRequestInterface;
30
31use function assert;
32use function preg_match_all;
33use function preg_quote;
34
35/**
36 * Autocomplete handler for source citations
37 */
38class AutoCompleteCitation extends AbstractAutocompleteHandler
39{
40    protected function search(ServerRequestInterface $request): Collection
41    {
42        $tree = $request->getAttribute('tree');
43        assert($tree instanceof Tree);
44
45        $query  = $request->getAttribute('query');
46        $xref   = $request->getQueryParams()['extra'] ?? '';
47        $source = Registry::sourceFactory()->make($xref, $tree);
48        $source = Auth::checkSourceAccess($source);
49
50        $regex_query = strtr(preg_quote($query, '/'), [' ' => '.+']);
51
52        // Fetch all records with a link to this source
53        $individuals = DB::table('individuals')
54            ->join('link', static function (JoinClause $join): void {
55                $join
56                    ->on('l_file', '=', 'i_file')
57                    ->on('l_from', '=', 'i_id');
58            })
59            ->where('i_file', '=', $tree->id())
60            ->where('l_to', '=', $source->xref())
61            ->where('l_type', '=', 'SOUR')
62            ->distinct()
63            ->select(['individuals.*'])
64            ->get()
65            ->map(Registry::individualFactory()->mapper($tree))
66            ->filter(GedcomRecord::accessFilter());
67
68        $families = DB::table('families')
69            ->join('link', static function (JoinClause $join): void {
70                $join
71                    ->on('l_file', '=', 'f_file')
72                    ->on('l_from', '=', 'f_id')
73                    ->where('l_type', '=', 'SOUR');
74            })
75            ->where('f_file', '=', $tree->id())
76            ->where('l_to', '=', $source->xref())
77            ->where('l_type', '=', 'SOUR')
78            ->distinct()
79            ->select(['families.*'])
80            ->get()
81            ->map(Registry::familyFactory()->mapper($tree))
82            ->filter(GedcomRecord::accessFilter());
83
84        $pages = new Collection();
85
86        foreach ($individuals->merge($families) as $record) {
87            if (preg_match_all('/\n1 SOUR @' . $source->xref() . '@(?:\n[2-9].*)*\n2 PAGE (.*' . $regex_query . '.*)/i', $record->gedcom(), $matches)) {
88                $pages = $pages->concat($matches[1]);
89            }
90
91            if (preg_match_all('/\n2 SOUR @' . $source->xref() . '@(?:\n[3-9].*)*\n3 PAGE (.*' . $regex_query . '.*)/i', $record->gedcom(), $matches)) {
92                $pages = $pages->concat($matches[1]);
93            }
94        }
95
96        return $pages->uniqueStrict();
97    }
98}
99