[AttachmentShowRelated] Move Attachment related to plugin

This commit is contained in:
Diogo Peralta Cordeiro 2021-04-30 01:51:59 +01:00
parent b196af5f36
commit c2f6665cce
6 changed files with 70 additions and 18 deletions

View File

@ -0,0 +1,45 @@
<?php
// {{{ License
// This file is part of GNU social - https://www.gnu.org/software/social
//
// GNU social is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// GNU social is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with GNU social. If not, see <http://www.gnu.org/licenses/>.
// }}}
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;
}
}

View File

@ -0,0 +1,6 @@
<div id="related-notes" style="margin: var(--unit-size);">
{{ 'Notes where this attachment appears' | trans }}
{% for note in related_notes %}
{% include '/note/view.html.twig' with {'note' : note, 'hide_attachments': true} only %}
{% endfor %}
</div>

View File

@ -0,0 +1,9 @@
<div id="related-tags" style="margin: var(--unit-size);">
{{ 'Tags for this attachment' | trans }}
<br/>
{% for tag in related_tags %}
<i>#{{ tag['tag'] }}</i>
{% else %}
{{ 'No tags' | trans }}
{% endfor %}
</div>

View File

@ -40,7 +40,7 @@ class Favourite extends Plugin
* HTML rendering event that adds the favourite form as a note * HTML rendering event that adds the favourite form as a note
* action, if a user is logged in * 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) { if (($user = Common::user()) == null) {
return Event::next; return Event::next;
@ -75,15 +75,16 @@ class Favourite extends Plugin
return Event::next; return Event::next;
} }
public function onInsertLeftPanelLink(string $user_nickname, &$res) public function onInsertLeftPanelLink(string $user_nickname, &$res): bool
{ {
$res[] = Formatting::twigRender(<<<END $res[] = Formatting::twigRender(<<<END
<a href="{{ path("favourites", {'nickname' : user_nickname}) }}" class='hover-effect {{ active("favourites") }}'>Favourites</a> <a href="{{ path("favourites", {'nickname' : user_nickname}) }}" class='hover-effect {{ active("favourites") }}'>Favourites</a>
<a href="{{ path("reverse_favourites", {'nickname' : user_nickname}) }}" class='hover-effect {{ active("reverse_favourites") }}'>Reverse Favs</a> <a href="{{ path("reverse_favourites", {'nickname' : user_nickname}) }}" class='hover-effect {{ active("reverse_favourites") }}'>Reverse Favs</a>
END, ['user_nickname' => $user_nickname]); 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('favourites', '/favourites/{nickname<' . Nickname::DISPLAY_FMT . '>}', [Controller\Favourite::class, 'favourites']);
$r->connect('reverse_favourites', '/reverse_favourites/{nickname<' . Nickname::DISPLAY_FMT . '>}', [Controller\Favourite::class, 'reverseFavourites']); $r->connect('reverse_favourites', '/reverse_favourites/{nickname<' . Nickname::DISPLAY_FMT . '>}', [Controller\Favourite::class, 'reverseFavourites']);

View File

@ -59,16 +59,11 @@ class Attachment extends Controller
$attachment = DB::findOneBy('attachment', ['id' => $id]); $attachment = DB::findOneBy('attachment', ['id' => $id]);
return $this->attachment($id, function ($res) use ($id, $attachment) { return $this->attachment($id, function ($res) use ($id, $attachment) {
return [ return [
'_template' => 'attachments/show.html.twig', '_template' => 'attachments/show.html.twig',
'title' => $res['title'], 'title' => $res['title'],
'download' => Router::url('attachment_download', ['id' => $id]), 'download' => Router::url('attachment_download', ['id' => $id]),
'attachment' => $attachment, 'attachment' => $attachment,
'related_notes' => DB::dql('select n from attachment_to_note an ' . 'right_panel_vars' => ['attachment_id' => $id],
'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) { } catch (NotFoundException) {

View File

@ -18,8 +18,4 @@
{% include '/attachments/view.html.twig' with {'attachment': attachment} only %} {% include '/attachments/view.html.twig' with {'attachment': attachment} only %}
</div> </div>
</div> </div>
{% endblock body %} {% endblock body %}
{% block right %}
{% endblock right %}