[AttachmentToNote][Attachment] Add title getter to Attachment

This commit is contained in:
Diogo Peralta Cordeiro 2021-08-12 03:43:11 +01:00 committed by Hugo Sales
parent 5fd91bf3a2
commit d076781c74
Signed by: someonewithpc
GPG Key ID: 7D0C7EAFC9D835A0
8 changed files with 50 additions and 8 deletions

View File

@ -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;
}

View File

@ -2,6 +2,6 @@
<img class="u-photo" src="{{ path('attachment_thumbnail', thumbnail_parameters) }}"
alt="{{ attachment.getFilename() }}">
<figcaption><a
href="{{ path('attachment_show', {'id': attachment.getId()}) }}">{{ attachment.getFilename() }}</a>
href="{{ path('attachment_show', {'id': attachment.getId()}) }}">{{ attachment.getBestTitle(note) }}</a>
</figcaption>
</figure>

View File

@ -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;
}

View File

@ -3,7 +3,7 @@
<video class="u-video" src="{{ path('attachment_view', {'id': attachment.getId()}) }}" controls poster="{{ path('attachment_thumbnail', thumbnail_parameters) }}">
</video>
<figcaption><a
href="{{ path('attachment_show', {'id': attachment.getId()}) }}">{{ attachment.getFilename() }}</a>
href="{{ path('attachment_show', {'id': attachment.getId()}) }}">{{ attachment.getBestTitle(note) }}</a>
</figcaption>
</figure>
</div>

View File

@ -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;

View File

@ -13,6 +13,6 @@
{% block body %}
<div class="content">
<a href="{{ download }}"> {{ 'Download link' | trans }}</a>
{% include '/attachments/view.html.twig' with {'attachment': attachment} only %}
{% include '/attachments/view.html.twig' with {'attachment': attachment, 'note': null} only %}
</div>
{% endblock body %}

View File

@ -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 %}
<div>
<i> <a href="{{ path('attachment_show', {'id': attachment.getId()}) }}">{{ attachment.getFilename() }}</a> </i>
<i> <a href="{{ path('attachment_show', {'id': attachment.getId()}) }}">{{ attachment.getBestTitle(note) }}</a> </i>
</div>
{% endif %}

View File

@ -42,7 +42,7 @@
{% if hide_attachments is not defined %}
<div class="note-attachments">
{% 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 %}
</div>
{% endif %}