xref: /webtrees/app/Services/SiteLogsService.php (revision 57bfa969ae5adb60a68faad19182a580933467cd)
1*57bfa969SGreg Roach<?php
2*57bfa969SGreg Roach
3*57bfa969SGreg Roach/**
4*57bfa969SGreg Roach * webtrees: online genealogy
5*57bfa969SGreg Roach * Copyright (C) 2019 webtrees development team
6*57bfa969SGreg Roach * This program is free software: you can redistribute it and/or modify
7*57bfa969SGreg Roach * it under the terms of the GNU General Public License as published by
8*57bfa969SGreg Roach * the Free Software Foundation, either version 3 of the License, or
9*57bfa969SGreg Roach * (at your option) any later version.
10*57bfa969SGreg Roach * This program is distributed in the hope that it will be useful,
11*57bfa969SGreg Roach * but WITHOUT ANY WARRANTY; without even the implied warranty of
12*57bfa969SGreg Roach * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13*57bfa969SGreg Roach * GNU General Public License for more details.
14*57bfa969SGreg Roach * You should have received a copy of the GNU General Public License
15*57bfa969SGreg Roach * along with this program. If not, see <http://www.gnu.org/licenses/>.
16*57bfa969SGreg Roach */
17*57bfa969SGreg Roach
18*57bfa969SGreg Roachdeclare(strict_types=1);
19*57bfa969SGreg Roach
20*57bfa969SGreg Roachnamespace Fisharebest\Webtrees\Services;
21*57bfa969SGreg Roach
22*57bfa969SGreg Roachuse Fisharebest\Webtrees\Carbon;
23*57bfa969SGreg Roachuse Illuminate\Database\Capsule\Manager as DB;
24*57bfa969SGreg Roachuse Illuminate\Database\Query\Builder;
25*57bfa969SGreg Roachuse Illuminate\Database\Query\Expression;
26*57bfa969SGreg Roach
27*57bfa969SGreg Roach/**
28*57bfa969SGreg Roach * Manage site logs
29*57bfa969SGreg Roach */
30*57bfa969SGreg Roachclass SiteLogsService
31*57bfa969SGreg Roach{
32*57bfa969SGreg Roach    /**
33*57bfa969SGreg Roach     * Generate a query for filtering the changes log.
34*57bfa969SGreg Roach     *
35*57bfa969SGreg Roach     * @param string[] $params
36*57bfa969SGreg Roach     *
37*57bfa969SGreg Roach     * @return Builder
38*57bfa969SGreg Roach     */
39*57bfa969SGreg Roach    public function logsQuery(array $params): Builder
40*57bfa969SGreg Roach    {
41*57bfa969SGreg Roach        $tree     = $params['tree'];
42*57bfa969SGreg Roach        $from     = $params['from'];
43*57bfa969SGreg Roach        $to       = $params['to'];
44*57bfa969SGreg Roach        $type     = $params['type'];
45*57bfa969SGreg Roach        $text     = $params['text'];
46*57bfa969SGreg Roach        $ip       = $params['ip'];
47*57bfa969SGreg Roach        $username = $params['username'];
48*57bfa969SGreg Roach
49*57bfa969SGreg Roach        $query = DB::table('log')
50*57bfa969SGreg Roach            ->leftJoin('user', 'user.user_id', '=', 'log.user_id')
51*57bfa969SGreg Roach            ->leftJoin('gedcom', 'gedcom.gedcom_id', '=', 'log.gedcom_id')
52*57bfa969SGreg Roach            ->select(['log.*', new Expression("COALESCE(user_name, '<none>') AS user_name"), new Expression("COALESCE(gedcom_name, '<none>') AS gedcom_name")]);
53*57bfa969SGreg Roach
54*57bfa969SGreg Roach        if ($from !== '') {
55*57bfa969SGreg Roach            $query->where('log_time', '>=', $from);
56*57bfa969SGreg Roach        }
57*57bfa969SGreg Roach
58*57bfa969SGreg Roach        if ($to !== '') {
59*57bfa969SGreg Roach            // before end of the day
60*57bfa969SGreg Roach            $query->where('log_time', '<', Carbon::make($to)->addDay());
61*57bfa969SGreg Roach        }
62*57bfa969SGreg Roach
63*57bfa969SGreg Roach        if ($type !== '') {
64*57bfa969SGreg Roach            $query->where('log_type', '=', $type);
65*57bfa969SGreg Roach        }
66*57bfa969SGreg Roach
67*57bfa969SGreg Roach        if ($text !== '') {
68*57bfa969SGreg Roach            $query->whereContains('log_message', $text);
69*57bfa969SGreg Roach        }
70*57bfa969SGreg Roach
71*57bfa969SGreg Roach        if ($ip !== '') {
72*57bfa969SGreg Roach            $query->whereContains('ip_address', $ip);
73*57bfa969SGreg Roach        }
74*57bfa969SGreg Roach
75*57bfa969SGreg Roach        if ($username !== '') {
76*57bfa969SGreg Roach            $query->whereContains('user_name', $ip);
77*57bfa969SGreg Roach        }
78*57bfa969SGreg Roach
79*57bfa969SGreg Roach        if ($tree !== '') {
80*57bfa969SGreg Roach            $query->where('gedcom_name', '=', $tree);
81*57bfa969SGreg Roach        }
82*57bfa969SGreg Roach
83*57bfa969SGreg Roach        return $query;
84*57bfa969SGreg Roach    }
85*57bfa969SGreg Roach}
86