summaryrefslogtreecommitdiff
path: root/app/Http/RequestHandlers/PendingChangesLogData.php
diff options
context:
space:
mode:
authorGreg Roach <fisharebest@webtrees.net>2019-10-24 11:29:15 +0100
committerGreg Roach <fisharebest@webtrees.net>2019-10-24 13:54:33 +0100
commit22e73debfe88ce217451588465f714b2b8a966ab (patch)
tree194ec6626d9944915fd109c52edd204e265824d1 /app/Http/RequestHandlers/PendingChangesLogData.php
parent506a570fa9c3f6601c12f783d0393f4ccacc5844 (diff)
downloadwebtrees-22e73debfe88ce217451588465f714b2b8a966ab.tar.gz
webtrees-22e73debfe88ce217451588465f714b2b8a966ab.tar.bz2
webtrees-22e73debfe88ce217451588465f714b2b8a966ab.zip
Refactor pending changes to use service and request handlers
Diffstat (limited to 'app/Http/RequestHandlers/PendingChangesLogData.php')
-rw-r--r--app/Http/RequestHandlers/PendingChangesLogData.php135
1 files changed, 135 insertions, 0 deletions
diff --git a/app/Http/RequestHandlers/PendingChangesLogData.php b/app/Http/RequestHandlers/PendingChangesLogData.php
new file mode 100644
index 0000000000..a4af95c3b3
--- /dev/null
+++ b/app/Http/RequestHandlers/PendingChangesLogData.php
@@ -0,0 +1,135 @@
+<?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\Algorithm\MyersDiff;
+use Fisharebest\Webtrees\Carbon;
+use Fisharebest\Webtrees\Gedcom;
+use Fisharebest\Webtrees\GedcomRecord;
+use Fisharebest\Webtrees\I18N;
+use Fisharebest\Webtrees\Services\DatatablesService;
+use Fisharebest\Webtrees\Services\PendingChangesService;
+use Fisharebest\Webtrees\Services\TreeService;
+use Fisharebest\Webtrees\Tree;
+use InvalidArgumentException;
+use Psr\Http\Message\ResponseInterface;
+use Psr\Http\Message\ServerRequestInterface;
+use Psr\Http\Server\RequestHandlerInterface;
+use stdClass;
+
+use function e;
+use function explode;
+use function implode;
+use function preg_replace_callback;
+
+/**
+ * Find pending changes.
+ */
+class PendingChangesLogData implements RequestHandlerInterface
+{
+ /** @var DatatablesService */
+ private $datatables_service;
+
+ /** @var MyersDiff */
+ private $myers_diff;
+
+ /** @var PendingChangesService */
+ private $pending_changes_service;
+
+ /** @var TreeService */
+ private $tree_service;
+
+ /**
+ * @param DatatablesService $datatables_service
+ * @param MyersDiff $myers_diff
+ * @param PendingChangesService $pending_changes_service
+ * @param TreeService $tree_service
+ */
+ public function __construct(
+ DatatablesService $datatables_service,
+ MyersDiff $myers_diff,
+ PendingChangesService $pending_changes_service,
+ TreeService $tree_service
+ ) {
+ $this->datatables_service = $datatables_service;
+ $this->myers_diff = $myers_diff;
+ $this->pending_changes_service = $pending_changes_service;
+ $this->tree_service = $tree_service;
+ }
+
+ /**
+ * @param ServerRequestInterface $request
+ *
+ * @return ResponseInterface
+ */
+ public function handle(ServerRequestInterface $request): ResponseInterface
+ {
+ $tree = $request->getAttribute('tree');
+ assert($tree instanceof Tree, new InvalidArgumentException());
+
+ $query = $this->pending_changes_service->changesQuery($request);
+
+ $callback = function (stdClass $row) use ($tree): array {
+ $old_lines = explode("\n", $row->old_gedcom);
+ $new_lines = explode("\n", $row->new_gedcom);
+
+ $differences = $this->myers_diff->calculate($old_lines, $new_lines);
+ $diff_lines = [];
+
+ foreach ($differences as $difference) {
+ switch ($difference[1]) {
+ case MyersDiff::DELETE:
+ $diff_lines[] = '<del>' . $difference[0] . '</del>';
+ break;
+ case MyersDiff::INSERT:
+ $diff_lines[] = '<ins>' . $difference[0] . '</ins>';
+ break;
+ default:
+ $diff_lines[] = $difference[0];
+ }
+ }
+
+ // Only convert valid xrefs to links
+ $record = GedcomRecord::getInstance($row->xref, $tree);
+
+ return [
+ $row->change_id,
+ Carbon::make($row->change_time)->local()->format('Y-m-d H:i:s'),
+ I18N::translate($row->status),
+ $record ? '<a href="' . e($record->url()) . '">' . $record->xref() . '</a>' : $row->xref,
+ '<div class="gedcom-data" dir="ltr">' .
+ preg_replace_callback(
+ '/@(' . Gedcom::REGEX_XREF . ')@/',
+ static function (array $match) use ($tree): string {
+ $record = GedcomRecord::getInstance($match[1], $tree);
+
+ return $record ? '<a href="' . e($record->url()) . '">' . $match[0] . '</a>' : $match[0];
+ },
+ implode("\n", $diff_lines)
+ ) .
+ '</div>',
+ $row->user_name,
+ $row->gedcom_name,
+ ];
+ };
+
+ return $this->datatables_service->handle($request, $query, [], [], $callback);
+ }
+}