From efc152e3c8195fc63df9a07c764873bc3329fd28 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Tue, 6 Jun 2023 23:15:14 +0200 Subject: [PATCH] Do not throw an exception during rendering of log detail page, if element has no time travel URL --- src/Services/EntityURLGenerator.php | 4 ++-- src/Twig/EntityExtension.php | 12 +++++++++++- templates/log_system/details/helper.macro.html.twig | 7 +++++-- 3 files changed, 18 insertions(+), 5 deletions(-) diff --git a/src/Services/EntityURLGenerator.php b/src/Services/EntityURLGenerator.php index 591abcc2..09295034 100644 --- a/src/Services/EntityURLGenerator.php +++ b/src/Services/EntityURLGenerator.php @@ -106,7 +106,7 @@ class EntityURLGenerator /** * Gets the URL to view the given element at a given timestamp. */ - public function timeTravelURL(AbstractDBElement $entity, DateTime $dateTime): string + public function timeTravelURL(AbstractDBElement $entity, \DateTimeInterface $dateTime): string { $map = [ Part::class => 'part_info', @@ -161,7 +161,7 @@ class EntityURLGenerator } //Otherwise throw an error - throw new EntityNotSupportedException('The given entity is not supported yet!'); + throw new EntityNotSupportedException('The given entity is not supported yet! Passed class type: '.get_class($entity)); } public function viewURL(Attachment $entity): string diff --git a/src/Twig/EntityExtension.php b/src/Twig/EntityExtension.php index c892c8f1..402a28b3 100644 --- a/src/Twig/EntityExtension.php +++ b/src/Twig/EntityExtension.php @@ -34,6 +34,7 @@ use App\Entity\Parts\Supplier; use App\Entity\PriceInformations\Currency; use App\Entity\UserSystem\Group; use App\Entity\UserSystem\User; +use App\Exceptions\EntityNotSupportedException; use App\Services\ElementTypeNameGenerator; use App\Services\EntityURLGenerator; use App\Services\Trees\TreeViewGenerator; @@ -72,7 +73,7 @@ final class EntityExtension extends AbstractExtension /* Returns the URL to the given entity */ new TwigFunction('entity_url', [$this, 'generateEntityURL']), /* Returns the URL to the given entity in timetravel mode */ - new TwigFunction('timetravel_url', [$this->entityURLGenerator, 'timetravelURL']), + new TwigFunction('timetravel_url', [$this, 'timeTravelURL']), /* Generates a JSON array of the given tree */ new TwigFunction('tree_data', [$this, 'treeData']), @@ -81,6 +82,15 @@ final class EntityExtension extends AbstractExtension ]; } + public function timeTravelURL(AbstractDBElement $element, \DateTimeInterface $dateTime): ?string + { + try { + return $this->entityURLGenerator->timeTravelURL($element, $dateTime); + } catch (EntityNotSupportedException $e) { + return null; + } + } + public function treeData(AbstractDBElement $element, string $type = 'newEdit'): string { $tree = $this->treeBuilder->getTreeView(get_class($element), null, $type, $element); diff --git a/templates/log_system/details/helper.macro.html.twig b/templates/log_system/details/helper.macro.html.twig index fec32ba2..d2af5201 100644 --- a/templates/log_system/details/helper.macro.html.twig +++ b/templates/log_system/details/helper.macro.html.twig @@ -32,10 +32,13 @@ {{ 'log.undo.revert.short' | trans }} + {% set url = timetravel_url(target_element, entry.timestamp) %} + {# View button #} {% if target_element and ((attribute(entry, 'oldDataInformation') is defined and entry.oldDataInformation) - or entry is instanceof('App\\Entity\\LogSystem\\CollectionElementDeleted')) %} - + or entry is instanceof('App\\Entity\\LogSystem\\CollectionElementDeleted')) + and url is not null %} + {% trans %}log.view_version{% endtrans %} {% endif %}