2022-09-18 16:45:12 +02:00
|
|
|
<?php
|
2022-11-29 21:21:26 +01:00
|
|
|
/*
|
|
|
|
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
|
|
|
|
*
|
|
|
|
|
* Copyright (C) 2019 - 2022 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/>.
|
|
|
|
|
*/
|
2022-09-18 16:45:12 +02:00
|
|
|
|
|
|
|
|
namespace App\Twig;
|
|
|
|
|
|
|
|
|
|
use App\Entity\Attachments\Attachment;
|
|
|
|
|
use App\Entity\Base\AbstractDBElement;
|
2022-12-18 20:34:25 +01:00
|
|
|
use App\Entity\ProjectSystem\Project;
|
2022-09-18 16:45:12 +02:00
|
|
|
use App\Entity\LabelSystem\LabelProfile;
|
|
|
|
|
use App\Entity\Parts\Category;
|
|
|
|
|
use App\Entity\Parts\Footprint;
|
|
|
|
|
use App\Entity\Parts\Manufacturer;
|
|
|
|
|
use App\Entity\Parts\MeasurementUnit;
|
|
|
|
|
use App\Entity\Parts\Part;
|
|
|
|
|
use App\Entity\Parts\Storelocation;
|
|
|
|
|
use App\Entity\Parts\Supplier;
|
|
|
|
|
use App\Entity\PriceInformations\Currency;
|
|
|
|
|
use App\Entity\UserSystem\Group;
|
|
|
|
|
use App\Entity\UserSystem\User;
|
2023-06-06 23:15:14 +02:00
|
|
|
use App\Exceptions\EntityNotSupportedException;
|
2022-09-18 17:50:25 +02:00
|
|
|
use App\Services\ElementTypeNameGenerator;
|
2022-09-18 16:45:12 +02:00
|
|
|
use App\Services\EntityURLGenerator;
|
2022-09-18 17:50:25 +02:00
|
|
|
use App\Services\Trees\TreeViewGenerator;
|
2022-09-18 16:45:12 +02:00
|
|
|
use Twig\Extension\AbstractExtension;
|
|
|
|
|
use Twig\TwigFunction;
|
|
|
|
|
use Twig\TwigTest;
|
|
|
|
|
|
2022-09-18 17:50:25 +02:00
|
|
|
final class EntityExtension extends AbstractExtension
|
2022-09-18 16:45:12 +02:00
|
|
|
{
|
2023-06-11 14:15:46 +02:00
|
|
|
public function __construct(protected EntityURLGenerator $entityURLGenerator, protected TreeViewGenerator $treeBuilder, private readonly ElementTypeNameGenerator $nameGenerator)
|
2022-09-18 16:45:12 +02:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getTests(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
/* Checks if the given variable is an entitity (instance of AbstractDBElement) */
|
2023-06-11 14:15:46 +02:00
|
|
|
new TwigTest('entity', static fn($var) => $var instanceof AbstractDBElement),
|
2022-09-18 16:45:12 +02:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getFunctions(): array
|
|
|
|
|
{
|
|
|
|
|
return [
|
|
|
|
|
/* Returns a string representation of the given entity */
|
2023-06-11 14:15:46 +02:00
|
|
|
new TwigFunction('entity_type', fn(object $entity): ?string => $this->getEntityType($entity)),
|
2022-09-18 17:50:25 +02:00
|
|
|
/* Returns the URL to the given entity */
|
2023-06-11 14:15:46 +02:00
|
|
|
new TwigFunction('entity_url', fn(\App\Entity\Base\AbstractDBElement $entity, string $method = 'info'): string => $this->generateEntityURL($entity, $method)),
|
2023-05-15 00:16:49 +02:00
|
|
|
/* Returns the URL to the given entity in timetravel mode */
|
2023-06-11 14:15:46 +02:00
|
|
|
new TwigFunction('timetravel_url', fn(\App\Entity\Base\AbstractDBElement $element, \DateTimeInterface $dateTime): ?string => $this->timeTravelURL($element, $dateTime)),
|
2022-09-18 17:50:25 +02:00
|
|
|
/* Generates a JSON array of the given tree */
|
2023-06-11 14:15:46 +02:00
|
|
|
new TwigFunction('tree_data', fn(\App\Entity\Base\AbstractDBElement $element, string $type = 'newEdit'): string => $this->treeData($element, $type)),
|
2022-09-18 17:50:25 +02:00
|
|
|
|
|
|
|
|
/* Gets a human readable label for the type of the given entity */
|
2023-06-11 14:15:46 +02:00
|
|
|
new TwigFunction('entity_type_label', fn(object|string $entity): string => $this->nameGenerator->getLocalizedTypeLabel($entity)),
|
2022-09-18 16:45:12 +02:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
2023-06-06 23:15:14 +02:00
|
|
|
public function timeTravelURL(AbstractDBElement $element, \DateTimeInterface $dateTime): ?string
|
|
|
|
|
{
|
|
|
|
|
try {
|
|
|
|
|
return $this->entityURLGenerator->timeTravelURL($element, $dateTime);
|
2023-06-11 14:15:46 +02:00
|
|
|
} catch (EntityNotSupportedException) {
|
2023-06-06 23:15:14 +02:00
|
|
|
return null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-18 17:50:25 +02:00
|
|
|
public function treeData(AbstractDBElement $element, string $type = 'newEdit'): string
|
|
|
|
|
{
|
2023-06-11 14:15:46 +02:00
|
|
|
$tree = $this->treeBuilder->getTreeView($element::class, null, $type, $element);
|
2022-09-18 17:50:25 +02:00
|
|
|
|
|
|
|
|
return json_encode($tree, JSON_THROW_ON_ERROR);
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-18 16:45:12 +02:00
|
|
|
public function generateEntityURL(AbstractDBElement $entity, string $method = 'info'): string
|
|
|
|
|
{
|
|
|
|
|
return $this->entityURLGenerator->getURL($entity, $method);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function getEntityType(object $entity): ?string
|
|
|
|
|
{
|
|
|
|
|
$map = [
|
|
|
|
|
Part::class => 'part',
|
|
|
|
|
Footprint::class => 'footprint',
|
|
|
|
|
Storelocation::class => 'storelocation',
|
|
|
|
|
Manufacturer::class => 'manufacturer',
|
|
|
|
|
Category::class => 'category',
|
2022-12-18 20:34:25 +01:00
|
|
|
Project::class => 'device',
|
2022-09-18 16:45:12 +02:00
|
|
|
Attachment::class => 'attachment',
|
|
|
|
|
Supplier::class => 'supplier',
|
|
|
|
|
User::class => 'user',
|
|
|
|
|
Group::class => 'group',
|
|
|
|
|
Currency::class => 'currency',
|
|
|
|
|
MeasurementUnit::class => 'measurement_unit',
|
|
|
|
|
LabelProfile::class => 'label_profile',
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
foreach ($map as $class => $type) {
|
|
|
|
|
if ($entity instanceof $class) {
|
|
|
|
|
return $type;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
}
|