xref: /webtrees/app/Http/RequestHandlers/PendingChangesLogData.php (revision 928e8f1ab8bf8a4eae9e8ec7a464d6b65ee174d5)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2023 webtrees development team
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <https://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use DateTimeImmutable;
23use DateTimeZone;
24use Fisharebest\Algorithm\MyersDiff;
25use Fisharebest\Webtrees\Auth;
26use Fisharebest\Webtrees\Contracts\UserInterface;
27use Fisharebest\Webtrees\Gedcom;
28use Fisharebest\Webtrees\I18N;
29use Fisharebest\Webtrees\Registry;
30use Fisharebest\Webtrees\Services\DatatablesService;
31use Fisharebest\Webtrees\Services\PendingChangesService;
32use Fisharebest\Webtrees\Validator;
33use Psr\Http\Message\ResponseInterface;
34use Psr\Http\Message\ServerRequestInterface;
35use Psr\Http\Server\RequestHandlerInterface;
36
37use function e;
38use function explode;
39use function implode;
40use function preg_replace_callback;
41
42/**
43 * Find pending changes.
44 */
45class PendingChangesLogData implements RequestHandlerInterface
46{
47    private DatatablesService $datatables_service;
48
49    private MyersDiff $myers_diff;
50
51    private PendingChangesService $pending_changes_service;
52
53    /**
54     * @param DatatablesService     $datatables_service
55     * @param MyersDiff             $myers_diff
56     * @param PendingChangesService $pending_changes_service
57     */
58    public function __construct(
59        DatatablesService $datatables_service,
60        MyersDiff $myers_diff,
61        PendingChangesService $pending_changes_service
62    ) {
63        $this->datatables_service      = $datatables_service;
64        $this->myers_diff              = $myers_diff;
65        $this->pending_changes_service = $pending_changes_service;
66    }
67
68    /**
69     * @param ServerRequestInterface $request
70     *
71     * @return ResponseInterface
72     */
73    public function handle(ServerRequestInterface $request): ResponseInterface
74    {
75        $tree           = Validator::attributes($request)->tree();
76        $params         = $request->getQueryParams();
77        $params['tree'] = $tree->name();
78
79        $query = $this->pending_changes_service->changesQuery($params);
80
81        $callback = function (object $row) use ($tree): array {
82            $old_lines = $row->old_gedcom === '' ? [] : explode("\n", $row->old_gedcom);
83            $new_lines = $row->new_gedcom === '' ? [] : explode("\n", $row->new_gedcom);
84
85            $differences = $this->myers_diff->calculate($old_lines, $new_lines);
86            $diff_lines  = [];
87
88            foreach ($differences as $difference) {
89                switch ($difference[1]) {
90                    case MyersDiff::DELETE:
91                        $diff_lines[] = '<del>' . e($difference[0]) . '</del>';
92                        break;
93                    case MyersDiff::INSERT:
94                        $diff_lines[] = '<ins>' . e($difference[0]) . '</ins>';
95                        break;
96                    default:
97                        $diff_lines[] = e($difference[0]);
98                }
99            }
100
101            // Only convert valid xrefs to links
102            $record = Registry::gedcomRecordFactory()->make($row->xref, $tree);
103
104            $change_time = DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $row->change_time, new DateTimeZone('UTC'))
105                ->setTimezone(new DateTimeZone(Auth::user()->getPreference(UserInterface::PREF_TIME_ZONE, 'UTC')))
106                ->format('Y-m-d H:i:s T');
107
108            return [
109                $row->change_id,
110                $change_time,
111                I18N::translate($row->status),
112                $record ? '<a href="' . e($record->url()) . '">' . $record->xref() . '</a>' : $row->xref,
113                '<div class="gedcom-data" dir="ltr">' .
114                preg_replace_callback(
115                    '/@(' . Gedcom::REGEX_XREF . ')@/',
116                    static function (array $match) use ($tree): string {
117                        $record = Registry::gedcomRecordFactory()->make($match[1], $tree);
118
119                        return $record ? '<a href="' . e($record->url()) . '">' . $match[0] . '</a>' : $match[0];
120                    },
121                    implode("\n", $diff_lines)
122                ) .
123                '</div>',
124                $row->user_name,
125                $row->gedcom_name,
126            ];
127        };
128
129        return $this->datatables_service->handleQuery($request, $query, [], [], $callback);
130    }
131}
132