summaryrefslogtreecommitdiff
path: root/app/Http/RequestHandlers
diff options
context:
space:
mode:
authorGreg Roach <fisharebest@webtrees.net>2019-10-15 20:51:18 +0100
committerGreg Roach <fisharebest@webtrees.net>2019-10-15 23:05:57 +0100
commit5229eade2a8cdd1381e19f96f67fc1c8b92ca95d (patch)
tree007749d01fdfd56b668f6ecee309371b6a6d1a33 /app/Http/RequestHandlers
parent9b5c959707d56fc73446942abbe205a42e3c07cf (diff)
downloadwebtrees-5229eade2a8cdd1381e19f96f67fc1c8b92ca95d.tar.gz
webtrees-5229eade2a8cdd1381e19f96f67fc1c8b92ca95d.tar.bz2
webtrees-5229eade2a8cdd1381e19f96f67fc1c8b92ca95d.zip
Refactor the re-ordering of children/spouses/names/media; add assertions
Diffstat (limited to 'app/Http/RequestHandlers')
-rw-r--r--app/Http/RequestHandlers/DeleteTreeAction.php4
-rw-r--r--app/Http/RequestHandlers/LoginPage.php3
-rw-r--r--app/Http/RequestHandlers/Logout.php4
-rw-r--r--app/Http/RequestHandlers/RegisterAction.php3
-rw-r--r--app/Http/RequestHandlers/ReorderChildrenAction.php88
-rw-r--r--app/Http/RequestHandlers/ReorderChildrenPage.php67
-rw-r--r--app/Http/RequestHandlers/ReorderMediaAction.php89
-rw-r--r--app/Http/RequestHandlers/ReorderMediaPage.php67
-rw-r--r--app/Http/RequestHandlers/ReorderNamesAction.php89
-rw-r--r--app/Http/RequestHandlers/ReorderNamesPage.php67
-rw-r--r--app/Http/RequestHandlers/ReorderSpousesAction.php89
-rw-r--r--app/Http/RequestHandlers/ReorderSpousesPage.php67
12 files changed, 637 insertions, 0 deletions
diff --git a/app/Http/RequestHandlers/DeleteTreeAction.php b/app/Http/RequestHandlers/DeleteTreeAction.php
index 38fd673a92..d37db2e190 100644
--- a/app/Http/RequestHandlers/DeleteTreeAction.php
+++ b/app/Http/RequestHandlers/DeleteTreeAction.php
@@ -23,9 +23,12 @@ use Fisharebest\Webtrees\FlashMessages;
use Fisharebest\Webtrees\Http\Controllers\AbstractBaseController;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Services\TreeService;
+use Fisharebest\Webtrees\Tree;
+use InvalidArgumentException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
+use function assert;
use function e;
use function response;
@@ -55,6 +58,7 @@ class DeleteTreeAction extends AbstractBaseController
public function handle(ServerRequestInterface $request): ResponseInterface
{
$tree = $request->getAttribute('tree');
+ assert($tree instanceof Tree, new InvalidArgumentException());
$this->tree_service->delete($tree);
diff --git a/app/Http/RequestHandlers/LoginPage.php b/app/Http/RequestHandlers/LoginPage.php
index 825c7ccbe4..41cc301697 100644
--- a/app/Http/RequestHandlers/LoginPage.php
+++ b/app/Http/RequestHandlers/LoginPage.php
@@ -21,9 +21,12 @@ namespace Fisharebest\Webtrees\Http\RequestHandlers;
use Fisharebest\Webtrees\Http\Controllers\AbstractBaseController;
use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Site;
+use Fisharebest\Webtrees\Tree;
use Fisharebest\Webtrees\User;
+use InvalidArgumentException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
+use function assert;
/**
* Show a login form.
diff --git a/app/Http/RequestHandlers/Logout.php b/app/Http/RequestHandlers/Logout.php
index 255ae7ec63..60a53779b8 100644
--- a/app/Http/RequestHandlers/Logout.php
+++ b/app/Http/RequestHandlers/Logout.php
@@ -24,10 +24,12 @@ use Fisharebest\Webtrees\I18N;
use Fisharebest\Webtrees\Log;
use Fisharebest\Webtrees\Tree;
use Fisharebest\Webtrees\User;
+use InvalidArgumentException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
+use function assert;
use function redirect;
use function route;
@@ -44,6 +46,8 @@ class Logout implements RequestHandlerInterface
public function handle(ServerRequestInterface $request): ResponseInterface
{
$tree = $request->getAttribute('tree');
+ assert($tree instanceof Tree, new InvalidArgumentException());
+
$user = $request->getAttribute('user');
if ($user instanceof User) {
diff --git a/app/Http/RequestHandlers/RegisterAction.php b/app/Http/RequestHandlers/RegisterAction.php
index 29eec1283d..315d152a29 100644
--- a/app/Http/RequestHandlers/RegisterAction.php
+++ b/app/Http/RequestHandlers/RegisterAction.php
@@ -31,10 +31,12 @@ use Fisharebest\Webtrees\SiteUser;
use Fisharebest\Webtrees\Tree;
use Fisharebest\Webtrees\TreeUser;
use Illuminate\Database\Capsule\Manager as DB;
+use InvalidArgumentException;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Ramsey\Uuid\Uuid;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
+use function assert;
/**
* Process a user registration.
@@ -73,6 +75,7 @@ class RegisterAction extends AbstractBaseController
public function handle(ServerRequestInterface $request): ResponseInterface
{
$tree = $request->getAttribute('tree');
+ assert($tree instanceof Tree, new InvalidArgumentException());
$this->checkRegistrationAllowed();
diff --git a/app/Http/RequestHandlers/ReorderChildrenAction.php b/app/Http/RequestHandlers/ReorderChildrenAction.php
new file mode 100644
index 0000000000..88d4bc46ba
--- /dev/null
+++ b/app/Http/RequestHandlers/ReorderChildrenAction.php
@@ -0,0 +1,88 @@
+<?php
+
+/**
+ * webtrees: online genealogy
+ * Copyright (C) 2019 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 <http://www.gnu.org/licenses/>.
+ */
+declare(strict_types=1);
+
+namespace Fisharebest\Webtrees\Http\RequestHandlers;
+
+use Fisharebest\Webtrees\Auth;
+use Fisharebest\Webtrees\Family;
+use Fisharebest\Webtrees\Tree;
+use InvalidArgumentException;
+use Psr\Http\Message\ResponseInterface;
+use Psr\Http\Message\ServerRequestInterface;
+use Psr\Http\Server\RequestHandlerInterface;
+
+use function array_merge;
+use function array_search;
+use function assert;
+use function implode;
+use function is_array;
+use function redirect;
+use function uksort;
+
+/**
+ * Reorder the children in a family.
+ */
+class ReorderChildrenAction implements RequestHandlerInterface
+{
+ /**
+ * @param ServerRequestInterface $request
+ *
+ * @return ResponseInterface
+ */
+ public function handle(ServerRequestInterface $request): ResponseInterface
+ {
+ $tree = $request->getAttribute('tree');
+ assert($tree instanceof Tree, new InvalidArgumentException());
+
+ $xref = $request->getAttribute('xref');
+ assert(is_string($xref), new InvalidArgumentException());
+
+ $family = Family::getInstance($xref, $tree);
+ assert($family instanceof Family, new InvalidArgumentException());
+
+ $order = $request->getParsedBody()['order'];
+ assert(is_array($order), new InvalidArgumentException());
+
+ Auth::checkFamilyAccess($family, true);
+
+ $dummy_facts = ['0 @' . $family->xref() . '@ FAM'];
+ $sort_facts = [];
+ $keep_facts = [];
+
+ // Split facts into FAMS and other
+ foreach ($family->facts() as $fact) {
+ if ($fact->getTag() === 'CHIL') {
+ $sort_facts[$fact->id()] = $fact->gedcom();
+ } else {
+ $keep_facts[] = $fact->gedcom();
+ }
+ }
+
+ // Sort the facts
+ uksort($sort_facts, static function ($x, $y) use ($order) {
+ return array_search($x, $order, true) - array_search($y, $order, true);
+ });
+
+ // Merge the facts
+ $gedcom = implode("\n", array_merge($dummy_facts, $sort_facts, $keep_facts));
+
+ $family->updateRecord($gedcom, false);
+
+ return redirect($family->url());
+ }
+}
diff --git a/app/Http/RequestHandlers/ReorderChildrenPage.php b/app/Http/RequestHandlers/ReorderChildrenPage.php
new file mode 100644
index 0000000000..941e95c251
--- /dev/null
+++ b/app/Http/RequestHandlers/ReorderChildrenPage.php
@@ -0,0 +1,67 @@
+<?php
+
+/**
+ * webtrees: online genealogy
+ * Copyright (C) 2019 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 <http://www.gnu.org/licenses/>.
+ */
+declare(strict_types=1);
+
+namespace Fisharebest\Webtrees\Http\RequestHandlers;
+
+use Fisharebest\Webtrees\Auth;
+use Fisharebest\Webtrees\Family;
+use Fisharebest\Webtrees\Http\ViewResponseTrait;
+use Fisharebest\Webtrees\I18N;
+use Fisharebest\Webtrees\Tree;
+use InvalidArgumentException;
+use Psr\Http\Message\ResponseInterface;
+use Psr\Http\Message\ServerRequestInterface;
+use Psr\Http\Server\RequestHandlerInterface;
+
+use function assert;
+use function is_string;
+
+/**
+ * Reorder the children in a family.
+ */
+class ReorderChildrenPage implements RequestHandlerInterface
+{
+ use ViewResponseTrait;
+
+ /**
+ * @param ServerRequestInterface $request
+ *
+ * @return ResponseInterface
+ */
+ public function handle(ServerRequestInterface $request): ResponseInterface
+ {
+ $tree = $request->getAttribute('tree');
+ assert($tree instanceof Tree, new InvalidArgumentException());
+
+ $xref = $request->getAttribute('xref');
+ assert(is_string($xref), new InvalidArgumentException());
+
+ $family = Family::getInstance($xref, $tree);
+ assert($family instanceof Family, new InvalidArgumentException());
+
+ Auth::checkFamilyAccess($family, true);
+
+ $title = $family->fullName() . ' — ' . I18N::translate('Re-order children');
+
+ return $this->viewResponse('edit/reorder-children', [
+ 'family' => $family,
+ 'title' => $title,
+ 'tree' => $tree,
+ ]);
+ }
+}
diff --git a/app/Http/RequestHandlers/ReorderMediaAction.php b/app/Http/RequestHandlers/ReorderMediaAction.php
new file mode 100644
index 0000000000..fda0dfd9f0
--- /dev/null
+++ b/app/Http/RequestHandlers/ReorderMediaAction.php
@@ -0,0 +1,89 @@
+<?php
+
+/**
+ * webtrees: online genealogy
+ * Copyright (C) 2019 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 <http://www.gnu.org/licenses/>.
+ */
+declare(strict_types=1);
+
+namespace Fisharebest\Webtrees\Http\RequestHandlers;
+
+use Fisharebest\Webtrees\Auth;
+use Fisharebest\Webtrees\Individual;
+use Fisharebest\Webtrees\Tree;
+use InvalidArgumentException;
+use Psr\Http\Message\ResponseInterface;
+use Psr\Http\Message\ServerRequestInterface;
+use Psr\Http\Server\RequestHandlerInterface;
+
+use function array_merge;
+use function array_search;
+use function assert;
+use function implode;
+use function is_array;
+use function is_string;
+use function redirect;
+use function uksort;
+
+/**
+ * Reorder the media of an individual.
+ */
+class ReorderMediaAction implements RequestHandlerInterface
+{
+ /**
+ * @param ServerRequestInterface $request
+ *
+ * @return ResponseInterface
+ */
+ public function handle(ServerRequestInterface $request): ResponseInterface
+ {
+ $tree = $request->getAttribute('tree');
+ assert($tree instanceof Tree, new InvalidArgumentException());
+
+ $xref = $request->getParsedBody()['xref'];
+ assert(is_string($xref), new InvalidArgumentException());
+
+ $individual = Individual::getInstance($xref, $tree);
+ assert($individual instanceof Individual, new InvalidArgumentException());
+
+ $order = $request->getParsedBody()['order'];
+ assert(is_array($order), new InvalidArgumentException());
+
+ Auth::checkIndividualAccess($individual, true);
+
+ $dummy_facts = ['0 @' . $individual->xref() . '@ INDI'];
+ $sort_facts = [];
+ $keep_facts = [];
+
+ // Split facts into OBJE and other
+ foreach ($individual->facts() as $fact) {
+ if ($fact->getTag() === 'OBJE') {
+ $sort_facts[$fact->id()] = $fact->gedcom();
+ } else {
+ $keep_facts[] = $fact->gedcom();
+ }
+ }
+
+ // Sort the facts
+ uksort($sort_facts, static function ($x, $y) use ($order) {
+ return array_search($x, $order, true) - array_search($y, $order, true);
+ });
+
+ // Merge the facts
+ $gedcom = implode("\n", array_merge($dummy_facts, $sort_facts, $keep_facts));
+
+ $individual->updateRecord($gedcom, false);
+
+ return redirect($individual->url());
+ }
+}
diff --git a/app/Http/RequestHandlers/ReorderMediaPage.php b/app/Http/RequestHandlers/ReorderMediaPage.php
new file mode 100644
index 0000000000..09bce44ddc
--- /dev/null
+++ b/app/Http/RequestHandlers/ReorderMediaPage.php
@@ -0,0 +1,67 @@
+<?php
+
+/**
+ * webtrees: online genealogy
+ * Copyright (C) 2019 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 <http://www.gnu.org/licenses/>.
+ */
+declare(strict_types=1);
+
+namespace Fisharebest\Webtrees\Http\RequestHandlers;
+
+use Fisharebest\Webtrees\Auth;
+use Fisharebest\Webtrees\Http\ViewResponseTrait;
+use Fisharebest\Webtrees\I18N;
+use Fisharebest\Webtrees\Individual;
+use Fisharebest\Webtrees\Tree;
+use InvalidArgumentException;
+use Psr\Http\Message\ResponseInterface;
+use Psr\Http\Message\ServerRequestInterface;
+use Psr\Http\Server\RequestHandlerInterface;
+
+use function assert;
+use function is_string;
+
+/**
+ * Reorder the media of an individual.
+ */
+class ReorderMediaPage implements RequestHandlerInterface
+{
+ use ViewResponseTrait;
+
+ /**
+ * @param ServerRequestInterface $request
+ *
+ * @return ResponseInterface
+ */
+ public function handle(ServerRequestInterface $request): ResponseInterface
+ {
+ $tree = $request->getAttribute('tree');
+ assert($tree instanceof Tree, new InvalidArgumentException());
+
+ $xref = $request->getQueryParams()['xref'];
+ assert(is_string($xref), new InvalidArgumentException());
+
+ $individual = Individual::getInstance($xref, $tree);
+ assert($individual instanceof Individual, new InvalidArgumentException());
+
+ Auth::checkIndividualAccess($individual, true);
+
+ $title = $individual->fullName() . ' — ' . I18N::translate('Re-order media');
+
+ return $this->viewResponse('edit/reorder-media', [
+ 'individual' => $individual,
+ 'title' => $title,
+ 'tree' => $tree,
+ ]);
+ }
+}
diff --git a/app/Http/RequestHandlers/ReorderNamesAction.php b/app/Http/RequestHandlers/ReorderNamesAction.php
new file mode 100644
index 0000000000..1a6b8d48b5
--- /dev/null
+++ b/app/Http/RequestHandlers/ReorderNamesAction.php
@@ -0,0 +1,89 @@
+<?php
+
+/**
+ * webtrees: online genealogy
+ * Copyright (C) 2019 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 <http://www.gnu.org/licenses/>.
+ */
+declare(strict_types=1);
+
+namespace Fisharebest\Webtrees\Http\RequestHandlers;
+
+use Fisharebest\Webtrees\Auth;
+use Fisharebest\Webtrees\Individual;
+use Fisharebest\Webtrees\Tree;
+use InvalidArgumentException;
+use Psr\Http\Message\ResponseInterface;
+use Psr\Http\Message\ServerRequestInterface;
+use Psr\Http\Server\RequestHandlerInterface;
+
+use function array_merge;
+use function array_search;
+use function assert;
+use function implode;
+use function is_array;
+use function is_string;
+use function redirect;
+use function uksort;
+
+/**
+ * Reorder the names of an individual.
+ */
+class ReorderNamesAction implements RequestHandlerInterface
+{
+ /**
+ * @param ServerRequestInterface $request
+ *
+ * @return ResponseInterface
+ */
+ public function handle(ServerRequestInterface $request): ResponseInterface
+ {
+ $tree = $request->getAttribute('tree');
+ assert($tree instanceof Tree, new InvalidArgumentException());
+
+ $xref = $request->getParsedBody()['xref'];
+ assert(is_string($xref), new InvalidArgumentException());
+
+ $individual = Individual::getInstance($xref, $tree);
+ assert($individual instanceof Individual, new InvalidArgumentException());
+
+ $order = $request->getParsedBody()['order'];
+ assert(is_array($order), new InvalidArgumentException());
+
+ Auth::checkIndividualAccess($individual, true);
+
+ $dummy_facts = ['0 @' . $individual->xref() . '@ INDI'];
+ $sort_facts = [];
+ $keep_facts = [];
+
+ // Split facts into NAME and other
+ foreach ($individual->facts() as $fact) {
+ if ($fact->getTag() === 'NAME') {
+ $sort_facts[$fact->id()] = $fact->gedcom();
+ } else {
+ $keep_facts[] = $fact->gedcom();
+ }
+ }
+
+ // Sort the facts
+ uksort($sort_facts, static function ($x, $y) use ($order) {
+ return array_search($x, $order, true) - array_search($y, $order, true);
+ });
+
+ // Merge the facts
+ $gedcom = implode("\n", array_merge($dummy_facts, $sort_facts, $keep_facts));
+
+ $individual->updateRecord($gedcom, false);
+
+ return redirect($individual->url());
+ }
+}
diff --git a/app/Http/RequestHandlers/ReorderNamesPage.php b/app/Http/RequestHandlers/ReorderNamesPage.php
new file mode 100644
index 0000000000..fa46c0a086
--- /dev/null
+++ b/app/Http/RequestHandlers/ReorderNamesPage.php
@@ -0,0 +1,67 @@
+<?php
+
+/**
+ * webtrees: online genealogy
+ * Copyright (C) 2019 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 <http://www.gnu.org/licenses/>.
+ */
+declare(strict_types=1);
+
+namespace Fisharebest\Webtrees\Http\RequestHandlers;
+
+use Fisharebest\Webtrees\Auth;
+use Fisharebest\Webtrees\Http\ViewResponseTrait;
+use Fisharebest\Webtrees\I18N;
+use Fisharebest\Webtrees\Individual;
+use Fisharebest\Webtrees\Tree;
+use InvalidArgumentException;
+use Psr\Http\Message\ResponseInterface;
+use Psr\Http\Message\ServerRequestInterface;
+use Psr\Http\Server\RequestHandlerInterface;
+
+use function assert;
+use function is_string;
+
+/**
+ * Reorder the names of an individual.
+ */
+class ReorderNamesPage implements RequestHandlerInterface
+{
+ use ViewResponseTrait;
+
+ /**
+ * @param ServerRequestInterface $request
+ *
+ * @return ResponseInterface
+ */
+ public function handle(ServerRequestInterface $request): ResponseInterface
+ {
+ $tree = $request->getAttribute('tree');
+ assert($tree instanceof Tree, new InvalidArgumentException());
+
+ $xref = $request->getQueryParams()['xref'];
+ assert(is_string($xref), new InvalidArgumentException());
+
+ $individual = Individual::getInstance($xref, $tree);
+ assert($individual instanceof InvalidArgumentException, new InvalidArgumentException());
+
+ Auth::checkIndividualAccess($individual, true);
+
+ $title = $individual->fullName() . ' — ' . I18N::translate('Re-order names');
+
+ return $this->viewResponse('edit/reorder-names', [
+ 'individual' => $individual,
+ 'title' => $title,
+ 'tree' => $tree,
+ ]);
+ }
+}
diff --git a/app/Http/RequestHandlers/ReorderSpousesAction.php b/app/Http/RequestHandlers/ReorderSpousesAction.php
new file mode 100644
index 0000000000..034b6edafd
--- /dev/null
+++ b/app/Http/RequestHandlers/ReorderSpousesAction.php
@@ -0,0 +1,89 @@
+<?php
+
+/**
+ * webtrees: online genealogy
+ * Copyright (C) 2019 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 <http://www.gnu.org/licenses/>.
+ */
+declare(strict_types=1);
+
+namespace Fisharebest\Webtrees\Http\RequestHandlers;
+
+use Fisharebest\Webtrees\Auth;
+use Fisharebest\Webtrees\Individual;
+use Fisharebest\Webtrees\Tree;
+use InvalidArgumentException;
+use Psr\Http\Message\ResponseInterface;
+use Psr\Http\Message\ServerRequestInterface;
+use Psr\Http\Server\RequestHandlerInterface;
+
+use function array_merge;
+use function array_search;
+use function assert;
+use function implode;
+use function is_array;
+use function is_string;
+use function redirect;
+use function uksort;
+
+/**
+ * Reorder the spouses of an individual.
+ */
+class ReorderSpousesAction implements RequestHandlerInterface
+{
+ /**
+ * @param ServerRequestInterface $request
+ *
+ * @return ResponseInterface
+ */
+ public function handle(ServerRequestInterface $request): ResponseInterface
+ {
+ $tree = $request->getAttribute('tree');
+ assert($tree instanceof Tree, new InvalidArgumentException());
+
+ $xref = $request->getAttribute('xref');
+ assert(is_string($xref), new InvalidArgumentException());
+
+ $individual = Individual::getInstance($xref, $tree);
+ assert($individual instanceof Individual, new InvalidArgumentException());
+
+ $order = $request->getParsedBody()['order'];
+ assert(is_array($order), new InvalidArgumentException());
+
+ Auth::checkIndividualAccess($individual, true);
+
+ $dummy_facts = ['0 @' . $individual->xref() . '@ INDI'];
+ $sort_facts = [];
+ $keep_facts = [];
+
+ // Split facts into FAMS and other
+ foreach ($individual->facts() as $fact) {
+ if ($fact->getTag() === 'FAMS') {
+ $sort_facts[$fact->id()] = $fact->gedcom();
+ } else {
+ $keep_facts[] = $fact->gedcom();
+ }
+ }
+
+ // Sort the facts
+ uksort($sort_facts, static function ($x, $y) use ($order) {
+ return array_search($x, $order, true) - array_search($y, $order, true);
+ });
+
+ // Merge the facts
+ $gedcom = implode("\n", array_merge($dummy_facts, $sort_facts, $keep_facts));
+
+ $individual->updateRecord($gedcom, false);
+
+ return redirect($individual->url());
+ }
+}
diff --git a/app/Http/RequestHandlers/ReorderSpousesPage.php b/app/Http/RequestHandlers/ReorderSpousesPage.php
new file mode 100644
index 0000000000..f0538ae599
--- /dev/null
+++ b/app/Http/RequestHandlers/ReorderSpousesPage.php
@@ -0,0 +1,67 @@
+<?php
+
+/**
+ * webtrees: online genealogy
+ * Copyright (C) 2019 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 <http://www.gnu.org/licenses/>.
+ */
+declare(strict_types=1);
+
+namespace Fisharebest\Webtrees\Http\RequestHandlers;
+
+use Fisharebest\Webtrees\Auth;
+use Fisharebest\Webtrees\Http\ViewResponseTrait;
+use Fisharebest\Webtrees\I18N;
+use Fisharebest\Webtrees\Individual;
+use Fisharebest\Webtrees\Tree;
+use InvalidArgumentException;
+use Psr\Http\Message\ResponseInterface;
+use Psr\Http\Message\ServerRequestInterface;
+use Psr\Http\Server\RequestHandlerInterface;
+
+use function assert;
+use function is_string;
+
+/**
+ * Reorder the spouses of an individual.
+ */
+class ReorderSpousesPage implements RequestHandlerInterface
+{
+ use ViewResponseTrait;
+
+ /**
+ * @param ServerRequestInterface $request
+ *
+ * @return ResponseInterface
+ */
+ public function handle(ServerRequestInterface $request): ResponseInterface
+ {
+ $tree = $request->getAttribute('tree');
+ assert($tree instanceof Tree, new InvalidArgumentException());
+
+ $xref = $request->getAttribute('xref');
+ assert(is_string($xref), new InvalidArgumentException());
+
+ $individual = Individual::getInstance($xref, $tree);
+ assert($individual instanceof Individual, new InvalidArgumentException());
+
+ Auth::checkIndividualAccess($individual, true);
+
+ $title = $individual->fullName() . ' — ' . I18N::translate('Re-order families');
+
+ return $this->viewResponse('edit/reorder-spouses', [
+ 'individual' => $individual,
+ 'title' => $title,
+ 'tree' => $tree,
+ ]);
+ }
+}