mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-24 10:49:00 +02:00
Use an enum for target_type in log entries
This commit is contained in:
parent
2da7463edf
commit
9adfcc7aec
12 changed files with 359 additions and 210 deletions
64
src/DataTables/Column/EnumColumn.php
Normal file
64
src/DataTables/Column/EnumColumn.php
Normal file
|
@ -0,0 +1,64 @@
|
|||
<?php
|
||||
/*
|
||||
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||
*
|
||||
* Copyright (C) 2019 - 2023 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;
|
||||
use UnitEnum;
|
||||
|
||||
/**
|
||||
* @template T of UnitEnum
|
||||
*/
|
||||
class EnumColumn extends AbstractColumn
|
||||
{
|
||||
|
||||
/**
|
||||
* @phpstan-return T
|
||||
*/
|
||||
public function normalize($value): UnitEnum
|
||||
{
|
||||
if (is_a($value, $this->getEnumClass())) {
|
||||
return $value;
|
||||
}
|
||||
|
||||
//@phpstan-ignore-next-line
|
||||
return ($this->getEnumClass())::from($value);
|
||||
}
|
||||
|
||||
protected function configureOptions(OptionsResolver $resolver)
|
||||
{
|
||||
parent::configureOptions($resolver);
|
||||
|
||||
$resolver->setRequired('class');
|
||||
$resolver->setAllowedTypes('class', 'string');
|
||||
$resolver->addAllowedValues('class', enum_exists(...));
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return class-string<T>
|
||||
*/
|
||||
public function getEnumClass(): string
|
||||
{
|
||||
return $this->options['class'];
|
||||
}
|
||||
}
|
|
@ -22,6 +22,8 @@ declare(strict_types=1);
|
|||
|
||||
namespace App\DataTables;
|
||||
|
||||
use App\DataTables\Column\EnumColumn;
|
||||
use App\Entity\LogSystem\LogTargetType;
|
||||
use Symfony\Bundle\SecurityBundle\Security;
|
||||
use App\DataTables\Column\IconLinkColumn;
|
||||
use App\DataTables\Column\LocaleDateTimeColumn;
|
||||
|
@ -186,11 +188,12 @@ class LogDataTable implements DataTableTypeInterface
|
|||
},
|
||||
]);
|
||||
|
||||
$dataTable->add('target_type', TextColumn::class, [
|
||||
$dataTable->add('target_type', EnumColumn::class, [
|
||||
'label' => 'log.target_type',
|
||||
'visible' => false,
|
||||
'render' => function ($value, AbstractLogEntry $context) {
|
||||
$class = $context->getTargetClass();
|
||||
'class' => LogTargetType::class,
|
||||
'render' => function (LogTargetType $value, AbstractLogEntry $context) {
|
||||
$class = $value->toClass();
|
||||
if (null !== $class) {
|
||||
return $this->elementTypeNameGenerator->getLocalizedTypeLabel($class);
|
||||
}
|
||||
|
@ -277,8 +280,8 @@ class LogDataTable implements DataTableTypeInterface
|
|||
->andWhere('log.target_type NOT IN (:disallowed)');
|
||||
|
||||
$builder->setParameter('disallowed', [
|
||||
AbstractLogEntry::targetTypeClassToID(User::class),
|
||||
AbstractLogEntry::targetTypeClassToID(Group::class),
|
||||
LogTargetType::USER,
|
||||
LogTargetType::GROUP,
|
||||
]);
|
||||
}
|
||||
|
||||
|
@ -286,9 +289,12 @@ class LogDataTable implements DataTableTypeInterface
|
|||
foreach ($options['filter_elements'] as $element) {
|
||||
/** @var AbstractDBElement $element */
|
||||
|
||||
$target_type = AbstractLogEntry::targetTypeClassToID($element::class);
|
||||
$target_type = LogTargetType::fromElementClass($element);
|
||||
$target_id = $element->getID();
|
||||
$builder->orWhere("log.target_type = ${target_type} AND log.target_id = ${target_id}");
|
||||
|
||||
$builder->orWhere('log.target_type = :filter_target_type AND log.target_id = :filter_target_id');
|
||||
$builder->setParameter('filter_target_type', $target_type);
|
||||
$builder->setParameter('filter_target_id', $target_id);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue