xref: /webtrees/app/Http/RequestHandlers/PendingChanges.php (revision dce3fc17a77de429d654952a4a15ef60b3ec9fec)
1<?php
2
3/**
4 * webtrees: online genealogy
5 * Copyright (C) 2019 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 <http://www.gnu.org/licenses/>.
16 */
17
18declare(strict_types=1);
19
20namespace Fisharebest\Webtrees\Http\RequestHandlers;
21
22use Fisharebest\Webtrees\Carbon;
23use Fisharebest\Webtrees\Family;
24use Fisharebest\Webtrees\Gedcom;
25use Fisharebest\Webtrees\GedcomRecord;
26use Fisharebest\Webtrees\Http\ViewResponseTrait;
27use Fisharebest\Webtrees\I18N;
28use Fisharebest\Webtrees\Individual;
29use Fisharebest\Webtrees\Media;
30use Fisharebest\Webtrees\Note;
31use Fisharebest\Webtrees\Repository;
32use Fisharebest\Webtrees\Services\PendingChangesService;
33use Fisharebest\Webtrees\Services\TreeService;
34use Fisharebest\Webtrees\Source;
35use Fisharebest\Webtrees\Tree;
36use Illuminate\Database\Capsule\Manager as DB;
37use InvalidArgumentException;
38use Psr\Http\Message\ResponseInterface;
39use Psr\Http\Message\ServerRequestInterface;
40use Psr\Http\Server\RequestHandlerInterface;
41
42use function assert;
43use function key;
44use function preg_match;
45use function reset;
46use function route;
47
48/**
49 * Show all pending changes.
50 */
51class PendingChanges implements RequestHandlerInterface
52{
53    use ViewResponseTrait;
54
55    /** @var PendingChangesService */
56    private $pending_changes_service;
57
58    /**
59     * @param TreeService $tree_service
60     */
61    public function __construct(TreeService $tree_service)
62    {
63        $this->tree_service = $tree_service;
64    }
65
66    /**
67     * @param ServerRequestInterface $request
68     *
69     * @return ResponseInterface
70     */
71    public function handle(ServerRequestInterface $request): ResponseInterface
72    {
73        $tree = $request->getAttribute('tree');
74        assert($tree instanceof Tree, new InvalidArgumentException());
75
76        $url = $request->getQueryParams()['url'] ?? route('tree-page', ['tree' => $tree->name()]);
77
78        $rows = DB::table('change')
79            ->join('user', 'user.user_id', '=', 'change.user_id')
80            ->join('gedcom', 'gedcom.gedcom_id', '=', 'change.gedcom_id')
81            ->where('status', '=', 'pending')
82            ->orderBy('change.gedcom_id')
83            ->orderBy('change.xref')
84            ->orderBy('change.change_id')
85            ->select(['change.*', 'user.user_name', 'user.real_name', 'gedcom_name'])
86            ->get();
87
88        $changes = [];
89        foreach ($rows as $row) {
90            $row->change_time = Carbon::make($row->change_time);
91
92            $change_tree = $this->tree_service->all()->get($row->gedcom_name);
93
94            preg_match('/^0 (?:@' . Gedcom::REGEX_XREF . '@ )?(' . Gedcom::REGEX_TAG . ')/', $row->old_gedcom . $row->new_gedcom, $match);
95
96            switch ($match[1]) {
97                case 'INDI':
98                    $row->record = new Individual($row->xref, $row->old_gedcom, $row->new_gedcom, $change_tree);
99                    break;
100                case 'FAM':
101                    $row->record = new Family($row->xref, $row->old_gedcom, $row->new_gedcom, $change_tree);
102                    break;
103                case 'SOUR':
104                    $row->record = new Source($row->xref, $row->old_gedcom, $row->new_gedcom, $change_tree);
105                    break;
106                case 'REPO':
107                    $row->record = new Repository($row->xref, $row->old_gedcom, $row->new_gedcom, $change_tree);
108                    break;
109                case 'OBJE':
110                    $row->record = new Media($row->xref, $row->old_gedcom, $row->new_gedcom, $change_tree);
111                    break;
112                case 'NOTE':
113                    $row->record = new Note($row->xref, $row->old_gedcom, $row->new_gedcom, $change_tree);
114                    break;
115                default:
116                    $row->record = new GedcomRecord($row->xref, $row->old_gedcom, $row->new_gedcom, $change_tree);
117                    break;
118            }
119
120            $changes[$row->gedcom_id][$row->xref][] = $row;
121        }
122
123        $title = I18N::translate('Pending changes');
124
125        // If the current tree has changes, activate that tab.  Otherwise activate the first tab.
126        if (($changes[$tree->id()] ?? []) === []) {
127            reset($changes);
128            $active_tree_id = key($changes);
129        } else {
130            $active_tree_id = $tree->id();
131        }
132
133        return $this->viewResponse('pending-changes-page', [
134            'active_tree_id' => $active_tree_id,
135            'changes'        => $changes,
136            'title'          => $title,
137            'tree'           => $tree,
138            'url'            => $url,
139        ]);
140    }
141}
142