xref: /webtrees/app/Http/RequestHandlers/DataFixPreview.php (revision b55cbc6b43247e8b2ad14af6f6d24dc6747195ff)
1ce42304aSGreg Roach<?php
2ce42304aSGreg Roach
3ce42304aSGreg Roach/**
4ce42304aSGreg Roach * webtrees: online genealogy
589f7189bSGreg Roach * Copyright (C) 2021 webtrees development team
6ce42304aSGreg Roach * This program is free software: you can redistribute it and/or modify
7ce42304aSGreg Roach * it under the terms of the GNU General Public License as published by
8ce42304aSGreg Roach * the Free Software Foundation, either version 3 of the License, or
9ce42304aSGreg Roach * (at your option) any later version.
10ce42304aSGreg Roach * This program is distributed in the hope that it will be useful,
11ce42304aSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12ce42304aSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13ce42304aSGreg Roach * GNU General Public License for more details.
14ce42304aSGreg 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/>.
16ce42304aSGreg Roach */
17ce42304aSGreg Roach
18ce42304aSGreg Roachdeclare(strict_types=1);
19ce42304aSGreg Roach
20ce42304aSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers;
21ce42304aSGreg Roach
22ce42304aSGreg Roachuse Fisharebest\Webtrees\Auth;
23ce42304aSGreg Roachuse Fisharebest\Webtrees\Http\ViewResponseTrait;
24ce42304aSGreg Roachuse Fisharebest\Webtrees\I18N;
25ce42304aSGreg Roachuse Fisharebest\Webtrees\Module\ModuleDataFixInterface;
266b9cb339SGreg Roachuse Fisharebest\Webtrees\Registry;
27ce42304aSGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
28*b55cbc6bSGreg Roachuse Fisharebest\Webtrees\Validator;
29ce42304aSGreg Roachuse Psr\Http\Message\ResponseInterface;
30ce42304aSGreg Roachuse Psr\Http\Message\ServerRequestInterface;
31ce42304aSGreg Roachuse Psr\Http\Server\RequestHandlerInterface;
32ce42304aSGreg Roach
33ce42304aSGreg Roachuse function assert;
34ce42304aSGreg Roach
35ce42304aSGreg Roach/**
36ce42304aSGreg Roach * Run a data-fix.
37ce42304aSGreg Roach */
38ce42304aSGreg Roachclass DataFixPreview implements RequestHandlerInterface
39ce42304aSGreg Roach{
40ce42304aSGreg Roach    use ViewResponseTrait;
41ce42304aSGreg Roach
42c4943cffSGreg Roach    private ModuleService $module_service;
43ce42304aSGreg Roach
44ce42304aSGreg Roach    /**
45ce42304aSGreg Roach     * DataFix constructor.
46ce42304aSGreg Roach     *
47ce42304aSGreg Roach     * @param ModuleService $module_service
48ce42304aSGreg Roach     */
49ce42304aSGreg Roach    public function __construct(ModuleService $module_service)
50ce42304aSGreg Roach    {
51ce42304aSGreg Roach        $this->module_service = $module_service;
52ce42304aSGreg Roach    }
53ce42304aSGreg Roach
54ce42304aSGreg Roach    /**
55ce42304aSGreg Roach     * @param ServerRequestInterface $request
56ce42304aSGreg Roach     *
57ce42304aSGreg Roach     * @return ResponseInterface
58ce42304aSGreg Roach     */
59ce42304aSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
60ce42304aSGreg Roach    {
61*b55cbc6bSGreg Roach        $tree     = Validator::attributes($request)->tree();
62*b55cbc6bSGreg Roach        $data_fix = Validator::attributes($request)->string('data_fix', '');
63ce42304aSGreg Roach        $module   = $this->module_service->findByName($data_fix);
64ce42304aSGreg Roach        assert($module instanceof ModuleDataFixInterface);
65ce42304aSGreg Roach
6637653836SGreg Roach        $params = $request->getQueryParams();
6737653836SGreg Roach        $xref   = $params['xref'] ?? '';
686b9cb339SGreg Roach        $record = Registry::gedcomRecordFactory()->make($xref, $tree);
69ce42304aSGreg Roach        $record = Auth::checkRecordAccess($record);
70ce42304aSGreg Roach
71ce42304aSGreg Roach        $this->layout = 'layouts/ajax';
72ce42304aSGreg Roach
73ce42304aSGreg Roach        if (!$record->isPendingDeletion() && $module->doesRecordNeedUpdate($record, $params)) {
74ce42304aSGreg Roach            $preview = $module->previewUpdate($record, $params);
75ce42304aSGreg Roach        } else {
76ce42304aSGreg Roach            $preview = '';
77ce42304aSGreg Roach        }
78ce42304aSGreg Roach
79ce42304aSGreg Roach        return $this->viewResponse('modals/data-fix-preview', [
80ce42304aSGreg Roach            'preview' => $preview,
81ce42304aSGreg Roach            'title'   => I18N::translate('Preview') . ' — ' . $record->fullName(),
82ce42304aSGreg Roach        ]);
83ce42304aSGreg Roach    }
84ce42304aSGreg Roach}
85