xref: /webtrees/app/Http/RequestHandlers/SearchPhoneticPage.php (revision ba1d7494e9648df5a87aaa3fece3e098e5ce26b1)
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 Psr\Http\Message\ResponseInterface;
29use Psr\Http\Message\ServerRequestInterface;
30use Psr\Http\Server\RequestHandlerInterface;
31
32use function array_filter;
33use function assert;
34use function in_array;
35
36/**
37 * Search for (and optionally replace) genealogy data
38 */
39class SearchPhoneticPage implements RequestHandlerInterface
40{
41    use ViewResponseTrait;
42
43    /** @var SearchService */
44    private $search_service;
45
46    /** @var TreeService */
47    private $tree_service;
48
49    /**
50     * SearchController constructor.
51     *
52     * @param SearchService $search_service
53     * @param TreeService   $tree_service
54     */
55    public function __construct(SearchService $search_service, TreeService $tree_service)
56    {
57        $this->search_service = $search_service;
58        $this->tree_service   = $tree_service;
59    }
60
61    /**
62     * The phonetic search.
63     *
64     * @param ServerRequestInterface $request
65     *
66     * @return ResponseInterface
67     */
68    public function handle(ServerRequestInterface $request): ResponseInterface
69    {
70        $tree = $request->getAttribute('tree');
71        assert($tree instanceof Tree);
72
73        $params    = $request->getQueryParams();
74        $firstname = $params['firstname'] ?? '';
75        $lastname  = $params['lastname'] ?? '';
76        $place     = $params['place'] ?? '';
77        $soundex   = $params['soundex'] ?? 'Russell';
78
79        // What trees to seach?
80        if (Site::getPreference('ALLOW_CHANGE_GEDCOM') === '1') {
81            $all_trees = $this->tree_service->all()->all();
82        } else {
83            $all_trees = [$tree];
84        }
85
86        $search_tree_names = $params['search_trees'] ?? [];
87
88        $search_trees = array_filter($all_trees, static function (Tree $tree) use ($search_tree_names): bool {
89            return in_array($tree->name(), $search_tree_names, true);
90        });
91
92        if ($search_trees === []) {
93            $search_trees = [$tree];
94        }
95
96        $individuals = [];
97
98        if ($lastname !== '' || $firstname !== '' || $place !== '') {
99            $individuals = $this->search_service->searchIndividualsPhonetic($soundex, $lastname, $firstname, $place, $search_trees);
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