export service

This commit is contained in:
Dr Masroor Ehsan 2025-01-10 12:43:34 +06:00
parent c5108dbfd7
commit 805293baac
11 changed files with 199 additions and 31 deletions

View File

@ -0,0 +1,11 @@
<?php
namespace App\Domain\Report;
enum ExportFormat: string
{
case Html = 'html';
case Word2003 = 'doc';
case Word2007 = 'docx';
case Pdf = 'pdf';
}

View File

@ -3,12 +3,12 @@
namespace App\Http\Controllers\Staff; namespace App\Http\Controllers\Staff;
use App\Domain\ACL\Permission; use App\Domain\ACL\Permission;
use App\Domain\Report\ExportFormat;
use App\Domain\Report\ReportStatus; use App\Domain\Report\ReportStatus;
use App\Http\Controllers\HashidControllerBase; use App\Http\Controllers\HashidControllerBase;
use App\Http\Requests\CreateReportRequest; use App\Http\Requests\CreateReportRequest;
use App\Models\Study; use App\Models\Study;
use App\Models\StudyReport; use App\Models\StudyReport;
use App\Services\DocumentGenerator\Word;
use App\Services\ReportStorage; use App\Services\ReportStorage;
class ReportController extends HashidControllerBase class ReportController extends HashidControllerBase
@ -76,7 +76,7 @@ public function download(string $uuid)
// $study = Study::with(['reports.radiologist', 'readingPhysician'])->where('accession_number', $uuid)->firstOrFail(); // $study = Study::with(['reports.radiologist', 'readingPhysician'])->where('accession_number', $uuid)->firstOrFail();
$report = StudyReport::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)); return response()->download(ReportStorage::abspath($path));
} }

View File

@ -1,27 +0,0 @@
<?php
namespace App\Services\DocumentGenerator;
use App\Models\Study;
use App\Models\StudyReport;
use App\Services\ReportStorage;
use CreateDocx;
final readonly class Word
{
public static function make(Study $study, StudyReport $report): string
{
$filename = $report->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;
}
}

View File

@ -0,0 +1,25 @@
<?php
namespace App\Services\Export;
use App\Models\Study;
use App\Models\StudyReport;
abstract class ExportDocumentBase implements IExportDocument
{
public function __construct(protected readonly Study $study, protected readonly StudyReport $report) {}
public function make(Study $study, StudyReport $report): string
{
return $this->handle();
}
abstract protected function handle(): string;
abstract protected function getExtension(): string;
protected function filename(): string
{
return $this->report->accession_number . '.' . $this->getExtension();
}
}

View File

@ -0,0 +1,26 @@
<?php
namespace App\Services\Export;
use App\Domain\Report\ExportFormat;
use App\Models\Study;
use App\Models\StudyReport;
use app\Services\Export\Formats\HtmlExport;
use app\Services\Export\Formats\PdfExport;
use app\Services\Export\Formats\Word2003Export;
use app\Services\Export\Formats\Word2007Export;
final readonly class Exporters
{
public static function make(Study $study, StudyReport $report, ExportFormat $format)
{
$exporter = match ($format) {
ExportFormat::Html => 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);
}
}

View File

@ -0,0 +1,29 @@
<?php
namespace app\Services\Export\Formats;
use App\Domain\Report\ExportFormat;
use App\Services\Export\ExportDocumentBase;
use App\Services\ReportStorage;
final class HtmlExport extends ExportDocumentBase
{
protected function handle(): string
{
$filename = $this->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;
}
}

View File

@ -0,0 +1,30 @@
<?php
namespace app\Services\Export\Formats;
use App\Domain\Report\ExportFormat;
use App\Services\Export\ExportDocumentBase;
use App\Services\ReportStorage;
final class PdfExport extends ExportDocumentBase
{
protected function handle(): string
{
$filename = $this->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;
}
}

View File

@ -0,0 +1,32 @@
<?php
namespace app\Services\Export\Formats;
use App\Domain\Report\ExportFormat;
use App\Services\Export\ExportDocumentBase;
use App\Services\ReportStorage;
use CreateDocx;
final class Word2003Export extends ExportDocumentBase
{
protected function handle(): string
{
$filename = $this->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;
}
}

View File

@ -0,0 +1,32 @@
<?php
namespace app\Services\Export\Formats;
use App\Domain\Report\ExportFormat;
use App\Services\Export\ExportDocumentBase;
use App\Services\ReportStorage;
use CreateDocx;
final class Word2007Export extends ExportDocumentBase
{
protected function handle(): string
{
$filename = $this->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;
}
}

View File

@ -0,0 +1,11 @@
<?php
namespace App\Services\Export;
use App\Models\Study;
use App\Models\StudyReport;
interface IExportDocument
{
public function make(Study $study, StudyReport $report): string;
}

View File

@ -24,7 +24,7 @@
class="ck-editor editor-container editor-container_classic-editor editor-container_include-word-count fixed-container" class="ck-editor editor-container editor-container_classic-editor editor-container_include-word-count fixed-container"
id="editor-container"> id="editor-container">
<div class="editor-container__editor"> <div class="editor-container__editor">
<input id="editor" name="content"/> <textarea id="editor" name="content"></textarea>
</div> </div>
<div class="editor_container__word-count" id="editor-word-count"></div> <div class="editor_container__word-count" id="editor-word-count"></div>
</div> </div>
@ -42,7 +42,6 @@ class="ck-editor editor-container editor-container_classic-editor editor-contain
<script src="https://cdn.ckeditor.com/ckeditor5/44.1.0/ckeditor5.umd.js" crossorigin></script> <script src="https://cdn.ckeditor.com/ckeditor5/44.1.0/ckeditor5.umd.js" crossorigin></script>
<script src="https://cdn.ckeditor.com/ckeditor5-premium-features/44.1.0/ckeditor5-premium-features.umd.js" <script src="https://cdn.ckeditor.com/ckeditor5-premium-features/44.1.0/ckeditor5-premium-features.umd.js"
crossorigin></script> crossorigin></script>
<script src="./splitter.js"></script>
<script> <script>
const { const {
ClassicEditor, ClassicEditor,