2020-01-26 20:12:08 +01:00
|
|
|
<?php
|
2020-02-22 18:14:36 +01:00
|
|
|
/**
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
*
|
2022-11-29 22:28:53 +01:00
|
|
|
* Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
|
2020-02-22 18:14:36 +01:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as published
|
|
|
|
* by the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2020-02-01 16:17:20 +01:00
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
2020-01-26 20:12:08 +01:00
|
|
|
namespace App\Services\LogSystem;
|
|
|
|
|
2020-03-07 22:12:32 +01:00
|
|
|
use App\Entity\Contracts\LogWithCommentInterface;
|
|
|
|
use App\Entity\Contracts\LogWithEventUndoInterface;
|
2020-01-26 20:12:08 +01:00
|
|
|
use App\Entity\LogSystem\AbstractLogEntry;
|
2020-02-29 22:53:53 +01:00
|
|
|
use App\Entity\LogSystem\CollectionElementDeleted;
|
2020-01-26 20:12:08 +01:00
|
|
|
use App\Entity\LogSystem\DatabaseUpdatedLogEntry;
|
|
|
|
use App\Entity\LogSystem\ElementCreatedLogEntry;
|
|
|
|
use App\Entity\LogSystem\ElementDeletedLogEntry;
|
|
|
|
use App\Entity\LogSystem\ElementEditedLogEntry;
|
|
|
|
use App\Entity\LogSystem\ExceptionLogEntry;
|
2023-01-08 01:22:02 +01:00
|
|
|
use App\Entity\LogSystem\LegacyInstockChangedLogEntry;
|
2023-01-08 01:41:04 +01:00
|
|
|
use App\Entity\LogSystem\PartStockChangedLogEntry;
|
2020-04-03 18:27:47 +02:00
|
|
|
use App\Entity\LogSystem\SecurityEventLogEntry;
|
2020-01-26 20:12:08 +01:00
|
|
|
use App\Entity\LogSystem\UserLoginLogEntry;
|
|
|
|
use App\Entity\LogSystem\UserLogoutLogEntry;
|
|
|
|
use App\Entity\LogSystem\UserNotAllowedLogEntry;
|
2023-01-08 01:41:04 +01:00
|
|
|
use App\Entity\Parts\PartLot;
|
2020-02-29 22:53:53 +01:00
|
|
|
use App\Services\ElementTypeNameGenerator;
|
2020-01-26 20:12:08 +01:00
|
|
|
use Symfony\Contracts\Translation\TranslatorInterface;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Format the Extra field of a log entry in a user readible form.
|
|
|
|
*/
|
|
|
|
class LogEntryExtraFormatter
|
|
|
|
{
|
2020-03-15 13:56:31 +01:00
|
|
|
protected const CONSOLE_SEARCH = ['<i class="fas fa-long-arrow-alt-right"></i>', '<i>', '</i>', '<b>', '</b>'];
|
|
|
|
protected const CONSOLE_REPLACE = ['→', '<info>', '</info>', '<error>', '</error>'];
|
2022-09-18 22:59:31 +02:00
|
|
|
protected TranslatorInterface $translator;
|
|
|
|
protected ElementTypeNameGenerator $elementTypeNameGenerator;
|
2020-01-26 20:12:08 +01:00
|
|
|
|
2020-02-29 22:53:53 +01:00
|
|
|
public function __construct(TranslatorInterface $translator, ElementTypeNameGenerator $elementTypeNameGenerator)
|
2020-01-26 20:12:08 +01:00
|
|
|
{
|
|
|
|
$this->translator = $translator;
|
2020-02-29 22:53:53 +01:00
|
|
|
$this->elementTypeNameGenerator = $elementTypeNameGenerator;
|
2020-01-26 20:12:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return an user viewable representation of the extra data in a log entry, styled for console output.
|
|
|
|
*/
|
|
|
|
public function formatConsole(AbstractLogEntry $logEntry): string
|
|
|
|
{
|
2020-03-07 22:12:32 +01:00
|
|
|
$arr = $this->getInternalFormat($logEntry);
|
|
|
|
$tmp = [];
|
2020-01-26 20:12:08 +01:00
|
|
|
|
2020-03-07 22:12:32 +01:00
|
|
|
//Make an array with entries in the form "<b>Key:</b> Value"
|
|
|
|
foreach ($arr as $key => $value) {
|
|
|
|
$str = '';
|
|
|
|
if (is_string($key)) {
|
2020-03-15 13:56:31 +01:00
|
|
|
$str .= '<error>'.$this->translator->trans($key).'</error>: ';
|
2020-03-07 22:12:32 +01:00
|
|
|
}
|
|
|
|
$str .= $value;
|
2020-08-21 21:36:22 +02:00
|
|
|
if (!empty($str)) {
|
2020-03-07 22:12:32 +01:00
|
|
|
$tmp[] = $str;
|
|
|
|
}
|
|
|
|
}
|
2020-01-26 20:12:08 +01:00
|
|
|
|
2020-03-15 13:56:31 +01:00
|
|
|
return str_replace(static::CONSOLE_SEARCH, static::CONSOLE_REPLACE, implode('; ', $tmp));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a HTML formatted string containing a user viewable form of the Extra data.
|
|
|
|
*/
|
|
|
|
public function format(AbstractLogEntry $context): string
|
|
|
|
{
|
|
|
|
$arr = $this->getInternalFormat($context);
|
|
|
|
$tmp = [];
|
|
|
|
|
|
|
|
//Make an array with entries in the form "<b>Key:</b> Value"
|
|
|
|
foreach ($arr as $key => $value) {
|
|
|
|
$str = '';
|
|
|
|
if (is_string($key)) {
|
|
|
|
$str .= '<b>'.$this->translator->trans($key).'</b>: ';
|
|
|
|
}
|
|
|
|
$str .= $value;
|
2020-08-21 21:36:22 +02:00
|
|
|
if (!empty($str)) {
|
2020-03-15 13:56:31 +01:00
|
|
|
$tmp[] = $str;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return implode('; ', $tmp);
|
2020-01-26 20:12:08 +01:00
|
|
|
}
|
|
|
|
|
2020-03-07 22:12:32 +01:00
|
|
|
protected function getInternalFormat(AbstractLogEntry $context): array
|
2020-01-26 20:12:08 +01:00
|
|
|
{
|
2020-03-07 22:12:32 +01:00
|
|
|
$array = [];
|
2020-04-03 18:27:47 +02:00
|
|
|
if ($context instanceof UserLoginLogEntry || $context instanceof UserLogoutLogEntry || $context instanceof SecurityEventLogEntry) {
|
2020-03-07 22:12:32 +01:00
|
|
|
$array['log.user_login.ip'] = htmlspecialchars($context->getIPAddress());
|
2020-01-26 20:12:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($context instanceof ExceptionLogEntry) {
|
2020-03-07 22:12:32 +01:00
|
|
|
$array[] = sprintf(
|
2020-01-26 20:12:08 +01:00
|
|
|
'<i>%s</i> %s:%d : %s',
|
|
|
|
htmlspecialchars($context->getExceptionClass()),
|
|
|
|
htmlspecialchars($context->getFile()),
|
|
|
|
$context->getLine(),
|
|
|
|
htmlspecialchars($context->getMessage())
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($context instanceof DatabaseUpdatedLogEntry) {
|
2020-03-07 22:12:32 +01:00
|
|
|
$array[] = sprintf(
|
2020-01-26 20:12:08 +01:00
|
|
|
'<i>%s</i> %s <i class="fas fa-long-arrow-alt-right"></i> %s',
|
|
|
|
$this->translator->trans($context->isSuccessful() ? 'log.database_updated.success' : 'log.database_updated.failure'),
|
|
|
|
$context->getOldVersion(),
|
|
|
|
$context->getNewVersion()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-08-21 22:43:37 +02:00
|
|
|
if (($context instanceof LogWithEventUndoInterface) && $context->isUndoEvent()) {
|
|
|
|
if ('undo' === $context->getUndoMode()) {
|
|
|
|
$array['log.undo_mode.undo'] = (string) $context->getUndoEventID();
|
|
|
|
} elseif ('revert' === $context->getUndoMode()) {
|
|
|
|
$array['log.undo_mode.revert'] = (string) $context->getUndoEventID();
|
2020-03-01 19:46:48 +01:00
|
|
|
}
|
2020-01-26 20:12:08 +01:00
|
|
|
}
|
|
|
|
|
2020-03-07 22:12:32 +01:00
|
|
|
if ($context instanceof LogWithCommentInterface && $context->hasComment()) {
|
|
|
|
$array[] = htmlspecialchars($context->getComment());
|
2020-01-26 20:12:08 +01:00
|
|
|
}
|
|
|
|
|
2020-03-07 22:12:32 +01:00
|
|
|
if ($context instanceof ElementCreatedLogEntry && $context->hasCreationInstockValue()) {
|
|
|
|
$array['log.element_created.original_instock'] = (string) $context->getCreationInstockValue();
|
|
|
|
}
|
2020-02-23 00:44:52 +01:00
|
|
|
|
2020-03-07 22:12:32 +01:00
|
|
|
if ($context instanceof ElementDeletedLogEntry) {
|
2020-03-15 13:56:31 +01:00
|
|
|
if (null !== $context->getOldName()) {
|
2020-03-07 22:12:32 +01:00
|
|
|
$array['log.element_deleted.old_name'] = htmlspecialchars($context->getOldName());
|
|
|
|
} else {
|
|
|
|
$array['log.element_deleted.old_name'] = $this->translator->trans('log.element_deleted.old_name.unknown');
|
2020-02-23 20:16:28 +01:00
|
|
|
}
|
2020-03-07 22:12:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if ($context instanceof ElementEditedLogEntry && $context->hasChangedFieldsInfo()) {
|
|
|
|
$array['log.element_edited.changed_fields'] = htmlspecialchars(implode(', ', $context->getChangedFields()));
|
2020-01-26 20:12:08 +01:00
|
|
|
}
|
|
|
|
|
2023-01-08 01:22:02 +01:00
|
|
|
if ($context instanceof LegacyInstockChangedLogEntry) {
|
2020-03-07 22:12:32 +01:00
|
|
|
$array[] = $this->translator->trans($context->isWithdrawal() ? 'log.instock_changed.withdrawal' : 'log.instock_changed.added');
|
|
|
|
$array[] = sprintf(
|
|
|
|
'%s <i class="fas fa-long-arrow-alt-right"></i> %s (%s)',
|
2020-01-26 20:12:08 +01:00
|
|
|
$context->getOldInstock(),
|
|
|
|
$context->getNewInstock(),
|
2020-08-21 21:36:22 +02:00
|
|
|
(!$context->isWithdrawal() ? '+' : '-').$context->getDifference(true)
|
2020-01-26 20:12:08 +01:00
|
|
|
);
|
2020-03-07 22:12:32 +01:00
|
|
|
$array['log.instock_changed.comment'] = htmlspecialchars($context->getComment());
|
2020-01-26 20:12:08 +01:00
|
|
|
}
|
|
|
|
|
2020-02-29 22:53:53 +01:00
|
|
|
if ($context instanceof CollectionElementDeleted) {
|
2020-03-07 22:12:32 +01:00
|
|
|
$array['log.collection_deleted.deleted'] = sprintf(
|
|
|
|
'%s: %s (%s)',
|
2020-02-29 22:53:53 +01:00
|
|
|
$this->elementTypeNameGenerator->getLocalizedTypeLabel($context->getDeletedElementClass()),
|
2020-08-21 22:43:37 +02:00
|
|
|
$context->getOldName() ?? (string) $context->getDeletedElementID(),
|
2020-02-29 22:53:53 +01:00
|
|
|
$context->getCollectionName()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-01-26 20:12:08 +01:00
|
|
|
if ($context instanceof UserNotAllowedLogEntry) {
|
2020-03-07 22:12:32 +01:00
|
|
|
$array[] = htmlspecialchars($context->getMessage());
|
|
|
|
}
|
|
|
|
|
2023-01-08 01:41:04 +01:00
|
|
|
if ($context instanceof PartStockChangedLogEntry) {
|
|
|
|
$array['log.part_stock_changed.change'] = sprintf("%s %s %s (%s)",
|
|
|
|
$context->getOldStock(),
|
|
|
|
'<i class="fa-solid fa-right-long"></i>',
|
|
|
|
$context->getNewStock(),
|
|
|
|
($context->getNewStock() > $context->getOldStock() ? '+' : '-'). $context->getChangeAmount(),
|
|
|
|
);
|
|
|
|
if (!empty($context->getComment())) {
|
|
|
|
$array['log.part_stock_changed.comment'] = htmlspecialchars($context->getComment());
|
|
|
|
}
|
|
|
|
if ($context->getInstockChangeType() === PartStockChangedLogEntry::TYPE_MOVE) {
|
|
|
|
$array['log.part_stock_changed.move_target'] =
|
|
|
|
$this->elementTypeNameGenerator->getLocalizedTypeLabel(PartLot::class)
|
|
|
|
.' ' . $context->getMoveToTargetID();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-07 22:12:32 +01:00
|
|
|
return $array;
|
|
|
|
}
|
2020-02-01 16:17:20 +01:00
|
|
|
}
|