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