diff --git a/src/Services/EntityURLGenerator.php b/src/Services/EntityURLGenerator.php index daafa5c9..49f8c2d1 100644 --- a/src/Services/EntityURLGenerator.php +++ b/src/Services/EntityURLGenerator.php @@ -96,12 +96,12 @@ class EntityURLGenerator * @param mixed $entity The element for which the page should be generated * @param string $type The page type. Currently supported: 'info', 'edit', 'create', 'clone', 'list'/'list_parts' * - * @return string|null the link to the desired page + * @return string the link to the desired page * * @throws EntityNotSupportedException thrown if the entity is not supported for the given type * @throws InvalidArgumentException thrown if the givent type is not existing */ - public function getURL($entity, string $type): ?string + public function getURL($entity, string $type): string { switch ($type) { case 'info': diff --git a/src/Twig/AppExtension.php b/src/Twig/AppExtension.php index 3e399e91..0bbc6365 100644 --- a/src/Twig/AppExtension.php +++ b/src/Twig/AppExtension.php @@ -77,7 +77,6 @@ use function get_class; class AppExtension extends AbstractExtension { - protected $entityURLGenerator; protected $markdownParser; protected $serializer; protected $treeBuilder; @@ -88,16 +87,13 @@ class AppExtension extends AbstractExtension protected $FAIconGenerator; protected $translator; - protected $objectNormalizer; - - public function __construct(EntityURLGenerator $entityURLGenerator, MarkdownParser $markdownParser, + public function __construct(MarkdownParser $markdownParser, SerializerInterface $serializer, TreeViewGenerator $treeBuilder, MoneyFormatter $moneyFormatter, SIFormatter $SIFormatter, AmountFormatter $amountFormatter, AttachmentURLGenerator $attachmentURLGenerator, - FAIconGenerator $FAIconGenerator, TranslatorInterface $translator, ObjectNormalizer $objectNormalizer) + FAIconGenerator $FAIconGenerator, TranslatorInterface $translator) { - $this->entityURLGenerator = $entityURLGenerator; $this->markdownParser = $markdownParser; $this->serializer = $serializer; $this->treeBuilder = $treeBuilder; @@ -107,14 +103,11 @@ class AppExtension extends AbstractExtension $this->attachmentURLGenerator = $attachmentURLGenerator; $this->FAIconGenerator = $FAIconGenerator; $this->translator = $translator; - - $this->objectNormalizer = $objectNormalizer; } public function getFilters(): array { return [ - new TwigFilter('entityURL', [$this, 'generateEntityURL']), new TwigFilter('markdown', [$this->markdownParser, 'markForRendering'], [ 'pre_escape' => 'html', 'is_safe' => ['html'], @@ -123,25 +116,10 @@ class AppExtension extends AbstractExtension new TwigFilter('siFormat', [$this, 'siFormat']), new TwigFilter('amountFormat', [$this, 'amountFormat']), new TwigFilter('loginPath', [$this, 'loginPath']), - - new TwigFilter('toArray', [$this, 'toArray']) ]; } - public function getTests(): array - { - return [ - new TwigTest('instanceof', static function ($var, $instance) { - return $var instanceof $instance; - }), - new TwigTest('entity', static function ($var) { - return $var instanceof AbstractDBElement; - }), - new TwigTest('object', static function ($var) { - return is_object($var); - }), - ]; - } + public function getFunctions(): array { @@ -149,31 +127,9 @@ class AppExtension extends AbstractExtension new TwigFunction('generateTreeData', [$this, 'treeData']), new TwigFunction('attachment_thumbnail', [$this->attachmentURLGenerator, 'getThumbnailURL']), new TwigFunction('ext_to_fa_icon', [$this->FAIconGenerator, 'fileExtensionToFAType']), - new TwigFunction('entity_type', [$this, 'getEntityType']), ]; } - public function getEntityType($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', - ]; - - return $map[get_class($entity)] ?? null; - } - public function treeData(AbstractDBElement $element, string $type = 'newEdit'): string { $tree = $this->treeBuilder->getTreeView(get_class($element), null, $type, $element); @@ -181,10 +137,7 @@ class AppExtension extends AbstractExtension return json_encode($tree, JSON_THROW_ON_ERROR); } - public function toArray($object): array - { - return $this->objectNormalizer->normalize($object, null); - } + /** * This function/filter generates an path. @@ -198,10 +151,7 @@ class AppExtension extends AbstractExtension return implode('/', $parts); } - public function generateEntityURL(AbstractDBElement $entity, string $method = 'info'): string - { - return $this->entityURLGenerator->getURL($entity, $method); - } + public function formatCurrency($amount, ?Currency $currency = null, int $decimals = 5): string { diff --git a/src/Twig/EntityExtension.php b/src/Twig/EntityExtension.php new file mode 100644 index 00000000..ca7b4fb7 --- /dev/null +++ b/src/Twig/EntityExtension.php @@ -0,0 +1,91 @@ +entityURLGenerator = $entityURLGenerator; + } + + public function getFilters(): array + { + return [ + + ]; + } + + 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']), + new TwigFunction('entity_url', [$this, 'generateEntityURL']), + ]; + } + + 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; + } +} \ No newline at end of file diff --git a/src/Twig/TwigCoreExtension.php b/src/Twig/TwigCoreExtension.php new file mode 100644 index 00000000..04f9f20f --- /dev/null +++ b/src/Twig/TwigCoreExtension.php @@ -0,0 +1,60 @@ +objectNormalizer = $objectNormalizer; + } + + public function getTests(): array + { + return [ + /* + * Checks if a given variable is an instance of a given class. E.g. ` x is instanceof('App\Entity\Parts\Part')` + */ + new TwigTest('instanceof', static function ($var, $instance) { + return $var instanceof $instance; + }), + /* Checks if a given variable is an object. E.g. `x is object` */ + new TwigTest('object', static function ($var) { + return is_object($var); + }), + ]; + } + + public function getFilters() + { + return [ + /* Converts the given object to an array representation of the public/accessible properties */ + new TwigFilter('to_array', [$this, 'toArray']), + ]; + } + + public function toArray($object) + { + if(! is_object($object) && ! is_array($object)) { + throw new \InvalidArgumentException('The given variable is not an object or array!'); + } + + //If it is already an array, we can just return it + if(is_array($object)) { + return $object; + } + + return $this->objectNormalizer->normalize($object, null); + } +} \ No newline at end of file diff --git a/src/Twig/LastUserExtension.php b/src/Twig/UserExtension.php similarity index 78% rename from src/Twig/LastUserExtension.php rename to src/Twig/UserExtension.php index ed781532..b6e051eb 100644 --- a/src/Twig/LastUserExtension.php +++ b/src/Twig/UserExtension.php @@ -29,7 +29,7 @@ use Doctrine\ORM\EntityManagerInterface; use Twig\Extension\AbstractExtension; use Twig\TwigFunction; -class LastUserExtension extends AbstractExtension +class UserExtension extends AbstractExtension { /** @var LogEntryRepository */ private $repo; @@ -42,8 +42,10 @@ class LastUserExtension extends AbstractExtension public function getFunctions(): array { return [ - new TwigFunction('getLastEditingUser', [$this->repo, 'getLastEditingUser']), - new TwigFunction('getCreatingUser', [$this->repo, 'getCreatingUser']), + /* Returns the user which has edited the given entity the last time. */ + new TwigFunction('last_editing_user', [$this->repo, 'getLastEditingUser']), + /* Returns the user which has created the given entity. */ + new TwigFunction('creating_user', [$this->repo, 'getCreatingUser']), ]; } } diff --git a/templates/AdminPages/_delete_form.html.twig b/templates/AdminPages/_delete_form.html.twig index 14a2c909..e6250d57 100644 --- a/templates/AdminPages/_delete_form.html.twig +++ b/templates/AdminPages/_delete_form.html.twig @@ -1,4 +1,4 @@ -