diff --git a/src/Controller/PartController.php b/src/Controller/PartController.php index 09f14038..33eb706c 100644 --- a/src/Controller/PartController.php +++ b/src/Controller/PartController.php @@ -102,6 +102,7 @@ class PartController extends AbstractController [ 'part' => $part, 'form' => $form->createView(), + 'attachment_helper' => $attachmentHelper ]); } @@ -113,7 +114,8 @@ class PartController extends AbstractController * @param TranslatorInterface $translator * @return \Symfony\Component\HttpFoundation\Response */ - public function new(Request $request, EntityManagerInterface $em, TranslatorInterface $translator) + public function new(Request $request, EntityManagerInterface $em, TranslatorInterface $translator, + AttachmentHelper $attachmentHelper) { $new_part = new Part(); @@ -129,6 +131,13 @@ class PartController extends AbstractController $form->handleRequest($request); if ($form->isSubmitted() && $form->isValid()) { + //Upload passed files + $attachments = $form['attachments']; + foreach ($attachments as $attachment) { + /** @var $attachment FormInterface */ + $attachmentHelper->upload( $attachment->getData(), $attachment['file']->getData()); + } + $em->persist($new_part); $em->flush(); $this->addFlash('success', $translator->trans('part.created_flash')); @@ -142,6 +151,7 @@ class PartController extends AbstractController [ 'part' => $new_part, 'form' => $form->createView(), + 'attachment_helper' => $attachmentHelper ]); } diff --git a/src/Services/AttachmentHelper.php b/src/Services/AttachmentHelper.php index d05b346e..a5d82582 100644 --- a/src/Services/AttachmentHelper.php +++ b/src/Services/AttachmentHelper.php @@ -100,6 +100,10 @@ class AttachmentHelper */ public function toAbsoluteFilePath(Attachment $attachment): ?string { + if (empty($attachment->getPath())) { + return null; + } + if ($attachment->isExternal()) { return null; } @@ -119,6 +123,10 @@ class AttachmentHelper */ public function isFileExisting(Attachment $attachment): bool { + if (empty($attachment->getPath())) { + return false; + } + return file_exists($this->toAbsoluteFilePath($attachment)) || $attachment->isExternal(); } diff --git a/templates/Parts/edit/_attachments.html.twig b/templates/Parts/edit/_attachments.html.twig index f9e6a573..e08f5e1c 100644 --- a/templates/Parts/edit/_attachments.html.twig +++ b/templates/Parts/edit/_attachments.html.twig @@ -14,7 +14,32 @@ {{ form_widget(attachment) }} - {{ delete_btn }} + {{ delete_btn }} + + {% set attach = attachment.vars.value %} + + {% if attachment_helper.fileExisting(attach) %} + {% if not attach.external %} +

+
+ + {{ attach.filename }} + +
+ + {{ attachment_helper.humanFileSize(attach) }} + +
+ {% endif %} + {% else %} +

+
+ + {% trans %}attachment.file_not_found{% endtrans %} + +
+ {% endif %} + {% endfor %}