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