xref: /webtrees/app/Http/RequestHandlers/DataFixData.php (revision 43f2f523bcb6d4090564d23802872c0679ede6bc)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2021 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use Fisharebest\Webtrees\GedcomRecord;
23use Fisharebest\Webtrees\I18N;
24use Fisharebest\Webtrees\Module\ModuleDataFixInterface;
25use Fisharebest\Webtrees\Services\DataFixService;
26use Fisharebest\Webtrees\Services\DatatablesService;
27use Fisharebest\Webtrees\Services\ModuleService;
28use Fisharebest\Webtrees\Tree;
29use Psr\Http\Message\ResponseInterface;
30use Psr\Http\Message\ServerRequestInterface;
31use Psr\Http\Server\RequestHandlerInterface;
32
33use function assert;
34use function e;
35use function route;
36use function view;
37
38/**
39 * Run a data-fix.
40 */
41class DataFixData implements RequestHandlerInterface
42{
43    private DataFixService $data_fix_service;
44
45    private DatatablesService $datatables_service;
46
47    private ModuleService $module_service;
48
49    /**
50     * DataFix constructor.
51     *
52     * @param DataFixService    $data_fix_service
53     * @param DatatablesService $datatables_service
54     * @param ModuleService     $module_service
55     */
56    public function __construct(
57        DataFixService $data_fix_service,
58        DatatablesService $datatables_service,
59        ModuleService $module_service
60    ) {
61        $this->data_fix_service   = $data_fix_service;
62        $this->module_service     = $module_service;
63        $this->datatables_service = $datatables_service;
64    }
65
66    /**
67     * @param ServerRequestInterface $request
68     *
69     * @return ResponseInterface
70     */
71    public function handle(ServerRequestInterface $request): ResponseInterface
72    {
73        $tree = $request->getAttribute('tree');
74        assert($tree instanceof Tree);
75
76        $data_fix = $request->getAttribute('data_fix') ?? '';
77        $module   = $this->module_service->findByName($data_fix);
78        assert($module instanceof ModuleDataFixInterface);
79
80        $params  = $request->getQueryParams();
81        $records = $module->recordsToFix($tree, $params);
82
83        $callback = function (object $row) use ($module, $params, $tree): array {
84            $record = $this->data_fix_service->getRecordByType($row->xref, $tree, $row->type);
85            assert($record instanceof GedcomRecord);
86
87            $col1 = '<a href="' . e($record->url()) . '">' . $record->fullName() . '</a>';
88
89            if ($module->doesRecordNeedUpdate($record, $params)) {
90                $preview_url = route(DataFixPreview::class, [
91                        'tree'     => $tree->name(),
92                        'data_fix' => $module->name(),
93                        'action'   => 'update',
94                        'xref'     => $row->xref,
95                    ] + $params);
96
97                $update_url = route(DataFixUpdate::class, [
98                        'tree'     => $tree->name(),
99                        'data_fix' => $module->name(),
100                        'action'   => 'update',
101                        'xref'     => $row->xref,
102                    ] + $params);
103
104                // wt-ajax-modal-title
105                $col2 = '<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-backdrop="static" data-bs-target="#wt-ajax-modal" data-wt-href="' . $preview_url . '">' . view('icons/search') . I18N::translate('Preview') . '</button>';
106                $col2 .= ' <button type="button" class="btn btn-primary" data-update-url="' . $update_url . '">' . view('icons/data-fix') . I18N::translate('Update') . '</button>';
107            } else {
108                $col2 = '—';
109            }
110
111            return [$col1, $col2];
112        };
113
114        return $this->datatables_service->handleCollection($request, $records, [], [], $callback);
115    }
116}
117