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