From 436528172c394c791ec571af2e9fc38c513b6964 Mon Sep 17 00:00:00 2001 From: Hugo Sales Date: Wed, 28 Apr 2021 21:25:35 +0000 Subject: [PATCH] [ATTACHMENTS] Add controller and templates for the attachment show page, which shows extra info about an attachment, such as related notes and tags --- src/Controller/Attachment.php | 42 +++++++++++++++++++++----- templates/attachments/show.html.twig | 32 ++++++++++++++------ templates/attachments/view.html.twig | 25 ++++++++++++++++ templates/base.html.twig | 21 ++++++++----- templates/note/view.html.twig | 44 +++++++--------------------- 5 files changed, 107 insertions(+), 57 deletions(-) create mode 100644 templates/attachments/view.html.twig diff --git a/src/Controller/Attachment.php b/src/Controller/Attachment.php index 3ace61237d..821b307328 100644 --- a/src/Controller/Attachment.php +++ b/src/Controller/Attachment.php @@ -25,6 +25,7 @@ use App\Core\Controller; use App\Core\DB\DB; use App\Core\Event; use App\Core\GSFile; +use App\Core\Router\Router; use App\Entity\AttachmentThumbnail; use App\Util\Common; use App\Util\Exception\ClientException; @@ -36,29 +37,56 @@ use Symfony\Component\HttpFoundation\Response; class Attachment extends Controller { - public function attachment_show(Request $request, int $id) + private function attachment(int $id, callable $handle) { - // If no one else claims this attachment, use the default representation if (Event::handle('AttachmentFileInfo', [$id, &$res]) != Event::stop) { + // If no one else claims this attachment, use the default representation $res = GSFile::getAttachmentFileInfo($id); } if (!empty($res)) { - return GSFile::sendFile($res['filepath'], $res['mimetype'], $res['title'], HeaderUtils::DISPOSITION_INLINE); + return $handle($res); } else { throw new ClientException('No such attachment', 404); } } + /** + * The page where the attachment and it's info is shown + */ + public function attachment_show(Request $request, int $id) + { + try { + $attachment = DB::findOneBy('attachment', ['id' => $id]); + return $this->attachment($id, function ($res) use ($id, $attachment) { + return [ + '_template' => 'attachments/show.html.twig', + 'title' => $res['title'], + 'download' => Router::url('attachment_download', ['id' => $id]), + 'attachment' => $attachment, + 'related_notes' => DB::dql('select n from attachment_to_note an ' . + 'join note n with n.id = an.note_id ' . + 'where an.attachment_id = :attachment_id', ['attachment_id' => $id]), + 'related_tags' => DB::dql('select distinct t.tag ' . + 'from attachment_to_note an join note_tag t with an.note_id = t.note_id ' . + 'where an.attachment_id = :attachment_id', ['attachment_id' => $id]), + ]; + }); + } catch (NotFoundException) { + throw new ClientException('No such attachment', 404); + } + } + + /** + * Display the attachment inline + */ public function attachment_view(Request $request, int $id) { - $res = GSFile::getAttachmentFileInfo($id); - return GSFile::sendFile($res['filepath'], $res['mimetype'], $res['title'], HeaderUtils::DISPOSITION_INLINE); + return $this->attachment($id, fn (array $res) => GSFile::sendFile($res['filepath'], $res['mimetype'], $res['title'], HeaderUtils::DISPOSITION_INLINE)); } public function attachment_download(Request $request, int $id) { - $res = GSFile::getAttachmentFileInfo($id); - return GSFile::sendFile($res['filepath'], $res['mimetype'], $res['title'], HeaderUtils::DISPOSITION_ATTACHMENT); + return $this->attachment($id, fn (array $res) => GSFile::sendFile($res['filepath'], $res['mimetype'], $res['title'], HeaderUtils::DISPOSITION_ATTACHMENT)); } /** diff --git a/templates/attachments/show.html.twig b/templates/attachments/show.html.twig index 82d268cd32..ae564503e2 100644 --- a/templates/attachments/show.html.twig +++ b/templates/attachments/show.html.twig @@ -10,16 +10,30 @@ media="screen and (max-width: 750px)"> {% endblock %} -{# {% block header %} #} -{# {{ parent() }} #} -{# {% endblock %} #} - -{# {% block left %} #} -{# {{ parent() }} #} -{# {% endblock %} #} - {% block body %}
- yooo +
+

{{ title | escape }}

+ {{ 'Download link' | trans }} + {% include '/attachments/view.html.twig' with {'attachment': attachment} only %} +
{% endblock body %} + +{% block right %} + + +{% endblock right %} diff --git a/templates/attachments/view.html.twig b/templates/attachments/view.html.twig new file mode 100644 index 0000000000..ac46a3edef --- /dev/null +++ b/templates/attachments/view.html.twig @@ -0,0 +1,25 @@ +{% set thumbnail_parameters = {'id': attachment.getId(), 'w': config('thumbnail','width'), 'h': config('thumbnail','height')} %} +{% if attachment.mimetype starts with 'image/' %} +
+
+ {{ attachment.getTitle() }} +
{{ attachment.getTitle() }}
+
+
+{% elseif attachment.mimetype starts with 'video/' %} +
+ +
+{% else %} + {% for show in handle_event('ShowAttachment', attachment) %} +
+ {{ show | raw }} +
+ {% else %} +
+ {{ attachment.getTitle() }} +
+ {% endfor %} +{% endif %} diff --git a/templates/base.html.twig b/templates/base.html.twig index a3be63ff97..8c6e626a98 100644 --- a/templates/base.html.twig +++ b/templates/base.html.twig @@ -40,15 +40,19 @@
- -
- -
-
-
-
-
+ +
+ +
+
+
+
+
+ {% block right %} + {% endblock right %} +
+ {% endblock header%} @@ -59,3 +63,4 @@ + diff --git a/templates/note/view.html.twig b/templates/note/view.html.twig index 040e7cf029..a7726bb49d 100644 --- a/templates/note/view.html.twig +++ b/templates/note/view.html.twig @@ -21,42 +21,20 @@ {% include '/'~ other.name ~ '/view.html.twig' with {'vars': other.vars} only %} {% endfor %} -
- {% for attachment in note.getAttachments() %} - {% set thumbnail_parameters = {'id': attachment.getId(), 'w': config('thumbnail','width'), 'h': config('thumbnail','height')} %} - {% if attachment.mimetype starts with 'image/' %} -
-
- {{ attachment.getTitle() }} -
{{ attachment.getTitle() }}
-
-
- {% elseif attachment.mimetype starts with 'video/' %} -
- -
- {% else %} - {% for show in handle_event('ShowAttachment', attachment) %} -
- {{ show | raw }} -
- {% else %} -
- {{ attachment.getTitle() }} -
+ {% if hide_attachments is not defined and hide_attachments != true %} +
+ {% for attachment in note.getAttachments() %} + {% include '/attachments/view.html.twig' with {'attachment': attachment} %} {% endfor %} - {% endif %} - {% endfor %} -
+
+ {% endif %}
- {% if have_user %} - {#{% for act in get_note_actions(note) %}#} - {#{{ form(act) }}#} - {#{% endfor %}#} - {% endif %} + {# {% if have_user %} #} + {# {\#{% for act in get_note_actions(note) %}#\} #} + {# {\#{{ form(act) }}#\} #} + {# {\#{% endfor %}#\} #} + {# {% endif %} #}
{% if replies is defined %}