xref: /webtrees/app/Http/RequestHandlers/DataFixData.php (revision 24931b29a0237a5f5f1b8620af661ea530451af0)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 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\Validator;
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     * @param DataFixService    $data_fix_service
51     * @param DatatablesService $datatables_service
52     * @param ModuleService     $module_service
53     */
54    public function __construct(
55        DataFixService $data_fix_service,
56        DatatablesService $datatables_service,
57        ModuleService $module_service
58    ) {
59        $this->data_fix_service   = $data_fix_service;
60        $this->module_service     = $module_service;
61        $this->datatables_service = $datatables_service;
62    }
63
64    /**
65     * @param ServerRequestInterface $request
66     *
67     * @return ResponseInterface
68     */
69    public function handle(ServerRequestInterface $request): ResponseInterface
70    {
71        $tree     = Validator::attributes($request)->tree();
72        $data_fix = Validator::attributes($request)->string('data_fix', '');
73        $module   = $this->module_service->findByName($data_fix);
74        assert($module instanceof ModuleDataFixInterface);
75
76        $params  = $request->getQueryParams();
77        $records = $module->recordsToFix($tree, $params);
78
79        $callback = function (object $row) use ($module, $params, $tree): array {
80            $record = $this->data_fix_service->getRecordByType($row->xref, $tree, $row->type);
81            assert($record instanceof GedcomRecord);
82
83            $col1 = '<a href="' . e($record->url()) . '">' . $record->fullName() . '</a>';
84
85            if ($module->doesRecordNeedUpdate($record, $params)) {
86                $preview_url = route(DataFixPreview::class, [
87                        'tree'     => $tree->name(),
88                        'data_fix' => $module->name(),
89                        'action'   => 'update',
90                        'xref'     => $row->xref,
91                    ] + $params);
92
93                $update_url = route(DataFixUpdate::class, [
94                        'tree'     => $tree->name(),
95                        'data_fix' => $module->name(),
96                        'action'   => 'update',
97                        'xref'     => $row->xref,
98                    ] + $params);
99
100                // wt-ajax-modal-title
101                $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>';
102                $col2 .= ' <button type="button" class="btn btn-primary" data-update-url="' . $update_url . '">' . view('icons/data-fix') . I18N::translate('Update') . '</button>';
103            } else {
104                $col2 = '—';
105            }
106
107            return [$col1, $col2];
108        };
109
110        return $this->datatables_service->handleCollection($request, $records, [], [], $callback);
111    }
112}
113