Show infos about filename and filesize on attachment edit tab.

This commit is contained in:
Jan Böhmer 2019-08-26 23:46:38 +02:00
parent 3a11933a89
commit 09a5948149
3 changed files with 45 additions and 2 deletions

View file

@ -102,6 +102,7 @@ class PartController extends AbstractController
[ [
'part' => $part, 'part' => $part,
'form' => $form->createView(), 'form' => $form->createView(),
'attachment_helper' => $attachmentHelper
]); ]);
} }
@ -113,7 +114,8 @@ class PartController extends AbstractController
* @param TranslatorInterface $translator * @param TranslatorInterface $translator
* @return \Symfony\Component\HttpFoundation\Response * @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(); $new_part = new Part();
@ -129,6 +131,13 @@ class PartController extends AbstractController
$form->handleRequest($request); $form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) { 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->persist($new_part);
$em->flush(); $em->flush();
$this->addFlash('success', $translator->trans('part.created_flash')); $this->addFlash('success', $translator->trans('part.created_flash'));
@ -142,6 +151,7 @@ class PartController extends AbstractController
[ [
'part' => $new_part, 'part' => $new_part,
'form' => $form->createView(), 'form' => $form->createView(),
'attachment_helper' => $attachmentHelper
]); ]);
} }

View file

@ -100,6 +100,10 @@ class AttachmentHelper
*/ */
public function toAbsoluteFilePath(Attachment $attachment): ?string public function toAbsoluteFilePath(Attachment $attachment): ?string
{ {
if (empty($attachment->getPath())) {
return null;
}
if ($attachment->isExternal()) { if ($attachment->isExternal()) {
return null; return null;
} }
@ -119,6 +123,10 @@ class AttachmentHelper
*/ */
public function isFileExisting(Attachment $attachment): bool public function isFileExisting(Attachment $attachment): bool
{ {
if (empty($attachment->getPath())) {
return false;
}
return file_exists($this->toAbsoluteFilePath($attachment)) || $attachment->isExternal(); return file_exists($this->toAbsoluteFilePath($attachment)) || $attachment->isExternal();
} }

View file

@ -14,7 +14,32 @@
{{ form_widget(attachment) }} {{ form_widget(attachment) }}
</td> </td>
<td> <td>
{{ delete_btn }} {{ delete_btn }}
{% set attach = attachment.vars.value %}
{% if attachment_helper.fileExisting(attach) %}
{% if not attach.external %}
<br><br>
<h6>
<span class="badge badge-primary">
<i class="fas fa-hdd fa-file"></i> {{ attach.filename }}
</span>
<br>
<span class="badge badge-secondary">
<i class="fas fa-hdd fa-fw"></i> {{ attachment_helper.humanFileSize(attach) }}
</span>
</h6>
{% endif %}
{% else %}
<br><br>
<h6>
<span class="badge badge-warning">
<i class="fas fa-exclamation-circle fa-fw"></i> {% trans %}attachment.file_not_found{% endtrans %}
</span>
</h6>
{% endif %}
</td> </td>
</tr> </tr>
{% endfor %} {% endfor %}