2022-09-18 16:45:12 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace App\Twig;
|
|
|
|
|
|
|
|
use App\Entity\Attachments\Attachment;
|
|
|
|
use App\Entity\Base\AbstractDBElement;
|
|
|
|
use App\Entity\Devices\Device;
|
|
|
|
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;
|
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
|
|
|
{
|
|
|
|
protected $entityURLGenerator;
|
2022-09-18 17:50:25 +02:00
|
|
|
protected $treeBuilder;
|
|
|
|
private $nameGenerator;
|
2022-09-18 16:45:12 +02:00
|
|
|
|
2022-09-18 17:50:25 +02:00
|
|
|
public function __construct(EntityURLGenerator $entityURLGenerator, TreeViewGenerator $treeBuilder, ElementTypeNameGenerator $elementTypeNameGenerator)
|
2022-09-18 16:45:12 +02:00
|
|
|
{
|
|
|
|
$this->entityURLGenerator = $entityURLGenerator;
|
2022-09-18 17:50:25 +02:00
|
|
|
$this->treeBuilder = $treeBuilder;
|
|
|
|
$this->nameGenerator = $elementTypeNameGenerator;
|
2022-09-18 16:45:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getTests(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
/* Checks if the given variable is an entitity (instance of AbstractDBElement) */
|
|
|
|
new TwigTest('entity', static function ($var) {
|
|
|
|
return $var instanceof AbstractDBElement;
|
|
|
|
}),
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function getFunctions(): array
|
|
|
|
{
|
|
|
|
return [
|
|
|
|
/* Returns a string representation of the given entity */
|
|
|
|
new TwigFunction('entity_type', [$this, 'getEntityType']),
|
2022-09-18 17:50:25 +02:00
|
|
|
/* Returns the URL to the given entity */
|
2022-09-18 16:45:12 +02:00
|
|
|
new TwigFunction('entity_url', [$this, 'generateEntityURL']),
|
2022-09-18 17:50:25 +02:00
|
|
|
/* Generates a JSON array of the given tree */
|
|
|
|
new TwigFunction('tree_data', [$this, 'treeData']),
|
|
|
|
|
|
|
|
/* Gets a human readable label for the type of the given entity */
|
|
|
|
new TwigFunction('entity_type_label', [$this->nameGenerator, 'getLocalizedTypeLabel']),
|
2022-09-18 16:45:12 +02:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2022-09-18 17:50:25 +02:00
|
|
|
public function treeData(AbstractDBElement $element, string $type = 'newEdit'): string
|
|
|
|
{
|
|
|
|
$tree = $this->treeBuilder->getTreeView(get_class($element), null, $type, $element);
|
|
|
|
|
|
|
|
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',
|
|
|
|
Device::class => 'device',
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|