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 @@ -
diff --git a/templates/AdminPages/_duplicate.html.twig b/templates/AdminPages/_duplicate.html.twig index a55129a2..1b18cd71 100644 --- a/templates/AdminPages/_duplicate.html.twig +++ b/templates/AdminPages/_duplicate.html.twig @@ -1,5 +1,5 @@
\ No newline at end of file diff --git a/templates/LabelSystem/dialog.html.twig b/templates/LabelSystem/dialog.html.twig index 336118f9..eb4b0da5 100644 --- a/templates/LabelSystem/dialog.html.twig +++ b/templates/LabelSystem/dialog.html.twig @@ -60,7 +60,7 @@
{{ profile.name ?? '-' }} {% if profile %} - {% endif %} diff --git a/templates/Parts/edit/edit_form_styles.html.twig b/templates/Parts/edit/edit_form_styles.html.twig index 47b66476..1931a1ee 100644 --- a/templates/Parts/edit/edit_form_styles.html.twig +++ b/templates/Parts/edit/edit_form_styles.html.twig @@ -143,11 +143,11 @@ {% if attach.secure and not is_granted('show_private', attach) %} {# Leave blank #} {% elseif attach.picture %} - + {% trans %}attachment.preview.alt{% endtrans %} {% else %} - {% trans %}attachment.view{% endtrans %} + {% trans %}attachment.view{% endtrans %} {% endif %} {% else %}

diff --git a/templates/Parts/edit/edit_part_info.html.twig b/templates/Parts/edit/edit_part_info.html.twig index d00a3fce..981cc874 100644 --- a/templates/Parts/edit/edit_part_info.html.twig +++ b/templates/Parts/edit/edit_part_info.html.twig @@ -7,7 +7,7 @@ {% block card_title %} {% trans with {'%name%': part.name} %}part.edit.card_title{% endtrans %} - {{ part.name }} + {{ part.name }}
{% trans %}id.label{% endtrans %}: {{ part.id }}
diff --git a/templates/Parts/info/_attachments_info.html.twig b/templates/Parts/info/_attachments_info.html.twig index 5f1dcc6e..4472a6ad 100644 --- a/templates/Parts/info/_attachments_info.html.twig +++ b/templates/Parts/info/_attachments_info.html.twig @@ -57,12 +57,12 @@
- - diff --git a/templates/Parts/info/_extended_infos.html.twig b/templates/Parts/info/_extended_infos.html.twig index fdf8bdf7..a9d45469 100644 --- a/templates/Parts/info/_extended_infos.html.twig +++ b/templates/Parts/info/_extended_infos.html.twig @@ -10,7 +10,7 @@ {% trans %}user.creating_user{% endtrans %} {% if is_granted('show_users', part) %} - {{ getCreatingUser(part).fullName(true) ?? 'Unknown'|trans }} + {{ creating_user(part).fullName(true) ?? 'Unknown'|trans }} {% else %} {% trans %}accessDenied{% endtrans %} {% endif %} @@ -25,7 +25,7 @@ {% trans %}user.last_editing_user{% endtrans %} {% if is_granted('show_users', part) %} - {{ getLastEditingUser(part).fullName(true) ?? 'Unknown'|trans }} + {{ last_editing_user(part).fullName(true) ?? 'Unknown'|trans }} {% else %} {% trans %}accessDenied{% endtrans %} {% endif %} diff --git a/templates/Parts/info/_main_infos.html.twig b/templates/Parts/info/_main_infos.html.twig index a8abe1d3..0b42cd4d 100644 --- a/templates/Parts/info/_main_infos.html.twig +++ b/templates/Parts/info/_main_infos.html.twig @@ -8,7 +8,7 @@
{% if part.manufacturer %} {% if part.manufacturer.id is not null %} - {{ part.manufacturer.name}} + {{ part.manufacturer.name}} {% else %} {{ part.manufacturer.name }} {% endif %} @@ -24,9 +24,9 @@

{{ part.name }} {# You need edit permission to use the edit button #} {% if timeTravel is not null %} - + {% elseif is_granted('edit', part) %} - + {% endif %}

{{ part.description|markdown(true) }}
diff --git a/templates/Parts/info/_order_infos.html.twig b/templates/Parts/info/_order_infos.html.twig index 0c44db2a..23965f72 100644 --- a/templates/Parts/info/_order_infos.html.twig +++ b/templates/Parts/info/_order_infos.html.twig @@ -12,7 +12,7 @@ {% for order in part.orderdetails %} - {{ order.supplier.name }} + {{ order.supplier.name }} {% if order.supplierProductUrl is not empty %} {{ order.supplierPartNr }} diff --git a/templates/Parts/info/_picture.html.twig b/templates/Parts/info/_picture.html.twig index 532d0f8f..f4645e9c 100644 --- a/templates/Parts/info/_picture.html.twig +++ b/templates/Parts/info/_picture.html.twig @@ -11,8 +11,8 @@ {% for pic in pictures %} {# @var pic App\Entity\Attachments\Attachment #}