xref: /webtrees/app/Http/RequestHandlers/SearchPhoneticPage.php (revision e3c147d0d53873311b7c137c41b4439e01d4189e)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 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 <http://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 seach?
81        if (Site::getPreference('ALLOW_CHANGE_GEDCOM') === '1') {
82            $all_trees = $this->tree_service->all()->all();
83        } else {
84            $all_trees = [$tree];
85        }
86
87        $search_tree_names = $params['search_trees'] ?? [];
88
89        $search_trees = array_filter($all_trees, static function (Tree $tree) use ($search_tree_names): bool {
90            return in_array($tree->name(), $search_tree_names, true);
91        });
92
93        if ($search_trees === []) {
94            $search_trees = [$tree];
95        }
96
97        $individuals = new Collection();
98
99        if ($lastname !== '' || $firstname !== '' || $place !== '') {
100            $individuals = $this->search_service->searchIndividualsPhonetic($soundex, $lastname, $firstname, $place, $search_trees);
101        }
102
103        $title = I18N::translate('Phonetic search');
104
105        return $this->viewResponse('search-phonetic-page', [
106            'all_trees'    => $all_trees,
107            'firstname'    => $firstname,
108            'individuals'  => $individuals,
109            'lastname'     => $lastname,
110            'place'        => $place,
111            'search_trees' => $search_trees,
112            'soundex'      => $soundex,
113            'title'        => $title,
114            'tree'         => $tree,
115        ]);
116    }
117}
118