1<?php 2 3/** 4 * webtrees: online genealogy 5 * Copyright (C) 2022 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\Validator; 26use Illuminate\Database\Capsule\Manager as DB; 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 protected function search(ServerRequestInterface $request): Collection 40 { 41 $tree = Validator::attributes($request)->tree(); 42 $query = $request->getQueryParams()['query'] ?? ''; 43 $xref = $request->getQueryParams()['extra'] ?? ''; 44 $source = Registry::sourceFactory()->make($xref, $tree); 45 $source = Auth::checkSourceAccess($source); 46 47 $regex_query = strtr(preg_quote($query, '/'), [' ' => '.+']); 48 49 // Fetch all records with a link to this source 50 $individuals = DB::table('individuals') 51 ->join('link', static function (JoinClause $join): void { 52 $join 53 ->on('l_file', '=', 'i_file') 54 ->on('l_from', '=', 'i_id'); 55 }) 56 ->where('i_file', '=', $tree->id()) 57 ->where('l_to', '=', $source->xref()) 58 ->where('l_type', '=', 'SOUR') 59 ->distinct() 60 ->select(['individuals.*']) 61 ->get() 62 ->map(Registry::individualFactory()->mapper($tree)) 63 ->filter(GedcomRecord::accessFilter()); 64 65 $families = DB::table('families') 66 ->join('link', static function (JoinClause $join): void { 67 $join 68 ->on('l_file', '=', 'f_file') 69 ->on('l_from', '=', 'f_id') 70 ->where('l_type', '=', 'SOUR'); 71 }) 72 ->where('f_file', '=', $tree->id()) 73 ->where('l_to', '=', $source->xref()) 74 ->where('l_type', '=', 'SOUR') 75 ->distinct() 76 ->select(['families.*']) 77 ->get() 78 ->map(Registry::familyFactory()->mapper($tree)) 79 ->filter(GedcomRecord::accessFilter()); 80 81 $pages = new Collection(); 82 83 foreach ($individuals->merge($families) as $record) { 84 if (preg_match_all('/\n1 SOUR @' . $source->xref() . '@(?:\n[2-9].*)*\n2 PAGE (.*' . $regex_query . '.*)/i', $record->gedcom(), $matches)) { 85 $pages = $pages->concat($matches[1]); 86 } 87 88 if (preg_match_all('/\n2 SOUR @' . $source->xref() . '@(?:\n[3-9].*)*\n3 PAGE (.*' . $regex_query . '.*)/i', $record->gedcom(), $matches)) { 89 $pages = $pages->concat($matches[1]); 90 } 91 } 92 93 return $pages->uniqueStrict(); 94 } 95} 96