xref: /webtrees/app/Http/RequestHandlers/SelectDefaultTree.php (revision fcfa147e10aaa6c7ff580c29bd6e5b88666befc1)
1da1c67ccSGreg Roach<?php
2da1c67ccSGreg Roach
3da1c67ccSGreg Roach/**
4da1c67ccSGreg Roach * webtrees: online genealogy
5da1c67ccSGreg Roach * Copyright (C) 2019 webtrees development team
6da1c67ccSGreg Roach * This program is free software: you can redistribute it and/or modify
7da1c67ccSGreg Roach * it under the terms of the GNU General Public License as published by
8da1c67ccSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9da1c67ccSGreg Roach * (at your option) any later version.
10da1c67ccSGreg Roach * This program is distributed in the hope that it will be useful,
11da1c67ccSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12da1c67ccSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13da1c67ccSGreg Roach * GNU General Public License for more details.
14da1c67ccSGreg Roach * You should have received a copy of the GNU General Public License
15da1c67ccSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
16da1c67ccSGreg Roach */
17*fcfa147eSGreg Roach
18da1c67ccSGreg Roachdeclare(strict_types=1);
19da1c67ccSGreg Roach
20da1c67ccSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
21da1c67ccSGreg Roach
22da1c67ccSGreg Roachuse Fig\Http\Message\StatusCodeInterface;
23da1c67ccSGreg Roachuse Fisharebest\Webtrees\FlashMessages;
24da1c67ccSGreg Roachuse Fisharebest\Webtrees\I18N;
25da1c67ccSGreg Roachuse Fisharebest\Webtrees\Site;
26da1c67ccSGreg Roachuse Fisharebest\Webtrees\Tree;
27da1c67ccSGreg Roachuse InvalidArgumentException;
28da1c67ccSGreg Roachuse Psr\Http\Message\ResponseInterface;
29da1c67ccSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
30da1c67ccSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
31da1c67ccSGreg Roach
32da1c67ccSGreg Roachuse function assert;
33da1c67ccSGreg Roachuse function e;
34da1c67ccSGreg Roachuse function redirect;
35da1c67ccSGreg Roachuse function route;
36da1c67ccSGreg Roach
37da1c67ccSGreg Roach/**
38da1c67ccSGreg Roach * Set the default tree.
39da1c67ccSGreg Roach */
40da1c67ccSGreg Roachclass SelectDefaultTree implements RequestHandlerInterface, StatusCodeInterface
41da1c67ccSGreg Roach{
42da1c67ccSGreg Roach    /**
43da1c67ccSGreg Roach     * @param ServerRequestInterface $request
44da1c67ccSGreg Roach     *
45da1c67ccSGreg Roach     * @return ResponseInterface
46da1c67ccSGreg Roach     */
47da1c67ccSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
48da1c67ccSGreg Roach    {
49da1c67ccSGreg Roach        $tree = $request->getAttribute('tree');
50da1c67ccSGreg Roach        assert($tree instanceof Tree, new InvalidArgumentException());
51da1c67ccSGreg Roach
52da1c67ccSGreg Roach        Site::setPreference('DEFAULT_GEDCOM', $tree->name());
53da1c67ccSGreg Roach
54da1c67ccSGreg Roach        /* I18N: %s is the name of a family tree */
55da1c67ccSGreg Roach        $message = I18N::translate('The family tree “%s” will be shown to visitors when they first arrive at this website.', e($tree->title()));
56da1c67ccSGreg Roach
57da1c67ccSGreg Roach        FlashMessages::addMessage($message, 'success');
58da1c67ccSGreg Roach
59da1c67ccSGreg Roach        $url = route('manage-trees', ['tree' => $tree->name()]);
60da1c67ccSGreg Roach
61da1c67ccSGreg Roach        return redirect($url);
62da1c67ccSGreg Roach    }
63da1c67ccSGreg Roach}
64