From d076781c7434f76423dc51fc883c0c6d904528dd Mon Sep 17 00:00:00 2001 From: Diogo Peralta Cordeiro Date: Thu, 12 Aug 2021 03:43:11 +0100 Subject: [PATCH] [AttachmentToNote][Attachment] Add title getter to Attachment --- plugins/ImageEncoder/ImageEncoder.php | 6 +++- .../imageEncoder/imageEncoderView.html.twig | 2 +- plugins/VideoEncoder/VideoEncoder.php | 6 +++- .../videoEncoder/videoEncoderView.html.twig | 2 +- src/Entity/Attachment.php | 34 +++++++++++++++++++ templates/attachments/show.html.twig | 2 +- templates/attachments/view.html.twig | 4 +-- templates/note/view.html.twig | 2 +- 8 files changed, 50 insertions(+), 8 deletions(-) diff --git a/plugins/ImageEncoder/ImageEncoder.php b/plugins/ImageEncoder/ImageEncoder.php index 6ce39cbc3b..0866204f65 100644 --- a/plugins/ImageEncoder/ImageEncoder.php +++ b/plugins/ImageEncoder/ImageEncoder.php @@ -126,7 +126,11 @@ class ImageEncoder extends Plugin */ public function onViewAttachmentImage(array $vars, array &$res): bool { - $res[] = Formatting::twigRenderFile('imageEncoder/imageEncoderView.html.twig', ['attachment' => $vars['attachment'], 'thumbnail_parameters' => $vars['thumbnail_parameters']]); + $res[] = Formatting::twigRenderFile('imageEncoder/imageEncoderView.html.twig', + ['attachment' => $vars['attachment'], + 'thumbnail_parameters' => $vars['thumbnail_parameters'], + 'note' => $vars['note'], + ]); return Event::stop; } diff --git a/plugins/ImageEncoder/templates/imageEncoder/imageEncoderView.html.twig b/plugins/ImageEncoder/templates/imageEncoder/imageEncoderView.html.twig index e26773cbc2..13bf76f3e1 100644 --- a/plugins/ImageEncoder/templates/imageEncoder/imageEncoderView.html.twig +++ b/plugins/ImageEncoder/templates/imageEncoder/imageEncoderView.html.twig @@ -2,6 +2,6 @@ {{ attachment.getFilename() }}
{{ attachment.getFilename() }} + href="{{ path('attachment_show', {'id': attachment.getId()}) }}">{{ attachment.getBestTitle(note) }}
diff --git a/plugins/VideoEncoder/VideoEncoder.php b/plugins/VideoEncoder/VideoEncoder.php index e7ff9a9b29..c33392a636 100644 --- a/plugins/VideoEncoder/VideoEncoder.php +++ b/plugins/VideoEncoder/VideoEncoder.php @@ -78,7 +78,11 @@ class VideoEncoder extends Plugin */ public function onViewAttachmentVideo(array $vars, array &$res): bool { - $res[] = Formatting::twigRenderFile('videoEncoder/videoEncoderView.html.twig', ['attachment' => $vars['attachment'], 'thumbnail_parameters' => $vars['thumbnail_parameters']]); + $res[] = Formatting::twigRenderFile('videoEncoder/videoEncoderView.html.twig', + ['attachment' => $vars['attachment'], + 'thumbnail_parameters' => $vars['thumbnail_parameters'], + 'note' => $vars['note'], + ]); return Event::stop; } diff --git a/plugins/VideoEncoder/templates/videoEncoder/videoEncoderView.html.twig b/plugins/VideoEncoder/templates/videoEncoder/videoEncoderView.html.twig index ca156c4660..0525643ab9 100644 --- a/plugins/VideoEncoder/templates/videoEncoder/videoEncoderView.html.twig +++ b/plugins/VideoEncoder/templates/videoEncoder/videoEncoderView.html.twig @@ -3,7 +3,7 @@
{{ attachment.getFilename() }} + href="{{ path('attachment_show', {'id': attachment.getId()}) }}">{{ attachment.getBestTitle(note) }}
\ No newline at end of file diff --git a/src/Entity/Attachment.php b/src/Entity/Attachment.php index bf8425541f..17d5a3f41f 100644 --- a/src/Entity/Attachment.php +++ b/src/Entity/Attachment.php @@ -24,6 +24,7 @@ namespace App\Entity; use App\Core\DB\DB; use App\Core\Entity; use App\Core\GSFile; +use function App\Core\I18n\_m; use App\Core\Log; use App\Util\Common; use DateTimeInterface; @@ -129,6 +130,39 @@ class Attachment extends Entity return $this->filename; } + /** + * TODO: Maybe this isn't the best way of handling titles + * + * @param null|Note $note + * + * @throws \App\Util\Exception\DuplicateFoundException + * @throws \App\Util\Exception\NotFoundException + * @throws \App\Util\Exception\ServerException + * + * @return string + */ + public function getBestTitle(?Note $note = null): string + { + // If we have a note, then the best title is the title itself + if (!is_null(($note))) { + $attachment_to_note = DB::findOneBy('attachment_to_note', [ + 'attachment_id' => $this->getId(), + 'note_id' => $note->getId(), + ]); + if (!is_null($attachment_to_note->getTitle())) { + return $attachment_to_note->getTitle(); + } + } + // Else + if (!is_null($filename = $this->getFilename())) { + // A filename would do just as well + return $filename; + } else { + // Welp + return _m('Untitled Attachment.'); + } + } + public function setSize(?int $size): self { $this->size = $size; diff --git a/templates/attachments/show.html.twig b/templates/attachments/show.html.twig index 0ef4d35f95..f341847619 100644 --- a/templates/attachments/show.html.twig +++ b/templates/attachments/show.html.twig @@ -13,6 +13,6 @@ {% block body %}
{{ 'Download link' | trans }} - {% include '/attachments/view.html.twig' with {'attachment': attachment} only %} + {% include '/attachments/view.html.twig' with {'attachment': attachment, 'note': null} only %}
{% endblock body %} \ No newline at end of file diff --git a/templates/attachments/view.html.twig b/templates/attachments/view.html.twig index c444ebb7a3..8a21bf1055 100644 --- a/templates/attachments/view.html.twig +++ b/templates/attachments/view.html.twig @@ -1,12 +1,12 @@ {% set thumbnail_parameters = {'id': attachment.getId(), 'w': config('thumbnail','width'), 'h': config('thumbnail','height')} %} {% set handled = false %} -{% for block in handle_event('ViewAttachment' ~ attachment.getMimetypeMajor() | capitalize , {'attachment': attachment, 'thumbnail_parameters': thumbnail_parameters}) %} +{% for block in handle_event('ViewAttachment' ~ attachment.getMimetypeMajor() | capitalize , {'attachment': attachment, 'thumbnail_parameters': thumbnail_parameters, 'note': note}) %} {% set handled = true %} {{ block | raw }} {% endfor %} {% if not handled %}
- {{ attachment.getFilename() }} + {{ attachment.getBestTitle(note) }}
{% endif %} diff --git a/templates/note/view.html.twig b/templates/note/view.html.twig index 31636645a9..4893620d28 100644 --- a/templates/note/view.html.twig +++ b/templates/note/view.html.twig @@ -42,7 +42,7 @@ {% if hide_attachments is not defined %}
{% for attachment in note.getAttachments() %} - {% include '/attachments/view.html.twig' with {'attachment': attachment} %} + {% include '/attachments/view.html.twig' with {'attachment': attachment, 'note': note} only%} {% endfor %}
{% endif %}