Skip to content

Commit

Permalink
Merge pull request #20 from punktDe/feature/add-excel-exporter
Browse files Browse the repository at this point in the history
FEATURE: refactored and added all changes
  • Loading branch information
LeCreator authored Nov 22, 2021
2 parents 21cd762 + ef17795 commit 65ae817
Show file tree
Hide file tree
Showing 4 changed files with 129 additions and 2 deletions.
114 changes: 114 additions & 0 deletions Classes/Domain/Exporter/ExcelExporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php
declare(strict_types=1);

namespace PunktDe\Form\Persistence\Domain\Exporter;

/*
* (c) 2020 punkt.de GmbH - Karlsruhe, Germany - http://punkt.de
* All rights reserved.
*/

use PhpOffice\PhpSpreadsheet\Exception;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Style\Alignment;
use PhpOffice\PhpSpreadsheet\IOFactory;
use PhpOffice\PhpSpreadsheet\Writer\Exception as WriterException;

class ExcelExporter implements FormDataExporterInterface
{

/**
* @var array
*/
protected $options = [];

/**
* @var string
*/
protected $fileName = 'FormData.xlsx';

public function setFileName(string $fileName): FormDataExporterInterface
{
$this->fileName = $fileName;
return $this;
}

public function setOptions(array $options): FormDataExporterInterface
{
$this->options = $options;
return $this;
}

/**
* @param iterable $formDataItems
* @return void
* @throws WriterException|Exception
*/
public function compileAndSend(iterable $formDataItems): void
{
header("Pragma: public"); // required
header("Expires: 0");
header('Cache-Control: max-age=0');
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private", false); // required for certain browsers
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header(
sprintf(
'Content-Disposition: attachment; filename="%s"',
$this->fileName

)
);
header("Content-Transfer-Encoding: binary");

$writer = IOFactory::createWriter($this->compileXLS($formDataItems), 'Xlsx');
$writer->save('php://output');
exit;
}

/**
* @throws Exception
* @throws WriterException
*/
public function compileAndSave(iterable $formDataItems, string $filePath): void
{
$writer = IOFactory::createWriter($this->compileXLS($formDataItems), 'Xlsx');
$writer->save($filePath);
}

/**
* @throws Exception
*/
protected function compileXLS(iterable $formDataItems) :Spreadsheet
{
$spreadsheet = new Spreadsheet();

$spreadsheet->getProperties()
->setCreator('creator')
->setTitle('title')
->setSubject('subject');
$headerColumns = array_keys($formDataItems[0]);
array_unshift($formDataItems ,$headerColumns );

//Format Headline
$prefixIndex = 64;
$prefixKey = '';
for ($i = 0; $i < count($headerColumns); $i++) {
$index = $i % 26;
$columnStyle = $spreadsheet->getActiveSheet()->getStyle($prefixKey . chr(65 + $index) . '1');
$columnStyle->getFont()->setBold(true);
$columnStyle->getAlignment()
->setHorizontal(Alignment::HORIZONTAL_CENTER)
->setVertical(Alignment::VERTICAL_CENTER);

if ($index + 1 > 25) {
$prefixIndex++;
$prefixKey = chr($prefixIndex);
}
}

$spreadsheet->setActiveSheetIndex(0);
$spreadsheet->getActiveSheet()->fromArray($formDataItems);
return $spreadsheet;
}
}
3 changes: 2 additions & 1 deletion Classes/Domain/ScheduledExport/ScheduledExportSender.php
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,8 @@ private function prepareErrorOnExportMail(Message $mail, FormData $formDataRepre
[
'formIdentifier' => $formDataRepresentative->getFormIdentifier(),
'formVersion' => $formDataRepresentative->getHash(),

'errorCode' => $exception->getCode(),
'errorMessage' => $exception->getMessage(),
],
null,
null,
Expand Down
11 changes: 11 additions & 0 deletions Configuration/Settings.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,21 @@ PunktDe:
exporter: csv
fileNamePattern: "Form-Export-{formIdentifier}-{currentDate}.csv"

excel:
label: Generic Xlsx
exporter: xlsx
fileNamePattern: "Form-Export-{formIdentifier}-{currentDate}.xlsx"



exporter:
csv:
className: PunktDe\Form\Persistence\Domain\Exporter\CsvExporter
options: [ ]
xlsx:
className: PunktDe\Form\Persistence\Domain\Exporter\ExcelExporter #phpofficeexporter
options: [ ]
#Writertype:

processorChain:
# Flatten nested fields into an array with namespaces
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@
"neos/form": "*",
"neos/fusion": "*",
"league/csv": "^9.0",
"neos/swiftmailer": ">=7.0"
"neos/swiftmailer": ">=7.0",
"phpoffice/phpspreadsheet": "^1.18"
},
"suggest": {
"neos/form-builder": "Adds a builder for the Flow Form Framework to the Neos CMS backend."
Expand Down

0 comments on commit 65ae817

Please sign in to comment.