xref: /webtrees/app/Http/RequestHandlers/HomePage.php (revision c4943cff72f95a28fbb9404e3c20b169ff098e5c)
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;
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
42*c4943cffSGreg Roach    private TreeService $tree_service;
430c0910bfSGreg Roach
440c0910bfSGreg Roach    /**
450c0910bfSGreg Roach     * HomePage constructor.
460c0910bfSGreg Roach     *
470c0910bfSGreg Roach     * @param TreeService $tree_service
480c0910bfSGreg Roach     */
490c0910bfSGreg Roach    public function __construct(TreeService $tree_service)
500c0910bfSGreg Roach    {
510c0910bfSGreg Roach        $this->tree_service = $tree_service;
520c0910bfSGreg Roach    }
530c0910bfSGreg Roach
540c0910bfSGreg Roach    /**
550c0910bfSGreg Roach     * @param ServerRequestInterface $request
560c0910bfSGreg Roach     *
570c0910bfSGreg Roach     * @return ResponseInterface
580c0910bfSGreg Roach     */
590c0910bfSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
600c0910bfSGreg Roach    {
610c0910bfSGreg Roach        $default = Site::getPreference('DEFAULT_GEDCOM');
621e653452SGreg Roach        $tree    = $this->tree_service->all()->get($default) ?? $this->tree_service->all()->first();
630c0910bfSGreg Roach        $user    = $request->getAttribute('user');
640c0910bfSGreg Roach
650c0910bfSGreg Roach        if ($tree instanceof Tree) {
660c0910bfSGreg Roach            if ($tree->getPreference('imported') === '1') {
670c0910bfSGreg Roach                // Logged in?  Go to the user's page.
680c0910bfSGreg Roach                if ($user instanceof User) {
698e0e1b25SGreg Roach                    return redirect(route(UserPage::class, ['tree' => $tree->name()]));
700c0910bfSGreg Roach                }
710c0910bfSGreg Roach
720c0910bfSGreg Roach                // Not logged in?  Go to the tree's page.
738e0e1b25SGreg Roach                return redirect(route(TreePage::class, ['tree' => $tree->name()]));
740c0910bfSGreg Roach            }
750c0910bfSGreg Roach
760c0910bfSGreg Roach            if (Auth::isManager($tree, $user)) {
776fd01894SGreg Roach                return redirect(route(ManageTrees::class, ['tree' => $tree->name()]));
780c0910bfSGreg Roach            }
790c0910bfSGreg Roach        }
800c0910bfSGreg Roach
810c0910bfSGreg Roach        // No tree available?  Create one.
820c0910bfSGreg Roach        if (Auth::isAdmin($user)) {
830c0910bfSGreg Roach            return redirect(route(CreateTreePage::class));
840c0910bfSGreg Roach        }
850c0910bfSGreg Roach
860c0910bfSGreg Roach        // Logged in, but no access to any tree.
870c0910bfSGreg Roach        if ($user instanceof User) {
88a49d0e3fSGreg Roach            return $this->viewResponse('errors/no-tree-access', ['title' => '', 'tree' => null]);
890c0910bfSGreg Roach        }
900c0910bfSGreg Roach
910c0910bfSGreg Roach        // Not logged in.
9271359d06SGreg Roach        return redirect(route(LoginPage::class, ['url' => '']));
930c0910bfSGreg Roach    }
940c0910bfSGreg Roach}
95