xref: /webtrees/app/Http/RequestHandlers/SearchGeneralPage.php (revision 75964c75fd01f8e12e2a313cc89fc2222413a03f)
1f5402f3dSGreg Roach<?php
2f5402f3dSGreg Roach
3f5402f3dSGreg Roach/**
4f5402f3dSGreg Roach * webtrees: online genealogy
5f5402f3dSGreg Roach * Copyright (C) 2019 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
15f5402f3dSGreg Roach * along with this program. If not, see <http://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;
28f5402f3dSGreg 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 assert;
34f5402f3dSGreg Roachuse function preg_match;
35f5402f3dSGreg Roachuse function str_replace;
36f5402f3dSGreg Roachuse function trim;
37f5402f3dSGreg Roach
38f5402f3dSGreg Roach/**
39f5402f3dSGreg Roach * Search for genealogy data
40f5402f3dSGreg Roach */
41f5402f3dSGreg Roachclass SearchGeneralPage implements RequestHandlerInterface
42f5402f3dSGreg Roach{
43f5402f3dSGreg Roach    use ViewResponseTrait;
44f5402f3dSGreg Roach
45f5402f3dSGreg Roach    /** @var SearchService */
46f5402f3dSGreg Roach    private $search_service;
47f5402f3dSGreg Roach
48f5402f3dSGreg Roach    /** @var TreeService */
49f5402f3dSGreg Roach    private $tree_service;
50f5402f3dSGreg Roach
51f5402f3dSGreg Roach    /**
52f5402f3dSGreg Roach     * SearchController constructor.
53f5402f3dSGreg Roach     *
54f5402f3dSGreg Roach     * @param SearchService $search_service
55f5402f3dSGreg Roach     * @param TreeService   $tree_service
56f5402f3dSGreg Roach     */
57f5402f3dSGreg Roach    public function __construct(SearchService $search_service, TreeService $tree_service)
58f5402f3dSGreg Roach    {
59f5402f3dSGreg Roach        $this->search_service = $search_service;
60f5402f3dSGreg Roach        $this->tree_service   = $tree_service;
61f5402f3dSGreg Roach    }
62f5402f3dSGreg Roach
63f5402f3dSGreg Roach    /**
64f5402f3dSGreg Roach     * The standard search.
65f5402f3dSGreg Roach     *
66f5402f3dSGreg Roach     * @param ServerRequestInterface $request
67f5402f3dSGreg Roach     *
68f5402f3dSGreg Roach     * @return ResponseInterface
69f5402f3dSGreg Roach     */
70f5402f3dSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
71f5402f3dSGreg Roach    {
72f5402f3dSGreg Roach        $tree = $request->getAttribute('tree');
73*75964c75SGreg Roach        assert($tree instanceof Tree);
74f5402f3dSGreg Roach
75f5402f3dSGreg Roach        $params = $request->getQueryParams();
76f5402f3dSGreg Roach        $query  = $params['query'] ?? '';
77f5402f3dSGreg Roach
78f5402f3dSGreg Roach        // What type of records to search?
79f5402f3dSGreg Roach        $search_individuals  = (bool) ($params['search_individuals'] ?? false);
80f5402f3dSGreg Roach        $search_families     = (bool) ($params['search_families'] ?? false);
81f5402f3dSGreg Roach        $search_repositories = (bool) ($params['search_repositories'] ?? false);
82f5402f3dSGreg Roach        $search_sources      = (bool) ($params['search_sources'] ?? false);
83f5402f3dSGreg Roach        $search_notes        = (bool) ($params['search_notes'] ?? false);
84f5402f3dSGreg Roach
85f5402f3dSGreg Roach        // Default to individuals only
86f5402f3dSGreg Roach        if (!$search_individuals && !$search_families && !$search_repositories && !$search_sources && !$search_notes) {
87f5402f3dSGreg Roach            $search_individuals = true;
88f5402f3dSGreg Roach        }
89f5402f3dSGreg Roach
90f5402f3dSGreg Roach        // What to search for?
91f5402f3dSGreg Roach        $search_terms = $this->extractSearchTerms($query);
92f5402f3dSGreg Roach
93f5402f3dSGreg Roach        // What trees to seach?
94f5402f3dSGreg Roach        if (Site::getPreference('ALLOW_CHANGE_GEDCOM') === '1') {
95f5402f3dSGreg Roach            $all_trees = $this->tree_service->all()->all();
96f5402f3dSGreg Roach        } else {
97f5402f3dSGreg Roach            $all_trees = [$tree];
98f5402f3dSGreg Roach        }
99f5402f3dSGreg Roach
100f5402f3dSGreg Roach        $search_tree_names = $params['search_trees'] ?? [];
101f5402f3dSGreg Roach
102f5402f3dSGreg Roach        $search_trees = array_filter($all_trees, static function (Tree $tree) use ($search_tree_names): bool {
103f5402f3dSGreg Roach            return in_array($tree->name(), $search_tree_names, true);
104f5402f3dSGreg Roach        });
105f5402f3dSGreg Roach
106f5402f3dSGreg Roach        if ($search_trees === []) {
107f5402f3dSGreg Roach            $search_trees = [$tree];
108f5402f3dSGreg Roach        }
109f5402f3dSGreg Roach
110f5402f3dSGreg Roach        // Do the search
111f5402f3dSGreg Roach        $individuals  = new Collection();
112f5402f3dSGreg Roach        $families     = new Collection();
113f5402f3dSGreg Roach        $repositories = new Collection();
114f5402f3dSGreg Roach        $sources      = new Collection();
115f5402f3dSGreg Roach        $notes        = new Collection();
116f5402f3dSGreg Roach
117f5402f3dSGreg Roach        if ($search_terms !== []) {
118f5402f3dSGreg Roach            if ($search_individuals) {
119f5402f3dSGreg Roach                $individuals = $this->search_service->searchIndividuals($search_trees, $search_terms);
120f5402f3dSGreg Roach            }
121f5402f3dSGreg Roach
122f5402f3dSGreg Roach            if ($search_families) {
123f5402f3dSGreg Roach                $tmp1 = $this->search_service->searchFamilies($search_trees, $search_terms);
124f5402f3dSGreg Roach                $tmp2 = $this->search_service->searchFamilyNames($search_trees, $search_terms);
125f5402f3dSGreg Roach
126f5402f3dSGreg Roach                $families = $tmp1->merge($tmp2)->unique();
127f5402f3dSGreg Roach            }
128f5402f3dSGreg Roach
129f5402f3dSGreg Roach            if ($search_repositories) {
130f5402f3dSGreg Roach                $repositories = $this->search_service->searchRepositories($search_trees, $search_terms);
131f5402f3dSGreg Roach            }
132f5402f3dSGreg Roach
133f5402f3dSGreg Roach            if ($search_sources) {
134f5402f3dSGreg Roach                $sources = $this->search_service->searchSources($search_trees, $search_terms);
135f5402f3dSGreg Roach            }
136f5402f3dSGreg Roach
137f5402f3dSGreg Roach            if ($search_notes) {
138f5402f3dSGreg Roach                $notes = $this->search_service->searchNotes($search_trees, $search_terms);
139f5402f3dSGreg Roach            }
140f5402f3dSGreg Roach        }
141f5402f3dSGreg Roach
142f5402f3dSGreg Roach        // If only 1 item is returned, automatically forward to that item
143f5402f3dSGreg Roach        if ($individuals->count() === 1 && $families->isEmpty() && $sources->isEmpty() && $notes->isEmpty()) {
144f5402f3dSGreg Roach            return redirect($individuals->first()->url());
145f5402f3dSGreg Roach        }
146f5402f3dSGreg Roach
147f5402f3dSGreg Roach        if ($individuals->isEmpty() && $families->count() === 1 && $sources->isEmpty() && $notes->isEmpty()) {
148f5402f3dSGreg Roach            return redirect($families->first()->url());
149f5402f3dSGreg Roach        }
150f5402f3dSGreg Roach
151f5402f3dSGreg Roach        if ($individuals->isEmpty() && $families->isEmpty() && $sources->count() === 1 && $notes->isEmpty()) {
152f5402f3dSGreg Roach            return redirect($sources->first()->url());
153f5402f3dSGreg Roach        }
154f5402f3dSGreg Roach
155f5402f3dSGreg Roach        if ($individuals->isEmpty() && $families->isEmpty() && $sources->isEmpty() && $notes->count() === 1) {
156f5402f3dSGreg Roach            return redirect($notes->first()->url());
157f5402f3dSGreg Roach        }
158f5402f3dSGreg Roach
159f5402f3dSGreg Roach        $title = I18N::translate('General search');
160f5402f3dSGreg Roach
161f5402f3dSGreg Roach        return $this->viewResponse('search-general-page', [
162f5402f3dSGreg Roach            'all_trees'           => $all_trees,
163f5402f3dSGreg Roach            'families'            => $families,
164f5402f3dSGreg Roach            'individuals'         => $individuals,
165f5402f3dSGreg Roach            'notes'               => $notes,
166f5402f3dSGreg Roach            'query'               => $query,
167f5402f3dSGreg Roach            'repositories'        => $repositories,
168f5402f3dSGreg Roach            'search_families'     => $search_families,
169f5402f3dSGreg Roach            'search_individuals'  => $search_individuals,
170f5402f3dSGreg Roach            'search_notes'        => $search_notes,
171f5402f3dSGreg Roach            'search_repositories' => $search_repositories,
172f5402f3dSGreg Roach            'search_sources'      => $search_sources,
173f5402f3dSGreg Roach            'search_trees'        => $search_trees,
174f5402f3dSGreg Roach            'sources'             => $sources,
175f5402f3dSGreg Roach            'title'               => $title,
176f5402f3dSGreg Roach            'tree'                => $tree,
177f5402f3dSGreg Roach        ]);
178f5402f3dSGreg Roach    }
179f5402f3dSGreg Roach
180f5402f3dSGreg Roach    /**
181f5402f3dSGreg Roach     * Convert the query into an array of search terms
182f5402f3dSGreg Roach     *
183f5402f3dSGreg Roach     * @param string $query
184f5402f3dSGreg Roach     *
185f5402f3dSGreg Roach     * @return string[]
186f5402f3dSGreg Roach     */
187f5402f3dSGreg Roach    private function extractSearchTerms(string $query): array
188f5402f3dSGreg Roach    {
189f5402f3dSGreg Roach        $search_terms = [];
190f5402f3dSGreg Roach
191f5402f3dSGreg Roach        // Words in double quotes stay together
192f5402f3dSGreg Roach        while (preg_match('/"([^"]+)"/', $query, $match)) {
193f5402f3dSGreg Roach            $search_terms[] = trim($match[1]);
194f5402f3dSGreg Roach            $query          = str_replace($match[0], '', $query);
195f5402f3dSGreg Roach        }
196f5402f3dSGreg Roach
197f5402f3dSGreg Roach        // Other words get treated separately
198f5402f3dSGreg Roach        while (preg_match('/[\S]+/', $query, $match)) {
199f5402f3dSGreg Roach            $search_terms[] = trim($match[0]);
200f5402f3dSGreg Roach            $query          = str_replace($match[0], '', $query);
201f5402f3dSGreg Roach        }
202f5402f3dSGreg Roach
203f5402f3dSGreg Roach        return $search_terms;
204f5402f3dSGreg Roach    }
205f5402f3dSGreg Roach}
206