From 805293baaccc33239c1b5d4dabcccfd3763b38e9 Mon Sep 17 00:00:00 2001 From: Dr Masroor Ehsan Date: Fri, 10 Jan 2025 12:43:34 +0600 Subject: [PATCH] export service --- app/Domain/Report/ExportFormat.php | 11 +++++++ .../Controllers/Staff/ReportController.php | 4 +-- app/Services/DocumentGenerator/Word.php | 27 ---------------- app/Services/Export/ExportDocumentBase.php | 25 +++++++++++++++ app/Services/Export/Exporters.php | 26 +++++++++++++++ app/Services/Export/Formats/HtmlExport.php | 29 +++++++++++++++++ app/Services/Export/Formats/PdfExport.php | 30 +++++++++++++++++ .../Export/Formats/Word2003Export.php | 32 +++++++++++++++++++ .../Export/Formats/Word2007Export.php | 32 +++++++++++++++++++ app/Services/Export/IExportDocument.php | 11 +++++++ .../views/staff/reports/create.blade.php | 3 +- 11 files changed, 199 insertions(+), 31 deletions(-) create mode 100644 app/Domain/Report/ExportFormat.php delete mode 100644 app/Services/DocumentGenerator/Word.php create mode 100644 app/Services/Export/ExportDocumentBase.php create mode 100644 app/Services/Export/Exporters.php create mode 100644 app/Services/Export/Formats/HtmlExport.php create mode 100644 app/Services/Export/Formats/PdfExport.php create mode 100644 app/Services/Export/Formats/Word2003Export.php create mode 100644 app/Services/Export/Formats/Word2007Export.php create mode 100644 app/Services/Export/IExportDocument.php diff --git a/app/Domain/Report/ExportFormat.php b/app/Domain/Report/ExportFormat.php new file mode 100644 index 0000000..a69f073 --- /dev/null +++ b/app/Domain/Report/ExportFormat.php @@ -0,0 +1,11 @@ +where('accession_number', $uuid)->firstOrFail(); $report = StudyReport::where('accession_number', $uuid)->firstOrFail(); - $path = Word::make($report->study, $report); + $path = Exporters::make($report->study, $report, ExportFormat::Word2007); return response()->download(ReportStorage::abspath($path)); } diff --git a/app/Services/DocumentGenerator/Word.php b/app/Services/DocumentGenerator/Word.php deleted file mode 100644 index 1b14dec..0000000 --- a/app/Services/DocumentGenerator/Word.php +++ /dev/null @@ -1,27 +0,0 @@ -accession_number . '.docx'; - $filepath = ReportStorage::customFilepath($study, $filename); - if (ReportStorage::exists($study, $filename)) { - return $filepath; - } - - $html = $report->getContent(); - $docx = new CreateDocx; - $docx->embedHTML($html); - $docx->createDocx(ReportStorage::abspath($filepath)); - - return $filepath; - } -} diff --git a/app/Services/Export/ExportDocumentBase.php b/app/Services/Export/ExportDocumentBase.php new file mode 100644 index 0000000..b6e7d4c --- /dev/null +++ b/app/Services/Export/ExportDocumentBase.php @@ -0,0 +1,25 @@ +handle(); + } + + abstract protected function handle(): string; + + abstract protected function getExtension(): string; + + protected function filename(): string + { + return $this->report->accession_number . '.' . $this->getExtension(); + } +} diff --git a/app/Services/Export/Exporters.php b/app/Services/Export/Exporters.php new file mode 100644 index 0000000..e55d12c --- /dev/null +++ b/app/Services/Export/Exporters.php @@ -0,0 +1,26 @@ + new HtmlExport($study, $report), + ExportFormat::Word2003 => new Word2003Export($study, $report), + ExportFormat::Word2007 => new Word2007Export($study, $report), + ExportFormat::Pdf => new PdfExport($study, $report), + }; + + return $exporter->make($study, $report); + } +} diff --git a/app/Services/Export/Formats/HtmlExport.php b/app/Services/Export/Formats/HtmlExport.php new file mode 100644 index 0000000..9a5379c --- /dev/null +++ b/app/Services/Export/Formats/HtmlExport.php @@ -0,0 +1,29 @@ +filename(); + $filepath = ReportStorage::customFilepath($this->study, $filename); + if (ReportStorage::exists($this->study, $filename)) { + return $filepath; + } + + $html = $this->report->getContent(); + file_put_contents(ReportStorage::abspath($filepath), $html); + + return $filepath; + } + + protected function getExtension(): string + { + return ExportFormat::Html->value; + } +} diff --git a/app/Services/Export/Formats/PdfExport.php b/app/Services/Export/Formats/PdfExport.php new file mode 100644 index 0000000..cf180de --- /dev/null +++ b/app/Services/Export/Formats/PdfExport.php @@ -0,0 +1,30 @@ +filename(); + $filepath = ReportStorage::customFilepath($this->study, $filename); + if (ReportStorage::exists($this->study, $filename)) { + return $filepath; + } + + $html = $this->report->getContent(); + // todo: implement pdf generation + file_put_contents(ReportStorage::abspath($filepath), $html); + + return $filepath; + } + + protected function getExtension(): string + { + return ExportFormat::Pdf->value; + } +} diff --git a/app/Services/Export/Formats/Word2003Export.php b/app/Services/Export/Formats/Word2003Export.php new file mode 100644 index 0000000..5edc850 --- /dev/null +++ b/app/Services/Export/Formats/Word2003Export.php @@ -0,0 +1,32 @@ +filename(); + $filepath = ReportStorage::customFilepath($this->study, $filename); + if (ReportStorage::exists($this->study, $filename)) { + return $filepath; + } + + $html = $this->report->getContent(); + $docx = new CreateDocx; + $docx->embedHTML($html); + $docx->createDocx(ReportStorage::abspath($filepath)); + + return $filepath; + } + + protected function getExtension(): string + { + return ExportFormat::Word2003->value; + } +} diff --git a/app/Services/Export/Formats/Word2007Export.php b/app/Services/Export/Formats/Word2007Export.php new file mode 100644 index 0000000..e74e310 --- /dev/null +++ b/app/Services/Export/Formats/Word2007Export.php @@ -0,0 +1,32 @@ +filename(); + $filepath = ReportStorage::customFilepath($this->study, $filename); + if (ReportStorage::exists($this->study, $filename)) { + return $filepath; + } + + $html = $this->report->getContent(); + $docx = new CreateDocx; + $docx->embedHTML($html); + $docx->createDocx(ReportStorage::abspath($filepath)); + + return $filepath; + } + + protected function getExtension(): string + { + return ExportFormat::Word2007->value; + } +} diff --git a/app/Services/Export/IExportDocument.php b/app/Services/Export/IExportDocument.php new file mode 100644 index 0000000..8e3ca32 --- /dev/null +++ b/app/Services/Export/IExportDocument.php @@ -0,0 +1,11 @@ +
- +
@@ -42,7 +42,6 @@ class="ck-editor editor-container editor-container_classic-editor editor-contain -