Passed
Push — master ( 94abca...7bb122 )
by Greg
05:28
created

DataFixData::handle()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 42
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 2
eloc 30
nc 1
nop 1
dl 0
loc 42
rs 9.44
c 1
b 1
f 0
1
<?php
2
3
/**
4
 * webtrees: online genealogy
5
 * Copyright (C) 2019 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 <http://www.gnu.org/licenses/>.
16
 */
17
18
declare(strict_types=1);
19
20
namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22
use Fisharebest\Webtrees\GedcomRecord;
23
use Fisharebest\Webtrees\I18N;
24
use Fisharebest\Webtrees\Module\ModuleDataFixInterface;
25
use Fisharebest\Webtrees\Services\DataFixService;
26
use Fisharebest\Webtrees\Services\DatatablesService;
27
use Fisharebest\Webtrees\Services\ModuleService;
28
use Fisharebest\Webtrees\Tree;
29
use Psr\Http\Message\ResponseInterface;
30
use Psr\Http\Message\ServerRequestInterface;
31
use Psr\Http\Server\RequestHandlerInterface;
32
use stdClass;
33
34
use function assert;
35
use function e;
36
use function route;
37
use function view;
38
39
/**
40
 * Run a data-fix.
41
 */
42
class 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-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