1f5402f3dSGreg Roach<?php 2f5402f3dSGreg Roach 3f5402f3dSGreg Roach/** 4f5402f3dSGreg Roach * webtrees: online genealogy 5*d11be702SGreg Roach * Copyright (C) 2023 webtrees development team 6f5402f3dSGreg Roach * This program is free software: you can redistribute it and/or modify 7f5402f3dSGreg Roach * it under the terms of the GNU General Public License as published by 8f5402f3dSGreg Roach * the Free Software Foundation, either version 3 of the License, or 9f5402f3dSGreg Roach * (at your option) any later version. 10f5402f3dSGreg Roach * This program is distributed in the hope that it will be useful, 11f5402f3dSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 12f5402f3dSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13f5402f3dSGreg Roach * GNU General Public License for more details. 14f5402f3dSGreg 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/>. 16f5402f3dSGreg Roach */ 17f5402f3dSGreg Roach 18f5402f3dSGreg Roachdeclare(strict_types=1); 19f5402f3dSGreg Roach 20f5402f3dSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers; 21f5402f3dSGreg Roach 2245ee34f2SGreg Roachuse Fisharebest\Webtrees\Auth; 23f5402f3dSGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait; 24f5402f3dSGreg Roachuse Fisharebest\Webtrees\I18N; 2545ee34f2SGreg Roachuse Fisharebest\Webtrees\Log; 26f5402f3dSGreg Roachuse Fisharebest\Webtrees\Services\SearchService; 27f5402f3dSGreg Roachuse Fisharebest\Webtrees\Services\TreeService; 28f5402f3dSGreg Roachuse Fisharebest\Webtrees\Site; 29f5402f3dSGreg Roachuse Fisharebest\Webtrees\Tree; 30b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator; 31075d1a05SGreg Roachuse Illuminate\Support\Collection; 32f5402f3dSGreg Roachuse Psr\Http\Message\ResponseInterface; 33f5402f3dSGreg Roachuse Psr\Http\Message\ServerRequestInterface; 34f5402f3dSGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 35f5402f3dSGreg Roach 36748dbe15SGreg Roachuse function in_array; 37748dbe15SGreg Roach 38f5402f3dSGreg Roach/** 39f5402f3dSGreg Roach * Search for (and optionally replace) genealogy data 40f5402f3dSGreg Roach */ 41f5402f3dSGreg Roachclass SearchPhoneticPage implements RequestHandlerInterface 42f5402f3dSGreg Roach{ 43f5402f3dSGreg Roach use ViewResponseTrait; 44f5402f3dSGreg Roach 45c4943cffSGreg Roach private SearchService $search_service; 46f5402f3dSGreg Roach 47c4943cffSGreg Roach private TreeService $tree_service; 48f5402f3dSGreg Roach 49f5402f3dSGreg Roach /** 50f5402f3dSGreg Roach * @param SearchService $search_service 51f5402f3dSGreg Roach * @param TreeService $tree_service 52f5402f3dSGreg Roach */ 53f5402f3dSGreg Roach public function __construct(SearchService $search_service, TreeService $tree_service) 54f5402f3dSGreg Roach { 55f5402f3dSGreg Roach $this->search_service = $search_service; 56f5402f3dSGreg Roach $this->tree_service = $tree_service; 57f5402f3dSGreg Roach } 58f5402f3dSGreg Roach 59f5402f3dSGreg Roach /** 60f5402f3dSGreg Roach * The phonetic search. 61f5402f3dSGreg Roach * 62f5402f3dSGreg Roach * @param ServerRequestInterface $request 63f5402f3dSGreg Roach * 64f5402f3dSGreg Roach * @return ResponseInterface 65f5402f3dSGreg Roach */ 66f5402f3dSGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 67f5402f3dSGreg Roach { 68b55cbc6bSGreg Roach $tree = Validator::attributes($request)->tree(); 69748dbe15SGreg Roach $firstname = Validator::queryParams($request)->string('firstname', ''); 70748dbe15SGreg Roach $lastname = Validator::queryParams($request)->string('lastname', ''); 71748dbe15SGreg Roach $place = Validator::queryParams($request)->string('place', ''); 72748dbe15SGreg Roach $soundex = Validator::queryParams($request)->isInArray(['DaitchM', 'Russell'])->string('soundex', 'Russell'); 73f5402f3dSGreg Roach 74748dbe15SGreg Roach // Where to search 75748dbe15SGreg Roach $search_tree_names = Validator::queryParams($request)->array('search_trees'); 76f5402f3dSGreg Roach 77fceda430SGreg Roach // What trees to search? 78f5402f3dSGreg Roach if (Site::getPreference('ALLOW_CHANGE_GEDCOM') === '1') { 79ca2d6785SGreg Roach $all_trees = $this->tree_service->all(); 80f5402f3dSGreg Roach } else { 81ca2d6785SGreg Roach $all_trees = new Collection([$tree]); 82f5402f3dSGreg Roach } 83f5402f3dSGreg Roach 84ca2d6785SGreg Roach $search_trees = $all_trees 85748dbe15SGreg Roach ->filter(static fn (Tree $tree): bool => in_array($tree->name(), $search_tree_names, true)); 86f5402f3dSGreg Roach 87ca2d6785SGreg Roach if ($search_trees->isEmpty()) { 88ca2d6785SGreg Roach $search_trees->add($tree); 89f5402f3dSGreg Roach } 90f5402f3dSGreg Roach 91075d1a05SGreg Roach $individuals = new Collection(); 92f5402f3dSGreg Roach 93f5402f3dSGreg Roach if ($lastname !== '' || $firstname !== '' || $place !== '') { 9445ee34f2SGreg Roach // Log search requests for visitors 9545ee34f2SGreg Roach if (Auth::id() === null) { 9645ee34f2SGreg Roach $message = 'Phonetic: first=' . $firstname . ', last=' . $lastname . ', place=' . $place; 9745ee34f2SGreg Roach Log::addSearchLog($message, $search_trees->all()); 9845ee34f2SGreg Roach } 9945ee34f2SGreg Roach 100ca2d6785SGreg Roach $individuals = $this->search_service->searchIndividualsPhonetic($soundex, $lastname, $firstname, $place, $search_trees->all()); 101f5402f3dSGreg Roach } 102f5402f3dSGreg Roach 103f5402f3dSGreg Roach $title = I18N::translate('Phonetic search'); 104f5402f3dSGreg Roach 105f5402f3dSGreg Roach return $this->viewResponse('search-phonetic-page', [ 106f5402f3dSGreg Roach 'all_trees' => $all_trees, 107f5402f3dSGreg Roach 'firstname' => $firstname, 108f5402f3dSGreg Roach 'individuals' => $individuals, 109f5402f3dSGreg Roach 'lastname' => $lastname, 110f5402f3dSGreg Roach 'place' => $place, 111f5402f3dSGreg Roach 'search_trees' => $search_trees, 112f5402f3dSGreg Roach 'soundex' => $soundex, 113f5402f3dSGreg Roach 'title' => $title, 114f5402f3dSGreg Roach 'tree' => $tree, 115f5402f3dSGreg Roach ]); 116f5402f3dSGreg Roach } 117f5402f3dSGreg Roach} 118