Skip to content

Commit 218f5e7

Browse files
committed
feat(export): added links to attachments in PDFs, closes #701
1 parent 850b8e9 commit 218f5e7

File tree

3 files changed

+33
-6
lines changed

3 files changed

+33
-6
lines changed

phpmyfaq/faq.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,9 @@
295295
// We need some Links from social networks
296296
$faqServices = new Services($faqConfig);
297297
$faqServices->setCategoryId($cat);
298-
$faqServices->setFaqId($id);
298+
$faqServices->setFaqId($faqId);
299299
$faqServices->setLanguage($lang);
300-
$faqServices->setQuestion($faq->getQuestion($id));
300+
$faqServices->setQuestion($faq->getQuestion($faqId));
301301

302302
// Check if category ID and FAQ ID are linked together
303303
if (!$category->categoryHasLinkToFaq($faqId, $currentCategory)) {

phpmyfaq/pdf.php

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,13 @@
1919
* @since 2003-02-12
2020
*/
2121

22+
use phpMyFAQ\Attachment\AttachmentException;
23+
use phpMyFAQ\Attachment\AttachmentFactory;
2224
use phpMyFAQ\Category;
2325
use phpMyFAQ\Enums\PermissionType;
2426
use phpMyFAQ\Export\Pdf;
2527
use phpMyFAQ\Filter;
28+
use phpMyFAQ\Helper\AttachmentHelper;
2629
use phpMyFAQ\Language;
2730
use phpMyFAQ\Strings;
2831
use phpMyFAQ\Tags;
@@ -100,7 +103,7 @@
100103

101104
$request = Request::createFromGlobals();
102105
$currentCategory = Filter::filterVar($request->query->get('cat'), FILTER_VALIDATE_INT);
103-
$id = Filter::filterVar($request->query->get('id'), FILTER_VALIDATE_INT);
106+
$faqId = Filter::filterVar($request->query->get('id'), FILTER_VALIDATE_INT);
104107
$getAll = Filter::filterVar($request->query->get('getAll'), FILTER_VALIDATE_BOOLEAN, false);
105108

106109
$faq = $container->get('phpmyfaq.faq');
@@ -130,16 +133,26 @@
130133
$filename = 'FAQs.pdf';
131134
$pdfFile = $pdf->generate(0, true, $lang);
132135
} else {
133-
if (is_null($currentCategory) || is_null($id)) {
136+
if (is_null($currentCategory) || is_null($faqId)) {
134137
$response->isRedirect($faqConfig->getDefaultUrl());
135138
$response->send();
136139
exit();
137140
}
138141

139-
$faq->getFaq($id);
142+
$faq->getFaq($faqId);
140143
$faq->faqRecord['category_id'] = $currentCategory;
141144

142-
$filename = 'FAQ-' . $id . '-' . $lang . '.pdf';
145+
if ($faqConfig->get('records.disableAttachments') && 'yes' === $faq->faqRecord['active']) {
146+
try {
147+
$attachmentHelper = new AttachmentHelper();
148+
$attList = AttachmentFactory::fetchByRecordId($faqConfig, $faqId);
149+
$faq->faqRecord['attachmentList'] = $attachmentHelper->getAttachmentList($attList);
150+
} catch (AttachmentException) {
151+
// handle exception
152+
}
153+
}
154+
155+
$filename = 'FAQ-' . $faqId . '-' . $lang . '.pdf';
143156
$pdfFile = $pdf->generateFile($faq->faqRecord, $filename);
144157
}
145158

phpmyfaq/src/phpMyFAQ/Export/Pdf.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,20 @@ public function generateFile(array $faqData, ?string $filename = null): string
217217
$this->pdf->WriteHTML((string) $faqData['content']);
218218
}
219219

220+
if (isset($faqData['attachmentList'])) {
221+
$this->pdf->Ln(10);
222+
$this->pdf->Ln();
223+
$this->pdf->Write(5, Translation::get('msgAttachedFiles') . ':');
224+
$this->pdf->Ln(5);
225+
$this->pdf->Ln();
226+
$listItems = '<ul class="pb-4 mb-4 border-bottom">';
227+
foreach ($faqData['attachmentList'] as $attachment) {
228+
$listItems .= sprintf('<li><a href="%s">%s</a></li>', $attachment['url'], $attachment['filename']);
229+
}
230+
$listItems .= '</ul>';
231+
$this->pdf->WriteHTML($listItems);
232+
}
233+
220234
$this->pdf->Ln(10);
221235
$this->pdf->Ln();
222236
$this->pdf->SetFont($this->pdf->getCurrentFont(), '', 9);

0 commit comments

Comments
 (0)