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