summaryrefslogtreecommitdiff
path: root/app/Http/RequestHandlers/FixLevel0MediaAction.php
diff options
context:
space:
mode:
authorGreg Roach <greg@subaqua.co.uk>2021-01-26 11:44:10 +0000
committerGreg Roach <greg@subaqua.co.uk>2021-01-26 13:57:16 +0000
commit4b3ef6caf72914d34581bb4a8d677e615e877a9b (patch)
treef79022245fefb5727eb6b17c78c1189f29b69c15 /app/Http/RequestHandlers/FixLevel0MediaAction.php
parent89f7189b61a494347591c99bdb92afb7d8b66e1b (diff)
downloadwebtrees-4b3ef6caf72914d34581bb4a8d677e615e877a9b.tar.gz
webtrees-4b3ef6caf72914d34581bb4a8d677e615e877a9b.tar.bz2
webtrees-4b3ef6caf72914d34581bb4a8d677e615e877a9b.zip
Convert controller to request handlers
Diffstat (limited to 'app/Http/RequestHandlers/FixLevel0MediaAction.php')
-rw-r--r--app/Http/RequestHandlers/FixLevel0MediaAction.php84
1 files changed, 84 insertions, 0 deletions
diff --git a/app/Http/RequestHandlers/FixLevel0MediaAction.php b/app/Http/RequestHandlers/FixLevel0MediaAction.php
new file mode 100644
index 0000000000..6421e14733
--- /dev/null
+++ b/app/Http/RequestHandlers/FixLevel0MediaAction.php
@@ -0,0 +1,84 @@
+<?php
+
+/**
+ * webtrees: online genealogy
+ * Copyright (C) 2021 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\Http\RequestHandlers;
+
+use Fisharebest\Webtrees\Registry;
+use Fisharebest\Webtrees\Services\TreeService;
+use Psr\Http\Message\ResponseInterface;
+use Psr\Http\Message\ServerRequestInterface;
+use Psr\Http\Server\RequestHandlerInterface;
+
+use function response;
+
+/**
+ * Move media links from records to facts.
+ */
+class FixLevel0MediaAction implements RequestHandlerInterface
+{
+ /** @var TreeService */
+ private $tree_service;
+
+ /**
+ * FixLevel0MediaController constructor.
+ *
+ * @param TreeService $tree_service
+ */
+ public function __construct(TreeService $tree_service)
+ {
+ $this->tree_service = $tree_service;
+ }
+
+ /**
+ * Move a link to a media object from a level 0 record to a level 1 record.
+ *
+ * @param ServerRequestInterface $request
+ *
+ * @return ResponseInterface
+ */
+ public function handle(ServerRequestInterface $request): ResponseInterface
+ {
+ $params = (array) $request->getParsedBody();
+
+ $fact_id = $params['fact_id'];
+ $indi_xref = $params['indi_xref'];
+ $obje_xref = $params['obje_xref'];
+ $tree_id = (int) $params['tree_id'];
+
+ $tree = $this->tree_service->find($tree_id);
+ $individual = Registry::individualFactory()->make($indi_xref, $tree);
+ $media = Registry::mediaFactory()->make($obje_xref, $tree);
+
+ if ($individual !== null && $media !== null) {
+ foreach ($individual->facts() as $fact1) {
+ if ($fact1->id() === $fact_id) {
+ $individual->updateFact($fact_id, $fact1->gedcom() . "\n2 OBJE @" . $obje_xref . '@', false);
+ foreach ($individual->facts(['OBJE']) as $fact2) {
+ if ($fact2->target() === $media) {
+ $individual->deleteFact($fact2->id(), false);
+ }
+ }
+ break;
+ }
+ }
+ }
+
+ return response();
+ }
+}