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