xref: /webtrees/app/Http/RequestHandlers/AutoCompleteCitation.php (revision 938d0c1f5ea8a86bc4fe288bcb4b32980ce3f7ae)
163763244SGreg Roach<?php
263763244SGreg Roach
363763244SGreg Roach/**
463763244SGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 webtrees development team
663763244SGreg Roach * This program is free software: you can redistribute it and/or modify
763763244SGreg Roach * it under the terms of the GNU General Public License as published by
863763244SGreg Roach * the Free Software Foundation, either version 3 of the License, or
963763244SGreg Roach * (at your option) any later version.
1063763244SGreg Roach * This program is distributed in the hope that it will be useful,
1163763244SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
1263763244SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1363763244SGreg Roach * GNU General Public License for more details.
1463763244SGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
1663763244SGreg Roach */
1763763244SGreg Roach
1863763244SGreg Roachdeclare(strict_types=1);
1963763244SGreg Roach
2063763244SGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
2163763244SGreg Roach
2263763244SGreg Roachuse Fisharebest\Webtrees\Auth;
2363763244SGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
2463763244SGreg Roachuse Fisharebest\Webtrees\Registry;
2563763244SGreg Roachuse Fisharebest\Webtrees\Tree;
2663763244SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
2763763244SGreg Roachuse Illuminate\Database\Query\JoinClause;
2863763244SGreg Roachuse Illuminate\Support\Collection;
2963763244SGreg Roachuse Psr\Http\Message\ServerRequestInterface;
3063763244SGreg Roach
3163763244SGreg Roachuse function assert;
3263763244SGreg Roachuse function preg_match_all;
3363763244SGreg Roachuse function preg_quote;
3463763244SGreg Roach
3563763244SGreg Roach/**
3663763244SGreg Roach * Autocomplete handler for source citations
3763763244SGreg Roach */
3863763244SGreg Roachclass AutoCompleteCitation extends AbstractAutocompleteHandler
3963763244SGreg Roach{
4063763244SGreg Roach    protected function search(ServerRequestInterface $request): Collection
4163763244SGreg Roach    {
4263763244SGreg Roach        $tree = $request->getAttribute('tree');
4363763244SGreg Roach        assert($tree instanceof Tree);
4463763244SGreg Roach
4563763244SGreg Roach        $query  = $request->getAttribute('query');
4663763244SGreg Roach        $xref   = $request->getQueryParams()['extra'] ?? '';
4763763244SGreg Roach        $source = Registry::sourceFactory()->make($xref, $tree);
4863763244SGreg Roach        $source = Auth::checkSourceAccess($source);
4963763244SGreg Roach
50*938d0c1fSGreg Roach        $regex_query = strtr(preg_quote($query, '/'), [' ' => '.+']);
5163763244SGreg Roach
5263763244SGreg Roach        // Fetch all records with a link to this source
5363763244SGreg Roach        $individuals = DB::table('individuals')
5463763244SGreg Roach            ->join('link', static function (JoinClause $join): void {
5563763244SGreg Roach                $join
5663763244SGreg Roach                    ->on('l_file', '=', 'i_file')
5763763244SGreg Roach                    ->on('l_from', '=', 'i_id');
5863763244SGreg Roach            })
5963763244SGreg Roach            ->where('i_file', '=', $tree->id())
6063763244SGreg Roach            ->where('l_to', '=', $source->xref())
6163763244SGreg Roach            ->where('l_type', '=', 'SOUR')
6263763244SGreg Roach            ->distinct()
6363763244SGreg Roach            ->select(['individuals.*'])
6463763244SGreg Roach            ->get()
6563763244SGreg Roach            ->map(Registry::individualFactory()->mapper($tree))
6663763244SGreg Roach            ->filter(GedcomRecord::accessFilter());
6763763244SGreg Roach
6863763244SGreg Roach        $families = DB::table('families')
6963763244SGreg Roach            ->join('link', static function (JoinClause $join): void {
7063763244SGreg Roach                $join
7163763244SGreg Roach                    ->on('l_file', '=', 'f_file')
7263763244SGreg Roach                    ->on('l_from', '=', 'f_id')
7363763244SGreg Roach                    ->where('l_type', '=', 'SOUR');
7463763244SGreg Roach            })
7563763244SGreg Roach            ->where('f_file', '=', $tree->id())
7663763244SGreg Roach            ->where('l_to', '=', $source->xref())
7763763244SGreg Roach            ->where('l_type', '=', 'SOUR')
7863763244SGreg Roach            ->distinct()
7963763244SGreg Roach            ->select(['families.*'])
8063763244SGreg Roach            ->get()
8163763244SGreg Roach            ->map(Registry::familyFactory()->mapper($tree))
8263763244SGreg Roach            ->filter(GedcomRecord::accessFilter());
8363763244SGreg Roach
8463763244SGreg Roach        $pages = new Collection();
8563763244SGreg Roach
8663763244SGreg Roach        foreach ($individuals->merge($families) as $record) {
8763763244SGreg Roach            if (preg_match_all('/\n1 SOUR @' . $source->xref() . '@(?:\n[2-9].*)*\n2 PAGE (.*' . $regex_query . '.*)/i', $record->gedcom(), $matches)) {
8863763244SGreg Roach                $pages = $pages->concat($matches[1]);
8963763244SGreg Roach            }
9063763244SGreg Roach
9163763244SGreg Roach            if (preg_match_all('/\n2 SOUR @' . $source->xref() . '@(?:\n[3-9].*)*\n3 PAGE (.*' . $regex_query . '.*)/i', $record->gedcom(), $matches)) {
9263763244SGreg Roach                $pages = $pages->concat($matches[1]);
9363763244SGreg Roach            }
9463763244SGreg Roach        }
9563763244SGreg Roach
9663763244SGreg Roach        return $pages->uniqueStrict();
9763763244SGreg Roach    }
9863763244SGreg Roach}
99