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 modify |
8
|
|
|
* it under the terms of the GNU Affero General Public License as published |
9
|
|
|
* by the Free Software Foundation, either version 3 of the License, or |
10
|
|
|
* (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 Affero General Public License for more details. |
16
|
|
|
* |
17
|
|
|
* You should have received a copy of the GNU Affero General Public License |
18
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>. |
19
|
|
|
*/ |
20
|
|
|
|
21
|
|
|
declare(strict_types=1); |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
25
|
|
|
* |
26
|
|
|
* Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics) |
27
|
|
|
* |
28
|
|
|
* This program is free software; you can redistribute it and/or |
29
|
|
|
* modify it under the terms of the GNU General Public License |
30
|
|
|
* as published by the Free Software Foundation; either version 2 |
31
|
|
|
* of the License, or (at your option) any later version. |
32
|
|
|
* |
33
|
|
|
* This program is distributed in the hope that it will be useful, |
34
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
35
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
36
|
|
|
* GNU General Public License for more details. |
37
|
|
|
* |
38
|
|
|
* You should have received a copy of the GNU General Public License |
39
|
|
|
* along with this program; if not, write to the Free Software |
40
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
41
|
|
|
*/ |
42
|
|
|
|
43
|
|
|
namespace App\Services\LogSystem; |
44
|
|
|
|
45
|
|
|
use App\Entity\Contracts\LogWithCommentInterface; |
46
|
|
|
use App\Entity\Contracts\LogWithEventUndoInterface; |
47
|
|
|
use App\Entity\LogSystem\AbstractLogEntry; |
48
|
|
|
use App\Entity\LogSystem\CollectionElementDeleted; |
49
|
|
|
use App\Entity\LogSystem\DatabaseUpdatedLogEntry; |
50
|
|
|
use App\Entity\LogSystem\ElementCreatedLogEntry; |
51
|
|
|
use App\Entity\LogSystem\ElementDeletedLogEntry; |
52
|
|
|
use App\Entity\LogSystem\ElementEditedLogEntry; |
53
|
|
|
use App\Entity\LogSystem\ExceptionLogEntry; |
54
|
|
|
use App\Entity\LogSystem\InstockChangedLogEntry; |
55
|
|
|
use App\Entity\LogSystem\UserLoginLogEntry; |
56
|
|
|
use App\Entity\LogSystem\UserLogoutLogEntry; |
57
|
|
|
use App\Entity\LogSystem\UserNotAllowedLogEntry; |
58
|
|
|
use App\Services\ElementTypeNameGenerator; |
59
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
60
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Format the Extra field of a log entry in a user readible form. |
64
|
|
|
*/ |
65
|
|
|
class LogEntryExtraFormatter |
66
|
|
|
{ |
67
|
|
|
protected $translator; |
68
|
|
|
protected $elementTypeNameGenerator; |
69
|
|
|
|
70
|
|
|
protected const CONSOLE_SEARCH = ['<i class="fas fa-long-arrow-alt-right"></i>', '<i>', '</i>', '<b>', '</b>', ]; |
71
|
|
|
protected const CONSOLE_REPLACE = ['→', '<info>', '</info>', '<error>', '</error>']; |
72
|
|
|
|
73
|
|
|
public function __construct(TranslatorInterface $translator, ElementTypeNameGenerator $elementTypeNameGenerator) |
74
|
|
|
{ |
75
|
|
|
$this->translator = $translator; |
76
|
|
|
$this->elementTypeNameGenerator = $elementTypeNameGenerator; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Return an user viewable representation of the extra data in a log entry, styled for console output. |
81
|
|
|
* |
82
|
|
|
* @return string |
83
|
|
|
*/ |
84
|
|
|
public function formatConsole(AbstractLogEntry $logEntry): string |
85
|
|
|
{ |
86
|
|
|
$arr = $this->getInternalFormat($logEntry); |
87
|
|
|
$tmp = []; |
88
|
|
|
|
89
|
|
|
//Make an array with entries in the form "<b>Key:</b> Value" |
90
|
|
|
foreach ($arr as $key => $value) { |
91
|
|
|
$str = ''; |
92
|
|
|
if (is_string($key)) { |
93
|
|
|
$str .= '<error>' . $this->translator->trans($key) . '</error>: '; |
94
|
|
|
} |
95
|
|
|
$str .= $value; |
96
|
|
|
if (!empty($str)) { |
97
|
|
|
$tmp[] = $str; |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
return str_replace(static::CONSOLE_SEARCH, static::CONSOLE_REPLACE, implode("; ", $tmp)); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
protected function getInternalFormat(AbstractLogEntry $context): array |
105
|
|
|
{ |
106
|
|
|
$array = []; |
107
|
|
|
if ($context instanceof UserLoginLogEntry || $context instanceof UserLogoutLogEntry) { |
108
|
|
|
$array['log.user_login.ip'] = htmlspecialchars($context->getIPAddress()); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
if ($context instanceof ExceptionLogEntry) { |
112
|
|
|
$array[] = sprintf( |
113
|
|
|
'<i>%s</i> %s:%d : %s', |
114
|
|
|
htmlspecialchars($context->getExceptionClass()), |
115
|
|
|
htmlspecialchars($context->getFile()), |
116
|
|
|
$context->getLine(), |
117
|
|
|
htmlspecialchars($context->getMessage()) |
118
|
|
|
); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
if ($context instanceof DatabaseUpdatedLogEntry) { |
122
|
|
|
$array[] = sprintf( |
123
|
|
|
'<i>%s</i> %s <i class="fas fa-long-arrow-alt-right"></i> %s', |
124
|
|
|
$this->translator->trans($context->isSuccessful() ? 'log.database_updated.success' : 'log.database_updated.failure'), |
125
|
|
|
$context->getOldVersion(), |
126
|
|
|
$context->getNewVersion() |
127
|
|
|
); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
if ($context instanceof LogWithEventUndoInterface) { |
131
|
|
|
if ($context->isUndoEvent()) { |
132
|
|
|
if ($context->getUndoMode() === 'undo') { |
133
|
|
|
$array['log.undo_mode.undo'] = (string) $context->getUndoEventID(); |
134
|
|
|
} elseif ($context->getUndoMode() === 'revert') { |
135
|
|
|
$array['log.undo_mode.revert'] = (string) $context->getUndoEventID(); |
136
|
|
|
} |
137
|
|
|
} |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
if ($context instanceof LogWithCommentInterface && $context->hasComment()) { |
141
|
|
|
$array[] = htmlspecialchars($context->getComment()); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
if ($context instanceof ElementCreatedLogEntry && $context->hasCreationInstockValue()) { |
145
|
|
|
$array['log.element_created.original_instock'] = (string) $context->getCreationInstockValue(); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
if ($context instanceof ElementDeletedLogEntry) { |
149
|
|
|
if ($context->getOldName() !== null) { |
150
|
|
|
$array['log.element_deleted.old_name'] = htmlspecialchars($context->getOldName()); |
151
|
|
|
} else { |
152
|
|
|
$array['log.element_deleted.old_name'] = $this->translator->trans('log.element_deleted.old_name.unknown'); |
153
|
|
|
} |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
if ($context instanceof ElementEditedLogEntry && $context->hasChangedFieldsInfo()) { |
157
|
|
|
$array['log.element_edited.changed_fields'] = htmlspecialchars(implode(', ', $context->getChangedFields())); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
if ($context instanceof InstockChangedLogEntry) { |
161
|
|
|
$array[] = $this->translator->trans($context->isWithdrawal() ? 'log.instock_changed.withdrawal' : 'log.instock_changed.added'); |
162
|
|
|
$array[] = sprintf( |
163
|
|
|
'%s <i class="fas fa-long-arrow-alt-right"></i> %s (%s)', |
164
|
|
|
$context->getOldInstock(), |
165
|
|
|
$context->getNewInstock(), |
166
|
|
|
(! $context->isWithdrawal() ? '+' : '-').$context->getDifference(true) |
167
|
|
|
); |
168
|
|
|
$array['log.instock_changed.comment'] = htmlspecialchars($context->getComment()); |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
if ($context instanceof CollectionElementDeleted) { |
172
|
|
|
$array['log.collection_deleted.deleted'] = sprintf( |
173
|
|
|
'%s: %s (%s)', |
174
|
|
|
$this->elementTypeNameGenerator->getLocalizedTypeLabel($context->getDeletedElementClass()), |
175
|
|
|
$context->getOldName() ?? $context->getDeletedElementID(), |
176
|
|
|
$context->getCollectionName() |
177
|
|
|
); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
if ($context instanceof UserNotAllowedLogEntry) { |
181
|
|
|
$array[] = htmlspecialchars($context->getMessage()); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
return $array; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Return a HTML formatted string containing a user viewable form of the Extra data. |
189
|
|
|
* |
190
|
|
|
* @return string |
191
|
|
|
*/ |
192
|
|
|
public function format(AbstractLogEntry $context): string |
193
|
|
|
{ |
194
|
|
|
$arr = $this->getInternalFormat($context); |
195
|
|
|
$tmp = []; |
196
|
|
|
|
197
|
|
|
//Make an array with entries in the form "<b>Key:</b> Value" |
198
|
|
|
foreach ($arr as $key => $value) { |
199
|
|
|
$str = ''; |
200
|
|
|
if (is_string($key)) { |
201
|
|
|
$str .= '<b>' . $this->translator->trans($key) . '</b>: '; |
202
|
|
|
} |
203
|
|
|
$str .= $value; |
204
|
|
|
if (!empty($str)) { |
205
|
|
|
$tmp[] = $str; |
206
|
|
|
} |
207
|
|
|
} |
208
|
|
|
|
209
|
|
|
return implode("; ", $tmp); |
210
|
|
|
} |
211
|
|
|
} |
212
|
|
|
|