xref: /webtrees/app/Http/RequestHandlers/PendingChangesLogPage.php (revision 22e73debfe88ce217451588465f714b2b8a966ab)
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\Http\ViewResponseTrait;
24use Fisharebest\Webtrees\I18N;
25use Fisharebest\Webtrees\Services\TreeService;
26use Fisharebest\Webtrees\Services\UserService;
27use Fisharebest\Webtrees\Tree;
28use Illuminate\Database\Capsule\Manager as DB;
29use InvalidArgumentException;
30use Psr\Http\Message\ResponseInterface;
31use Psr\Http\Message\ServerRequestInterface;
32use Psr\Http\Server\RequestHandlerInterface;
33
34use function array_key_exists;
35use function reset;
36
37/**
38 * Show pending changes.
39 */
40class PendingChangesLogPage implements RequestHandlerInterface
41{
42    use ViewResponseTrait;
43
44    /** @var TreeService */
45    private $tree_service;
46
47    /** @var UserService */
48    private $user_service;
49
50    /**
51     * @param TreeService $tree_service
52     * @param UserService $user_service
53     */
54    public function __construct(TreeService $tree_service, UserService $user_service)
55    {
56        $this->tree_service = $tree_service;
57        $this->user_service = $user_service;
58    }
59
60    /**
61     * @param ServerRequestInterface $request
62     *
63     * @return ResponseInterface
64     */
65    public function handle(ServerRequestInterface $request): ResponseInterface
66    {
67        $this->layout = 'layouts/administration';
68
69        $tree = $request->getAttribute('tree');
70        assert($tree instanceof Tree, new InvalidArgumentException());
71
72        $trees = $this->tree_service->titles();
73
74        $users = ['' => ''];
75        foreach ($this->user_service->all() as $user) {
76            $user_name             = $user->userName();
77            $users[$user_name] = $user_name;
78        }
79
80        // First and last change in the database.
81        $earliest = DB::table('change')->min('change_time');
82        $latest   = DB::table('change')->max('change_time');
83
84        $earliest = $earliest !== null ? Carbon::make($earliest) : Carbon::now();
85        $latest   = $latest !== null ? Carbon::make($latest) : Carbon::now();
86
87        $earliest = $earliest->toDateString();
88        $latest   = $latest->toDateString();
89
90        $from     = $request->getQueryParams()['from'] ?? $earliest;
91        $to       = $request->getQueryParams()['to'] ?? $latest;
92        $type     = $request->getQueryParams()['type'] ?? '';
93        $oldged   = $request->getQueryParams()['oldged'] ?? '';
94        $newged   = $request->getQueryParams()['newged'] ?? '';
95        $xref     = $request->getQueryParams()['xref'] ?? '';
96        $username = $request->getQueryParams()['username'] ?? '';
97
98        return $this->viewResponse('admin/changes-log', [
99            'earliest'  => $earliest,
100            'from'      => $from,
101            'latest'    => $latest,
102            'newged'    => $newged,
103            'oldged'    => $oldged,
104            'statuses'  => $this->changeStatuses(),
105            'title'     => I18N::translate('Changes log'),
106            'to'        => $to,
107            'tree'      => $tree,
108            'trees'     => $trees,
109            'type'      => $type,
110            'username'  => $username,
111            'users'     => $users,
112            'xref'      => $xref,
113        ]);
114    }
115
116    /**
117     * Labels for the various statuses.
118     *
119     * @return array
120     */
121    private function changeStatuses(): array
122    {
123        return [
124            ''         => '',
125            /* I18N: the status of an edit accepted/rejected/pending */
126            'accepted' => I18N::translate('accepted'),
127            /* I18N: the status of an edit accepted/rejected/pending */
128            'rejected' => I18N::translate('rejected'),
129            /* I18N: the status of an edit accepted/rejected/pending */
130            'pending'  => I18N::translate('pending'),
131        ];
132    }
133}
134