From 8ab9cf14177c660908fd4c2d0229c6bece55cfd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20B=C3=B6hmer?= Date: Mon, 13 Nov 2023 00:11:58 +0100 Subject: [PATCH] Added very basic possibility to add an association --- migrations/Version20231112211329.php | 39 +++++++++++++ src/Entity/LogSystem/LogTargetType.php | 4 ++ ...ssociationType.php => AssociationType.php} | 2 +- src/Entity/Parts/PartAssociation.php | 10 ++-- src/Form/Part/PartAssociationType.php | 57 +++++++++++++++++++ src/Form/Part/PartBaseType.php | 10 ++++ .../parts/edit/_associated_parts.html.twig | 18 ++++++ templates/parts/edit/edit_part_info.html.twig | 9 +++ 8 files changed, 143 insertions(+), 6 deletions(-) create mode 100644 migrations/Version20231112211329.php rename src/Entity/Parts/{PartAssociationType.php => AssociationType.php} (97%) create mode 100644 src/Form/Part/PartAssociationType.php create mode 100644 templates/parts/edit/_associated_parts.html.twig diff --git a/migrations/Version20231112211329.php b/migrations/Version20231112211329.php new file mode 100644 index 00000000..8fa7409c --- /dev/null +++ b/migrations/Version20231112211329.php @@ -0,0 +1,39 @@ +addSql('CREATE TABLE part_association (id INT AUTO_INCREMENT NOT NULL, owner_id INT NOT NULL, other_id INT NOT NULL, type SMALLINT NOT NULL, comment LONGTEXT DEFAULT NULL, last_modified DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL, datetime_added DATETIME DEFAULT CURRENT_TIMESTAMP NOT NULL, INDEX IDX_61B952E07E3C61F9 (owner_id), INDEX IDX_61B952E0998D9879 (other_id), PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB'); + $this->addSql('ALTER TABLE part_association ADD CONSTRAINT FK_61B952E07E3C61F9 FOREIGN KEY (owner_id) REFERENCES `parts` (id) ON DELETE CASCADE'); + $this->addSql('ALTER TABLE part_association ADD CONSTRAINT FK_61B952E0998D9879 FOREIGN KEY (other_id) REFERENCES `parts` (id) ON DELETE CASCADE'); + $this->addSql('ALTER TABLE part_lots ADD vendor_barcode VARCHAR(255) DEFAULT NULL'); + $this->addSql('CREATE INDEX part_lots_idx_barcode ON part_lots (vendor_barcode)'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('ALTER TABLE part_association DROP FOREIGN KEY FK_61B952E07E3C61F9'); + $this->addSql('ALTER TABLE part_association DROP FOREIGN KEY FK_61B952E0998D9879'); + $this->addSql('DROP TABLE part_association'); + $this->addSql('DROP INDEX part_lots_idx_barcode ON part_lots'); + $this->addSql('ALTER TABLE part_lots DROP vendor_barcode'); + } +} diff --git a/src/Entity/LogSystem/LogTargetType.php b/src/Entity/LogSystem/LogTargetType.php index eb3346d8..6e413079 100644 --- a/src/Entity/LogSystem/LogTargetType.php +++ b/src/Entity/LogSystem/LogTargetType.php @@ -29,6 +29,7 @@ use App\Entity\Parts\Footprint; use App\Entity\Parts\Manufacturer; use App\Entity\Parts\MeasurementUnit; use App\Entity\Parts\Part; +use App\Entity\Parts\PartAssociation; use App\Entity\Parts\PartLot; use App\Entity\Parts\StorageLocation; use App\Entity\Parts\Supplier; @@ -63,6 +64,8 @@ enum LogTargetType: int case PARAMETER = 18; case LABEL_PROFILE = 19; + case PART_ASSOCIATION = 20; + /** * Returns the class name of the target type or null if the target type is NONE. * @return string|null @@ -90,6 +93,7 @@ enum LogTargetType: int self::MEASUREMENT_UNIT => MeasurementUnit::class, self::PARAMETER => AbstractParameter::class, self::LABEL_PROFILE => LabelProfile::class, + self::PART_ASSOCIATION => PartAssociation::class, }; } diff --git a/src/Entity/Parts/PartAssociationType.php b/src/Entity/Parts/AssociationType.php similarity index 97% rename from src/Entity/Parts/PartAssociationType.php rename to src/Entity/Parts/AssociationType.php index d9320288..6980f600 100644 --- a/src/Entity/Parts/PartAssociationType.php +++ b/src/Entity/Parts/AssociationType.php @@ -26,7 +26,7 @@ namespace App\Entity\Parts; /** * The values of this enums are used to describe how two parts are associated with each other. */ -enum PartAssociationType: int +enum AssociationType: int { /** A user definable association type, which can be described in the comment field */ case OTHER = 0; diff --git a/src/Entity/Parts/PartAssociation.php b/src/Entity/Parts/PartAssociation.php index 1fc0530d..ae3e1271 100644 --- a/src/Entity/Parts/PartAssociation.php +++ b/src/Entity/Parts/PartAssociation.php @@ -41,10 +41,10 @@ class PartAssociation extends AbstractDBElement use TimestampTrait; /** - * @var PartAssociationType The type of this association (how the two parts are related) + * @var AssociationType The type of this association (how the two parts are related) */ - #[ORM\Column(type: Types::SMALLINT, enumType: PartAssociationType::class)] - protected PartAssociationType $type = PartAssociationType::OTHER; + #[ORM\Column(type: Types::SMALLINT, enumType: AssociationType::class)] + protected AssociationType $type = AssociationType::OTHER; /** * @var string|null A comment describing this association further. Can also be used to specify the OTHER type @@ -69,12 +69,12 @@ class PartAssociation extends AbstractDBElement #[Assert\NotNull] protected ?Part $other = null; - public function getType(): PartAssociationType + public function getType(): AssociationType { return $this->type; } - public function setType(PartAssociationType $type): PartAssociation + public function setType(AssociationType $type): PartAssociation { $this->type = $type; return $this; diff --git a/src/Form/Part/PartAssociationType.php b/src/Form/Part/PartAssociationType.php new file mode 100644 index 00000000..7163c257 --- /dev/null +++ b/src/Form/Part/PartAssociationType.php @@ -0,0 +1,57 @@ +. + */ + +declare(strict_types=1); + + +namespace App\Form\Part; + +use App\Entity\Parts\AssociationType; +use App\Entity\Parts\PartAssociation; +use App\Form\Type\PartSelectType; +use Symfony\Component\Form\AbstractType; +use Symfony\Component\Form\Extension\Core\Type\EnumType; +use Symfony\Component\Form\Extension\Core\Type\TextType; +use Symfony\Component\Form\FormBuilderInterface; +use Symfony\Component\OptionsResolver\OptionsResolver; + +class PartAssociationType extends AbstractType +{ + public function buildForm(FormBuilderInterface $builder, array $options): void + { + $builder + ->add('type', EnumType::class, [ + 'class' => AssociationType::class, + ]) + ->add('other', PartSelectType::class) + ->add('comment', TextType::class, [ + 'required' => false, + ]) + ; + + } + + public function configureOptions(OptionsResolver $resolver) + { + $resolver->setDefaults([ + 'data_class' => PartAssociation::class, + ]); + } +} \ No newline at end of file diff --git a/src/Form/Part/PartBaseType.php b/src/Form/Part/PartBaseType.php index b15ec29f..f7243e64 100644 --- a/src/Form/Part/PartBaseType.php +++ b/src/Form/Part/PartBaseType.php @@ -245,6 +245,16 @@ class PartBaseType extends AbstractType ], ]); + //Part associations + $builder->add('associated_parts_as_owner', CollectionType::class, [ + 'entry_type' => PartAssociationType::class, + 'allow_add' => true, + 'allow_delete' => true, + 'reindex_enable' => true, + 'label' => false, + 'by_reference' => false, + ]); + $builder->add('log_comment', TextType::class, [ 'label' => 'edit.log_comment', 'mapped' => false, diff --git a/templates/parts/edit/_associated_parts.html.twig b/templates/parts/edit/_associated_parts.html.twig new file mode 100644 index 00000000..beef96be --- /dev/null +++ b/templates/parts/edit/_associated_parts.html.twig @@ -0,0 +1,18 @@ +{% form_theme form with ['parts/edit/edit_form_styles.html.twig'] %} +{% import 'components/collection_type.macro.html.twig' as collection %} + +
+ + + {% for assoc in form.associated_parts_as_owner %} + {{ form_widget(assoc) }} + {% endfor %} + +
+ + +
\ No newline at end of file diff --git a/templates/parts/edit/edit_part_info.html.twig b/templates/parts/edit/edit_part_info.html.twig index 51b5d865..4dae8949 100644 --- a/templates/parts/edit/edit_part_info.html.twig +++ b/templates/parts/edit/edit_part_info.html.twig @@ -58,6 +58,12 @@ {% trans %}part.edit.tab.specifications{% endtrans %} +