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