Passed
Push — master ( 506a57...22e73d )
by Greg
06:57
created

PendingChanges::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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