xref: /webtrees/app/Http/RequestHandlers/HomePage.php (revision a49d0e3f7d5d6400af7be7d1e718b56f3b490f38)
10c0910bfSGreg Roach<?php
20c0910bfSGreg Roach
30c0910bfSGreg Roach/**
40c0910bfSGreg Roach * webtrees: online genealogy
50c0910bfSGreg Roach * Copyright (C) 2019 webtrees development team
60c0910bfSGreg Roach * This program is free software: you can redistribute it and/or modify
70c0910bfSGreg Roach * it under the terms of the GNU General Public License as published by
80c0910bfSGreg Roach * the Free Software Foundation, either version 3 of the License, or
90c0910bfSGreg Roach * (at your option) any later version.
100c0910bfSGreg Roach * This program is distributed in the hope that it will be useful,
110c0910bfSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
120c0910bfSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
130c0910bfSGreg Roach * GNU General Public License for more details.
140c0910bfSGreg Roach * You should have received a copy of the GNU General Public License
150c0910bfSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
160c0910bfSGreg Roach */
17fcfa147eSGreg Roach
180c0910bfSGreg Roachdeclare(strict_types=1);
190c0910bfSGreg Roach
200c0910bfSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
210c0910bfSGreg Roach
220c0910bfSGreg Roachuse Fisharebest\Webtrees\Auth;
230c0910bfSGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
240c0910bfSGreg Roachuse Fisharebest\Webtrees\Services\TreeService;
250c0910bfSGreg Roachuse Fisharebest\Webtrees\Site;
260c0910bfSGreg Roachuse Fisharebest\Webtrees\Tree;
270c0910bfSGreg Roachuse Fisharebest\Webtrees\User;
280c0910bfSGreg Roachuse Psr\Http\Message\ResponseInterface;
290c0910bfSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
300c0910bfSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
310c0910bfSGreg Roach
320c0910bfSGreg Roachuse function redirect;
330c0910bfSGreg Roachuse function route;
340c0910bfSGreg Roach
350c0910bfSGreg Roach/**
360c0910bfSGreg Roach * Redirect to a user/tree page.
370c0910bfSGreg Roach */
380c0910bfSGreg Roachclass HomePage implements RequestHandlerInterface
390c0910bfSGreg Roach{
400c0910bfSGreg Roach    use ViewResponseTrait;
410c0910bfSGreg Roach
420c0910bfSGreg Roach    /** @var TreeService */
430c0910bfSGreg Roach    private $tree_service;
440c0910bfSGreg Roach
450c0910bfSGreg Roach    /**
460c0910bfSGreg Roach     * HomePage constructor.
470c0910bfSGreg Roach     *
480c0910bfSGreg Roach     * @param TreeService $tree_service
490c0910bfSGreg Roach     */
500c0910bfSGreg Roach    public function __construct(TreeService $tree_service)
510c0910bfSGreg Roach    {
520c0910bfSGreg Roach        $this->tree_service = $tree_service;
530c0910bfSGreg Roach    }
540c0910bfSGreg Roach
550c0910bfSGreg Roach    /**
560c0910bfSGreg Roach     * @param ServerRequestInterface $request
570c0910bfSGreg Roach     *
580c0910bfSGreg Roach     * @return ResponseInterface
590c0910bfSGreg Roach     */
600c0910bfSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
610c0910bfSGreg Roach    {
620c0910bfSGreg Roach        $default = Site::getPreference('DEFAULT_GEDCOM');
631e653452SGreg Roach        $tree    = $this->tree_service->all()->get($default) ?? $this->tree_service->all()->first();
640c0910bfSGreg Roach        $user    = $request->getAttribute('user');
650c0910bfSGreg Roach
660c0910bfSGreg Roach        if ($tree instanceof Tree) {
670c0910bfSGreg Roach            if ($tree->getPreference('imported') === '1') {
680c0910bfSGreg Roach                // Logged in?  Go to the user's page.
690c0910bfSGreg Roach                if ($user instanceof User) {
700c0910bfSGreg Roach                    return redirect(route('user-page', ['tree' => $tree->name()]));
710c0910bfSGreg Roach                }
720c0910bfSGreg Roach
730c0910bfSGreg Roach                // Not logged in?  Go to the tree's page.
740c0910bfSGreg Roach                return redirect(route('tree-page', ['tree' => $tree->name()]));
750c0910bfSGreg Roach            }
760c0910bfSGreg Roach
770c0910bfSGreg Roach            if (Auth::isManager($tree, $user)) {
780c0910bfSGreg Roach                return redirect(route('manage-trees', ['tree' => $tree->name()]));
790c0910bfSGreg Roach            }
800c0910bfSGreg Roach        }
810c0910bfSGreg Roach
820c0910bfSGreg Roach        // No tree available?  Create one.
830c0910bfSGreg Roach        if (Auth::isAdmin($user)) {
840c0910bfSGreg Roach            return redirect(route(CreateTreePage::class));
850c0910bfSGreg Roach        }
860c0910bfSGreg Roach
870c0910bfSGreg Roach        // Logged in, but no access to any tree.
880c0910bfSGreg Roach        if ($user instanceof User) {
89*a49d0e3fSGreg Roach            return $this->viewResponse('errors/no-tree-access', ['title' => '', 'tree' => null]);
900c0910bfSGreg Roach        }
910c0910bfSGreg Roach
920c0910bfSGreg Roach        // Not logged in.
930c0910bfSGreg Roach        return redirect(route(LoginPage::class, ['url' => $request->getUri()]));
940c0910bfSGreg Roach    }
950c0910bfSGreg Roach}
96