xref: /webtrees/app/Http/RequestHandlers/DataFixData.php (revision 99125a335d0be11b975e213c69fc4ede0c5303f1)
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;
32use stdClass;
33
34use function assert;
35use function e;
36use function route;
37use function view;
38
39/**
40 * Run a data-fix.
41 */
42class DataFixData implements RequestHandlerInterface
43{
44    private DataFixService $data_fix_service;
45
46    private DatatablesService $datatables_service;
47
48    private ModuleService $module_service;
49
50    /**
51     * DataFix constructor.
52     *
53     * @param DataFixService    $data_fix_service
54     * @param DatatablesService $datatables_service
55     * @param ModuleService     $module_service
56     */
57    public function __construct(
58        DataFixService $data_fix_service,
59        DatatablesService $datatables_service,
60        ModuleService $module_service
61    ) {
62        $this->data_fix_service   = $data_fix_service;
63        $this->module_service     = $module_service;
64        $this->datatables_service = $datatables_service;
65    }
66
67    /**
68     * @param ServerRequestInterface $request
69     *
70     * @return ResponseInterface
71     */
72    public function handle(ServerRequestInterface $request): ResponseInterface
73    {
74        $tree = $request->getAttribute('tree');
75        assert($tree instanceof Tree);
76
77        $data_fix = $request->getAttribute('data_fix') ?? '';
78        $module   = $this->module_service->findByName($data_fix);
79        assert($module instanceof ModuleDataFixInterface);
80
81        $params  = $request->getQueryParams();
82        $records = $module->recordsToFix($tree, $params);
83
84        $callback = function (stdClass $row) use ($module, $params, $tree): array {
85            $record = $this->data_fix_service->getRecordByType($row->xref, $tree, $row->type);
86            assert($record instanceof GedcomRecord);
87
88            $col1 = '<a href="' . e($record->url()) . '">' . $record->fullName() . '</a>';
89
90            if ($module->doesRecordNeedUpdate($record, $params)) {
91                $preview_url = route(DataFixPreview::class, [
92                        'tree'     => $tree->name(),
93                        'data_fix' => $module->name(),
94                        'action'   => 'update',
95                        'xref'     => $row->xref,
96                    ] + $params);
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                // wt-ajax-modal-title
104                $col2 = '<button type="button" class="btn btn-primary" data-toggle="modal" data-backdrop="static" data-target="#wt-ajax-modal" data-href="' . $preview_url . '">' . view('icons/search') . I18N::translate('Preview') . '</button>';
105                $col2 .= ' <button type="button" class="btn btn-primary" data-update-url="' . $update_url . '">' . view('icons/data-fix') . I18N::translate('Update') . '</button>';
106            } else {
107                $col2 = '—';
108            }
109
110            return [$col1, $col2];
111        };
112
113        return $this->datatables_service->handleCollection($request, $records, [], [], $callback);
114    }
115}
116