diff --git a/composer.json b/composer.json index 44b34e3..f29417f 100644 --- a/composer.json +++ b/composer.json @@ -25,7 +25,8 @@ "proprietary" ], "require": { - "oxid-esales/oxideshop-ce": "~6.3" + "oxid-esales/oxideshop-ce": "~6.3", + "league/csv": "^9.0" }, "extra": { "oxideshop": { diff --git a/src/Application/Controller/Admin/d3ExportWizard.php b/src/Application/Controller/Admin/d3ExportWizard.php index efb015f..13ca2f5 100644 --- a/src/Application/Controller/Admin/d3ExportWizard.php +++ b/src/Application/Controller/Admin/d3ExportWizard.php @@ -17,6 +17,8 @@ namespace D3\DataWizard\Application\Controller\Admin; use D3\DataWizard\Application\Model\Configuration; use Doctrine\DBAL\DBALException; +use League\Csv\CannotInsertRecord; +use League\Csv\Exception; use OxidEsales\Eshop\Application\Controller\Admin\AdminDetailsController; use OxidEsales\Eshop\Core\Exception\StandardException; use OxidEsales\Eshop\Core\Registry; @@ -46,7 +48,9 @@ class d3ExportWizard extends AdminDetailsController } /** + * @throws CannotInsertRecord * @throws DBALException + * @throws Exception */ public function doExport() { @@ -62,11 +66,6 @@ class d3ExportWizard extends AdminDetailsController } $export->run(); - - throw oxNew( - StandardException::class, - Registry::getLang()->translateString('D3_DATAWIZARD_ERR_NOEXPORTCONTENT') - ); } catch (StandardException $e) { Registry::getUtilsView()->addErrorToDisplay($e); } diff --git a/src/Application/Model/ExportBase.php b/src/Application/Model/ExportBase.php index fb10aca..28d17dd 100644 --- a/src/Application/Model/ExportBase.php +++ b/src/Application/Model/ExportBase.php @@ -15,10 +15,15 @@ namespace D3\DataWizard\Application\Model; -use D3\ModCfg\Application\Model\d3database; +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; use OxidEsales\Eshop\Core\Exception\StandardException; @@ -26,13 +31,17 @@ use OxidEsales\Eshop\Core\Registry; abstract class ExportBase implements QueryBase { + const FORMAT_CSV = 'CSV'; + /** + * @throws CannotInsertRecord + * @throws DatabaseConnectionException + * @throws DatabaseErrorException + * @throws Exception * @throws StandardException * @throws d3ShopCompatibilityAdapterException * @throws d3_cfg_mod_exception * @throws DBALException - * @throws DatabaseConnectionException - * @throws DatabaseErrorException */ public function run() { @@ -46,11 +55,53 @@ abstract class ExportBase implements QueryBase ); } - d3database::getInstance()->downloadExportCsvByQuery($this->getExportFilename(), $this->getQuery()); + $rows = DatabaseProvider::getDb(DatabaseProvider::FETCH_MODE_ASSOC)->getAll($query); + + if (count($rows) <= 0) { + throw oxNew( + StandardException::class, + Registry::getLang()->translateString('D3_DATAWIZARD_ERR_NOEXPORTCONTENT') + ); + } + + $fieldNames = array_keys($rows[0]); + + $csv = $this->getCsv(); + $csv->insertOne($fieldNames); + $csv->insertAll($rows); + + /** @var $oFS d3filesystem */ + $oFS = oxNew(d3filesystem::class); + $oFS->startDirectDownload($this->getExportFilename(), $csv->getContent()); } public function getButtonText() : string { return "D3_DATAWIZARD_EXPORT_SUBMIT"; } + + /** + * @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/views/admin/de/d3DataWizard_lang.php b/src/Application/views/admin/de/d3DataWizard_lang.php index 02ca429..9b67961 100644 --- a/src/Application/views/admin/de/d3DataWizard_lang.php +++ b/src/Application/views/admin/de/d3DataWizard_lang.php @@ -36,6 +36,7 @@ $aLang = array( 'D3_DATAWIZARD_GROUP_ORDERS' => 'Bestellungen', 'D3_DATAWIZARD_EXPORT_SUBMIT' => 'Export starten', + 'D3_DATAWIZARD_EXPORT_FORMAT_CSV' => 'CSV-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 63abbcf..8cdf0b9 100644 --- a/src/Application/views/admin/tpl/d3ExportWizard.tpl +++ b/src/Application/views/admin/tpl/d3ExportWizard.tpl @@ -32,6 +32,7 @@ + [{assign var="groups" value=$oView->getGroups()}] [{if $groups|count}] @@ -59,21 +60,43 @@

[{$export->getDescription()}]

- + }, 3000); + document.getElementById('mask').className='on'; + document.getElementById('popup2').className='d3loader-2 on'; + document.getElementById('exportid').value = '[{$id}]'; + document.getElementById('myedit').submit(); + " + > + + [{oxmultilang ident=$export->getButtonText()}] + + + + +