xref: /webtrees/app/Http/RequestHandlers/ReportSetupAction.php (revision 89f7189b61a494347591c99bdb92afb7d8b66e1b)
11cfe16bdSGreg Roach<?php
21cfe16bdSGreg Roach
31cfe16bdSGreg Roach/**
41cfe16bdSGreg Roach * webtrees: online genealogy
5*89f7189bSGreg Roach * Copyright (C) 2021 webtrees development team
61cfe16bdSGreg Roach * This program is free software: you can redistribute it and/or modify
71cfe16bdSGreg Roach * it under the terms of the GNU General Public License as published by
81cfe16bdSGreg Roach * the Free Software Foundation, either version 3 of the License, or
91cfe16bdSGreg Roach * (at your option) any later version.
101cfe16bdSGreg Roach * This program is distributed in the hope that it will be useful,
111cfe16bdSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
121cfe16bdSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
131cfe16bdSGreg Roach * GNU General Public License for more details.
141cfe16bdSGreg Roach * You should have received a copy of the GNU General Public License
15*89f7189bSGreg Roach * along with this program. If not, see <https://www.gnu.org/licenses/>.
161cfe16bdSGreg Roach */
171cfe16bdSGreg Roach
181cfe16bdSGreg Roachdeclare(strict_types=1);
191cfe16bdSGreg Roach
201cfe16bdSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
211cfe16bdSGreg Roach
221cfe16bdSGreg Roachuse Fisharebest\Webtrees\Auth;
231cfe16bdSGreg Roachuse Fisharebest\Webtrees\Contracts\UserInterface;
241cfe16bdSGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
251cfe16bdSGreg Roachuse Fisharebest\Webtrees\Module\ModuleReportInterface;
261cfe16bdSGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
271cfe16bdSGreg Roachuse Fisharebest\Webtrees\Tree;
281cfe16bdSGreg Roachuse Psr\Http\Message\ResponseInterface;
291cfe16bdSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
301cfe16bdSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
311cfe16bdSGreg Roach
321cfe16bdSGreg Roachuse function assert;
331cfe16bdSGreg Roachuse function redirect;
341cfe16bdSGreg Roachuse function route;
351cfe16bdSGreg Roach
361cfe16bdSGreg Roach/**
371cfe16bdSGreg Roach * Get parameters for a report.
381cfe16bdSGreg Roach */
391cfe16bdSGreg Roachclass ReportSetupAction implements RequestHandlerInterface
401cfe16bdSGreg Roach{
411cfe16bdSGreg Roach    use ViewResponseTrait;
421cfe16bdSGreg Roach
431cfe16bdSGreg Roach    /**
441cfe16bdSGreg Roach     * @var ModuleService
451cfe16bdSGreg Roach     */
461cfe16bdSGreg Roach    private $module_service;
471cfe16bdSGreg Roach
481cfe16bdSGreg Roach    /**
491cfe16bdSGreg Roach     * ReportEngineController constructor.
501cfe16bdSGreg Roach     *
511cfe16bdSGreg Roach     * @param ModuleService $module_service
521cfe16bdSGreg Roach     */
531cfe16bdSGreg Roach    public function __construct(ModuleService $module_service)
541cfe16bdSGreg Roach    {
551cfe16bdSGreg Roach        $this->module_service = $module_service;
561cfe16bdSGreg Roach    }
571cfe16bdSGreg Roach
581cfe16bdSGreg Roach    /**
591cfe16bdSGreg Roach     * @param ServerRequestInterface $request
601cfe16bdSGreg Roach     *
611cfe16bdSGreg Roach     * @return ResponseInterface
621cfe16bdSGreg Roach     */
631cfe16bdSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
641cfe16bdSGreg Roach    {
651cfe16bdSGreg Roach        $tree = $request->getAttribute('tree');
6675964c75SGreg Roach        assert($tree instanceof Tree);
671cfe16bdSGreg Roach
681cfe16bdSGreg Roach        $user = $request->getAttribute('user');
6975964c75SGreg Roach        assert($user instanceof UserInterface);
701cfe16bdSGreg Roach
711cfe16bdSGreg Roach        $report = $request->getAttribute('report');
721cfe16bdSGreg Roach        $module = $this->module_service->findByName($report);
731cfe16bdSGreg Roach
741cfe16bdSGreg Roach        if (!$module instanceof ModuleReportInterface) {
751cfe16bdSGreg Roach            return redirect(route(ReportListPage::class, ['tree' => $tree->name()]));
761cfe16bdSGreg Roach        }
771cfe16bdSGreg Roach
78ef483801SGreg Roach        Auth::checkComponentAccess($module, ModuleReportInterface::class, $tree, $user);
791cfe16bdSGreg Roach
80b46c87bdSGreg Roach        $params = (array) $request->getParsedBody();
811cfe16bdSGreg Roach
821cfe16bdSGreg Roach        $params['tree']   = $tree->name();
831cfe16bdSGreg Roach        $params['report'] = $report;
841cfe16bdSGreg Roach
851cfe16bdSGreg Roach        return redirect(route(ReportGenerate::class, $params));
861cfe16bdSGreg Roach    }
871cfe16bdSGreg Roach}
88