From 1e215d2979f867818b8f9ad1f99ca6690284b000 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michal=20Charva=CC=81t?= Date: Fri, 28 Jun 2024 11:46:11 +0200 Subject: [PATCH] #1155 Remove final from exportToBlob method, extract colorizeHeaders from finishExport to add option colorize headers on all sheets --- .../core/data/convert/AbstractConverter.php | 4 +- www/go/core/data/convert/Spreadsheet.php | 51 ++++++++++++++++--- 2 files changed, 47 insertions(+), 8 deletions(-) diff --git a/www/go/core/data/convert/AbstractConverter.php b/www/go/core/data/convert/AbstractConverter.php index 0eeeb69e29..eeb55350ae 100644 --- a/www/go/core/data/convert/AbstractConverter.php +++ b/www/go/core/data/convert/AbstractConverter.php @@ -290,7 +290,7 @@ protected function finishImport(): void * @param array $params * @return Blob */ - public final function exportToBlob(Query $entities, array $params = []): Blob + public function exportToBlob(Query $entities, array $params = []): Blob { $this->clientParams = $params; $this->entitiesQuery = $entities; @@ -310,7 +310,7 @@ public final function exportToBlob(Query $entities, array $params = []): Blob /** * @var Query */ - private $entitiesQuery; + protected $entitiesQuery; /** * The query used for exporting entities diff --git a/www/go/core/data/convert/Spreadsheet.php b/www/go/core/data/convert/Spreadsheet.php index 6b99caf6da..627d51611f 100644 --- a/www/go/core/data/convert/Spreadsheet.php +++ b/www/go/core/data/convert/Spreadsheet.php @@ -228,18 +228,39 @@ protected function exportEntity(Entity $entity): void $this->writeRecord($record); } + /** + * @param \go\core\orm\Query $entities + * @param array $params + * @return \go\core\fs\Blob + * @throws \go\core\orm\exception\SaveException + */ + public function exportToBlob(Query $entities, array $params = []): Blob + { + $this->clientParams = $params; + $this->entitiesQuery = $entities; + $this->initExport(); - protected function finishExport(): Blob + $this->index = 0; + foreach($entities as $entity) { + $this->exportEntity($entity); + $this->index++; + } + + $this->colorizeHeaders(); + + return $this->finishExport(); + } + + /** + * @return void + */ + protected function colorizeHeaders(): void { - switch($this->extension) { + switch ($this->extension) { case 'html': - fputs($this->fp, ""); - break; case 'csv': - break; default: - $headerStyle = [ 'font' => [ 'bold' => true, @@ -258,7 +279,25 @@ protected function finishExport(): Blob $colDim = $this->spreadsheet->getActiveSheet()->getColumnDimension($col->getColumnIndex()); $colDim->setAutoSize(true); } + break; + } + } + /** + * @return \go\core\fs\Blob + * @throws \PhpOffice\PhpSpreadsheet\Writer\Exception + * @throws \go\core\orm\exception\SaveException + */ + protected function finishExport(): Blob + { + switch($this->extension) { + case 'html': + fputs($this->fp, ""); + break; + case 'csv': + + break; + default: $writer = new \PhpOffice\PhpSpreadsheet\Writer\Xlsx($this->spreadsheet); $writer->setPreCalculateFormulas(false); $writer->save($this->tempFile->getPath());