xref: /webtrees/app/Http/RequestHandlers/SiteLogsPage.php (revision 8634febf6487fcb3ee7b59e88f828071a22cba22)
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 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\Tree;
28use Fisharebest\Webtrees\User;
29use Fisharebest\Webtrees\Validator;
30use Illuminate\Database\Capsule\Manager as DB;
31use Psr\Http\Message\ResponseInterface;
32use Psr\Http\Message\ServerRequestInterface;
33use Psr\Http\Server\RequestHandlerInterface;
34
35use function max;
36use function min;
37
38/**
39 * Show logs.
40 */
41class SiteLogsPage implements RequestHandlerInterface
42{
43    use ViewResponseTrait;
44
45    private TreeService $tree_service;
46
47    private UserService $user_service;
48
49    /**
50     * @param TreeService $tree_service
51     * @param UserService $user_service
52     */
53    public function __construct(TreeService $tree_service, UserService $user_service)
54    {
55        $this->tree_service = $tree_service;
56        $this->user_service = $user_service;
57    }
58
59    /**
60     * @param ServerRequestInterface $request
61     *
62     * @return ResponseInterface
63     */
64    public function handle(ServerRequestInterface $request): ResponseInterface
65    {
66        $this->layout = 'layouts/administration';
67
68        $earliest = DB::table('log')->min('log_time');
69        $latest   = DB::table('log')->max('log_time');
70
71        $earliest = Registry::timestampFactory()->fromString($earliest)->toDateString();
72        $latest   = Registry::timestampFactory()->fromString($latest)->toDateString();
73
74        $action   = Validator::queryParams($request)->string('action', '');
75        $from     = Validator::queryParams($request)->string('from', $earliest);
76        $to       = Validator::queryParams($request)->string('to', $latest);
77        $type     = Validator::queryParams($request)->string('type', '');
78        $text     = Validator::queryParams($request)->string('text', '');
79        $ip       = Validator::queryParams($request)->string('ip', '');
80        $username = Validator::queryParams($request)->string('username', '');
81        $tree     = Validator::queryParams($request)->string('tree', '');
82
83        $from = max($from, $earliest);
84        $to   = min(max($from, $to), $latest);
85
86        $user_options = $this->user_service->all()->mapWithKeys(static function (User $user): array {
87            return [$user->userName() => $user->userName()];
88        });
89        $user_options->prepend('', '');
90
91        $tree_options = $this->tree_service->all()->mapWithKeys(static function (Tree $tree): array {
92            return [$tree->name() => $tree->title()];
93        });
94        $tree_options->prepend('', '');
95
96        $title = I18N::translate('Website logs');
97
98        return $this->viewResponse('admin/site-logs', [
99            'action'       => $action,
100            'earliest'     => $earliest,
101            'from'         => $from,
102            'tree'         => $tree,
103            'ip'           => $ip,
104            'latest'       => $latest,
105            'tree_options' => $tree_options,
106            'title'        => $title,
107            'to'           => $to,
108            'text'         => $text,
109            'type'         => $type,
110            'username'     => $username,
111            'user_options' => $user_options,
112        ]);
113    }
114}
115