Skip to content

Commit eb70a4e

Browse files
committed
refactor: removed deprecated method and moved layout code to template
1 parent f319250 commit eb70a4e

File tree

5 files changed

+54
-45
lines changed

5 files changed

+54
-45
lines changed

phpmyfaq/assets/templates/default/faq.twig

Lines changed: 27 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
11
{% extends 'index.twig' %}
22

33
{% block richSnippets %}
4-
<script type="application/ld+json">
5-
{
6-
"@context": "https://schema.org",
7-
"@type": "FAQPage",
8-
"mainEntity": [
9-
{
10-
"@type": "Question",
11-
"name": "{{ question }}",
4+
<script type="application/ld+json">
5+
{
6+
"@context": "https://schema.org",
7+
"@type": "FAQPage",
8+
"mainEntity": [
9+
{
10+
"@type": "Question",
11+
"name": "{{ question }}",
1212
"acceptedAnswer": {
1313
"@type": "Answer",
1414
"text": "{{ answer | striptags }}"
1515
}
1616
}
1717
]
1818
}
19-
</script>
19+
</script>
2020
{% endblock %}
2121

2222
{% block content %}
@@ -26,6 +26,18 @@
2626

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

29+
{% if attachmentList is not empty %}
30+
<p>{{ 'msgAttachedFiles' | translate }}:</p>
31+
<ul class="pb-4 mb-4 border-bottom">
32+
{% for attachment in attachmentList %}
33+
<li>
34+
<i class="bi bi-{{ attachment.icon }}" aria-hidden="true"></i>
35+
<a href="{{ attachment.url }}">{{ attachment.filename|e }}</a>
36+
</li>
37+
{% endfor %}
38+
</ul>
39+
{% endif %}
40+
2941
<p class="d-print-none">{{ writeCommentMsg|raw }}</p>
3042
<div id="pmf-comment-add-success"></div>
3143
<div class="d-print-none" id="comments">{{ renderComments|raw }}</div>
@@ -67,12 +79,12 @@
6779
{{ msgAddBookmark }}
6880
</a>
6981
</li>
70-
<li class="list-group-item bg-transparent">
71-
<i aria-hidden="true" class="bi bi-file-pdf"></i>
72-
<a target="_blank" href="{{ linkToPdf }}" rel="noopener" class="text-decoration-none">
73-
{{ msgPdf }}
74-
</a>
75-
</li>
82+
<li class="list-group-item bg-transparent">
83+
<i aria-hidden="true" class="bi bi-file-pdf"></i>
84+
<a target="_blank" href="{{ linkToPdf }}" rel="noopener" class="text-decoration-none">
85+
{{ msgPdf }}
86+
</a>
87+
</li>
7688
{% endif %}
7789
{% if enableSendToFriend == true %}
7890
<li class="list-group-item bg-transparent">

phpmyfaq/faq.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@
165165
if ($faqConfig->get('records.disableAttachments') && 'yes' == $faq->faqRecord['active']) {
166166
try {
167167
$attList = AttachmentFactory::fetchByRecordId($faqConfig, $faqId);
168-
$answer .= $attachmentHelper->renderAttachmentList($attList);
168+
$attachmentList = $attachmentHelper->getAttachmentList($attList);
169169
} catch (AttachmentException) {
170170
// handle exception
171171
}
@@ -318,6 +318,7 @@
318318
'solutionIdLink' => Link::getSystemRelativeUri() . '?solution_id=' . $faq->faqRecord['solution_id'],
319319
'question' => $question,
320320
'answer' => $answer,
321+
'attachmentList' => $attachmentList,
321322
'faqDate' => $date->format($faq->faqRecord['date']),
322323
'faqAuthor' => Strings::htmlentities($author),
323324
'msgPdf' => Translation::get('msgPDF'),

phpmyfaq/src/phpMyFAQ/Helper/AttachmentHelper.php

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@
1818
namespace phpMyFAQ\Helper;
1919

2020
use phpMyFAQ\Attachment\AbstractAttachment;
21-
use phpMyFAQ\Strings;
22-
use phpMyFAQ\Translation;
2321

2422
/**
2523
* Class AttachmentHelper
@@ -28,29 +26,23 @@
2826
class AttachmentHelper
2927
{
3028
/**
31-
* Returns an HTML list of attached files.
29+
* Returns a list of attached files.
3230
*
3331
* @param AbstractAttachment[] $attachmentList
34-
* @deprecated Rewrite this method to use Twig, will be removed in v4.1
3532
*/
36-
public function renderAttachmentList(array $attachmentList): string
33+
public function getAttachmentList(array $attachmentList): array
3734
{
3835
if ($attachmentList === []) {
39-
return '';
36+
return [];
4037
}
4138

42-
$html = sprintf('<p>%s:</p><ul>', Translation::get('msgAttachedFiles'));
43-
44-
foreach ($attachmentList as $attachment) {
45-
$html .= sprintf(
46-
'<li><i class="bi bi-%s" aria-hidden="true"></i> <a href="%s">%s</a></li>',
47-
$this->mapMimeTypeToIcon($attachment->getMimeType()),
48-
$attachment->buildUrl(),
49-
Strings::htmlentities($attachment->getFilename())
50-
);
51-
}
52-
53-
return $html . '</ul>';
39+
return array_map(function ($attachment) {
40+
return [
41+
'icon' => $this->mapMimeTypeToIcon($attachment->getMimeType()),
42+
'url' => $attachment->buildUrl(),
43+
'filename' => $attachment->getFilename(),
44+
];
45+
}, $attachmentList);
5446
}
5547

5648
private function mapMimeTypeToIcon(string $mimeType): string

phpmyfaq/src/phpMyFAQ/Helper/CommentHelper.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class CommentHelper extends AbstractHelper
3131
* Returns all user comments (HTML formatted) from a record by type.
3232
*
3333
* @param Comment[] $comments
34-
* @deprecated Rewrite this method to use Twig
34+
* @deprecated Rewrite this method to use Twig, will be removed in v4.1
3535
*/
3636
public function getComments(array $comments): string
3737
{

tests/phpMyFAQ/Helper/AttachmentHelperTest.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,28 +22,32 @@ protected function setUp(): void
2222
$this->attachmentHelper = new AttachmentHelper();
2323
}
2424

25-
public function testRenderAttachmentListEmpty()
25+
public function testGetAttachmentListEmpty()
2626
{
2727
$attachmentList = [];
28-
$result = $this->attachmentHelper->renderAttachmentList($attachmentList);
29-
$this->assertEquals('', $result);
28+
$result = $this->attachmentHelper->getAttachmentList($attachmentList);
29+
$this->assertEquals([], $result);
3030
}
3131

32-
public function testRenderAttachmentListWithAttachments()
32+
public function testGetAttachmentListWithAttachments()
3333
{
3434
$attachmentMock = $this->createMock(AbstractAttachment::class);
3535
$attachmentMock->method('getMimeType')->willReturn('application/pdf');
36-
$attachmentMock->method('buildUrl')->willReturn('http://example.com/file.pdf');
36+
$attachmentMock->method('buildUrl')->willReturn('https://example.com/file.pdf');
3737
$attachmentMock->method('getFilename')->willReturn('file.pdf');
3838

3939
$attachmentList = [$attachmentMock];
4040

41-
$result = $this->attachmentHelper->renderAttachmentList($attachmentList);
41+
$result = $this->attachmentHelper->getAttachmentList($attachmentList);
4242

43-
$expectedHtml = '<p>Attached files:</p><ul>';
44-
$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>';
45-
$expectedHtml .= '</ul>';
43+
$expectedResult = [
44+
[
45+
'icon' => 'file-pdf-o',
46+
'url' => 'https://example.com/file.pdf',
47+
'filename' => 'file.pdf',
48+
],
49+
];
4650

47-
$this->assertEquals($expectedHtml, $result);
51+
$this->assertEquals($expectedResult, $result);
4852
}
4953
}

0 commit comments

Comments
 (0)