1*ce42304aSGreg Roach<?php 2*ce42304aSGreg Roach 3*ce42304aSGreg Roach/** 4*ce42304aSGreg Roach * webtrees: online genealogy 5*ce42304aSGreg Roach * Copyright (C) 2019 webtrees development team 6*ce42304aSGreg Roach * This program is free software: you can redistribute it and/or modify 7*ce42304aSGreg Roach * it under the terms of the GNU General Public License as published by 8*ce42304aSGreg Roach * the Free Software Foundation, either version 3 of the License, or 9*ce42304aSGreg Roach * (at your option) any later version. 10*ce42304aSGreg Roach * This program is distributed in the hope that it will be useful, 11*ce42304aSGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of 12*ce42304aSGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13*ce42304aSGreg Roach * GNU General Public License for more details. 14*ce42304aSGreg Roach * You should have received a copy of the GNU General Public License 15*ce42304aSGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>. 16*ce42304aSGreg Roach */ 17*ce42304aSGreg Roach 18*ce42304aSGreg Roachdeclare(strict_types=1); 19*ce42304aSGreg Roach 20*ce42304aSGreg Roachnamespace Fisharebest\Webtrees\Http\RequestHandlers; 21*ce42304aSGreg Roach 22*ce42304aSGreg Roachuse Fisharebest\Webtrees\GedcomRecord; 23*ce42304aSGreg Roachuse Fisharebest\Webtrees\I18N; 24*ce42304aSGreg Roachuse Fisharebest\Webtrees\Module\ModuleDataFixInterface; 25*ce42304aSGreg Roachuse Fisharebest\Webtrees\Services\DataFixService; 26*ce42304aSGreg Roachuse Fisharebest\Webtrees\Services\DatatablesService; 27*ce42304aSGreg Roachuse Fisharebest\Webtrees\Services\ModuleService; 28*ce42304aSGreg Roachuse Fisharebest\Webtrees\Tree; 29*ce42304aSGreg Roachuse Psr\Http\Message\ResponseInterface; 30*ce42304aSGreg Roachuse Psr\Http\Message\ServerRequestInterface; 31*ce42304aSGreg Roachuse Psr\Http\Server\RequestHandlerInterface; 32*ce42304aSGreg Roachuse stdClass; 33*ce42304aSGreg Roach 34*ce42304aSGreg Roachuse function assert; 35*ce42304aSGreg Roachuse function e; 36*ce42304aSGreg Roachuse function route; 37*ce42304aSGreg Roachuse function view; 38*ce42304aSGreg Roach 39*ce42304aSGreg Roach/** 40*ce42304aSGreg Roach * Run a data-fix. 41*ce42304aSGreg Roach */ 42*ce42304aSGreg Roachclass DataFixData implements RequestHandlerInterface 43*ce42304aSGreg Roach{ 44*ce42304aSGreg Roach /** @var DataFixService */ 45*ce42304aSGreg Roach private $data_fix_service; 46*ce42304aSGreg Roach 47*ce42304aSGreg Roach /** @var DatatablesService */ 48*ce42304aSGreg Roach private $datatables_service; 49*ce42304aSGreg Roach 50*ce42304aSGreg Roach /** @var ModuleService */ 51*ce42304aSGreg Roach private $module_service; 52*ce42304aSGreg Roach 53*ce42304aSGreg Roach /** 54*ce42304aSGreg Roach * DataFix constructor. 55*ce42304aSGreg Roach * 56*ce42304aSGreg Roach * @param DataFixService $data_fix_service 57*ce42304aSGreg Roach * @param DatatablesService $datatables_service 58*ce42304aSGreg Roach * @param ModuleService $module_service 59*ce42304aSGreg Roach */ 60*ce42304aSGreg Roach public function __construct( 61*ce42304aSGreg Roach DataFixService $data_fix_service, 62*ce42304aSGreg Roach DatatablesService $datatables_service, 63*ce42304aSGreg Roach ModuleService $module_service 64*ce42304aSGreg Roach ) { 65*ce42304aSGreg Roach $this->data_fix_service = $data_fix_service; 66*ce42304aSGreg Roach $this->module_service = $module_service; 67*ce42304aSGreg Roach $this->datatables_service = $datatables_service; 68*ce42304aSGreg Roach } 69*ce42304aSGreg Roach 70*ce42304aSGreg Roach /** 71*ce42304aSGreg Roach * @param ServerRequestInterface $request 72*ce42304aSGreg Roach * 73*ce42304aSGreg Roach * @return ResponseInterface 74*ce42304aSGreg Roach */ 75*ce42304aSGreg Roach public function handle(ServerRequestInterface $request): ResponseInterface 76*ce42304aSGreg Roach { 77*ce42304aSGreg Roach $tree = $request->getAttribute('tree'); 78*ce42304aSGreg Roach assert($tree instanceof Tree); 79*ce42304aSGreg Roach 80*ce42304aSGreg Roach $data_fix = $request->getAttribute('data_fix') ?? ''; 81*ce42304aSGreg Roach $module = $this->module_service->findByName($data_fix); 82*ce42304aSGreg Roach assert($module instanceof ModuleDataFixInterface); 83*ce42304aSGreg Roach 84*ce42304aSGreg Roach $params = $request->getQueryParams(); 85*ce42304aSGreg Roach $records = $module->recordsToFix($tree, $params); 86*ce42304aSGreg Roach 87*ce42304aSGreg Roach $callback = function (stdClass $row) use ($module, $params, $tree): array { 88*ce42304aSGreg Roach $record = $this->data_fix_service->getRecordByType($row->xref, $tree, $row->type); 89*ce42304aSGreg Roach assert($record instanceof GedcomRecord); 90*ce42304aSGreg Roach 91*ce42304aSGreg Roach $col1 = '<a href="' . e($record->url()) . '">' . $record->fullName() . '</a>'; 92*ce42304aSGreg Roach 93*ce42304aSGreg Roach if ($module->doesRecordNeedUpdate($record, $params)) { 94*ce42304aSGreg Roach $preview_url = route(DataFixPreview::class, [ 95*ce42304aSGreg Roach 'tree' => $tree->name(), 96*ce42304aSGreg Roach 'data_fix' => $module->name(), 97*ce42304aSGreg Roach 'action' => 'update', 98*ce42304aSGreg Roach 'xref' => $row->xref, 99*ce42304aSGreg Roach ] + $params); 100*ce42304aSGreg Roach $update_url = route(DataFixUpdate::class, [ 101*ce42304aSGreg Roach 'tree' => $tree->name(), 102*ce42304aSGreg Roach 'data_fix' => $module->name(), 103*ce42304aSGreg Roach 'action' => 'update', 104*ce42304aSGreg Roach 'xref' => $row->xref, 105*ce42304aSGreg Roach ] + $params); 106*ce42304aSGreg Roach // wt-ajax-modal-title 107*ce42304aSGreg Roach $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*ce42304aSGreg Roach $col2 .= ' <button type="button" class="btn btn-primary" data-update-url="' . $update_url . '">' . view('icons/data-fix') . I18N::translate('Update') . '</button>'; 109*ce42304aSGreg Roach } else { 110*ce42304aSGreg Roach $col2 = '—'; 111*ce42304aSGreg Roach } 112*ce42304aSGreg Roach 113*ce42304aSGreg Roach return [$col1, $col2]; 114*ce42304aSGreg Roach }; 115*ce42304aSGreg Roach 116*ce42304aSGreg Roach return $this->datatables_service->handleCollection($request, $records, [], [], $callback); 117*ce42304aSGreg Roach } 118*ce42304aSGreg Roach} 119