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