From 8cb64ede7f30a908d0cddaa962755cb6f89c4e01 Mon Sep 17 00:00:00 2001 From: Diogo Peralta Cordeiro Date: Fri, 30 Apr 2021 01:51:59 +0100 Subject: [PATCH] [AttachmentShowRelated] Move Attachment related to plugin --- .../AttachmentShowRelated.php | 45 +++++++++++++++++++ .../AttachmentRelatedNotes.html.twig | 6 +++ .../templates/AttachmentRelatedTags.html.twig | 9 ++++ plugins/Favourite/Favourite.php | 7 +-- src/Controller/Attachment.php | 15 +++---- templates/attachments/show.html.twig | 6 +-- 6 files changed, 70 insertions(+), 18 deletions(-) create mode 100644 plugins/AttachmentShowRelated/AttachmentShowRelated.php create mode 100644 plugins/AttachmentShowRelated/templates/AttachmentRelatedNotes.html.twig create mode 100644 plugins/AttachmentShowRelated/templates/AttachmentRelatedTags.html.twig diff --git a/plugins/AttachmentShowRelated/AttachmentShowRelated.php b/plugins/AttachmentShowRelated/AttachmentShowRelated.php new file mode 100644 index 0000000000..5959e4f444 --- /dev/null +++ b/plugins/AttachmentShowRelated/AttachmentShowRelated.php @@ -0,0 +1,45 @@ +. + +// }}} + +namespace Plugin\AttachmentShowRelated; + +use App\Core\DB\DB; +use App\Core\Event; +use App\Core\Modules\Plugin; +use App\Util\Formatting; + +class AttachmentShowRelated extends Plugin +{ + public function onAppendRightPanelBlock($vars, &$res): bool + { + if ($vars['path'] == 'attachment_show') { + $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' => $vars['vars']['attachment_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' => $vars['vars']['attachment_id']]); + $res[] = Formatting::twigRender(file_get_contents(__DIR__ . '/templates/AttachmentRelatedNotes.html.twig'), ['related_notes' => $related_notes]); + $res[] = Formatting::twigRender(file_get_contents(__DIR__ . '/templates/AttachmentRelatedTags.html.twig'), ['related_tags' => $related_tags]); + } + return Event::next; + } +} \ No newline at end of file diff --git a/plugins/AttachmentShowRelated/templates/AttachmentRelatedNotes.html.twig b/plugins/AttachmentShowRelated/templates/AttachmentRelatedNotes.html.twig new file mode 100644 index 0000000000..3a9df248f8 --- /dev/null +++ b/plugins/AttachmentShowRelated/templates/AttachmentRelatedNotes.html.twig @@ -0,0 +1,6 @@ + diff --git a/plugins/AttachmentShowRelated/templates/AttachmentRelatedTags.html.twig b/plugins/AttachmentShowRelated/templates/AttachmentRelatedTags.html.twig new file mode 100644 index 0000000000..881dc2cdec --- /dev/null +++ b/plugins/AttachmentShowRelated/templates/AttachmentRelatedTags.html.twig @@ -0,0 +1,9 @@ + \ No newline at end of file diff --git a/plugins/Favourite/Favourite.php b/plugins/Favourite/Favourite.php index 2921debc6b..7f542f2f22 100644 --- a/plugins/Favourite/Favourite.php +++ b/plugins/Favourite/Favourite.php @@ -40,7 +40,7 @@ class Favourite extends Plugin * HTML rendering event that adds the favourite form as a note * action, if a user is logged in */ - public function onAddNoteActions(Request $request, Note $note, array &$actions) + public function onAddNoteActions(Request $request, Note $note, array &$actions): bool { if (($user = Common::user()) == null) { return Event::next; @@ -75,15 +75,16 @@ class Favourite extends Plugin return Event::next; } - public function onInsertLeftPanelLink(string $user_nickname, &$res) + public function onInsertLeftPanelLink(string $user_nickname, &$res): bool { $res[] = Formatting::twigRender(<<Favourites Reverse Favs END, ['user_nickname' => $user_nickname]); + return Event::next; } - public function onAddRoute(RouteLoader $r) + public function onAddRoute(RouteLoader $r): bool { $r->connect('favourites', '/favourites/{nickname<' . Nickname::DISPLAY_FMT . '>}', [Controller\Favourite::class, 'favourites']); $r->connect('reverse_favourites', '/reverse_favourites/{nickname<' . Nickname::DISPLAY_FMT . '>}', [Controller\Favourite::class, 'reverseFavourites']); diff --git a/src/Controller/Attachment.php b/src/Controller/Attachment.php index ea6fc3b427..88d17e612e 100644 --- a/src/Controller/Attachment.php +++ b/src/Controller/Attachment.php @@ -59,16 +59,11 @@ class Attachment extends Controller $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]), + '_template' => 'attachments/show.html.twig', + 'title' => $res['title'], + 'download' => Router::url('attachment_download', ['id' => $id]), + 'attachment' => $attachment, + 'right_panel_vars' => ['attachment_id' => $id], ]; }); } catch (NotFoundException) { diff --git a/templates/attachments/show.html.twig b/templates/attachments/show.html.twig index fdb5d498fa..5d1ae63390 100644 --- a/templates/attachments/show.html.twig +++ b/templates/attachments/show.html.twig @@ -18,8 +18,4 @@ {% include '/attachments/view.html.twig' with {'attachment': attachment} only %} -{% endblock body %} - -{% block right %} - -{% endblock right %} +{% endblock body %} \ No newline at end of file