[AttachmentToNote][Attachment] Add title getter to Attachment

This commit is contained in:
2021-08-12 03:43:11 +01:00
committed by Hugo Sales
parent 5fd91bf3a2
commit d076781c74
8 changed files with 50 additions and 8 deletions

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;