xref: /webtrees/app/Http/RequestHandlers/SearchPhoneticPage.php (revision 748dbe155a6d19d66918ad136947fa23ee8f8469)
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\Http\ViewResponseTrait;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Services\SearchService;
25use Fisharebest\Webtrees\Services\TreeService;
26use Fisharebest\Webtrees\Site;
27use Fisharebest\Webtrees\Tree;
28use Fisharebest\Webtrees\Validator;
29use Illuminate\Support\Collection;
30use Psr\Http\Message\ResponseInterface;
31use Psr\Http\Message\ServerRequestInterface;
32use Psr\Http\Server\RequestHandlerInterface;
33
34use function in_array;
35
36/**
37 * Search for (and optionally replace) genealogy data
38 */
39class SearchPhoneticPage implements RequestHandlerInterface
40{
41    use ViewResponseTrait;
42
43    private SearchService $search_service;
44
45    private TreeService $tree_service;
46
47    /**
48     * SearchController constructor.
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            $individuals = $this->search_service->searchIndividualsPhonetic($soundex, $lastname, $firstname, $place, $search_trees->all());
95        }
96
97        $title = I18N::translate('Phonetic search');
98
99        return $this->viewResponse('search-phonetic-page', [
100            'all_trees'    => $all_trees,
101            'firstname'    => $firstname,
102            'individuals'  => $individuals,
103            'lastname'     => $lastname,
104            'place'        => $place,
105            'search_trees' => $search_trees,
106            'soundex'      => $soundex,
107            'title'        => $title,
108            'tree'         => $tree,
109        ]);
110    }
111}
112