xref: /webtrees/app/Http/RequestHandlers/HomePage.php (revision b55cbc6b43247e8b2ad14af6f6d24dc6747195ff)
10c0910bfSGreg Roach<?php
20c0910bfSGreg Roach
30c0910bfSGreg Roach/**
40c0910bfSGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 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
1589f7189bSGreg Roach * along with this program. If not, see <https://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;
28*b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
290c0910bfSGreg Roachuse Psr\Http\Message\ResponseInterface;
300c0910bfSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
310c0910bfSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
320c0910bfSGreg Roach
330c0910bfSGreg Roachuse function redirect;
340c0910bfSGreg Roachuse function route;
350c0910bfSGreg Roach
360c0910bfSGreg Roach/**
370c0910bfSGreg Roach * Redirect to a user/tree page.
380c0910bfSGreg Roach */
390c0910bfSGreg Roachclass HomePage implements RequestHandlerInterface
400c0910bfSGreg Roach{
410c0910bfSGreg Roach    use ViewResponseTrait;
420c0910bfSGreg Roach
43c4943cffSGreg Roach    private TreeService $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();
64*b55cbc6bSGreg Roach        $user    = Validator::attributes($request)->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) {
708e0e1b25SGreg Roach                    return redirect(route(UserPage::class, ['tree' => $tree->name()]));
710c0910bfSGreg Roach                }
720c0910bfSGreg Roach
730c0910bfSGreg Roach                // Not logged in?  Go to the tree's page.
748e0e1b25SGreg Roach                return redirect(route(TreePage::class, ['tree' => $tree->name()]));
750c0910bfSGreg Roach            }
760c0910bfSGreg Roach
770c0910bfSGreg Roach            if (Auth::isManager($tree, $user)) {
786fd01894SGreg Roach                return redirect(route(ManageTrees::class, ['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) {
89a49d0e3fSGreg Roach            return $this->viewResponse('errors/no-tree-access', ['title' => '', 'tree' => null]);
900c0910bfSGreg Roach        }
910c0910bfSGreg Roach
920c0910bfSGreg Roach        // Not logged in.
9371359d06SGreg Roach        return redirect(route(LoginPage::class, ['url' => '']));
940c0910bfSGreg Roach    }
950c0910bfSGreg Roach}
96