1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony). |
4
|
|
|
* |
5
|
|
|
* Copyright (C) 2019 - 2022 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
|
|
|
namespace App\Services\LogSystem; |
24
|
|
|
|
25
|
|
|
use App\Entity\Contracts\LogWithCommentInterface; |
26
|
|
|
use App\Entity\Contracts\LogWithEventUndoInterface; |
27
|
|
|
use App\Entity\LogSystem\AbstractLogEntry; |
28
|
|
|
use App\Entity\LogSystem\CollectionElementDeleted; |
29
|
|
|
use App\Entity\LogSystem\DatabaseUpdatedLogEntry; |
30
|
|
|
use App\Entity\LogSystem\ElementCreatedLogEntry; |
31
|
|
|
use App\Entity\LogSystem\ElementDeletedLogEntry; |
32
|
|
|
use App\Entity\LogSystem\ElementEditedLogEntry; |
33
|
|
|
use App\Entity\LogSystem\ExceptionLogEntry; |
34
|
|
|
use App\Entity\LogSystem\LegacyInstockChangedLogEntry; |
35
|
|
|
use App\Entity\LogSystem\PartStockChangedLogEntry; |
36
|
|
|
use App\Entity\LogSystem\SecurityEventLogEntry; |
37
|
|
|
use App\Entity\LogSystem\UserLoginLogEntry; |
38
|
|
|
use App\Entity\LogSystem\UserLogoutLogEntry; |
39
|
|
|
use App\Entity\LogSystem\UserNotAllowedLogEntry; |
40
|
|
|
use App\Entity\Parts\PartLot; |
41
|
|
|
use App\Services\ElementTypeNameGenerator; |
42
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Format the Extra field of a log entry in a user readible form. |
46
|
|
|
*/ |
47
|
|
|
class LogEntryExtraFormatter |
48
|
|
|
{ |
49
|
|
|
protected const CONSOLE_SEARCH = ['<i class="fas fa-long-arrow-alt-right"></i>', '<i>', '</i>', '<b>', '</b>']; |
50
|
|
|
protected const CONSOLE_REPLACE = ['→', '<info>', '</info>', '<error>', '</error>']; |
51
|
|
|
protected TranslatorInterface $translator; |
52
|
|
|
protected ElementTypeNameGenerator $elementTypeNameGenerator; |
53
|
|
|
|
54
|
|
|
public function __construct(TranslatorInterface $translator, ElementTypeNameGenerator $elementTypeNameGenerator) |
55
|
|
|
{ |
56
|
|
|
$this->translator = $translator; |
57
|
|
|
$this->elementTypeNameGenerator = $elementTypeNameGenerator; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Return an user viewable representation of the extra data in a log entry, styled for console output. |
62
|
|
|
*/ |
63
|
|
|
public function formatConsole(AbstractLogEntry $logEntry): string |
64
|
|
|
{ |
65
|
|
|
$arr = $this->getInternalFormat($logEntry); |
66
|
|
|
$tmp = []; |
67
|
|
|
|
68
|
|
|
//Make an array with entries in the form "<b>Key:</b> Value" |
69
|
|
|
foreach ($arr as $key => $value) { |
70
|
|
|
$str = ''; |
71
|
|
|
if (is_string($key)) { |
72
|
|
|
$str .= '<error>'.$this->translator->trans($key).'</error>: '; |
73
|
|
|
} |
74
|
|
|
$str .= $value; |
75
|
|
|
if (!empty($str)) { |
76
|
|
|
$tmp[] = $str; |
77
|
|
|
} |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
return str_replace(static::CONSOLE_SEARCH, static::CONSOLE_REPLACE, implode('; ', $tmp)); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Return a HTML formatted string containing a user viewable form of the Extra data. |
85
|
|
|
*/ |
86
|
|
|
public function format(AbstractLogEntry $context): string |
87
|
|
|
{ |
88
|
|
|
$arr = $this->getInternalFormat($context); |
89
|
|
|
$tmp = []; |
90
|
|
|
|
91
|
|
|
//Make an array with entries in the form "<b>Key:</b> Value" |
92
|
|
|
foreach ($arr as $key => $value) { |
93
|
|
|
$str = ''; |
94
|
|
|
if (is_string($key)) { |
95
|
|
|
$str .= '<b>'.$this->translator->trans($key).'</b>: '; |
96
|
|
|
} |
97
|
|
|
$str .= $value; |
98
|
|
|
if (!empty($str)) { |
99
|
|
|
$tmp[] = $str; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return implode('; ', $tmp); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
protected function getInternalFormat(AbstractLogEntry $context): array |
107
|
|
|
{ |
108
|
|
|
$array = []; |
109
|
|
|
if ($context instanceof UserLoginLogEntry || $context instanceof UserLogoutLogEntry || $context instanceof SecurityEventLogEntry) { |
110
|
|
|
$array['log.user_login.ip'] = htmlspecialchars($context->getIPAddress()); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
if ($context instanceof ExceptionLogEntry) { |
114
|
|
|
$array[] = sprintf( |
115
|
|
|
'<i>%s</i> %s:%d : %s', |
116
|
|
|
htmlspecialchars($context->getExceptionClass()), |
117
|
|
|
htmlspecialchars($context->getFile()), |
118
|
|
|
$context->getLine(), |
119
|
|
|
htmlspecialchars($context->getMessage()) |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if ($context instanceof DatabaseUpdatedLogEntry) { |
124
|
|
|
$array[] = sprintf( |
125
|
|
|
'<i>%s</i> %s <i class="fas fa-long-arrow-alt-right"></i> %s', |
126
|
|
|
$this->translator->trans($context->isSuccessful() ? 'log.database_updated.success' : 'log.database_updated.failure'), |
127
|
|
|
$context->getOldVersion(), |
128
|
|
|
$context->getNewVersion() |
129
|
|
|
); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
if (($context instanceof LogWithEventUndoInterface) && $context->isUndoEvent()) { |
133
|
|
|
if ('undo' === $context->getUndoMode()) { |
134
|
|
|
$array['log.undo_mode.undo'] = (string) $context->getUndoEventID(); |
135
|
|
|
} elseif ('revert' === $context->getUndoMode()) { |
136
|
|
|
$array['log.undo_mode.revert'] = (string) $context->getUndoEventID(); |
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 (null !== $context->getOldName()) { |
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'] = $this->getChangedFieldsTranslated($context); |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
if ($context instanceof LegacyInstockChangedLogEntry) { |
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() ?? (string) $context->getDeletedElementID(), |
176
|
|
|
$context->getCollectionName() |
177
|
|
|
); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
if ($context instanceof UserNotAllowedLogEntry) { |
181
|
|
|
$array[] = htmlspecialchars($context->getMessage()); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
if ($context instanceof PartStockChangedLogEntry) { |
185
|
|
|
$array['log.part_stock_changed.change'] = sprintf("%s %s %s (%s)", |
186
|
|
|
$context->getOldStock(), |
187
|
|
|
'<i class="fa-solid fa-right-long"></i>', |
188
|
|
|
$context->getNewStock(), |
189
|
|
|
($context->getNewStock() > $context->getOldStock() ? '+' : '-'). $context->getChangeAmount(), |
190
|
|
|
); |
191
|
|
|
if (!empty($context->getComment())) { |
192
|
|
|
$array['log.part_stock_changed.comment'] = htmlspecialchars($context->getComment()); |
193
|
|
|
} |
194
|
|
|
if ($context->getInstockChangeType() === PartStockChangedLogEntry::TYPE_MOVE) { |
195
|
|
|
$array['log.part_stock_changed.move_target'] = |
196
|
|
|
$this->elementTypeNameGenerator->getLocalizedTypeLabel(PartLot::class) |
197
|
|
|
.' ' . $context->getMoveToTargetID(); |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return $array; |
202
|
|
|
} |
203
|
|
|
|
204
|
|
|
private function getChangedFieldsTranslated(ElementEditedLogEntry $entry): string |
205
|
|
|
{ |
206
|
|
|
$output = []; |
207
|
|
|
|
208
|
|
|
foreach($entry->getChangedFields() as $field) { |
209
|
|
|
$key = 'log.element_edited.changed_fields.'.$field; |
210
|
|
|
//If the key is not found, use the field name as a fallback |
211
|
|
|
$tmp = $this->translator->trans($key); |
212
|
|
|
if ($key === $tmp) { |
213
|
|
|
$tmp = $field; |
214
|
|
|
} |
215
|
|
|
$output[] = htmlspecialchars($tmp); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
return implode(', ', $output); |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|