xref: /webtrees/app/Http/RequestHandlers/SearchPhoneticPage.php (revision 52288ec72a577b9a3a455ccc0f26d11b0df31667)
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\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     * SearchController constructor.
51     *
52     * @param SearchService $search_service
53     * @param TreeService   $tree_service
54     */
55    public function __construct(SearchService $search_service, TreeService $tree_service)
56    {
57        $this->search_service = $search_service;
58        $this->tree_service   = $tree_service;
59    }
60
61    /**
62     * The phonetic search.
63     *
64     * @param ServerRequestInterface $request
65     *
66     * @return ResponseInterface
67     */
68    public function handle(ServerRequestInterface $request): ResponseInterface
69    {
70        $tree      = Validator::attributes($request)->tree();
71        $firstname = Validator::queryParams($request)->string('firstname', '');
72        $lastname  = Validator::queryParams($request)->string('lastname', '');
73        $place     = Validator::queryParams($request)->string('place', '');
74        $soundex   = Validator::queryParams($request)->isInArray(['DaitchM', 'Russell'])->string('soundex', 'Russell');
75
76        // Where to search
77        $search_tree_names = Validator::queryParams($request)->array('search_trees');
78
79        // What trees to search?
80        if (Site::getPreference('ALLOW_CHANGE_GEDCOM') === '1') {
81            $all_trees = $this->tree_service->all();
82        } else {
83            $all_trees = new Collection([$tree]);
84        }
85
86        $search_trees = $all_trees
87            ->filter(static fn (Tree $tree): bool => in_array($tree->name(), $search_tree_names, true));
88
89        if ($search_trees->isEmpty()) {
90            $search_trees->add($tree);
91        }
92
93        $individuals = new Collection();
94
95        if ($lastname !== '' || $firstname !== '' || $place !== '') {
96            // Log search requests for visitors
97            if (Auth::id() === null) {
98                $message = 'Phonetic: first=' . $firstname . ', last=' . $lastname . ', place=' . $place;
99                Log::addSearchLog($message, $search_trees->all());
100            }
101
102            $individuals = $this->search_service->searchIndividualsPhonetic($soundex, $lastname, $firstname, $place, $search_trees->all());
103        }
104
105        $title = I18N::translate('Phonetic search');
106
107        return $this->viewResponse('search-phonetic-page', [
108            'all_trees'    => $all_trees,
109            'firstname'    => $firstname,
110            'individuals'  => $individuals,
111            'lastname'     => $lastname,
112            'place'        => $place,
113            'search_trees' => $search_trees,
114            'soundex'      => $soundex,
115            'title'        => $title,
116            'tree'         => $tree,
117        ]);
118    }
119}
120