translator = $translator;
}
/**
* Return an user viewable representation of the extra data in a log entry, styled for console output.
* @param AbstractLogEntry $logEntry
* @return string
*/
public function formatConsole(AbstractLogEntry $logEntry): string
{
$tmp = $this->format($logEntry);
//Just a simple tweak to make the console output more pretty.
$search = ['', '', '', '', ' '];
$replace = ['', '', '', '', '→'];
return str_replace($search, $replace, $tmp);
}
/**
* Return a HTML formatted string containing a user viewable form of the Extra data
* @param AbstractLogEntry $context
* @return string
*/
public function format(AbstractLogEntry $context): string
{
if ($context instanceof UserLoginLogEntry || $context instanceof UserLogoutLogEntry) {
return sprintf(
"%s: %s",
$this->translator->trans('log.user_login.ip'),
htmlspecialchars($context->getIPAddress())
);
}
if ($context instanceof ExceptionLogEntry) {
return sprintf(
'%s %s:%d : %s',
htmlspecialchars($context->getExceptionClass()),
htmlspecialchars($context->getFile()),
$context->getLine(),
htmlspecialchars($context->getMessage())
);
}
if ($context instanceof DatabaseUpdatedLogEntry) {
return sprintf(
'%s %s %s',
$this->translator->trans($context->isSuccessful() ? 'log.database_updated.success' : 'log.database_updated.failure'),
$context->getOldVersion(),
$context->getNewVersion()
);
}
if ($context instanceof ElementCreatedLogEntry && $context->hasCreationInstockValue()) {
return sprintf(
'%s: %s',
$this->translator->trans('log.element_created.original_instock'),
$context->getCreationInstockValue()
);
}
if ($context instanceof ElementDeletedLogEntry) {
return sprintf(
'%s: %s',
$this->translator->trans('log.element_deleted.old_name'),
$context->getOldName()
);
}
if ($context instanceof ElementEditedLogEntry && !empty($context->getMessage())) {
return htmlspecialchars($context->getMessage());
}
if ($context instanceof InstockChangedLogEntry) {
return sprintf(
'%s; %s %s (%s); %s: %s',
$this->translator->trans($context->isWithdrawal() ? 'log.instock_changed.withdrawal' : 'log.instock_changed.added'),
$context->getOldInstock(),
$context->getNewInstock(),
(!$context->isWithdrawal() ? '+' : '-') . $context->getDifference(true),
$this->translator->trans('log.instock_changed.comment'),
htmlspecialchars($context->getComment())
);
}
if ($context instanceof UserNotAllowedLogEntry) {
return htmlspecialchars($context->getMessage());
}
return "";
}
}