From 6e421753fd5b039b2b7f4609c7907de9e97a4074 Mon Sep 17 00:00:00 2001 From: Daniel Seifert Date: Mon, 19 Apr 2021 13:30:52 +0200 Subject: [PATCH] extrace CSV renderer from controller, add pretty renderer --- composer.json | 3 +- .../Controller/Admin/d3ExportWizard.php | 2 +- src/Application/Model/ExportBase.php | 61 ++++++++-------- src/Application/Model/ExportRenderer/Csv.php | 72 +++++++++++++++++++ .../Model/ExportRenderer/Pretty.php | 32 +++++++++ .../Model/ExportRenderer/RendererBridge.php | 38 ++++++++++ .../ExportRenderer/RendererInterface.php | 23 ++++++ src/Application/Model/QueryBase.php | 2 +- .../views/admin/de/d3DataWizard_lang.php | 1 + .../views/admin/tpl/d3ExportWizard.tpl | 14 ++++ 10 files changed, 215 insertions(+), 33 deletions(-) create mode 100644 src/Application/Model/ExportRenderer/Csv.php create mode 100644 src/Application/Model/ExportRenderer/Pretty.php create mode 100644 src/Application/Model/ExportRenderer/RendererBridge.php create mode 100644 src/Application/Model/ExportRenderer/RendererInterface.php diff --git a/composer.json b/composer.json index f29417f..df92b9f 100644 --- a/composer.json +++ b/composer.json @@ -26,7 +26,8 @@ ], "require": { "oxid-esales/oxideshop-ce": "~6.3", - "league/csv": "^9.0" + "league/csv": "^9.0", + "mathieuviossat/arraytotexttable": "^1.0" }, "extra": { "oxideshop": { diff --git a/src/Application/Controller/Admin/d3ExportWizard.php b/src/Application/Controller/Admin/d3ExportWizard.php index 13ca2f5..3422d24 100644 --- a/src/Application/Controller/Admin/d3ExportWizard.php +++ b/src/Application/Controller/Admin/d3ExportWizard.php @@ -65,7 +65,7 @@ class d3ExportWizard extends AdminDetailsController ); } - $export->run(); + $export->run(Registry::getRequest()->getRequestEscapedParameter('exportformat')); } catch (StandardException $e) { Registry::getUtilsView()->addErrorToDisplay($e); } diff --git a/src/Application/Model/ExportBase.php b/src/Application/Model/ExportBase.php index 28d17dd..77fbbca 100644 --- a/src/Application/Model/ExportBase.php +++ b/src/Application/Model/ExportBase.php @@ -15,14 +15,13 @@ namespace D3\DataWizard\Application\Model; +use D3\DataWizard\Application\Model\ExportRenderer\RendererBridge; use D3\ModCfg\Application\Model\d3filesystem; use D3\ModCfg\Application\Model\Exception\d3_cfg_mod_exception; use D3\ModCfg\Application\Model\Exception\d3ShopCompatibilityAdapterException; use Doctrine\DBAL\DBALException; use League\Csv\CannotInsertRecord; -use League\Csv\EncloseField; use League\Csv\Exception; -use League\Csv\Writer; use OxidEsales\Eshop\Core\DatabaseProvider; use OxidEsales\Eshop\Core\Exception\DatabaseConnectionException; use OxidEsales\Eshop\Core\Exception\DatabaseErrorException; @@ -31,19 +30,17 @@ use OxidEsales\Eshop\Core\Registry; abstract class ExportBase implements QueryBase { - const FORMAT_CSV = 'CSV'; - /** - * @throws CannotInsertRecord + * @param string $format + * + * @throws DBALException * @throws DatabaseConnectionException * @throws DatabaseErrorException - * @throws Exception * @throws StandardException * @throws d3ShopCompatibilityAdapterException * @throws d3_cfg_mod_exception - * @throws DBALException */ - public function run() + public function run($format = RendererBridge::FORMAT_CSV) { $query = trim($this->getQuery()); @@ -66,13 +63,14 @@ abstract class ExportBase implements QueryBase $fieldNames = array_keys($rows[0]); - $csv = $this->getCsv(); - $csv->insertOne($fieldNames); - $csv->insertAll($rows); + $content = $this->renderContent($rows, $fieldNames, $format); /** @var $oFS d3filesystem */ $oFS = oxNew(d3filesystem::class); - $oFS->startDirectDownload($this->getExportFilename(), $csv->getContent()); + $oFS->startDirectDownload( + $this->getExportFilenameBase().'.'.$this->getFileExtension($format), + $content + ); } public function getButtonText() : string @@ -81,27 +79,30 @@ abstract class ExportBase implements QueryBase } /** - * @return Writer - * @throws Exception + * @param $format + * + * @return ExportRenderer\RendererInterface */ - protected function getCsv(): Writer + public function getRenderer($format): ExportRenderer\RendererInterface { - $csv = Writer::createFromString(); + return oxNew(RendererBridge::class)->getRenderer($format); + } - EncloseField::addTo($csv, "\t\x1f"); + public function getFileExtension($format) + { + return $this->getRenderer($format)->getFileExtension(); + } - $sEncloser = Registry::getConfig()->getConfigParam('sGiCsvFieldEncloser'); - if (false == $sEncloser) { - $sEncloser = '"'; - } - $csv->setEnclosure($sEncloser); - - $sDelimiter = Registry::getConfig()->getConfigParam('sCSVSign'); - if (false == $sDelimiter) { - $sDelimiter = ';'; - } - $csv->setDelimiter($sDelimiter); - - return $csv; + /** + * @param $rows + * @param $fieldnames + * @param $format + * + * @return mixed + */ + public function renderContent($rows, $fieldnames, $format) + { + $renderer = $this->getRenderer($format); + return $renderer->getContent($rows, $fieldnames); } } \ No newline at end of file diff --git a/src/Application/Model/ExportRenderer/Csv.php b/src/Application/Model/ExportRenderer/Csv.php new file mode 100644 index 0000000..52929ed --- /dev/null +++ b/src/Application/Model/ExportRenderer/Csv.php @@ -0,0 +1,72 @@ + + * @link http://www.oxidmodule.com + */ + +namespace D3\DataWizard\Application\Model\ExportRenderer; + +use League\Csv\CannotInsertRecord; +use League\Csv\EncloseField; +use League\Csv\Exception; +use League\Csv\Writer; +use OxidEsales\Eshop\Core\Registry; + +class Csv implements RendererInterface +{ + /** + * @param $rows + * @param $fieldNames + * + * @return string + * @throws Exception + * @throws CannotInsertRecord + */ + public function getContent($rows, $fieldNames) + { + $csv = $this->getCsv(); + $csv->insertOne($fieldNames); + $csv->insertAll($rows); + + return $csv->getContent(); + } + + public function getFileExtension(): string + { + return 'csv'; + } + + /** + * @return Writer + * @throws Exception + */ + protected function getCsv(): Writer + { + $csv = Writer::createFromString(); + + EncloseField::addTo($csv, "\t\x1f"); + + $sEncloser = Registry::getConfig()->getConfigParam('sGiCsvFieldEncloser'); + if (false == $sEncloser) { + $sEncloser = '"'; + } + $csv->setEnclosure($sEncloser); + + $sDelimiter = Registry::getConfig()->getConfigParam('sCSVSign'); + if (false == $sDelimiter) { + $sDelimiter = ';'; + } + $csv->setDelimiter($sDelimiter); + + return $csv; + } +} \ No newline at end of file diff --git a/src/Application/Model/ExportRenderer/Pretty.php b/src/Application/Model/ExportRenderer/Pretty.php new file mode 100644 index 0000000..289a81f --- /dev/null +++ b/src/Application/Model/ExportRenderer/Pretty.php @@ -0,0 +1,32 @@ + + * @link http://www.oxidmodule.com + */ + +namespace D3\DataWizard\Application\Model\ExportRenderer; + +use MathieuViossat\Util\ArrayToTextTable; + +class Pretty implements RendererInterface +{ + public function getContent($rows, $fieldnames) + { + $renderer = oxNew(ArrayToTextTable::class, $rows); + return $renderer->getTable(); + } + + public function getFileExtension(): string + { + return 'txt'; + } +} \ No newline at end of file diff --git a/src/Application/Model/ExportRenderer/RendererBridge.php b/src/Application/Model/ExportRenderer/RendererBridge.php new file mode 100644 index 0000000..d87fbe3 --- /dev/null +++ b/src/Application/Model/ExportRenderer/RendererBridge.php @@ -0,0 +1,38 @@ + + * @link http://www.oxidmodule.com + */ + +namespace D3\DataWizard\Application\Model\ExportRenderer; + +class RendererBridge +{ + const FORMAT_CSV = 'CSV'; + const FORMAT_PRETTY = 'Pretty'; + + /** + * @param string $format + * + * @return RendererInterface + */ + public function getRenderer($format = self::FORMAT_CSV): RendererInterface + { + switch ($format) { + case self::FORMAT_PRETTY: + return oxNew(Pretty::class); + case self::FORMAT_CSV: + default: + return oxNew(Csv::class); + } + } +} \ No newline at end of file diff --git a/src/Application/Model/ExportRenderer/RendererInterface.php b/src/Application/Model/ExportRenderer/RendererInterface.php new file mode 100644 index 0000000..0663eaa --- /dev/null +++ b/src/Application/Model/ExportRenderer/RendererInterface.php @@ -0,0 +1,23 @@ + + * @link http://www.oxidmodule.com + */ + +namespace D3\DataWizard\Application\Model\ExportRenderer; + +interface RendererInterface +{ + public function getContent($rows, $fieldnames); + + public function getFileExtension(); +} \ No newline at end of file diff --git a/src/Application/Model/QueryBase.php b/src/Application/Model/QueryBase.php index bffe874..b455a62 100644 --- a/src/Application/Model/QueryBase.php +++ b/src/Application/Model/QueryBase.php @@ -22,7 +22,7 @@ interface QueryBase /** * @return string */ - public function getExportFilename() : string; + public function getExportFilenameBase() : string; /** * @return string diff --git a/src/Application/views/admin/de/d3DataWizard_lang.php b/src/Application/views/admin/de/d3DataWizard_lang.php index 9cefa22..bec2cd8 100644 --- a/src/Application/views/admin/de/d3DataWizard_lang.php +++ b/src/Application/views/admin/de/d3DataWizard_lang.php @@ -37,6 +37,7 @@ $aLang = array( 'D3_DATAWIZARD_EXPORT_SUBMIT' => 'Export starten', 'D3_DATAWIZARD_EXPORT_FORMAT_CSV' => 'CSV-Format', + 'D3_DATAWIZARD_EXPORT_FORMAT_PRETTY' => 'Pretty-Format', 'D3_DATAWIZARD_EXPORT_NOSELECT' => 'Export kann nicht ausgeführt werden. Exporte erfordern SELECT Query.', diff --git a/src/Application/views/admin/tpl/d3ExportWizard.tpl b/src/Application/views/admin/tpl/d3ExportWizard.tpl index 8cdf0b9..694a051 100644 --- a/src/Application/views/admin/tpl/d3ExportWizard.tpl +++ b/src/Application/views/admin/tpl/d3ExportWizard.tpl @@ -70,6 +70,7 @@ document.getElementById('mask').className='on'; document.getElementById('popup2').className='d3loader-2 on'; document.getElementById('exportid').value = '[{$id}]'; + document.getElementById('exportformat').value = 'CSV'; document.getElementById('myedit').submit(); " > @@ -91,9 +92,22 @@ document.getElementById('mask').className='on'; document.getElementById('popup2').className='d3loader-2 on'; document.getElementById('exportid').value = '[{$id}]'; + document.getElementById('exportformat').value = 'CSV'; document.getElementById('myedit').submit(); " >[{oxmultilang ident="D3_DATAWIZARD_EXPORT_FORMAT_CSV"}] +