mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-30 13:34:28 +02:00
Show element history on part info page in history tab.
This commit is contained in:
parent
fff1864a68
commit
c14d6d91ff
12 changed files with 331 additions and 26 deletions
108
src/DataTables/Column/IconLinkColumn.php
Normal file
108
src/DataTables/Column/IconLinkColumn.php
Normal file
|
@ -0,0 +1,108 @@
|
|||
<?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\DataTables\Column;
|
||||
|
||||
|
||||
use Omines\DataTablesBundle\Column\AbstractColumn;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
class IconLinkColumn extends AbstractColumn
|
||||
{
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
public function normalize($value)
|
||||
{
|
||||
return $value;
|
||||
}
|
||||
|
||||
public function configureOptions(OptionsResolver $resolver)
|
||||
{
|
||||
parent::configureOptions($resolver);
|
||||
$resolver->setDefaults([
|
||||
'icon' => 'fas fa-fw fa-edit',
|
||||
'title' => null,
|
||||
'href' => null,
|
||||
]);
|
||||
|
||||
$resolver->setAllowedTypes('title', ['null', 'string', 'callable']);
|
||||
$resolver->setAllowedTypes('icon', ['null', 'string', 'callable']);
|
||||
$resolver->setAllowedTypes('href', ['null', 'string', 'callable']);
|
||||
}
|
||||
|
||||
public function render($value, $context)
|
||||
{
|
||||
$href = $this->getHref($value, $context);
|
||||
$icon = $this->getIcon($value, $context);
|
||||
$title = $this->getTitle($value, $context);
|
||||
|
||||
if ($href !== null) {
|
||||
return sprintf(
|
||||
'<a href="%s" title="%s"><i class="%s"></i></a>',
|
||||
$href,
|
||||
$title,
|
||||
$icon
|
||||
);
|
||||
}
|
||||
|
||||
return "";
|
||||
}
|
||||
|
||||
protected function getHref($value, $context): ?string
|
||||
{
|
||||
$provider = $this->options['href'];
|
||||
if (is_string($provider)) {
|
||||
return $provider;
|
||||
}
|
||||
if (is_callable($provider)) {
|
||||
return call_user_func($provider, $value, $context);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
protected function getIcon($value, $context): ?string
|
||||
{
|
||||
$provider = $this->options['icon'];
|
||||
if (is_string($provider)) {
|
||||
return $provider;
|
||||
}
|
||||
if (is_callable($provider)) {
|
||||
return call_user_func($provider, $value, $context);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
protected function getTitle($value, $context): ?string
|
||||
{
|
||||
$provider = $this->options['title'];
|
||||
if (is_string($provider)) {
|
||||
return $provider;
|
||||
}
|
||||
if (is_callable($provider)) {
|
||||
return call_user_func($provider, $value, $context);
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
|
@ -42,36 +42,63 @@ declare(strict_types=1);
|
|||
|
||||
namespace App\DataTables;
|
||||
|
||||
use App\DataTables\Column\IconLinkColumn;
|
||||
use App\DataTables\Column\LocaleDateTimeColumn;
|
||||
use App\DataTables\Column\LogEntryExtraColumn;
|
||||
use App\DataTables\Column\LogEntryTargetColumn;
|
||||
use App\Entity\Base\AbstractDBElement;
|
||||
use App\Entity\Contracts\TimeTravelInterface;
|
||||
use App\Entity\LogSystem\AbstractLogEntry;
|
||||
use App\Exceptions\EntityNotSupportedException;
|
||||
use App\Services\ElementTypeNameGenerator;
|
||||
use App\Services\EntityURLGenerator;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Doctrine\ORM\QueryBuilder;
|
||||
use Omines\DataTablesBundle\Adapter\Doctrine\ORMAdapter;
|
||||
use Omines\DataTablesBundle\Column\TextColumn;
|
||||
use Omines\DataTablesBundle\DataTable;
|
||||
use Omines\DataTablesBundle\DataTableTypeInterface;
|
||||
use Psr\Log\LogLevel;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
use Symfony\Flex\Options;
|
||||
|
||||
class LogDataTable implements DataTableTypeInterface
|
||||
{
|
||||
protected $elementTypeNameGenerator;
|
||||
protected $translator;
|
||||
protected $urlGenerator;
|
||||
protected $entityURLGenerator;
|
||||
protected $logRepo;
|
||||
|
||||
public function __construct(ElementTypeNameGenerator $elementTypeNameGenerator, TranslatorInterface $translator,
|
||||
UrlGeneratorInterface $urlGenerator)
|
||||
UrlGeneratorInterface $urlGenerator, EntityURLGenerator $entityURLGenerator, EntityManagerInterface $entityManager)
|
||||
{
|
||||
$this->elementTypeNameGenerator = $elementTypeNameGenerator;
|
||||
$this->translator = $translator;
|
||||
$this->urlGenerator = $urlGenerator;
|
||||
$this->entityURLGenerator = $entityURLGenerator;
|
||||
$this->logRepo = $entityManager->getRepository(AbstractLogEntry::class);
|
||||
}
|
||||
|
||||
public function configureOptions(OptionsResolver $optionsResolver)
|
||||
{
|
||||
$optionsResolver->setDefaults([
|
||||
'mode' => 'system_log',
|
||||
'filter_elements' => [],
|
||||
]);
|
||||
|
||||
$optionsResolver->setAllowedValues('mode', ['system_log', 'element_history']);
|
||||
}
|
||||
|
||||
public function configure(DataTable $dataTable, array $options): void
|
||||
{
|
||||
$resolver = new OptionsResolver();
|
||||
$this->configureOptions($resolver);
|
||||
$options = $resolver->resolve($options);
|
||||
|
||||
|
||||
$dataTable->add('symbol', TextColumn::class, [
|
||||
'label' => '',
|
||||
'render' => function ($value, AbstractLogEntry $context) {
|
||||
|
@ -138,6 +165,7 @@ class LogDataTable implements DataTableTypeInterface
|
|||
|
||||
$dataTable->add('level', TextColumn::class, [
|
||||
'label' => $this->translator->trans('log.level'),
|
||||
'visible' => $options['mode'] === 'system_log',
|
||||
'propertyPath' => 'levelString',
|
||||
'render' => function (string $value, AbstractLogEntry $context) {
|
||||
return $value;
|
||||
|
@ -178,21 +206,51 @@ class LogDataTable implements DataTableTypeInterface
|
|||
'label' => $this->translator->trans('log.extra'),
|
||||
]);
|
||||
|
||||
$dataTable->add('timeTravel', IconLinkColumn::class,[
|
||||
'label' => '',
|
||||
'icon' => 'fas fa-fw fa-eye',
|
||||
'href' => function ($value, AbstractLogEntry $context) {
|
||||
if (
|
||||
$context instanceof TimeTravelInterface
|
||||
&& $context->hasOldDataInformations()
|
||||
) {
|
||||
try {
|
||||
$target = $this->logRepo->getTargetElement($context);
|
||||
$str = $this->entityURLGenerator->timeTravelURL($target, $context->getTimestamp());
|
||||
return $str;
|
||||
} catch (EntityNotSupportedException $exception) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
]);
|
||||
|
||||
$dataTable->addOrderBy('timestamp', DataTable::SORT_DESCENDING);
|
||||
|
||||
$dataTable->createAdapter(ORMAdapter::class, [
|
||||
'entity' => AbstractLogEntry::class,
|
||||
'query' => function (QueryBuilder $builder): void {
|
||||
$this->getQuery($builder);
|
||||
'query' => function (QueryBuilder $builder) use ($options): void {
|
||||
$this->getQuery($builder, $options);
|
||||
},
|
||||
]);
|
||||
}
|
||||
|
||||
protected function getQuery(QueryBuilder $builder): void
|
||||
protected function getQuery(QueryBuilder $builder, array $options): void
|
||||
{
|
||||
$builder->distinct()->select('log')
|
||||
->addSelect('user')
|
||||
->from(AbstractLogEntry::class, 'log')
|
||||
->leftJoin('log.user', 'user');
|
||||
|
||||
if (!empty($options['filter_elements'])) {
|
||||
foreach ($options['filter_elements'] as $element) {
|
||||
/** @var AbstractDBElement $element */
|
||||
|
||||
$target_type = AbstractLogEntry::targetTypeClassToID(get_class($element));
|
||||
$target_id = $element->getID();
|
||||
$builder->orWhere("log.target_type = $target_type AND log.target_id = $target_id");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue