Link to associated element in log column field if possible

This commit is contained in:
Jan Böhmer 2020-03-28 18:46:15 +01:00
parent d757d17f77
commit 9035d97996
2 changed files with 40 additions and 13 deletions

View file

@ -42,9 +42,13 @@ declare(strict_types=1);
namespace App\DataTables\Column; namespace App\DataTables\Column;
use App\Entity\Attachments\Attachment;
use App\Entity\Base\AbstractDBElement; use App\Entity\Base\AbstractDBElement;
use App\Entity\Base\AbstractNamedDBElement; use App\Entity\Base\AbstractNamedDBElement;
use App\Entity\LogSystem\AbstractLogEntry; use App\Entity\LogSystem\AbstractLogEntry;
use App\Entity\Parameters\AbstractParameter;
use App\Entity\PriceInformations\Orderdetail;
use App\Entity\PriceInformations\Pricedetail;
use App\Exceptions\EntityNotSupportedException; use App\Exceptions\EntityNotSupportedException;
use App\Services\ElementTypeNameGenerator; use App\Services\ElementTypeNameGenerator;
use App\Services\EntityURLGenerator; use App\Services\EntityURLGenerator;
@ -80,6 +84,7 @@ class LogEntryTargetColumn extends AbstractColumn
public function configureOptions(OptionsResolver $resolver) public function configureOptions(OptionsResolver $resolver)
{ {
parent::configureOptions($resolver); parent::configureOptions($resolver);
$resolver->setDefault('show_associated', true);
return $this; return $this;
} }
@ -89,31 +94,27 @@ class LogEntryTargetColumn extends AbstractColumn
/** @var AbstractLogEntry $context */ /** @var AbstractLogEntry $context */
$target = $this->entryRepository->getTargetElement($context); $target = $this->entryRepository->getTargetElement($context);
$tmp = '';
//The element is existing //The element is existing
if ($target instanceof AbstractNamedDBElement) { if ($target instanceof AbstractNamedDBElement) {
try { try {
return sprintf( $tmp = sprintf(
'<a href="%s">%s</a>', '<a href="%s">%s</a>',
$this->entityURLGenerator->infoURL($target), $this->entityURLGenerator->infoURL($target),
$this->elementTypeNameGenerator->getTypeNameCombination($target, true) $this->elementTypeNameGenerator->getTypeNameCombination($target, true)
); );
} catch (EntityNotSupportedException $exception) { } catch (EntityNotSupportedException $exception) {
return $this->elementTypeNameGenerator->getTypeNameCombination($target, true); $tmp = $this->elementTypeNameGenerator->getTypeNameCombination($target, true);
} }
} } elseif ($target instanceof AbstractDBElement) { //Target does not have a name
$tmp = sprintf(
//Target does not have a name
if ($target instanceof AbstractDBElement) {
return sprintf(
'<i>%s</i>: %s', '<i>%s</i>: %s',
$this->elementTypeNameGenerator->getLocalizedTypeLabel($target), $this->elementTypeNameGenerator->getLocalizedTypeLabel($target),
$target->getID() $target->getID()
); );
} } elseif (null === $target && $context->hasTarget()) { //Element was deleted
$tmp = sprintf(
//Element was deleted
if (null === $target && $context->hasTarget()) {
return sprintf(
'<i>%s</i>: %s [%s]', '<i>%s</i>: %s [%s]',
$this->elementTypeNameGenerator->getLocalizedTypeLabel($context->getTargetClass()), $this->elementTypeNameGenerator->getLocalizedTypeLabel($context->getTargetClass()),
$context->getTargetID(), $context->getTargetID(),
@ -121,7 +122,32 @@ class LogEntryTargetColumn extends AbstractColumn
); );
} }
//Add a hint to the associated element if possible
if (null !== $target && $this->options['show_associated']) {
if ($target instanceof Attachment && $target->getElement() !== null) {
$on = $target->getElement();
} elseif ($target instanceof AbstractParameter && $target->getElement() !== null) {
$on = $target->getElement();
} elseif ($target instanceof Orderdetail && $target->getPart() !== null) {
$on = $target->getPart();
} elseif ($target instanceof Pricedetail && $target->getOrderdetail() !== null && $target->getOrderdetail()->getPart() !== null) {
$on = $target->getOrderdetail()->getPart();
}
if (isset($on) && is_object($on)) {
try {
$tmp .= sprintf(
' (<a href="%s">%s</a>)',
$this->entityURLGenerator->infoURL($on),
$this->elementTypeNameGenerator->getTypeNameCombination($on, true)
);
} catch (EntityNotSupportedException $exception) {
$tmp .= ' (' . $this->elementTypeNameGenerator->getTypeNameCombination($target, true) .')';
}
}
}
//Log is not associated with an element //Log is not associated with an element
return ''; return $tmp;
} }
} }

View file

@ -212,6 +212,7 @@ class LogDataTable implements DataTableTypeInterface
$dataTable->add('target', LogEntryTargetColumn::class, [ $dataTable->add('target', LogEntryTargetColumn::class, [
'label' => $this->translator->trans('log.target'), 'label' => $this->translator->trans('log.target'),
'show_associated' => $options['mode'] !== 'element_history',
]); ]);
$dataTable->add('extra', LogEntryExtraColumn::class, [ $dataTable->add('extra', LogEntryExtraColumn::class, [