xref: /webtrees/app/Http/RequestHandlers/SearchPhoneticPage.php (revision ca2d6785e88b0b2a61b6ad9e066776ff8ff6f9d1)
1f5402f3dSGreg Roach<?php
2f5402f3dSGreg Roach
3f5402f3dSGreg Roach/**
4f5402f3dSGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 webtrees development team
6f5402f3dSGreg Roach * This program is free software: you can redistribute it and/or modify
7f5402f3dSGreg Roach * it under the terms of the GNU General Public License as published by
8f5402f3dSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9f5402f3dSGreg Roach * (at your option) any later version.
10f5402f3dSGreg Roach * This program is distributed in the hope that it will be useful,
11f5402f3dSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12f5402f3dSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13f5402f3dSGreg Roach * GNU General Public License for more details.
14f5402f3dSGreg Roach * You should have received a copy of the GNU General Public License
1589f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
16f5402f3dSGreg Roach */
17f5402f3dSGreg Roach
18f5402f3dSGreg Roachdeclare(strict_types=1);
19f5402f3dSGreg Roach
20f5402f3dSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
21f5402f3dSGreg Roach
22f5402f3dSGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
23f5402f3dSGreg Roachuse Fisharebest\Webtrees\I18N;
24f5402f3dSGreg Roachuse Fisharebest\Webtrees\Services\SearchService;
25f5402f3dSGreg Roachuse Fisharebest\Webtrees\Services\TreeService;
26f5402f3dSGreg Roachuse Fisharebest\Webtrees\Site;
27f5402f3dSGreg Roachuse Fisharebest\Webtrees\Tree;
28075d1a05SGreg Roachuse Illuminate\Support\Collection;
29f5402f3dSGreg Roachuse Psr\Http\Message\ResponseInterface;
30f5402f3dSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
31f5402f3dSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
32f5402f3dSGreg Roach
33f5402f3dSGreg Roachuse function array_filter;
34f5402f3dSGreg Roachuse function assert;
35f5402f3dSGreg Roachuse function in_array;
36f5402f3dSGreg Roach
37f5402f3dSGreg Roach/**
38f5402f3dSGreg Roach * Search for (and optionally replace) genealogy data
39f5402f3dSGreg Roach */
40f5402f3dSGreg Roachclass SearchPhoneticPage implements RequestHandlerInterface
41f5402f3dSGreg Roach{
42f5402f3dSGreg Roach    use ViewResponseTrait;
43f5402f3dSGreg Roach
44f5402f3dSGreg Roach    /** @var SearchService */
45f5402f3dSGreg Roach    private $search_service;
46f5402f3dSGreg Roach
47f5402f3dSGreg Roach    /** @var TreeService */
48f5402f3dSGreg Roach    private $tree_service;
49f5402f3dSGreg Roach
50f5402f3dSGreg Roach    /**
51f5402f3dSGreg Roach     * SearchController constructor.
52f5402f3dSGreg Roach     *
53f5402f3dSGreg Roach     * @param SearchService $search_service
54f5402f3dSGreg Roach     * @param TreeService   $tree_service
55f5402f3dSGreg Roach     */
56f5402f3dSGreg Roach    public function __construct(SearchService $search_service, TreeService $tree_service)
57f5402f3dSGreg Roach    {
58f5402f3dSGreg Roach        $this->search_service = $search_service;
59f5402f3dSGreg Roach        $this->tree_service   = $tree_service;
60f5402f3dSGreg Roach    }
61f5402f3dSGreg Roach
62f5402f3dSGreg Roach    /**
63f5402f3dSGreg Roach     * The phonetic search.
64f5402f3dSGreg Roach     *
65f5402f3dSGreg Roach     * @param ServerRequestInterface $request
66f5402f3dSGreg Roach     *
67f5402f3dSGreg Roach     * @return ResponseInterface
68f5402f3dSGreg Roach     */
69f5402f3dSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
70f5402f3dSGreg Roach    {
71f5402f3dSGreg Roach        $tree = $request->getAttribute('tree');
7275964c75SGreg Roach        assert($tree instanceof Tree);
73f5402f3dSGreg Roach
74f5402f3dSGreg Roach        $params    = $request->getQueryParams();
75f5402f3dSGreg Roach        $firstname = $params['firstname'] ?? '';
76f5402f3dSGreg Roach        $lastname  = $params['lastname'] ?? '';
77f5402f3dSGreg Roach        $place     = $params['place'] ?? '';
78f5402f3dSGreg Roach        $soundex   = $params['soundex'] ?? 'Russell';
79f5402f3dSGreg Roach
80fceda430SGreg Roach        // What trees to search?
81f5402f3dSGreg Roach        if (Site::getPreference('ALLOW_CHANGE_GEDCOM') === '1') {
82*ca2d6785SGreg Roach            $all_trees = $this->tree_service->all();
83f5402f3dSGreg Roach        } else {
84*ca2d6785SGreg Roach            $all_trees = new Collection([$tree]);
85f5402f3dSGreg Roach        }
86f5402f3dSGreg Roach
87*ca2d6785SGreg Roach        $search_tree_names = new Collection($params['search_trees'] ?? []);
88f5402f3dSGreg Roach
89*ca2d6785SGreg Roach        $search_trees = $all_trees
90*ca2d6785SGreg Roach            ->filter(static function (Tree $tree) use ($search_tree_names): bool {
91*ca2d6785SGreg Roach                return $search_tree_names->containsStrict($tree->name());
92f5402f3dSGreg Roach            });
93f5402f3dSGreg Roach
94*ca2d6785SGreg Roach        if ($search_trees->isEmpty()) {
95*ca2d6785SGreg Roach            $search_trees->add($tree);
96f5402f3dSGreg Roach        }
97f5402f3dSGreg Roach
98075d1a05SGreg Roach        $individuals = new Collection();
99f5402f3dSGreg Roach
100f5402f3dSGreg Roach        if ($lastname !== '' || $firstname !== '' || $place !== '') {
101*ca2d6785SGreg Roach            $individuals = $this->search_service->searchIndividualsPhonetic($soundex, $lastname, $firstname, $place, $search_trees->all());
102f5402f3dSGreg Roach        }
103f5402f3dSGreg Roach
104f5402f3dSGreg Roach        $title = I18N::translate('Phonetic search');
105f5402f3dSGreg Roach
106f5402f3dSGreg Roach        return $this->viewResponse('search-phonetic-page', [
107f5402f3dSGreg Roach            'all_trees'    => $all_trees,
108f5402f3dSGreg Roach            'firstname'    => $firstname,
109f5402f3dSGreg Roach            'individuals'  => $individuals,
110f5402f3dSGreg Roach            'lastname'     => $lastname,
111f5402f3dSGreg Roach            'place'        => $place,
112f5402f3dSGreg Roach            'search_trees' => $search_trees,
113f5402f3dSGreg Roach            'soundex'      => $soundex,
114f5402f3dSGreg Roach            'title'        => $title,
115f5402f3dSGreg Roach            'tree'         => $tree,
116f5402f3dSGreg Roach        ]);
117f5402f3dSGreg Roach    }
118f5402f3dSGreg Roach}
119