Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions app/Http/RequestHandlers/AddChildToFamilyAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Fisharebest\Webtrees\Auth;
use Fisharebest\Webtrees\Individual;
use Fisharebest\Webtrees\Registry;
use Fisharebest\Webtrees\Services\FamilyService;
use Fisharebest\Webtrees\Services\GedcomEditService;
use Fisharebest\Webtrees\Validator;
use Psr\Http\Message\ResponseInterface;
Expand All @@ -36,13 +37,16 @@
class AddChildToFamilyAction implements RequestHandlerInterface
{
private GedcomEditService $gedcom_edit_service;
private FamilyService $family_service;

/**
* @param GedcomEditService $gedcom_edit_service
* @param FamilyService $family_service
*/
public function __construct(GedcomEditService $gedcom_edit_service)
public function __construct(GedcomEditService $gedcom_edit_service, FamilyService $family_service)
{
$this->gedcom_edit_service = $gedcom_edit_service;
$this->family_service = $family_service;
}

/**
Expand All @@ -66,7 +70,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
$child = $tree->createIndividual("0 @@ INDI\n1 FAMC @" . $xref . '@' . $gedcom);

// Link the child to the family
$family->createFact('1 CHIL @' . $child->xref() . '@', false);
$this->family_service->addChildToFamily($child, $family);

$url = Validator::parsedBody($request)->isLocalUrl()->string('url', $child->url());

Expand Down
13 changes: 12 additions & 1 deletion app/Http/RequestHandlers/LinkChildToFamilyAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Fisharebest\Webtrees\Auth;
use Fisharebest\Webtrees\Elements\PedigreeLinkageType;
use Fisharebest\Webtrees\Registry;
use Fisharebest\Webtrees\Services\FamilyService;
use Fisharebest\Webtrees\Validator;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
Expand All @@ -34,6 +35,16 @@
*/
class LinkChildToFamilyAction implements RequestHandlerInterface
{
private FamilyService $family_service;

/**
* @param FamilyService $family_service
*/
public function __construct(FamilyService $family_service)
{
$this->family_service = $family_service;
}

/**
* @param ServerRequestInterface $request
*
Expand Down Expand Up @@ -90,7 +101,7 @@ public function handle(ServerRequestInterface $request): ResponseInterface
}

if (!$chil_link_exists) {
$family->createFact('1 CHIL @' . $individual->xref() . '@', true);
$this->family_service->addChildToFamily($individual, $family);
}

return redirect($individual->url());
Expand Down
63 changes: 63 additions & 0 deletions app/Services/FamilyService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

/**
* webtrees: online genealogy
* Copyright (C) 2025 webtrees development team
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

declare(strict_types=1);

namespace Fisharebest\Webtrees\Services;

use Fisharebest\Webtrees\Family;
use Fisharebest\Webtrees\Individual;

use function implode;

class FamilyService
{
/**
* Add a child to a family.
*
* @param Individual $child
* @param Family $family
*
* @return void
*/
public function addChildToFamily(Individual $child, Family $family): void
{
$child_birth_day = $child->getBirthDate()->julianDay();
$child_gedcom = '1 CHIL @' . $child->xref() . '@';
$family_facts = ['0 @' . $family->xref() . '@ FAM'];

// Insert new child at the right place
$done = false;
foreach ($family->facts() as $fact) {
if ($fact->tag() === 'FAM:CHIL' && !$done) {
// insert new child when born before this child
if ($child_birth_day < $fact->target()->getBirthDate()->julianDay()) {
$family_facts[] = $child_gedcom;
$done = true;
}
}
$family_facts[] = $fact->gedcom();
}
if (!$done) {
// Append child at end
$family_facts[] = $child_gedcom;
}

$gedcom = implode("\n", $family_facts);
$family->updateRecord($gedcom, false);
}
}
33 changes: 33 additions & 0 deletions tests/app/Services/FamilyServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

/**
* webtrees: online genealogy
* Copyright (C) 2025 webtrees development team
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

declare(strict_types=1);

namespace Fisharebest\Webtrees\Services;

use Fisharebest\Webtrees\TestCase;
use PHPUnit\Framework\Attributes\CoversClass;

#[CoversClass(FamilyService::class)]
class FamilyServiceTest extends TestCase
{
public function testClass(): void
{
self::assertTrue(class_exists(FamilyService::class));
}

}
Loading