xref: /webtrees/app/Http/RequestHandlers/SearchPhoneticPage.php (revision 6930e9b42b9925bfc3a874fc2aaa59aabd0d2418)
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
34/**
35 * Search for (and optionally replace) genealogy data
36 */
37class SearchPhoneticPage implements RequestHandlerInterface
38{
39    use ViewResponseTrait;
40
41    private SearchService $search_service;
42
43    private TreeService $tree_service;
44
45    /**
46     * SearchController constructor.
47     *
48     * @param SearchService $search_service
49     * @param TreeService   $tree_service
50     */
51    public function __construct(SearchService $search_service, TreeService $tree_service)
52    {
53        $this->search_service = $search_service;
54        $this->tree_service   = $tree_service;
55    }
56
57    /**
58     * The phonetic search.
59     *
60     * @param ServerRequestInterface $request
61     *
62     * @return ResponseInterface
63     */
64    public function handle(ServerRequestInterface $request): ResponseInterface
65    {
66        $tree = Validator::attributes($request)->tree();
67
68        $params    = $request->getQueryParams();
69        $firstname = $params['firstname'] ?? '';
70        $lastname  = $params['lastname'] ?? '';
71        $place     = $params['place'] ?? '';
72        $soundex   = $params['soundex'] ?? 'Russell';
73
74        // What trees to search?
75        if (Site::getPreference('ALLOW_CHANGE_GEDCOM') === '1') {
76            $all_trees = $this->tree_service->all();
77        } else {
78            $all_trees = new Collection([$tree]);
79        }
80
81        $search_tree_names = new Collection($params['search_trees'] ?? []);
82
83        $search_trees = $all_trees
84            ->filter(static function (Tree $tree) use ($search_tree_names): bool {
85                return $search_tree_names->containsStrict($tree->name());
86            });
87
88        if ($search_trees->isEmpty()) {
89            $search_trees->add($tree);
90        }
91
92        $individuals = new Collection();
93
94        if ($lastname !== '' || $firstname !== '' || $place !== '') {
95            $individuals = $this->search_service->searchIndividualsPhonetic($soundex, $lastname, $firstname, $place, $search_trees->all());
96        }
97
98        $title = I18N::translate('Phonetic search');
99
100        return $this->viewResponse('search-phonetic-page', [
101            'all_trees'    => $all_trees,
102            'firstname'    => $firstname,
103            'individuals'  => $individuals,
104            'lastname'     => $lastname,
105            'place'        => $place,
106            'search_trees' => $search_trees,
107            'soundex'      => $soundex,
108            'title'        => $title,
109            'tree'         => $tree,
110        ]);
111    }
112}
113