summaryrefslogtreecommitdiff
path: root/app/Http/RequestHandlers/LinkSpouseToIndividualAction.php
diff options
context:
space:
mode:
authorBert Koorengevel <BertKoor@users.noreply.github.com>2025-12-24 10:54:50 +0100
committerGitHub <noreply@github.com>2025-12-24 09:54:50 +0000
commit90d3f405d5c664c122bee34bacb3937c102ee864 (patch)
treebf9adfd8b2b793387e99f1445294be623c76a780 /app/Http/RequestHandlers/LinkSpouseToIndividualAction.php
parent96eac16fc8aab503603e8022a5b3984b7db81491 (diff)
downloadwebtrees-90d3f405d5c664c122bee34bacb3937c102ee864.tar.gz
webtrees-90d3f405d5c664c122bee34bacb3937c102ee864.tar.bz2
webtrees-90d3f405d5c664c122bee34bacb3937c102ee864.zip
fix #4511 & #5137: when adding a child to a family, put it between older and younger children (#5260)
* fix #4511, #5137: when adding a child to a family, put it between older and younger children * when adding a spouse, put the marriage between earlier and later marriages * always update_chan=true when linking individuals to families * PR #5260 review: check fact target, Date::compare, first($filter)
Diffstat (limited to 'app/Http/RequestHandlers/LinkSpouseToIndividualAction.php')
-rw-r--r--app/Http/RequestHandlers/LinkSpouseToIndividualAction.php32
1 files changed, 22 insertions, 10 deletions
diff --git a/app/Http/RequestHandlers/LinkSpouseToIndividualAction.php b/app/Http/RequestHandlers/LinkSpouseToIndividualAction.php
index 09091d1d60..c3a215d2d2 100644
--- a/app/Http/RequestHandlers/LinkSpouseToIndividualAction.php
+++ b/app/Http/RequestHandlers/LinkSpouseToIndividualAction.php
@@ -20,7 +20,10 @@ declare(strict_types=1);
namespace Fisharebest\Webtrees\Http\RequestHandlers;
use Fisharebest\Webtrees\Auth;
+use Fisharebest\Webtrees\Date;
+use Fisharebest\Webtrees\Fact;
use Fisharebest\Webtrees\Family;
+use Fisharebest\Webtrees\Individual;
use Fisharebest\Webtrees\Registry;
use Fisharebest\Webtrees\Services\GedcomEditService;
use Fisharebest\Webtrees\Validator;
@@ -37,19 +40,11 @@ class LinkSpouseToIndividualAction implements RequestHandlerInterface
{
private GedcomEditService $gedcom_edit_service;
- /**
- * @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 = Validator::attributes($request)->tree();
@@ -76,9 +71,26 @@ class LinkSpouseToIndividualAction implements RequestHandlerInterface
$family = $tree->createFamily($gedcom);
- $individual->createFact('1 FAMS @' . $family->xref() . '@', false);
- $spouse->createFact('1 FAMS @' . $family->xref() . '@', false);
+ // Link the individual to the family
+ $before_id = $this->famsFactOfLaterMarriage($individual, $family)?->id() ?? '';
+ $individual->createFact('1 FAMS @' . $family->xref() . '@', true, $before_id);
+
+ // Link the spouse to the family
+ $before_id = $this->famsFactOfLaterMarriage($spouse, $family)?->id() ?? '';
+ $spouse->createFact('1 FAMS @' . $family->xref() . '@', true, $before_id);
return redirect($family->url());
}
+
+ private function famsFactOfLaterMarriage(Individual $partner, Family $family): Fact | null
+ {
+ $filter = function (Fact $fact) use ($family): bool {
+ return $fact->target() instanceof Family &&
+ Date::compare($family->getMarriageDate(), $fact->target()->getMarriageDate()) < 0;
+ };
+ return $partner
+ ->facts(['FAMS'], false, Auth::PRIV_HIDE, true)
+ ->first($filter);
+ }
+
}