Skip to content

Commit

Permalink
refactor: removed deprecated method and moved layout code to template
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed Jan 12, 2025
1 parent f319250 commit eb70a4e
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 45 deletions.
42 changes: 27 additions & 15 deletions phpmyfaq/assets/templates/default/faq.twig
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
{% extends 'index.twig' %}

{% block richSnippets %}
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "FAQPage",
"mainEntity": [
{
"@type": "Question",
"name": "{{ question }}",
<script type="application/ld+json">
{
"@context": "https://schema.org",
"@type": "FAQPage",
"mainEntity": [
{
"@type": "Question",
"name": "{{ question }}",
"acceptedAnswer": {
"@type": "Answer",
"text": "{{ answer | striptags }}"
}
}
]
}
</script>
</script>
{% endblock %}

{% block content %}
Expand All @@ -26,6 +26,18 @@

<article class="pmf-faq-body pb-4 mb-4 border-bottom">{{ answer|raw }}</article>

{% if attachmentList is not empty %}
<p>{{ 'msgAttachedFiles' | translate }}:</p>
<ul class="pb-4 mb-4 border-bottom">
{% for attachment in attachmentList %}
<li>
<i class="bi bi-{{ attachment.icon }}" aria-hidden="true"></i>
<a href="{{ attachment.url }}">{{ attachment.filename|e }}</a>
</li>
{% endfor %}
</ul>
{% endif %}

<p class="d-print-none">{{ writeCommentMsg|raw }}</p>
<div id="pmf-comment-add-success"></div>
<div class="d-print-none" id="comments">{{ renderComments|raw }}</div>
Expand Down Expand Up @@ -67,12 +79,12 @@
{{ msgAddBookmark }}
</a>
</li>
<li class="list-group-item bg-transparent">
<i aria-hidden="true" class="bi bi-file-pdf"></i>
<a target="_blank" href="{{ linkToPdf }}" rel="noopener" class="text-decoration-none">
{{ msgPdf }}
</a>
</li>
<li class="list-group-item bg-transparent">
<i aria-hidden="true" class="bi bi-file-pdf"></i>
<a target="_blank" href="{{ linkToPdf }}" rel="noopener" class="text-decoration-none">
{{ msgPdf }}
</a>
</li>
{% endif %}
{% if enableSendToFriend == true %}
<li class="list-group-item bg-transparent">
Expand Down
3 changes: 2 additions & 1 deletion phpmyfaq/faq.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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'),
Expand Down
28 changes: 10 additions & 18 deletions phpmyfaq/src/phpMyFAQ/Helper/AttachmentHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
namespace phpMyFAQ\Helper;

use phpMyFAQ\Attachment\AbstractAttachment;
use phpMyFAQ\Strings;
use phpMyFAQ\Translation;

/**
* Class AttachmentHelper
Expand All @@ -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('<p>%s:</p><ul>', Translation::get('msgAttachedFiles'));

foreach ($attachmentList as $attachment) {
$html .= sprintf(
'<li><i class="bi bi-%s" aria-hidden="true"></i> <a href="%s">%s</a></li>',
$this->mapMimeTypeToIcon($attachment->getMimeType()),
$attachment->buildUrl(),
Strings::htmlentities($attachment->getFilename())
);
}

return $html . '</ul>';
return array_map(function ($attachment) {
return [
'icon' => $this->mapMimeTypeToIcon($attachment->getMimeType()),
'url' => $attachment->buildUrl(),
'filename' => $attachment->getFilename(),
];
}, $attachmentList);
}

private function mapMimeTypeToIcon(string $mimeType): string
Expand Down
2 changes: 1 addition & 1 deletion phpmyfaq/src/phpMyFAQ/Helper/CommentHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
24 changes: 14 additions & 10 deletions tests/phpMyFAQ/Helper/AttachmentHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = '<p>Attached files:</p><ul>';
$expectedHtml .= '<li><i class="bi bi-file-pdf-o" aria-hidden="true"></i> <a href="http://example.com/file.pdf">file&period;pdf</a></li>';
$expectedHtml .= '</ul>';
$expectedResult = [
[
'icon' => 'file-pdf-o',
'url' => 'https://example.com/file.pdf',
'filename' => 'file.pdf',
],
];

$this->assertEquals($expectedHtml, $result);
$this->assertEquals($expectedResult, $result);
}
}

0 comments on commit eb70a4e

Please sign in to comment.