Improved log extra column.

This commit is contained in:
Jan Böhmer 2020-03-07 22:12:32 +01:00
parent 84d268aba3
commit a05b1684e6

View file

@ -42,6 +42,8 @@ declare(strict_types=1);
namespace App\Services\LogSystem; namespace App\Services\LogSystem;
use App\Entity\Contracts\LogWithCommentInterface;
use App\Entity\Contracts\LogWithEventUndoInterface;
use App\Entity\LogSystem\AbstractLogEntry; use App\Entity\LogSystem\AbstractLogEntry;
use App\Entity\LogSystem\CollectionElementDeleted; use App\Entity\LogSystem\CollectionElementDeleted;
use App\Entity\LogSystem\DatabaseUpdatedLogEntry; use App\Entity\LogSystem\DatabaseUpdatedLogEntry;
@ -65,6 +67,9 @@ class LogEntryExtraFormatter
protected $translator; protected $translator;
protected $elementTypeNameGenerator; protected $elementTypeNameGenerator;
protected const CONSOLE_SEARCH = ['<i>', '</i>', '<b>', '</b>', ' <i class="fas fa-long-arrow-alt-right">'];
protected const CONSOLE_REPLACE = ['<info>', '</info>', '<error>', '</error>', '→'];
public function __construct(TranslatorInterface $translator, ElementTypeNameGenerator $elementTypeNameGenerator) public function __construct(TranslatorInterface $translator, ElementTypeNameGenerator $elementTypeNameGenerator)
{ {
$this->translator = $translator; $this->translator = $translator;
@ -78,32 +83,33 @@ class LogEntryExtraFormatter
*/ */
public function formatConsole(AbstractLogEntry $logEntry): string public function formatConsole(AbstractLogEntry $logEntry): string
{ {
$tmp = $this->format($logEntry); $arr = $this->getInternalFormat($logEntry);
$tmp = [];
//Just a simple tweak to make the console output more pretty. //Make an array with entries in the form "<b>Key:</b> Value"
$search = ['<i>', '</i>', '<b>', '</b>', ' <i class="fas fa-long-arrow-alt-right">']; foreach ($arr as $key => $value) {
$replace = ['<info>', '</info>', '<error>', '</error>', '→']; $str = '';
if (is_string($key)) {
$str .= '<error>' . $this->translator->trans($key) . '</error>: ';
}
$str .= $value;
if (!empty($str)) {
$tmp[] = $str;
}
}
return str_replace($search, $replace, $tmp); return str_replace(static::CONSOLE_SEARCH, static::CONSOLE_REPLACE, implode("; ", $tmp));
} }
/** protected function getInternalFormat(AbstractLogEntry $context): array
* Return a HTML formatted string containing a user viewable form of the Extra data.
*
* @return string
*/
public function format(AbstractLogEntry $context): string
{ {
$array = [];
if ($context instanceof UserLoginLogEntry || $context instanceof UserLogoutLogEntry) { if ($context instanceof UserLoginLogEntry || $context instanceof UserLogoutLogEntry) {
return sprintf( $array['log.user_login.ip'] = htmlspecialchars($context->getIPAddress());
'<i>%s</i>: %s',
$this->translator->trans('log.user_login.ip'),
htmlspecialchars($context->getIPAddress())
);
} }
if ($context instanceof ExceptionLogEntry) { if ($context instanceof ExceptionLogEntry) {
return sprintf( $array[] = sprintf(
'<i>%s</i> %s:%d : %s', '<i>%s</i> %s:%d : %s',
htmlspecialchars($context->getExceptionClass()), htmlspecialchars($context->getExceptionClass()),
htmlspecialchars($context->getFile()), htmlspecialchars($context->getFile()),
@ -113,7 +119,7 @@ class LogEntryExtraFormatter
} }
if ($context instanceof DatabaseUpdatedLogEntry) { if ($context instanceof DatabaseUpdatedLogEntry) {
return sprintf( $array[] = sprintf(
'<i>%s</i> %s <i class="fas fa-long-arrow-alt-right"></i> %s', '<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'), $this->translator->trans($context->isSuccessful() ? 'log.database_updated.success' : 'log.database_updated.failure'),
$context->getOldVersion(), $context->getOldVersion(),
@ -121,97 +127,50 @@ class LogEntryExtraFormatter
); );
} }
if ($context instanceof ElementCreatedLogEntry ) { if ($context instanceof LogWithEventUndoInterface) {
$comment = '';
if ($context->isUndoEvent()) { if ($context->isUndoEvent()) {
if ($context->getUndoMode() === 'undo') { if ($context->getUndoMode() === 'undo') {
$comment .= $this->translator->trans('log.undo_mode.undo').': '.$context->getUndoEventID().';'; $array['log.undo_mode.undo'] = (string) $context->getUndoEventID();
} elseif($context->getUndoMode() === 'revert') { } elseif ($context->getUndoMode() === 'revert') {
$comment .= $this->translator->trans('log.undo_mode.revert').': '.$context->getUndoEventID().';'; $array['log.undo_mode.revert'] = (string) $context->getUndoEventID();
} }
} }
if ($context->hasComment()) { }
$comment .= htmlspecialchars($context->getComment()) . '; ';
} if ($context instanceof LogWithCommentInterface && $context->hasComment()) {
if($context->hasCreationInstockValue()) { $array[] = htmlspecialchars($context->getComment());
return $comment . sprintf( }
'<i>%s</i>: %s',
$this->translator->trans('log.element_created.original_instock'), if ($context instanceof ElementCreatedLogEntry && $context->hasCreationInstockValue()) {
$context->getCreationInstockValue() $array['log.element_created.original_instock'] = (string) $context->getCreationInstockValue();
);
}
return $comment;
} }
if ($context instanceof ElementDeletedLogEntry) { if ($context instanceof ElementDeletedLogEntry) {
$comment = ''; if ($context->getOldName() !== null) {
if ($context->isUndoEvent()) { $array['log.element_deleted.old_name'] = htmlspecialchars($context->getOldName());
if ($context->getUndoMode() === 'undo') { } else {
$comment .= $this->translator->trans('log.undo_mode.undo').': '.$context->getUndoEventID().';'; $array['log.element_deleted.old_name'] = $this->translator->trans('log.element_deleted.old_name.unknown');
} elseif($context->getUndoMode() === 'revert') {
$comment .= $this->translator->trans('log.undo_mode.revert').': '.$context->getUndoEventID().';';
}
} }
if ($context->hasComment()) {
$comment .= htmlspecialchars($context->getComment()) . '; ';
}
return $comment . sprintf(
'<i>%s</i>: %s',
$this->translator->trans('log.element_deleted.old_name'),
$context->getOldName() ?? $this->translator->trans('log.element_deleted.old_name.unknown')
);
} }
if ($context instanceof ElementEditedLogEntry) { if ($context instanceof ElementEditedLogEntry && $context->hasChangedFieldsInfo()) {
$str = ''; $array['log.element_edited.changed_fields'] = htmlspecialchars(implode(', ', $context->getChangedFields()));
if ($context->isUndoEvent()) {
if ($context->getUndoMode() === 'undo') {
$str .= $this->translator->trans('log.undo_mode.undo').': '.$context->getUndoEventID().';';
} elseif($context->getUndoMode() === 'revert') {
$str .= $this->translator->trans('log.undo_mode.revert').': '.$context->getUndoEventID().';';
}
}
if ($context->hasComment()) {
$str .= htmlspecialchars($context->getComment());
}
if ($context->hasChangedFieldsInfo()) {
if (!empty($str)) {
$str .= '; ';
}
$str .= sprintf(
"<i>%s:</i> %s",
$this->translator->trans('log.element_edited.changed_fields'),
htmlspecialchars(implode(', ', $context->getChangedFields()))
);
}
return $str;
} }
if ($context instanceof InstockChangedLogEntry) { if ($context instanceof InstockChangedLogEntry) {
return sprintf( $array[] = $this->translator->trans($context->isWithdrawal() ? 'log.instock_changed.withdrawal' : 'log.instock_changed.added');
'<i>%s</i>; %s <i class="fas fa-long-arrow-alt-right"></i> %s (%s); %s: %s', $array[] = sprintf(
$this->translator->trans($context->isWithdrawal() ? 'log.instock_changed.withdrawal' : 'log.instock_changed.added'), '%s <i class="fas fa-long-arrow-alt-right"></i> %s (%s)',
$context->getOldInstock(), $context->getOldInstock(),
$context->getNewInstock(), $context->getNewInstock(),
(! $context->isWithdrawal() ? '+' : '-').$context->getDifference(true), (! $context->isWithdrawal() ? '+' : '-').$context->getDifference(true)
$this->translator->trans('log.instock_changed.comment'),
htmlspecialchars($context->getComment())
); );
$array['log.instock_changed.comment'] = htmlspecialchars($context->getComment());
} }
if ($context instanceof CollectionElementDeleted) { if ($context instanceof CollectionElementDeleted) {
$comment = ''; $array['log.collection_deleted.deleted'] = sprintf(
if ($context->isUndoEvent()) { '%s: %s (%s)',
if ($context->getUndoMode() === 'undo') {
$comment .= $this->translator->trans('log.undo_mode.undo').': '.$context->getUndoEventID().';';
} elseif($context->getUndoMode() === 'revert') {
$comment .= $this->translator->trans('log.undo_mode.revert').': '.$context->getUndoEventID().';';
}
}
return $comment . sprintf('<i>%s</i>: %s: %s (%s)',
$this->translator->trans('log.collection_deleted.deleted'),
$this->elementTypeNameGenerator->getLocalizedTypeLabel($context->getDeletedElementClass()), $this->elementTypeNameGenerator->getLocalizedTypeLabel($context->getDeletedElementClass()),
$context->getOldName() ?? $context->getDeletedElementID(), $context->getOldName() ?? $context->getDeletedElementID(),
$context->getCollectionName() $context->getCollectionName()
@ -219,9 +178,34 @@ class LogEntryExtraFormatter
} }
if ($context instanceof UserNotAllowedLogEntry) { if ($context instanceof UserNotAllowedLogEntry) {
return htmlspecialchars($context->getMessage()); $array[] = htmlspecialchars($context->getMessage());
} }
return ''; return $array;
}
/**
* Return a HTML formatted string containing a user viewable form of the Extra data.
*
* @return string
*/
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;
if (!empty($str)) {
$tmp[] = $str;
}
}
return implode("; ", $tmp);
} }
} }