xref: /webtrees/app/Http/RequestHandlers/SearchQuickAction.php (revision f5402f3dbd759ec94d88460e5ae9815f9b823dae)
1*f5402f3dSGreg Roach<?php
2*f5402f3dSGreg Roach
3*f5402f3dSGreg Roach/**
4*f5402f3dSGreg Roach * webtrees: online genealogy
5*f5402f3dSGreg Roach * Copyright (C) 2019 webtrees development team
6*f5402f3dSGreg Roach * This program is free software: you can redistribute it and/or modify
7*f5402f3dSGreg Roach * it under the terms of the GNU General Public License as published by
8*f5402f3dSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9*f5402f3dSGreg Roach * (at your option) any later version.
10*f5402f3dSGreg Roach * This program is distributed in the hope that it will be useful,
11*f5402f3dSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*f5402f3dSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13*f5402f3dSGreg Roach * GNU General Public License for more details.
14*f5402f3dSGreg Roach * You should have received a copy of the GNU General Public License
15*f5402f3dSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
16*f5402f3dSGreg Roach */
17*f5402f3dSGreg Roach
18*f5402f3dSGreg Roachdeclare(strict_types=1);
19*f5402f3dSGreg Roach
20*f5402f3dSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
21*f5402f3dSGreg Roach
22*f5402f3dSGreg Roachuse Fisharebest\Webtrees\GedcomRecord;
23*f5402f3dSGreg Roachuse Fisharebest\Webtrees\Tree;
24*f5402f3dSGreg Roachuse InvalidArgumentException;
25*f5402f3dSGreg Roachuse Psr\Http\Message\ResponseInterface;
26*f5402f3dSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
27*f5402f3dSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
28*f5402f3dSGreg Roach
29*f5402f3dSGreg Roachuse function assert;
30*f5402f3dSGreg Roachuse function redirect;
31*f5402f3dSGreg Roachuse function route;
32*f5402f3dSGreg Roach
33*f5402f3dSGreg Roach/**
34*f5402f3dSGreg Roach * Search for genealogy data
35*f5402f3dSGreg Roach */
36*f5402f3dSGreg Roachclass SearchQuickAction implements RequestHandlerInterface
37*f5402f3dSGreg Roach{
38*f5402f3dSGreg Roach    /**
39*f5402f3dSGreg Roach     * The "omni-search" box in the header.
40*f5402f3dSGreg Roach     *
41*f5402f3dSGreg Roach     * @param ServerRequestInterface $request
42*f5402f3dSGreg Roach     *
43*f5402f3dSGreg Roach     * @return ResponseInterface
44*f5402f3dSGreg Roach     */
45*f5402f3dSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
46*f5402f3dSGreg Roach    {
47*f5402f3dSGreg Roach        $tree = $request->getAttribute('tree');
48*f5402f3dSGreg Roach        assert($tree instanceof Tree, new InvalidArgumentException());
49*f5402f3dSGreg Roach
50*f5402f3dSGreg Roach        $query = $request->getParsedBody()['query'] ?? '';
51*f5402f3dSGreg Roach
52*f5402f3dSGreg Roach        // Was the search query an XREF in the current tree?
53*f5402f3dSGreg Roach        // If so, go straight to it.
54*f5402f3dSGreg Roach        $record = GedcomRecord::getInstance($query, $tree);
55*f5402f3dSGreg Roach
56*f5402f3dSGreg Roach        if ($record instanceof GedcomRecord && $record->canShow()) {
57*f5402f3dSGreg Roach            return redirect($record->url());
58*f5402f3dSGreg Roach        }
59*f5402f3dSGreg Roach
60*f5402f3dSGreg Roach        return redirect(route(SearchGeneralPage::class, ['tree' => $tree->name(), 'query' => $query]));
61*f5402f3dSGreg Roach    }
62*f5402f3dSGreg Roach}
63