. */ declare(strict_types=1); namespace Fisharebest\Webtrees\Http\RequestHandlers; use Fisharebest\Webtrees\Services\GedcomEditService; use Fisharebest\Webtrees\Tree; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; use Psr\Http\Server\RequestHandlerInterface; use function assert; use function preg_match_all; use function redirect; use function route; /** * Create a new unlinked individual. */ class AddUnlinkedAction implements RequestHandlerInterface { /** @var GedcomEditService */ private $gedcom_edit_service; /** * AddChildToFamilyAction constructor. * * @param GedcomEditService $gedcom_edit_service */ public function __construct(GedcomEditService $gedcom_edit_service) { $this->gedcom_edit_service = $gedcom_edit_service; } /** * @param ServerRequestInterface $request * * @return ResponseInterface */ public function handle(ServerRequestInterface $request): ResponseInterface { $tree = $request->getAttribute('tree'); assert($tree instanceof Tree); $params = (array) $request->getParsedBody(); $this->gedcom_edit_service->glevels = $params['glevels'] ?? []; $this->gedcom_edit_service->tag = $params['tag'] ?? []; $this->gedcom_edit_service->text = $params['text'] ?? []; $this->gedcom_edit_service->islink = $params['islink'] ?? []; $this->gedcom_edit_service->splitSource(); $gedrec = '0 @@ INDI'; $gedrec .= $this->gedcom_edit_service->addNewName($request, $tree); $gedrec .= $this->gedcom_edit_service->addNewSex($request); if (preg_match_all('/([A-Z0-9_]+)/', $tree->getPreference('QUICK_REQUIRED_FACTS'), $matches)) { foreach ($matches[1] as $match) { $gedrec .= $this->gedcom_edit_service->addNewFact($request, $tree, $match); } } if ($params['SOUR_INDI'] ?? false) { $gedrec = $this->gedcom_edit_service->handleUpdates($gedrec); } else { $gedrec = $this->gedcom_edit_service->updateRest($gedrec); } $new_indi = $tree->createIndividual($gedrec); if (($params['goto'] ?? '') === 'new') { return redirect($new_indi->url()); } return redirect(route('manage-trees', ['tree' => $tree->name()])); } }