66
77use Chamilo \CoreBundle \Entity \Course ;
88use Chamilo \CoreBundle \Entity \Session ;
9+ use Chamilo \CoreBundle \Settings \SettingsManager ;
910use Chamilo \CourseBundle \Entity \CGlossary ;
1011use Chamilo \CourseBundle \Repository \CGlossaryRepository ;
1112use Doctrine \ORM \EntityManager ;
1415use Doctrine \ORM \OptimisticLockException ;
1516use Doctrine \ORM \TransactionRequiredException ;
1617use PDF ;
17- use PhpOffice \PhpSpreadsheet \IOFactory ;
18- use PhpOffice \PhpSpreadsheet \Spreadsheet ;
1918use PhpOffice \PhpSpreadsheet \Writer \Exception ;
2019use Symfony \Component \HttpFoundation \BinaryFileResponse ;
2120use Symfony \Component \HttpFoundation \File \File ;
2221use Symfony \Component \HttpFoundation \Request ;
2322use Symfony \Component \HttpFoundation \Response ;
2423use Symfony \Component \HttpFoundation \ResponseHeaderBag ;
2524use Symfony \Component \HttpKernel \Exception \BadRequestHttpException ;
26- use Symfony \Component \HttpKernel \KernelInterface ;
2725use Symfony \Contracts \Translation \TranslatorInterface ;
2826
2927readonly 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