. */ declare(strict_types=1); namespace Fisharebest\Webtrees\Http\RequestHandlers; use Fisharebest\Webtrees\GedcomRecord; use Fisharebest\Webtrees\Http\ViewResponseTrait; use Fisharebest\Webtrees\I18N; use Fisharebest\Webtrees\Tree; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; use function assert; use function redirect; /** * Merge records */ class MergeFactsPage implements RequestHandlerInterface { use ViewResponseTrait; /** * Merge two genealogy records. * * @param ServerRequestInterface $request * * @return ResponseInterface */ public function handle(ServerRequestInterface $request): ResponseInterface { $this->layout = 'layouts/administration'; $tree = $request->getAttribute('tree'); assert($tree instanceof Tree); $xref1 = $request->getQueryParams()['xref1'] ?? ''; $xref2 = $request->getQueryParams()['xref2'] ?? ''; $title = I18N::translate('Merge records') . ' — ' . e($tree->title()); $record1 = GedcomRecord::getInstance($xref1, $tree); $record2 = GedcomRecord::getInstance($xref2, $tree); if ( $record1 === null || $record2 === null || $record1 === $record2 || $record1::RECORD_TYPE !== $record2::RECORD_TYPE || $record1->isPendingDeletion() || $record2->isPendingDeletion() ) { return redirect(route(MergeRecordsPage::class, [ 'tree' => $tree->name(), 'xref1' => $xref1, 'xref2' => $xref2, ])); } // Facts found both records $facts = []; // Facts found in only one record $facts1 = []; $facts2 = []; foreach ($record1->facts() as $fact) { if (!$fact->isPendingDeletion() && $fact->getTag() !== 'CHAN') { $facts1[$fact->id()] = $fact; } } foreach ($record2->facts() as $fact) { if (!$fact->isPendingDeletion() && $fact->getTag() !== 'CHAN') { $facts2[$fact->id()] = $fact; } } foreach ($facts1 as $id1 => $fact1) { foreach ($facts2 as $id2 => $fact2) { if ($fact1->id() === $fact2->id()) { $facts[] = $fact1; unset($facts1[$id1], $facts2[$id2]); } } } return $this->viewResponse('admin/merge-records-step-2', [ 'facts' => $facts, 'facts1' => $facts1, 'facts2' => $facts2, 'record1' => $record1, 'record2' => $record2, 'title' => $title, 'tree' => $tree, ]); } }