163763244SGreg Roach<?php 263763244SGreg Roach 363763244SGreg Roach/** 463763244SGreg Roach * webtrees: online genealogy 5*d11be702SGreg Roach * Copyright (C) 2023 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; 25b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator; 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 preg_match_all; 3263763244SGreg Roachuse function preg_quote; 3363763244SGreg Roach 3463763244SGreg Roach/** 3563763244SGreg Roach * Autocomplete handler for source citations 3663763244SGreg Roach */ 3763763244SGreg Roachclass AutoCompleteCitation extends AbstractAutocompleteHandler 3863763244SGreg Roach{ 3992a78a2fSGreg Roach /** 4092a78a2fSGreg Roach * @param ServerRequestInterface $request 4192a78a2fSGreg Roach * 4292a78a2fSGreg Roach * @return Collection<int,string> 4392a78a2fSGreg Roach */ 4463763244SGreg Roach protected function search(ServerRequestInterface $request): Collection 4563763244SGreg Roach { 46b55cbc6bSGreg Roach $tree = Validator::attributes($request)->tree(); 47bb5c0006SGreg Roach $query = Validator::queryParams($request)->string('query'); 48bb5c0006SGreg Roach $xref = Validator::queryParams($request)->string('extra', ''); 4963763244SGreg Roach $source = Registry::sourceFactory()->make($xref, $tree); 5063763244SGreg Roach $source = Auth::checkSourceAccess($source); 5163763244SGreg Roach 52938d0c1fSGreg Roach $regex_query = strtr(preg_quote($query, '/'), [' ' => '.+']); 5363763244SGreg Roach 5463763244SGreg Roach // Fetch all records with a link to this source 5563763244SGreg Roach $individuals = DB::table('individuals') 5663763244SGreg Roach ->join('link', static function (JoinClause $join): void { 5763763244SGreg Roach $join 5863763244SGreg Roach ->on('l_file', '=', 'i_file') 5963763244SGreg Roach ->on('l_from', '=', 'i_id'); 6063763244SGreg Roach }) 6163763244SGreg Roach ->where('i_file', '=', $tree->id()) 6263763244SGreg Roach ->where('l_to', '=', $source->xref()) 6363763244SGreg Roach ->where('l_type', '=', 'SOUR') 6463763244SGreg Roach ->distinct() 6563763244SGreg Roach ->select(['individuals.*']) 6663763244SGreg Roach ->get() 6763763244SGreg Roach ->map(Registry::individualFactory()->mapper($tree)) 6863763244SGreg Roach ->filter(GedcomRecord::accessFilter()); 6963763244SGreg Roach 7063763244SGreg Roach $families = DB::table('families') 7163763244SGreg Roach ->join('link', static function (JoinClause $join): void { 7263763244SGreg Roach $join 7363763244SGreg Roach ->on('l_file', '=', 'f_file') 7463763244SGreg Roach ->on('l_from', '=', 'f_id') 7563763244SGreg Roach ->where('l_type', '=', 'SOUR'); 7663763244SGreg Roach }) 7763763244SGreg Roach ->where('f_file', '=', $tree->id()) 7863763244SGreg Roach ->where('l_to', '=', $source->xref()) 7963763244SGreg Roach ->where('l_type', '=', 'SOUR') 8063763244SGreg Roach ->distinct() 8163763244SGreg Roach ->select(['families.*']) 8263763244SGreg Roach ->get() 8363763244SGreg Roach ->map(Registry::familyFactory()->mapper($tree)) 8463763244SGreg Roach ->filter(GedcomRecord::accessFilter()); 8563763244SGreg Roach 8663763244SGreg Roach $pages = new Collection(); 8763763244SGreg Roach 8863763244SGreg Roach foreach ($individuals->merge($families) as $record) { 8963763244SGreg Roach if (preg_match_all('/\n1 SOUR @' . $source->xref() . '@(?:\n[2-9].*)*\n2 PAGE (.*' . $regex_query . '.*)/i', $record->gedcom(), $matches)) { 9063763244SGreg Roach $pages = $pages->concat($matches[1]); 9163763244SGreg Roach } 9263763244SGreg Roach 9363763244SGreg Roach if (preg_match_all('/\n2 SOUR @' . $source->xref() . '@(?:\n[3-9].*)*\n3 PAGE (.*' . $regex_query . '.*)/i', $record->gedcom(), $matches)) { 9463763244SGreg Roach $pages = $pages->concat($matches[1]); 9563763244SGreg Roach } 9663763244SGreg Roach } 9763763244SGreg Roach 9863763244SGreg Roach return $pages->uniqueStrict(); 9963763244SGreg Roach } 10063763244SGreg Roach} 101