[AttachmentToNote][Attachment] Add title getter to Attachment

This commit is contained in:
Diogo Peralta Cordeiro 2021-08-12 03:43:11 +01:00
parent c12eacc758
commit 9781ddc8e0
Signed by: diogo
GPG Key ID: 18D2D35001FBFAB0
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 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; return Event::stop;
} }

View File

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

View File

@ -78,7 +78,11 @@ class VideoEncoder extends Plugin
*/ */
public function onViewAttachmentVideo(array $vars, array &$res): bool 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; 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 class="u-video" src="{{ path('attachment_view', {'id': attachment.getId()}) }}" controls poster="{{ path('attachment_thumbnail', thumbnail_parameters) }}">
</video> </video>
<figcaption><a <figcaption><a
href="{{ path('attachment_show', {'id': attachment.getId()}) }}">{{ attachment.getFilename() }}</a> href="{{ path('attachment_show', {'id': attachment.getId()}) }}">{{ attachment.getBestTitle(note) }}</a>
</figcaption> </figcaption>
</figure> </figure>
</div> </div>

View File

@ -24,6 +24,7 @@ namespace App\Entity;
use App\Core\DB\DB; use App\Core\DB\DB;
use App\Core\Entity; use App\Core\Entity;
use App\Core\GSFile; use App\Core\GSFile;
use function App\Core\I18n\_m;
use App\Core\Log; use App\Core\Log;
use App\Util\Common; use App\Util\Common;
use DateTimeInterface; use DateTimeInterface;
@ -129,6 +130,39 @@ class Attachment extends Entity
return $this->filename; 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 public function setSize(?int $size): self
{ {
$this->size = $size; $this->size = $size;

View File

@ -13,6 +13,6 @@
{% block body %} {% block body %}
<div class="content"> <div class="content">
<a href="{{ download }}"> {{ 'Download link' | trans }}</a> <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> </div>
{% endblock body %} {% endblock body %}

View File

@ -1,12 +1,12 @@
{% set thumbnail_parameters = {'id': attachment.getId(), 'w': config('thumbnail','width'), 'h': config('thumbnail','height')} %} {% set thumbnail_parameters = {'id': attachment.getId(), 'w': config('thumbnail','width'), 'h': config('thumbnail','height')} %}
{% set handled = false %} {% 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 %} {% set handled = true %}
{{ block | raw }} {{ block | raw }}
{% endfor %} {% endfor %}
{% if not handled %} {% if not handled %}
<div> <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> </div>
{% endif %} {% endif %}

View File

@ -42,7 +42,7 @@
{% if hide_attachments is not defined %} {% if hide_attachments is not defined %}
<div class="note-attachments"> <div class="note-attachments">
{% for attachment in note.getAttachments() %} {% 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 %} {% endfor %}
</div> </div>
{% endif %} {% endif %}