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