diff --git a/src/Controller/PartController.php b/src/Controller/PartController.php index 28051ec7..11ebd22f 100644 --- a/src/Controller/PartController.php +++ b/src/Controller/PartController.php @@ -224,6 +224,7 @@ class PartController extends AbstractController /** * @Route("/new", name="part_new") + * @Route("/{id}/clone", name="part_clone") * * @param Request $request * @param EntityManagerInterface $em @@ -233,16 +234,20 @@ class PartController extends AbstractController * @return Response */ public function new(Request $request, EntityManagerInterface $em, TranslatorInterface $translator, - AttachmentManager $attachmentHelper, AttachmentSubmitHandler $attachmentSubmitHandler): Response + AttachmentManager $attachmentHelper, AttachmentSubmitHandler $attachmentSubmitHandler, ?Part $part = null): Response { - $new_part = new Part(); + if($part === null) { + $new_part = new Part(); + } else { + $new_part = clone $part; + } $this->denyAccessUnlessGranted('create', $new_part); $cid = $request->get('cid', 1); $category = $em->find(Category::class, $cid); - if (null !== $category) { + if (null !== $category && $new_part->getCategory() === null) { $new_part->setCategory($category); } @@ -292,7 +297,7 @@ class PartController extends AbstractController } /** - * @Route("/{id}/clone", name="part_clone") + * //@Route("/{id}/clone", name="part_clone") * * @param Part $part * @param Request $request @@ -323,6 +328,7 @@ class PartController extends AbstractController [ 'part' => $new_part, 'form' => $form->createView(), + 'attachment_helper' => $attachmentHelper, ]); } } diff --git a/src/Entity/Attachments/AttachmentContainingDBElement.php b/src/Entity/Attachments/AttachmentContainingDBElement.php index 196db2a8..e6df1976 100644 --- a/src/Entity/Attachments/AttachmentContainingDBElement.php +++ b/src/Entity/Attachments/AttachmentContainingDBElement.php @@ -95,4 +95,23 @@ abstract class AttachmentContainingDBElement extends AbstractNamedDBElement impl return $this; } + + public function __clone() + { + if ($this->id) { + $attachments = $this->attachments; + $this->attachments = new ArrayCollection(); + //Set master attachment is needed + foreach ($attachments as $attachment) { + $clone = clone $attachment; + if ($attachment === $this->master_picture_attachment) { + $this->setMasterPictureAttachment($clone); + } + $this->addAttachment($clone); + } + } + + //Parent has to be last call, as it resets the ID + parent::__clone(); + } } diff --git a/src/Entity/Base/AbstractDBElement.php b/src/Entity/Base/AbstractDBElement.php index 46dac31f..1bc72c56 100644 --- a/src/Entity/Base/AbstractDBElement.php +++ b/src/Entity/Base/AbstractDBElement.php @@ -68,8 +68,10 @@ abstract class AbstractDBElement implements \JsonSerializable public function __clone() { - //Set ID to null, so that an new entry is created - $this->id = null; + if ($this->id) { + //Set ID to null, so that an new entry is created + $this->id = null; + } } /** diff --git a/src/Entity/Base/AbstractNamedDBElement.php b/src/Entity/Base/AbstractNamedDBElement.php index 212211f3..d0531924 100644 --- a/src/Entity/Base/AbstractNamedDBElement.php +++ b/src/Entity/Base/AbstractNamedDBElement.php @@ -89,7 +89,15 @@ abstract class AbstractNamedDBElement extends AbstractDBElement implements Named public function setName(string $new_name): self { $this->name = $new_name; - return $this; } + + public function __clone() + { + if ($this->id) { + //We create a new object, so give it a new creation date + $this->addedDate = null; + } + parent::__clone(); // TODO: Change the autogenerated stub + } } diff --git a/src/Entity/Parts/Part.php b/src/Entity/Parts/Part.php index a94fa37f..e2cd7623 100644 --- a/src/Entity/Parts/Part.php +++ b/src/Entity/Parts/Part.php @@ -156,4 +156,24 @@ class Part extends AttachmentContainingDBElement { return $this->devices; } + + public function __clone() + { + if ($this->id) { + //Deep clone part lots + $lots = $this->partLots; + $this->partLots = new ArrayCollection(); + foreach ($lots as $lot) { + $this->addPartLot(clone $lot); + } + + //Deep clone order details + $orderdetails = $this->orderdetails; + $this->orderdetails = new ArrayCollection(); + foreach ($orderdetails as $orderdetail) { + $this->addOrderdetail(clone $orderdetail); + } + } + parent::__clone(); + } } diff --git a/src/Entity/Parts/PartLot.php b/src/Entity/Parts/PartLot.php index ef738bd8..e1fb6cc1 100644 --- a/src/Entity/Parts/PartLot.php +++ b/src/Entity/Parts/PartLot.php @@ -325,4 +325,12 @@ class PartLot extends AbstractDBElement implements TimeStampableInterface return $this; } + + public function __clone() + { + if($this->id) { + $this->addedDate = null; + } + parent::__clone(); + } } diff --git a/src/Entity/PriceInformations/Orderdetail.php b/src/Entity/PriceInformations/Orderdetail.php index 04bf748c..72962a37 100644 --- a/src/Entity/PriceInformations/Orderdetail.php +++ b/src/Entity/PriceInformations/Orderdetail.php @@ -348,4 +348,18 @@ class Orderdetail extends AbstractDBElement implements TimeStampableInterface return $this; } + + public function __clone() + { + if ($this->id) { + $this->addedDate = null; + $pricedetails = $this->pricedetails; + $this->pricedetails = new ArrayCollection(); + //Set master attachment is needed + foreach ($pricedetails as $pricedetail) { + $this->addPricedetail(clone $pricedetail); + } + } + parent::__clone(); + } } diff --git a/src/Entity/PriceInformations/Pricedetail.php b/src/Entity/PriceInformations/Pricedetail.php index d7f19608..234bac54 100644 --- a/src/Entity/PriceInformations/Pricedetail.php +++ b/src/Entity/PriceInformations/Pricedetail.php @@ -328,4 +328,12 @@ class Pricedetail extends AbstractDBElement implements TimeStampableInterface { return 'PD'.sprintf('%06d', $this->getID()); } + + public function __clone() + { + if ($this->id) { + $this->addedDate = null; + } + parent::__clone(); + } }