xref: /webtrees/app/Http/RequestHandlers/SearchPhoneticPage.php (revision 24931b29a0237a5f5f1b8620af661ea530451af0)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 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\Http\ViewResponseTrait;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Log;
26use Fisharebest\Webtrees\Services\SearchService;
27use Fisharebest\Webtrees\Services\TreeService;
28use Fisharebest\Webtrees\Site;
29use Fisharebest\Webtrees\Tree;
30use Fisharebest\Webtrees\Validator;
31use Illuminate\Support\Collection;
32use Psr\Http\Message\ResponseInterface;
33use Psr\Http\Message\ServerRequestInterface;
34use Psr\Http\Server\RequestHandlerInterface;
35
36use function in_array;
37
38/**
39 * Search for (and optionally replace) genealogy data
40 */
41class SearchPhoneticPage implements RequestHandlerInterface
42{
43    use ViewResponseTrait;
44
45    private SearchService $search_service;
46
47    private TreeService $tree_service;
48
49    /**
50     * @param SearchService $search_service
51     * @param TreeService   $tree_service
52     */
53    public function __construct(SearchService $search_service, TreeService $tree_service)
54    {
55        $this->search_service = $search_service;
56        $this->tree_service   = $tree_service;
57    }
58
59    /**
60     * The phonetic search.
61     *
62     * @param ServerRequestInterface $request
63     *
64     * @return ResponseInterface
65     */
66    public function handle(ServerRequestInterface $request): ResponseInterface
67    {
68        $tree      = Validator::attributes($request)->tree();
69        $firstname = Validator::queryParams($request)->string('firstname', '');
70        $lastname  = Validator::queryParams($request)->string('lastname', '');
71        $place     = Validator::queryParams($request)->string('place', '');
72        $soundex   = Validator::queryParams($request)->isInArray(['DaitchM', 'Russell'])->string('soundex', 'Russell');
73
74        // Where to search
75        $search_tree_names = Validator::queryParams($request)->array('search_trees');
76
77        // What trees to search?
78        if (Site::getPreference('ALLOW_CHANGE_GEDCOM') === '1') {
79            $all_trees = $this->tree_service->all();
80        } else {
81            $all_trees = new Collection([$tree]);
82        }
83
84        $search_trees = $all_trees
85            ->filter(static fn (Tree $tree): bool => in_array($tree->name(), $search_tree_names, true));
86
87        if ($search_trees->isEmpty()) {
88            $search_trees->add($tree);
89        }
90
91        $individuals = new Collection();
92
93        if ($lastname !== '' || $firstname !== '' || $place !== '') {
94            // Log search requests for visitors
95            if (Auth::id() === null) {
96                $message = 'Phonetic: first=' . $firstname . ', last=' . $lastname . ', place=' . $place;
97                Log::addSearchLog($message, $search_trees->all());
98            }
99
100            $individuals = $this->search_service->searchIndividualsPhonetic($soundex, $lastname, $firstname, $place, $search_trees->all());
101        }
102
103        $title = I18N::translate('Phonetic search');
104
105        return $this->viewResponse('search-phonetic-page', [
106            'all_trees'    => $all_trees,
107            'firstname'    => $firstname,
108            'individuals'  => $individuals,
109            'lastname'     => $lastname,
110            'place'        => $place,
111            'search_trees' => $search_trees,
112            'soundex'      => $soundex,
113            'title'        => $title,
114            'tree'         => $tree,
115        ]);
116    }
117}
118