xref: /webtrees/app/Http/RequestHandlers/SiteLogsPage.php (revision b8fc901f205cd6af65496b916bf63547a3065a2f)
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 Fisharebest\Webtrees\User;
29use Illuminate\Database\Capsule\Manager as DB;
30use Illuminate\Support\Collection;
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    /** @var TreeService */
46    private $tree_service;
47
48    /** @var UserService */
49    private $user_service;
50
51    /**
52     * @param TreeService $tree_service
53     * @param UserService $user_service
54     */
55    public function __construct(TreeService $tree_service, UserService $user_service)
56    {
57        $this->tree_service = $tree_service;
58        $this->user_service = $user_service;
59    }
60
61    /**
62     * @param ServerRequestInterface $request
63     *
64     * @return ResponseInterface
65     */
66    public function handle(ServerRequestInterface $request): ResponseInterface
67    {
68        $this->layout = 'layouts/administration';
69
70        $earliest = DB::table('log')->min('log_time');
71        $latest   = DB::table('log')->max('log_time');
72
73        $earliest = $earliest === null ? Carbon::now() : Carbon::make($earliest);
74        $latest   = $latest === null ? Carbon::now() : Carbon::make($latest);
75
76        $earliest = $earliest->toDateString();
77        $latest   = $latest->toDateString();
78
79        $params   = $request->getQueryParams();
80        $action   = $params['action'] ?? '';
81        $from     = $params['from'] ?? $earliest;
82        $to       = $params['to'] ?? $latest;
83        $type     = $params['type'] ?? '';
84        $text     = $params['text'] ?? '';
85        $ip       = $params['ip'] ?? '';
86        $username = $params['username'] ?? '';
87        $tree     = $params['tree'] ?? '';
88
89        $from = max($from, $earliest);
90        $to   = min(max($from, $to), $latest);
91
92        $user_options = $this->user_service->all()->mapWithKeys(static function (User $user): array {
93            return [$user->userName() => $user->userName()];
94        });
95        $user_options = (new Collection(['' => '']))->merge($user_options);
96
97        $tree_options = $this->tree_service->all()->mapWithKeys(static function (Tree $tree): array {
98            return [$tree->name() => $tree->title()];
99        });
100        $tree_options = (new Collection(['' => '']))->merge($tree_options);
101
102        $title = I18N::translate('Website logs');
103
104        return $this->viewResponse('admin/site-logs', [
105            'action'       => $action,
106            'earliest'     => $earliest,
107            'from'         => $from,
108            'tree'         => $tree,
109            'ip'           => $ip,
110            'latest'       => $latest,
111            'tree_options' => $tree_options,
112            'title'        => $title,
113            'to'           => $to,
114            'text'         => $text,
115            'type'         => $type,
116            'username'     => $username,
117            'user_options' => $user_options,
118        ]);
119    }
120}
121