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