From 7f612bc371c27d6b04a18d8a1036ba84aa5f4a8d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Wed, 15 Nov 2023 00:44:45 +0100 Subject: [PATCH] Added ability to view part relations on a part info page --- config/packages/twig.yaml | 1 + src/Entity/Parts/PartAssociation.php | 12 +++++ .../Parts/PartTraits/AssociationTrait.php | 16 +++++++ templates/helper.twig | 9 ++++ templates/parts/info/_associations.html.twig | 40 ++++++++++++++++ templates/parts/info/show_part_info.html.twig | 17 +++++++ translations/messages.en.xlf | 48 +++++++++++++++++++ 7 files changed, 143 insertions(+) create mode 100644 templates/parts/info/_associations.html.twig diff --git a/config/packages/twig.yaml b/config/packages/twig.yaml index 05dec32c..5b2d64e5 100644 --- a/config/packages/twig.yaml +++ b/config/packages/twig.yaml @@ -20,6 +20,7 @@ twig: avatar_helper: '@App\Services\UserSystem\UserAvatarHelper' available_themes: '%partdb.available_themes%' saml_enabled: '%partdb.saml.enabled%' + part_preview_generator: '@App\Services\Attachments\PartPreviewGenerator' when@test: twig: diff --git a/src/Entity/Parts/PartAssociation.php b/src/Entity/Parts/PartAssociation.php index 98becd9c..81732057 100644 --- a/src/Entity/Parts/PartAssociation.php +++ b/src/Entity/Parts/PartAssociation.php @@ -136,5 +136,17 @@ class PartAssociation extends AbstractDBElement return $this; } + /** + * Returns the translation key for the type of this association. + * If the type is set to OTHER, then the other_type field value is used. + * @return string + */ + public function getTypeTranslationKey(): string + { + if ($this->type === AssociationType::OTHER) { + return $this->other_type ?? 'Unknown'; + } + return $this->type->getTranslationKey(); + } } \ No newline at end of file diff --git a/src/Entity/Parts/PartTraits/AssociationTrait.php b/src/Entity/Parts/PartTraits/AssociationTrait.php index 82c46f27..6cc1b973 100644 --- a/src/Entity/Parts/PartTraits/AssociationTrait.php +++ b/src/Entity/Parts/PartTraits/AssociationTrait.php @@ -23,7 +23,9 @@ declare(strict_types=1); namespace App\Entity\Parts\PartTraits; +use App\Entity\Parts\Part; use App\Entity\Parts\PartAssociation; +use Doctrine\Common\Collections\ArrayCollection; use Doctrine\Common\Collections\Collection; use Symfony\Component\Validator\Constraints\Valid; use Doctrine\ORM\Mapping as ORM; @@ -89,4 +91,18 @@ trait AssociationTrait { return $this->associated_parts_as_other; } + + /** + * Returns all associations where this part is the owned or other part. + * @return Collection + */ + public function getAssociatedPartsAll(): Collection + { + return new ArrayCollection( + array_merge( + $this->associated_parts_as_owner->toArray(), + $this->associated_parts_as_other->toArray() + ) + ); + } } \ No newline at end of file diff --git a/templates/helper.twig b/templates/helper.twig index d0ea72be..f6de89d6 100644 --- a/templates/helper.twig +++ b/templates/helper.twig @@ -188,6 +188,15 @@ {% endif %} {% endmacro %} +{% macro part_icon_link(part) %} + {% set preview_attach = part_preview_generator.tablePreviewAttachment(part) %} + {% if preview_attach %} + Part image + {% endif %} + {{ part.name }} +{% endmacro %} + {% macro entity_preview_sm(entity) %} {# @var entity \App\Entity\Contracts\HasMasterAttachmentInterface #} {% if entity.masterPictureAttachment and entity.masterPictureAttachment.picture and attachment_manager.fileExisting(entity.masterPictureAttachment) %} diff --git a/templates/parts/info/_associations.html.twig b/templates/parts/info/_associations.html.twig new file mode 100644 index 00000000..7325a5c7 --- /dev/null +++ b/templates/parts/info/_associations.html.twig @@ -0,0 +1,40 @@ +{% import "helper.twig" as helper %} + +{% macro assoc_row(assoc) %} + {# @var assoc \App\Entity\Parts\PartAssociation #} + + {{ helper.part_icon_link(assoc.owner) }} + {{ assoc.typeTranslationKey | trans }} + {{ helper.part_icon_link(assoc.other) }} + {{ assoc.comment }} + +{% endmacro %} + +{% macro assoc_table(assocs, caption) %} +
+ + + + + + + + + + + + {% for assoc in assocs %} + {{ _self.assoc_row(assoc) }} + {% endfor %} + +
{{ caption | trans }}:
{% trans %}part_association.table.from{% endtrans %}{% trans %}part_association.table.type{% endtrans %}{% trans %}part_association.table.to{% endtrans %}{% trans %}part_association.edit.comment{% endtrans %}
+
+{% endmacro %} + +{% if part.associatedPartsAsOwner is not empty %} + {{ _self.assoc_table(part.associatedPartsAsOwner, 'part_association.table.from_this_part') }} +{% endif %} + +{% if part.associatedPartsAsOther is not empty %} + {{ _self.assoc_table(part.associatedPartsAsOther, 'part_association.table.to_this_part') }} +{% endif %} \ No newline at end of file diff --git a/templates/parts/info/show_part_info.html.twig b/templates/parts/info/show_part_info.html.twig index 6ac7bd52..41daba20 100644 --- a/templates/parts/info/show_part_info.html.twig +++ b/templates/parts/info/show_part_info.html.twig @@ -88,6 +88,17 @@ {% endif %} + + {% if part.associatedPartsAll is not empty %} + + {% endif %} +