summaryrefslogtreecommitdiff
path: root/app/Http/RequestHandlers/AddChildToFamilyAction.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/AddChildToFamilyAction.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/AddChildToFamilyAction.php')
-rw-r--r--app/Http/RequestHandlers/AddChildToFamilyAction.php26
1 files changed, 17 insertions, 9 deletions
diff --git a/app/Http/RequestHandlers/AddChildToFamilyAction.php b/app/Http/RequestHandlers/AddChildToFamilyAction.php
index fe0c2d7429..d8b401ba1d 100644
--- a/app/Http/RequestHandlers/AddChildToFamilyAction.php
+++ b/app/Http/RequestHandlers/AddChildToFamilyAction.php
@@ -20,6 +20,9 @@ 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;
@@ -37,19 +40,11 @@ class AddChildToFamilyAction 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();
@@ -66,10 +61,23 @@ class AddChildToFamilyAction implements RequestHandlerInterface
$child = $tree->createIndividual("0 @@ INDI\n1 FAMC @" . $xref . '@' . $gedcom);
// Link the child to the family
- $family->createFact('1 CHIL @' . $child->xref() . '@', false);
+ $before_id = $this->childFactOfYoungerSibling($family, $child)?->id() ?? '';
+ $family->createFact('1 CHIL @' . $child->xref() . '@', true, $before_id);
$url = Validator::parsedBody($request)->isLocalUrl()->string('url', $child->url());
return redirect($url);
}
+
+ private function childFactOfYoungerSibling(Family $family, Individual $child): Fact | null
+ {
+ $filter = function (Fact $fact) use ($child): bool {
+ return $fact->target() instanceof Individual &&
+ Date::compare($child->getBirthDate(), $fact->target()->getBirthDate()) < 0;
+ };
+ return $family
+ ->facts(['CHIL'], false, Auth::PRIV_HIDE, true)
+ ->first($filter);
+ }
+
}