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