xref: /webtrees/app/Http/RequestHandlers/RedirectDescendencyPhp.php (revision e873f434551745f888937263ff89e80db3b0f785)
155134edcSGreg Roach<?php
255134edcSGreg Roach
355134edcSGreg Roach/**
455134edcSGreg Roach * webtrees: online genealogy
5d11be702SGreg Roach * Copyright (C) 2023 webtrees development team
655134edcSGreg Roach * This program is free software: you can redistribute it and/or modify
755134edcSGreg Roach * it under the terms of the GNU General Public License as published by
855134edcSGreg Roach * the Free Software Foundation, either version 3 of the License, or
955134edcSGreg Roach * (at your option) any later version.
1055134edcSGreg Roach * This program is distributed in the hope that it will be useful,
1155134edcSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
1255134edcSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
1355134edcSGreg Roach * GNU General Public License for more details.
1455134edcSGreg 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/>.
1655134edcSGreg Roach */
1755134edcSGreg Roach
1855134edcSGreg Roachdeclare(strict_types=1);
1955134edcSGreg Roach
2055134edcSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
2155134edcSGreg Roach
2255134edcSGreg Roachuse Fig\Http\Message\StatusCodeInterface;
2355134edcSGreg Roachuse Fisharebest\Webtrees\Auth;
2415c4f62cSGreg Roachuse Fisharebest\Webtrees\Http\Exceptions\HttpGoneException;
2555134edcSGreg Roachuse Fisharebest\Webtrees\Module\DescendancyChartModule;
2600ef1d3aSGreg Roachuse Fisharebest\Webtrees\Module\ModuleChartInterface;
276b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
283340ecd2SGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
2955134edcSGreg Roachuse Fisharebest\Webtrees\Services\TreeService;
3055134edcSGreg Roachuse Fisharebest\Webtrees\Site;
3155134edcSGreg Roachuse Fisharebest\Webtrees\Tree;
32266e9c61SGreg Roachuse Fisharebest\Webtrees\Validator;
3355134edcSGreg Roachuse Psr\Http\Message\ResponseInterface;
3455134edcSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
3555134edcSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
3655134edcSGreg Roach
3755134edcSGreg Roach/**
3855134edcSGreg Roach * Redirect URLs created by webtrees 1.x (and PhpGedView).
3955134edcSGreg Roach */
4055134edcSGreg Roachclass RedirectDescendencyPhp implements RequestHandlerInterface
4155134edcSGreg Roach{
42*e873f434SGreg Roach    private const array CHART_STYLES = [
433d15214dSGreg Roach        0 => 'tree',
443d15214dSGreg Roach        1 => 'tree',
453d15214dSGreg Roach        2 => 'individuals',
463d15214dSGreg Roach        3 => 'families',
4755134edcSGreg Roach    ];
4855134edcSGreg Roach
4915c4f62cSGreg Roach    public function __construct(
5015c4f62cSGreg Roach        private readonly ModuleService $module_service,
5115c4f62cSGreg Roach        private readonly TreeService $tree_service,
5215c4f62cSGreg Roach    ) {
5355134edcSGreg Roach    }
5455134edcSGreg Roach
5555134edcSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
5655134edcSGreg Roach    {
57266e9c61SGreg Roach        $ged  = Validator::queryParams($request)->string('ged', Site::getPreference('DEFAULT_GEDCOM'));
5800ef1d3aSGreg Roach        $tree = $this->tree_service->all()->get($ged);
5900ef1d3aSGreg Roach
6000ef1d3aSGreg Roach        if ($tree instanceof Tree) {
6100ef1d3aSGreg Roach            $module = $this->module_service
6200ef1d3aSGreg Roach                ->findByComponent(ModuleChartInterface::class, $tree, Auth::user())
6300ef1d3aSGreg Roach                ->first(static fn (ModuleChartInterface $module): bool => $module instanceof DescendancyChartModule);
6400ef1d3aSGreg Roach
6500ef1d3aSGreg Roach            if ($module instanceof DescendancyChartModule) {
66266e9c61SGreg Roach                $root_id     = Validator::queryParams($request)->string('rootid', '');
67266e9c61SGreg Roach                $generations = Validator::queryParams($request)->string('generations', DescendancyChartModule::DEFAULT_GENERATIONS);
68266e9c61SGreg Roach                $chart_style = Validator::queryParams($request)->string('chart_style', '');
696b9cb339SGreg Roach                $individual  = Registry::individualFactory()->make($root_id, $tree) ?? $tree->significantIndividual(Auth::user());
7055134edcSGreg Roach
713340ecd2SGreg Roach                $url = $module->chartUrl($individual, [
7255134edcSGreg Roach                    'generations' => $generations,
73266e9c61SGreg Roach                    'style'       => self::CHART_STYLES[$chart_style] ?? DescendancyChartModule::CHART_STYLE_TREE,
7455134edcSGreg Roach                ]);
7555134edcSGreg Roach
7615c4f62cSGreg Roach                return Registry::responseFactory()
7715c4f62cSGreg Roach                    ->redirectUrl($url, StatusCodeInterface::STATUS_MOVED_PERMANENTLY)
7815c4f62cSGreg Roach                    ->withHeader('Link', '<' . $url . '>; rel="canonical"');
7955134edcSGreg Roach            }
8000ef1d3aSGreg Roach        }
8155134edcSGreg Roach
8215c4f62cSGreg Roach        throw new HttpGoneException();
8355134edcSGreg Roach    }
8455134edcSGreg Roach}
85