diff --git a/phpmyfaq/assets/templates/default/faq.twig b/phpmyfaq/assets/templates/default/faq.twig index 5babaeeb21..689c8c6863 100644 --- a/phpmyfaq/assets/templates/default/faq.twig +++ b/phpmyfaq/assets/templates/default/faq.twig @@ -1,14 +1,14 @@ {% extends 'index.twig' %} {% block richSnippets %} - + {% endblock %} {% block content %} @@ -26,6 +26,18 @@
{{ answer|raw }}
+ {% if attachmentList is not empty %} +

{{ 'msgAttachedFiles' | translate }}:

+ + {% endif %} +

{{ writeCommentMsg|raw }}

{{ renderComments|raw }}
@@ -67,12 +79,12 @@ {{ msgAddBookmark }} -
  • - - - {{ msgPdf }} - -
  • +
  • + + + {{ msgPdf }} + +
  • {% endif %} {% if enableSendToFriend == true %}
  • diff --git a/phpmyfaq/faq.php b/phpmyfaq/faq.php index 0dd3ca7a5b..881d9d572f 100644 --- a/phpmyfaq/faq.php +++ b/phpmyfaq/faq.php @@ -165,7 +165,7 @@ if ($faqConfig->get('records.disableAttachments') && 'yes' == $faq->faqRecord['active']) { try { $attList = AttachmentFactory::fetchByRecordId($faqConfig, $faqId); - $answer .= $attachmentHelper->renderAttachmentList($attList); + $attachmentList = $attachmentHelper->getAttachmentList($attList); } catch (AttachmentException) { // handle exception } @@ -318,6 +318,7 @@ 'solutionIdLink' => Link::getSystemRelativeUri() . '?solution_id=' . $faq->faqRecord['solution_id'], 'question' => $question, 'answer' => $answer, + 'attachmentList' => $attachmentList, 'faqDate' => $date->format($faq->faqRecord['date']), 'faqAuthor' => Strings::htmlentities($author), 'msgPdf' => Translation::get('msgPDF'), diff --git a/phpmyfaq/src/phpMyFAQ/Helper/AttachmentHelper.php b/phpmyfaq/src/phpMyFAQ/Helper/AttachmentHelper.php index 2058b720b1..ed6170d4f3 100644 --- a/phpmyfaq/src/phpMyFAQ/Helper/AttachmentHelper.php +++ b/phpmyfaq/src/phpMyFAQ/Helper/AttachmentHelper.php @@ -18,8 +18,6 @@ namespace phpMyFAQ\Helper; use phpMyFAQ\Attachment\AbstractAttachment; -use phpMyFAQ\Strings; -use phpMyFAQ\Translation; /** * Class AttachmentHelper @@ -28,29 +26,23 @@ class AttachmentHelper { /** - * Returns an HTML list of attached files. + * Returns a list of attached files. * * @param AbstractAttachment[] $attachmentList - * @deprecated Rewrite this method to use Twig, will be removed in v4.1 */ - public function renderAttachmentList(array $attachmentList): string + public function getAttachmentList(array $attachmentList): array { if ($attachmentList === []) { - return ''; + return []; } - $html = sprintf('

    %s:

    '; + return array_map(function ($attachment) { + return [ + 'icon' => $this->mapMimeTypeToIcon($attachment->getMimeType()), + 'url' => $attachment->buildUrl(), + 'filename' => $attachment->getFilename(), + ]; + }, $attachmentList); } private function mapMimeTypeToIcon(string $mimeType): string diff --git a/phpmyfaq/src/phpMyFAQ/Helper/CommentHelper.php b/phpmyfaq/src/phpMyFAQ/Helper/CommentHelper.php index e8b209932b..f96513dc14 100644 --- a/phpmyfaq/src/phpMyFAQ/Helper/CommentHelper.php +++ b/phpmyfaq/src/phpMyFAQ/Helper/CommentHelper.php @@ -31,7 +31,7 @@ class CommentHelper extends AbstractHelper * Returns all user comments (HTML formatted) from a record by type. * * @param Comment[] $comments - * @deprecated Rewrite this method to use Twig + * @deprecated Rewrite this method to use Twig, will be removed in v4.1 */ public function getComments(array $comments): string { diff --git a/tests/phpMyFAQ/Helper/AttachmentHelperTest.php b/tests/phpMyFAQ/Helper/AttachmentHelperTest.php index 30498955fe..2c64bc1916 100644 --- a/tests/phpMyFAQ/Helper/AttachmentHelperTest.php +++ b/tests/phpMyFAQ/Helper/AttachmentHelperTest.php @@ -22,28 +22,32 @@ protected function setUp(): void $this->attachmentHelper = new AttachmentHelper(); } - public function testRenderAttachmentListEmpty() + public function testGetAttachmentListEmpty() { $attachmentList = []; - $result = $this->attachmentHelper->renderAttachmentList($attachmentList); - $this->assertEquals('', $result); + $result = $this->attachmentHelper->getAttachmentList($attachmentList); + $this->assertEquals([], $result); } - public function testRenderAttachmentListWithAttachments() + public function testGetAttachmentListWithAttachments() { $attachmentMock = $this->createMock(AbstractAttachment::class); $attachmentMock->method('getMimeType')->willReturn('application/pdf'); - $attachmentMock->method('buildUrl')->willReturn('http://example.com/file.pdf'); + $attachmentMock->method('buildUrl')->willReturn('https://example.com/file.pdf'); $attachmentMock->method('getFilename')->willReturn('file.pdf'); $attachmentList = [$attachmentMock]; - $result = $this->attachmentHelper->renderAttachmentList($attachmentList); + $result = $this->attachmentHelper->getAttachmentList($attachmentList); - $expectedHtml = '

    Attached files:

    '; + $expectedResult = [ + [ + 'icon' => 'file-pdf-o', + 'url' => 'https://example.com/file.pdf', + 'filename' => 'file.pdf', + ], + ]; - $this->assertEquals($expectedHtml, $result); + $this->assertEquals($expectedResult, $result); } }