xref: /webtrees/app/Http/RequestHandlers/DataFixData.php (revision e93a8df2f8d797005750082cc3766c0e80799688)
1ce42304aSGreg Roach<?php
2ce42304aSGreg Roach
3ce42304aSGreg Roach/**
4ce42304aSGreg Roach * webtrees: online genealogy
5*d11be702SGreg Roach * Copyright (C) 2023 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\GedcomRecord;
23ce42304aSGreg Roachuse Fisharebest\Webtrees\I18N;
24ce42304aSGreg Roachuse Fisharebest\Webtrees\Module\ModuleDataFixInterface;
25ce42304aSGreg Roachuse Fisharebest\Webtrees\Services\DataFixService;
26ce42304aSGreg Roachuse Fisharebest\Webtrees\Services\DatatablesService;
27ce42304aSGreg Roachuse Fisharebest\Webtrees\Services\ModuleService;
28b55cbc6bSGreg 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 Roachuse function e;
35ce42304aSGreg Roachuse function route;
36ce42304aSGreg Roachuse function view;
37ce42304aSGreg Roach
38ce42304aSGreg Roach/**
39ce42304aSGreg Roach * Run a data-fix.
40ce42304aSGreg Roach */
41ce42304aSGreg Roachclass DataFixData implements RequestHandlerInterface
42ce42304aSGreg Roach{
43c4943cffSGreg Roach    private DataFixService $data_fix_service;
44ce42304aSGreg Roach
45c4943cffSGreg Roach    private DatatablesService $datatables_service;
46ce42304aSGreg Roach
47c4943cffSGreg Roach    private ModuleService $module_service;
48ce42304aSGreg Roach
49ce42304aSGreg Roach    /**
50ce42304aSGreg Roach     * @param DataFixService    $data_fix_service
51ce42304aSGreg Roach     * @param DatatablesService $datatables_service
52ce42304aSGreg Roach     * @param ModuleService     $module_service
53ce42304aSGreg Roach     */
54ce42304aSGreg Roach    public function __construct(
55ce42304aSGreg Roach        DataFixService $data_fix_service,
56ce42304aSGreg Roach        DatatablesService $datatables_service,
57ce42304aSGreg Roach        ModuleService $module_service
58ce42304aSGreg Roach    ) {
59ce42304aSGreg Roach        $this->data_fix_service   = $data_fix_service;
60ce42304aSGreg Roach        $this->module_service     = $module_service;
61ce42304aSGreg Roach        $this->datatables_service = $datatables_service;
62ce42304aSGreg Roach    }
63ce42304aSGreg Roach
64ce42304aSGreg Roach    /**
65ce42304aSGreg Roach     * @param ServerRequestInterface $request
66ce42304aSGreg Roach     *
67ce42304aSGreg Roach     * @return ResponseInterface
68ce42304aSGreg Roach     */
69ce42304aSGreg Roach    public function handle(ServerRequestInterface $request): ResponseInterface
70ce42304aSGreg Roach    {
71b55cbc6bSGreg Roach        $tree     = Validator::attributes($request)->tree();
72b55cbc6bSGreg Roach        $data_fix = Validator::attributes($request)->string('data_fix', '');
73ce42304aSGreg Roach        $module   = $this->module_service->findByName($data_fix);
74ce42304aSGreg Roach        assert($module instanceof ModuleDataFixInterface);
75ce42304aSGreg Roach
76ce42304aSGreg Roach        $params  = $request->getQueryParams();
77ce42304aSGreg Roach        $records = $module->recordsToFix($tree, $params);
78ce42304aSGreg Roach
79f70bcff5SGreg Roach        $callback = function (object $row) use ($module, $params, $tree): array {
80ce42304aSGreg Roach            $record = $this->data_fix_service->getRecordByType($row->xref, $tree, $row->type);
81ce42304aSGreg Roach            assert($record instanceof GedcomRecord);
82ce42304aSGreg Roach
83ce42304aSGreg Roach            $col1 = '<a href="' . e($record->url()) . '">' . $record->fullName() . '</a>';
84ce42304aSGreg Roach
85ce42304aSGreg Roach            if ($module->doesRecordNeedUpdate($record, $params)) {
86ce42304aSGreg Roach                $preview_url = route(DataFixPreview::class, [
87ce42304aSGreg Roach                        'tree'     => $tree->name(),
88ce42304aSGreg Roach                        'data_fix' => $module->name(),
89ce42304aSGreg Roach                        'action'   => 'update',
90ce42304aSGreg Roach                        'xref'     => $row->xref,
91ce42304aSGreg Roach                    ] + $params);
924fedbbe7SGreg Roach
93ce42304aSGreg Roach                $update_url = route(DataFixUpdate::class, [
94ce42304aSGreg Roach                        'tree'     => $tree->name(),
95ce42304aSGreg Roach                        'data_fix' => $module->name(),
96ce42304aSGreg Roach                        'action'   => 'update',
97ce42304aSGreg Roach                        'xref'     => $row->xref,
98ce42304aSGreg Roach                    ] + $params);
994fedbbe7SGreg Roach
100ce42304aSGreg Roach                // wt-ajax-modal-title
101d4786c66SGreg Roach                $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>';
102ce42304aSGreg Roach                $col2 .= ' <button type="button" class="btn btn-primary" data-update-url="' . $update_url . '">' . view('icons/data-fix') . I18N::translate('Update') . '</button>';
103ce42304aSGreg Roach            } else {
104ce42304aSGreg Roach                $col2 = '—';
105ce42304aSGreg Roach            }
106ce42304aSGreg Roach
107ce42304aSGreg Roach            return [$col1, $col2];
108ce42304aSGreg Roach        };
109ce42304aSGreg Roach
110ce42304aSGreg Roach        return $this->datatables_service->handleCollection($request, $records, [], [], $callback);
111ce42304aSGreg Roach    }
112ce42304aSGreg Roach}
113