use separate CSV generator, prepare for further formats

This commit is contained in:
Daniel Seifert 2021-04-19 00:27:33 +02:00
parent 602cdf43ee
commit d79b61f045
Signed by: DanielS
GPG Key ID: 6A513E13AEE66170
5 changed files with 98 additions and 23 deletions

View File

@ -25,7 +25,8 @@
"proprietary"
],
"require": {
"oxid-esales/oxideshop-ce": "~6.3"
"oxid-esales/oxideshop-ce": "~6.3",
"league/csv": "^9.0"
},
"extra": {
"oxideshop": {

View File

@ -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);
}

View File

@ -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;
}
}

View File

@ -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.',

View File

@ -32,6 +32,7 @@
<input type="hidden" name="cl" value="[{$oViewConf->getActiveClassName()}]">
<input type="hidden" name="fnc" value="doExport">
<input type="hidden" name="exportid" id="exportid" value="">
<input type="hidden" name="exportformat" id="exportformat" value="CSV">
[{assign var="groups" value=$oView->getGroups()}]
[{if $groups|count}]
@ -59,21 +60,43 @@
<p class="card-text">
[{$export->getDescription()}]
</p>
<button class="btn btn-primary"
onclick="
setTimeout(function(){
<div class="btn-group">
<button type="button" class="btn btn-primary" onclick="
setTimeout(function(){
document.getElementById('mask').className='';
document.getElementById('popup2').className='d3loader-2';
}, 3000);
document.getElementById('mask').className='on';
document.getElementById('popup2').className='d3loader-2 on';
document.getElementById('exportid').value = '[{$id}]';
document.getElementById('myedit').submit();
"
>
<i class="fas fa-magic"></i>
[{oxmultilang ident=$export->getButtonText()}]
</button>
}, 3000);
document.getElementById('mask').className='on';
document.getElementById('popup2').className='d3loader-2 on';
document.getElementById('exportid').value = '[{$id}]';
document.getElementById('myedit').submit();
"
>
<i class="fas fa-magic"></i>
[{oxmultilang ident=$export->getButtonText()}]
</button>
<button type="button" class="btn btn-primary dropdown-toggle dropdown-toggle-split" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
<span class="sr-only">
<i class="fas fa-magic"></i>
[{oxmultilang ident=$export->getButtonText()}]
</span>
</button>
<div class="dropdown-menu">
<button class="dropdown-item" onclick="
setTimeout(function(){
document.getElementById('mask').className='';
document.getElementById('popup2').className='d3loader-2';
}, 3000);
document.getElementById('mask').className='on';
document.getElementById('popup2').className='d3loader-2 on';
document.getElementById('exportid').value = '[{$id}]';
document.getElementById('myedit').submit();
"
>[{oxmultilang ident="D3_DATAWIZARD_EXPORT_FORMAT_CSV"}]</button>
</div>
</div>
</div>
</div>
</div>