Allow to undo a change from event log.

This commit is contained in:
Jan Böhmer 2020-03-01 19:46:48 +01:00
parent 15d25cf2b2
commit 5a5d7b24be
24 changed files with 659 additions and 30 deletions

View file

@ -0,0 +1,90 @@
<?php
/**
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2020 Jan Böhmer (https://github.com/jbtronics)
*
* 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/>.
*/
namespace App\Services\LogSystem;
use App\Entity\LogSystem\AbstractLogEntry;
class EventUndoHelper
{
public const MODE_UNDO = 'undo';
public const MODE_REVERT = 'revert';
protected const ALLOWED_MODES = [self::MODE_REVERT, self::MODE_UNDO];
protected $undone_event;
protected $mode;
public function __construct()
{
$undone_event = null;
$this->mode = self::MODE_UNDO;
}
public function setMode(string $mode): void
{
if (!in_array($mode, self::ALLOWED_MODES)) {
throw new \InvalidArgumentException('Invalid mode passed!');
}
$this->mode = $mode;
}
public function getMode(): string
{
return $this->mode;
}
/**
* Set which event log is currently undone.
* After the flush this message is cleared.
* @param AbstractLogEntry|null $message
*/
public function setUndoneEvent(?AbstractLogEntry $undone_event): void
{
$this->undone_event = $undone_event;
}
/**
* Returns event that is currently undone.
* @return string|null
*/
public function getUndoneEvent(): ?AbstractLogEntry
{
return $this->undone_event;
}
/**
* Clear the currently the set undone event.
*/
public function clearUndoneEvent(): void
{
$this->undone_event = null;
}
/**
* Check if a event is undone
* @return bool
*/
public function isUndo(): bool
{
return ($this->undone_event instanceof AbstractLogEntry);
}
}

View file

@ -123,8 +123,15 @@ class LogEntryExtraFormatter
if ($context instanceof ElementCreatedLogEntry ) {
$comment = '';
if ($context->isUndoEvent()) {
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().';';
}
}
if ($context->hasComment()) {
$comment = htmlspecialchars($context->getComment()) . '; ';
$comment .= htmlspecialchars($context->getComment()) . '; ';
}
if($context->hasCreationInstockValue()) {
return $comment . sprintf(
@ -138,8 +145,15 @@ class LogEntryExtraFormatter
if ($context instanceof ElementDeletedLogEntry) {
$comment = '';
if ($context->isUndoEvent()) {
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().';';
}
}
if ($context->hasComment()) {
$comment = htmlspecialchars($context->getComment()) . '; ';
$comment .= htmlspecialchars($context->getComment()) . '; ';
}
return $comment . sprintf(
'<i>%s</i>: %s',
@ -150,6 +164,13 @@ class LogEntryExtraFormatter
if ($context instanceof ElementEditedLogEntry) {
$str = '';
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());
}
@ -180,7 +201,16 @@ class LogEntryExtraFormatter
}
if ($context instanceof CollectionElementDeleted) {
return sprintf('<i>%s</i>: %s: %s (%s)',
$comment = '';
if ($context->isUndoEvent()) {
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()),
$context->getOldName() ?? $context->getDeletedElementID(),

View file

@ -60,6 +60,9 @@ class TimeTravel
//Set internal ID so the element can be reverted
$this->setField($element, 'id', $id);
//Let database determine when it will be created
$this->setField($element,'addedDate', null);
return $element;
}