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