Skip to content

Commit a5d1513

Browse files
committed
Glossary: Fix XLSX and CSV exported #6722
1 parent 42f642c commit a5d1513

File tree

2 files changed

+44
-53
lines changed

2 files changed

+44
-53
lines changed

public/main/inc/lib/export.lib.inc.php

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ public static function arrayToCsv(
4646
}
4747

4848
$enclosure = $enclosure !== '' ? $enclosure : '"';
49-
$filePath = api_get_path(SYS_ARCHIVE_PATH) . uniqid() . '.csv';
49+
$filePath = api_get_path(SYS_ARCHIVE_PATH);
50+
$filePath .= $writeOnly ? $filename : uniqid('');
51+
$filePath .= '.csv';
5052

5153
// define a single-character escape
5254
$escapeChar = '\\';
@@ -74,7 +76,9 @@ public static function arrayToCsv(
7476
*/
7577
public static function arrayToCsvSimple(array $data, string $filename = 'export', bool $writeOnly = false, array $header = [])
7678
{
77-
$file = api_get_path(SYS_ARCHIVE_PATH) . uniqid('') . '.csv';
79+
$file = api_get_path(SYS_ARCHIVE_PATH);
80+
$file .= $writeOnly ? $filename : uniqid('');
81+
$file .= '.csv';
7882

7983
$handle = fopen($file, 'w');
8084

@@ -104,7 +108,7 @@ public static function arrayToCsvSimple(array $data, string $filename = 'export'
104108
/**
105109
* Export tabular data to XLS-file.
106110
*/
107-
public static function arrayToXls(array $data, string $filename = 'export')
111+
public static function arrayToXls(array $data, string $filename = 'export', bool $writeOnly = false)
108112
{
109113
if (empty($data)) {
110114
return false;
@@ -122,12 +126,19 @@ public static function arrayToXls(array $data, string $filename = 'export')
122126
$rowNumber++;
123127
}
124128

125-
$filePath = api_get_path(SYS_ARCHIVE_PATH).uniqid('').'.xlsx';
129+
$filePath = api_get_path(SYS_ARCHIVE_PATH);
130+
$filePath .= $writeOnly ? $filename : uniqid('');
131+
$filePath .= '.xlsx';
132+
126133
$writer = new Xlsx($spreadsheet);
127134
$writer->save($filePath);
128135

129-
DocumentManager::file_send_for_download($filePath, true, $filename.'.xlsx');
130-
exit;
136+
if (!$writeOnly) {
137+
DocumentManager::file_send_for_download($filePath, true, $filename.'.xlsx');
138+
exit;
139+
}
140+
141+
return $filePath;
131142
}
132143

133144
/**

src/CoreBundle/Controller/Api/ExportCGlossaryAction.php

Lines changed: 27 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use Chamilo\CoreBundle\Entity\Course;
88
use Chamilo\CoreBundle\Entity\Session;
9+
use Chamilo\CoreBundle\Settings\SettingsManager;
910
use Chamilo\CourseBundle\Entity\CGlossary;
1011
use Chamilo\CourseBundle\Repository\CGlossaryRepository;
1112
use Doctrine\ORM\EntityManager;
@@ -14,22 +15,20 @@
1415
use Doctrine\ORM\OptimisticLockException;
1516
use Doctrine\ORM\TransactionRequiredException;
1617
use PDF;
17-
use PhpOffice\PhpSpreadsheet\IOFactory;
18-
use PhpOffice\PhpSpreadsheet\Spreadsheet;
1918
use PhpOffice\PhpSpreadsheet\Writer\Exception;
2019
use Symfony\Component\HttpFoundation\BinaryFileResponse;
2120
use Symfony\Component\HttpFoundation\File\File;
2221
use Symfony\Component\HttpFoundation\Request;
2322
use Symfony\Component\HttpFoundation\Response;
2423
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
2524
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
26-
use Symfony\Component\HttpKernel\KernelInterface;
2725
use Symfony\Contracts\Translation\TranslatorInterface;
2826

2927
readonly class ExportCGlossaryAction
3028
{
3129
public function __construct(
3230
private TranslatorInterface $translator,
31+
private SettingsManager $settingsManager,
3332
) {}
3433

3534
/**
@@ -39,7 +38,6 @@ public function __invoke(
3938
Request $request,
4039
CGlossaryRepository $repo,
4140
EntityManager $em,
42-
KernelInterface $kernel,
4341
TranslatorInterface $translator
4442
): Response {
4543
$format = $request->get('format');
@@ -50,7 +48,6 @@ public function __invoke(
5048
throw new BadRequestHttpException('Invalid export format');
5149
}
5250

53-
$exportPath = $kernel->getCacheDir();
5451
$course = null;
5552
$session = null;
5653
if (0 !== $cid) {
@@ -66,9 +63,7 @@ public function __invoke(
6663
$exportFilePath = $this->generateExportFile(
6764
$glossaryItems,
6865
$format,
69-
$exportPath,
7066
$course,
71-
$session
7267
);
7368

7469
$response = new BinaryFileResponse(
@@ -88,59 +83,44 @@ public function __invoke(
8883
private function generateExportFile(
8984
array $glossaryItems,
9085
string $format,
91-
string $exportPath,
9286
?Course $course,
93-
?Session $session = null,
9487
): string {
95-
return match ($format) {
96-
'csv' => $this->generateCsvFile($glossaryItems, $exportPath),
97-
'xls' => $this->generateExcelFile($glossaryItems, $exportPath),
98-
'pdf' => $this->generatePdfFile($glossaryItems, $course, $session),
99-
default => throw new NotSupported('Export format not supported'),
100-
};
101-
}
88+
if ('pdf' === $format) {
89+
return $this->generatePdfFile($glossaryItems, $course);
90+
}
10291

103-
private function generateCsvFile(array $glossaryItems, string $exportPath): string
104-
{
105-
$csvFilePath = $exportPath.'/glossary.csv';
106-
$csvContent = '';
92+
$list = [];
93+
$list[] = ['term', 'definition'];
94+
95+
$allowStrip = 'true' === $this->settingsManager->getSetting('glossary.allow_remove_tags_in_glossary_export');
10796

108-
/** @var CGlossary $item */
10997
foreach ($glossaryItems as $item) {
110-
$csvContent .= $item->getTitle().','.$item->getDescription()."\n";
98+
$definition = $item->getDescription();
99+
100+
if ($allowStrip) {
101+
$definition = htmlspecialchars_decode(strip_tags($definition), ENT_QUOTES);
102+
}
103+
104+
$list[] = [$item->getTitle(), $definition];
111105
}
112-
file_put_contents($csvFilePath, $csvContent);
113106

114-
return $csvFilePath;
107+
return match ($format) {
108+
'csv' => $this->generateCsvFile($list, $course),
109+
'xls' => $this->generateExcelFile($list, $course),
110+
};
115111
}
116112

117-
/**
118-
* @throws Exception
119-
*/
120-
private function generateExcelFile(array $glossaryItems, string $exportPath): string
113+
private function generateCsvFile(array $glossaryItems, Course $course): string
121114
{
122-
$excelFilePath = $exportPath.'/glossary.xlsx';
123-
$spreadsheet = new Spreadsheet();
124-
$sheet = $spreadsheet->getActiveSheet();
125-
126-
/** @var CGlossary $item */
127-
foreach ($glossaryItems as $index => $item) {
128-
$row = $index + 1;
129-
$sheet->setCellValue('A'.$row, $item->getTitle());
130-
$sheet->setCellValue('B'.$row, $item->getDescription());
131-
}
132-
133-
$writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
134-
$writer->save($excelFilePath);
115+
return \Export::arrayToCsv($glossaryItems, 'glossary_course_'.$course->getCode(), true);
116+
}
135117

136-
return $excelFilePath;
118+
private function generateExcelFile(array $glossaryItems, Course $course): string
119+
{
120+
return \Export::arrayToXls($glossaryItems, 'glossary_course_'.$course->getCode(), true);
137121
}
138122

139-
private function generatePdfFile(
140-
array $glossaryItems,
141-
?Course $course,
142-
?Session $session = null,
143-
): string
123+
private function generatePdfFile(array $glossaryItems, Course $course): string
144124
{
145125
$html = '<h1>'.$this->translator->trans('Glossary').'</h1>';
146126
$html .= '<table>';

0 commit comments

Comments
 (0)