1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
4
|
|
|
* |
5
|
|
|
* Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics) |
6
|
|
|
* |
7
|
|
|
* This program is free software; you can redistribute it and/or |
8
|
|
|
* modify it under the terms of the GNU General Public License |
9
|
|
|
* as published by the Free Software Foundation; either version 2 |
10
|
|
|
* of the License, or (at your option) any later version. |
11
|
|
|
* |
12
|
|
|
* This program is distributed in the hope that it will be useful, |
13
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
14
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15
|
|
|
* GNU General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU General Public License |
18
|
|
|
* along with this program; if not, write to the Free Software |
19
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
namespace App\Services\LogSystem; |
23
|
|
|
|
24
|
|
|
|
25
|
|
|
use App\Entity\LogSystem\AbstractLogEntry; |
26
|
|
|
use App\Entity\LogSystem\DatabaseUpdatedLogEntry; |
27
|
|
|
use App\Entity\LogSystem\ElementCreatedLogEntry; |
28
|
|
|
use App\Entity\LogSystem\ElementDeletedLogEntry; |
29
|
|
|
use App\Entity\LogSystem\ElementEditedLogEntry; |
30
|
|
|
use App\Entity\LogSystem\ExceptionLogEntry; |
31
|
|
|
use App\Entity\LogSystem\InstockChangedLogEntry; |
32
|
|
|
use App\Entity\LogSystem\UserLoginLogEntry; |
33
|
|
|
use App\Entity\LogSystem\UserLogoutLogEntry; |
34
|
|
|
use App\Entity\LogSystem\UserNotAllowedLogEntry; |
35
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Format the Extra field of a log entry in a user readible form. |
39
|
|
|
* @package App\Services\LogSystem |
40
|
|
|
*/ |
41
|
|
|
class LogEntryExtraFormatter |
42
|
|
|
{ |
43
|
|
|
protected $translator; |
44
|
|
|
|
45
|
|
|
public function __construct(TranslatorInterface $translator) |
46
|
|
|
{ |
47
|
|
|
$this->translator = $translator; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Return an user viewable representation of the extra data in a log entry, styled for console output. |
52
|
|
|
* @param AbstractLogEntry $logEntry |
53
|
|
|
* @return string |
54
|
|
|
*/ |
55
|
|
|
public function formatConsole(AbstractLogEntry $logEntry): string |
56
|
|
|
{ |
57
|
|
|
$tmp = $this->format($logEntry); |
58
|
|
|
|
59
|
|
|
//Just a simple tweak to make the console output more pretty. |
60
|
|
|
$search = ['<i>', '</i>', '<b>', '</b>', ' <i class="fas fa-long-arrow-alt-right">']; |
61
|
|
|
$replace = ['<info>', '</info>', '<error>', '</error>', '→']; |
62
|
|
|
|
63
|
|
|
return str_replace($search, $replace, $tmp); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Return a HTML formatted string containing a user viewable form of the Extra data |
68
|
|
|
* @param AbstractLogEntry $context |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
public function format(AbstractLogEntry $context): string |
72
|
|
|
{ |
73
|
|
|
if ($context instanceof UserLoginLogEntry || $context instanceof UserLogoutLogEntry) { |
74
|
|
|
return sprintf( |
75
|
|
|
"<i>%s</i>: %s", |
76
|
|
|
$this->translator->trans('log.user_login.ip'), |
77
|
|
|
htmlspecialchars($context->getIPAddress()) |
78
|
|
|
); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
if ($context instanceof ExceptionLogEntry) { |
82
|
|
|
return sprintf( |
83
|
|
|
'<i>%s</i> %s:%d : %s', |
84
|
|
|
htmlspecialchars($context->getExceptionClass()), |
85
|
|
|
htmlspecialchars($context->getFile()), |
86
|
|
|
$context->getLine(), |
87
|
|
|
htmlspecialchars($context->getMessage()) |
88
|
|
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
if ($context instanceof DatabaseUpdatedLogEntry) { |
92
|
|
|
return sprintf( |
93
|
|
|
'<i>%s</i> %s <i class="fas fa-long-arrow-alt-right"></i> %s', |
94
|
|
|
$this->translator->trans($context->isSuccessful() ? 'log.database_updated.success' : 'log.database_updated.failure'), |
95
|
|
|
$context->getOldVersion(), |
96
|
|
|
$context->getNewVersion() |
97
|
|
|
); |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
if ($context instanceof ElementCreatedLogEntry && $context->hasCreationInstockValue()) { |
101
|
|
|
return sprintf( |
102
|
|
|
'<i>%s</i>: %s', |
103
|
|
|
$this->translator->trans('log.element_created.original_instock'), |
104
|
|
|
$context->getCreationInstockValue() |
105
|
|
|
); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
if ($context instanceof ElementDeletedLogEntry) { |
109
|
|
|
return sprintf( |
110
|
|
|
'<i>%s</i>: %s', |
111
|
|
|
$this->translator->trans('log.element_deleted.old_name'), |
112
|
|
|
$context->getOldName() |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
if ($context instanceof ElementEditedLogEntry && !empty($context->getMessage())) { |
117
|
|
|
return htmlspecialchars($context->getMessage()); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
if ($context instanceof InstockChangedLogEntry) { |
121
|
|
|
return sprintf( |
122
|
|
|
'<i>%s</i>; %s <i class="fas fa-long-arrow-alt-right"></i> %s (%s); %s: %s', |
123
|
|
|
$this->translator->trans($context->isWithdrawal() ? 'log.instock_changed.withdrawal' : 'log.instock_changed.added'), |
124
|
|
|
$context->getOldInstock(), |
125
|
|
|
$context->getNewInstock(), |
126
|
|
|
(!$context->isWithdrawal() ? '+' : '-') . $context->getDifference(true), |
127
|
|
|
$this->translator->trans('log.instock_changed.comment'), |
128
|
|
|
htmlspecialchars($context->getComment()) |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
if ($context instanceof UserNotAllowedLogEntry) { |
133
|
|
|
return htmlspecialchars($context->getMessage()); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return ""; |
137
|
|
|
} |
138
|
|
|
} |