xref: /webtrees/app/Http/RequestHandlers/HomePage.php (revision e93a8df2f8d797005750082cc3766c0e80799688)
10c0910bfSGreg Roach<?php
20c0910bfSGreg Roach
30c0910bfSGreg Roach/**
40c0910bfSGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 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;
28b55cbc6bSGreg 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     * @param TreeService $tree_service
470c0910bfSGreg Roach     */
480c0910bfSGreg Roach    public function __construct(TreeService $tree_service)
490c0910bfSGreg Roach    {
500c0910bfSGreg Roach        $this->tree_service = $tree_service;
510c0910bfSGreg Roach    }
520c0910bfSGreg Roach
530c0910bfSGreg Roach    /**
540c0910bfSGreg Roach     * @param ServerRequestInterface $request
550c0910bfSGreg Roach     *
560c0910bfSGreg Roach     * @return ResponseInterface
570c0910bfSGreg Roach     */
580c0910bfSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
590c0910bfSGreg Roach    {
600c0910bfSGreg Roach        $default = Site::getPreference('DEFAULT_GEDCOM');
611e653452SGreg Roach        $tree    = $this->tree_service->all()->get($default) ?? $this->tree_service->all()->first();
62b55cbc6bSGreg Roach        $user    = Validator::attributes($request)->user();
630c0910bfSGreg Roach
640c0910bfSGreg Roach        if ($tree instanceof Tree) {
650c0910bfSGreg Roach            if ($tree->getPreference('imported') === '1') {
660c0910bfSGreg Roach                // Logged in?  Go to the user's page.
670c0910bfSGreg Roach                if ($user instanceof User) {
688e0e1b25SGreg Roach                    return redirect(route(UserPage::class, ['tree' => $tree->name()]));
690c0910bfSGreg Roach                }
700c0910bfSGreg Roach
710c0910bfSGreg Roach                // Not logged in?  Go to the tree's page.
728e0e1b25SGreg Roach                return redirect(route(TreePage::class, ['tree' => $tree->name()]));
730c0910bfSGreg Roach            }
740c0910bfSGreg Roach
750c0910bfSGreg Roach            if (Auth::isManager($tree, $user)) {
766fd01894SGreg Roach                return redirect(route(ManageTrees::class, ['tree' => $tree->name()]));
770c0910bfSGreg Roach            }
780c0910bfSGreg Roach        }
790c0910bfSGreg Roach
800c0910bfSGreg Roach        // No tree available?  Create one.
810c0910bfSGreg Roach        if (Auth::isAdmin($user)) {
820c0910bfSGreg Roach            return redirect(route(CreateTreePage::class));
830c0910bfSGreg Roach        }
840c0910bfSGreg Roach
850c0910bfSGreg Roach        // Logged in, but no access to any tree.
860c0910bfSGreg Roach        if ($user instanceof User) {
87a49d0e3fSGreg Roach            return $this->viewResponse('errors/no-tree-access', ['title' => '', 'tree' => null]);
880c0910bfSGreg Roach        }
890c0910bfSGreg Roach
900c0910bfSGreg Roach        // Not logged in.
9171359d06SGreg Roach        return redirect(route(LoginPage::class, ['url' => '']));
920c0910bfSGreg Roach    }
930c0910bfSGreg Roach}
94