mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-21 01:25:55 +02:00
Added button to revert part to a given timestamp.
This commit is contained in:
parent
5a5d7b24be
commit
654c5bd59f
4 changed files with 74 additions and 15 deletions
|
@ -105,14 +105,61 @@ class LogController extends AbstractController
|
|||
* @Route("/undo", name="log_undo", methods={"POST"})
|
||||
* @param Request $request
|
||||
*/
|
||||
public function undoLog(Request $request, EventUndoHelper $eventUndoHelper)
|
||||
public function undoRevertLog(Request $request, EventUndoHelper $eventUndoHelper)
|
||||
{
|
||||
$mode = EventUndoHelper::MODE_UNDO;
|
||||
$id = $request->request->get('undo');
|
||||
$log_element = $this->entityManager->find(AbstractLogEntry::class, $id);
|
||||
|
||||
$eventUndoHelper->setMode(EventUndoHelper::MODE_UNDO);
|
||||
//If no undo value was set check if a revert was set
|
||||
if ($id === null) {
|
||||
$id = $request->get('revert');
|
||||
$mode = EventUndoHelper::MODE_REVERT;
|
||||
}
|
||||
|
||||
$log_element = $this->entityManager->find(AbstractLogEntry::class, $id);
|
||||
if ($log_element === null) {
|
||||
throw new \InvalidArgumentException('No log entry with the given ID is existing!');
|
||||
}
|
||||
|
||||
$eventUndoHelper->setMode($mode);
|
||||
$eventUndoHelper->setUndoneEvent($log_element);
|
||||
|
||||
if ($mode === EventUndoHelper::MODE_UNDO) {
|
||||
$this->undoLog($log_element);
|
||||
} elseif ($mode === EventUndoHelper::MODE_REVERT) {
|
||||
$this->revertLog($log_element);
|
||||
}
|
||||
|
||||
$eventUndoHelper->clearUndoneEvent();
|
||||
|
||||
$redirect = $request->request->get('redirect_back');
|
||||
return $this->redirect($redirect);
|
||||
}
|
||||
|
||||
protected function revertLog(AbstractLogEntry $logEntry): void
|
||||
{
|
||||
$timestamp = $logEntry->getTimestamp();
|
||||
$element = $this->entityManager->find($logEntry->getTargetClass(), $logEntry->getTargetID());
|
||||
//If the element is not available in DB try to undelete it
|
||||
if ($element === null) {
|
||||
$element = $this->timeTravel->undeleteEntity($logEntry->getTargetClass(), $logEntry->getTargetID());
|
||||
$this->entityManager->persist($element);
|
||||
$this->entityManager->flush();
|
||||
$this->dbRepository->changeID($element, $logEntry->getTargetID());
|
||||
}
|
||||
|
||||
if (!$element instanceof AbstractDBElement) {
|
||||
$this->addFlash('error', 'log.undo.target_not_found');
|
||||
return;
|
||||
}
|
||||
|
||||
$this->timeTravel->revertEntityToTimestamp($element, $timestamp);
|
||||
$this->entityManager->flush();
|
||||
$this->addFlash('success', 'log.undo.revert_success');
|
||||
}
|
||||
|
||||
protected function undoLog(AbstractLogEntry $log_element): void
|
||||
{
|
||||
if ($log_element instanceof ElementDeletedLogEntry || $log_element instanceof CollectionElementDeleted) {
|
||||
if ($log_element instanceof ElementDeletedLogEntry) {
|
||||
$element_class = $log_element->getTargetClass();
|
||||
|
@ -153,10 +200,5 @@ class LogController extends AbstractController
|
|||
} else {
|
||||
$this->addFlash('error', 'log.undo.log_type_invalid');
|
||||
}
|
||||
|
||||
$eventUndoHelper->clearUndoneEvent();
|
||||
|
||||
$redirect = $request->request->get('redirect_back');
|
||||
return $this->redirect($redirect);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -48,22 +48,39 @@ class RevertLogColumn extends AbstractColumn
|
|||
|
||||
public function render($value, $context)
|
||||
{
|
||||
if ($context instanceof ElementDeletedLogEntry || $context instanceof CollectionElementDeleted) {
|
||||
$revertable = true;
|
||||
if (
|
||||
$context instanceof CollectionElementDeleted
|
||||
|| ($context instanceof ElementDeletedLogEntry && $context->hasOldDataInformations())
|
||||
) {
|
||||
$icon = 'fa-trash-restore';
|
||||
$title = $this->translator->trans('log.undo.undelete');
|
||||
} elseif ($context instanceof ElementEditedLogEntry || $context instanceof ElementCreatedLogEntry) {
|
||||
} elseif (
|
||||
$context instanceof ElementCreatedLogEntry
|
||||
|| ($context instanceof ElementEditedLogEntry && $context->hasOldDataInformations())
|
||||
) {
|
||||
$icon = 'fa-undo';
|
||||
$title = $this->translator->trans('log.undo.undo');
|
||||
} else {
|
||||
return '';
|
||||
}
|
||||
|
||||
|
||||
return sprintf(
|
||||
'<button type="submit" class="btn btn-outline-secondary btn-sm" name="undo" value="%d"><i class="fas fa-fw %s" title="%s"></i></button>',
|
||||
$tmp = '<div class="btn-group btn-group-sm">';
|
||||
$tmp .= sprintf(
|
||||
'<button type="submit" class="btn btn-outline-secondary" name="undo" value="%d"><i class="fas fa-fw %s" title="%s"></i></button>',
|
||||
$context->getID(),
|
||||
$icon,
|
||||
$title
|
||||
);
|
||||
|
||||
$tmp .= sprintf(
|
||||
'<button type="submit" class="btn btn-outline-secondary" name="revert" value="%d"><i class="fas fa-fw fa-backward" title="%s"></i></button>',
|
||||
$context->getID(),
|
||||
$this->translator->trans('log.undo.revert')
|
||||
);
|
||||
|
||||
$tmp .= '</div>';
|
||||
|
||||
return $tmp;
|
||||
}
|
||||
}
|
|
@ -1,5 +1,5 @@
|
|||
<form method="post" action="{{ url("log_undo") }}" data-delete-form data-title="{% trans %}log.undo.confirm_title{% endtrans %}"
|
||||
data-message="{% trans %}log.undo.confirm_message{% endtrans %}">>
|
||||
data-message="{% trans %}log.undo.confirm_message{% endtrans %}">
|
||||
<input type="hidden" name="redirect_back" value="{{ app.request.uri }}">
|
||||
<div id="part_list" class="" data-datatable data-settings='{{ datatable_settings(datatable) }}'>
|
||||
<div class="card-body">
|
||||
|
|
|
@ -24,7 +24,7 @@
|
|||
<h3>{{ part.name }}
|
||||
{# You need edit permission to use the edit button #}
|
||||
{% if timeTravel is not null %}
|
||||
<a href="{{ part|entityURL('info') }}"><i title="{% trans %}part.back_to_info{% endtrans %}" class="fas fa-fw fa-undo"></i></a>
|
||||
<a href="{{ part|entityURL('info') }}"><i title="{% trans %}part.back_to_info{% endtrans %}" class="fas fa-fw fa-arrow-circle-left"></i></a>
|
||||
{% elseif is_granted('edit', part) %}
|
||||
<a href="{{ part|entityURL('edit') }}"><i class="fas fa-fw fa-sm fa-edit"></i></a>
|
||||
{% endif %}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue