extrace CSV renderer from controller, add pretty renderer

This commit is contained in:
Daniel Seifert 2021-04-19 13:30:52 +02:00
parent fcdc9a751d
commit 6e421753fd
Signed by: DanielS
GPG Key ID: 8A7C4C6ED1915C6F
10 changed files with 215 additions and 33 deletions

View File

@ -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": {

View File

@ -65,7 +65,7 @@ class d3ExportWizard extends AdminDetailsController
);
}
$export->run();
$export->run(Registry::getRequest()->getRequestEscapedParameter('exportformat'));
} catch (StandardException $e) {
Registry::getUtilsView()->addErrorToDisplay($e);
}

View File

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

View File

@ -0,0 +1,72 @@
<?php
/**
* This Software is the property of Data Development and is protected
* by copyright law - it is NOT Freeware.
* Any unauthorized use of this software without a valid license
* is a violation of the license agreement and will be prosecuted by
* civil and criminal law.
* http://www.shopmodule.com
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
* @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;
}
}

View File

@ -0,0 +1,32 @@
<?php
/**
* This Software is the property of Data Development and is protected
* by copyright law - it is NOT Freeware.
* Any unauthorized use of this software without a valid license
* is a violation of the license agreement and will be prosecuted by
* civil and criminal law.
* http://www.shopmodule.com
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
* @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';
}
}

View File

@ -0,0 +1,38 @@
<?php
/**
* This Software is the property of Data Development and is protected
* by copyright law - it is NOT Freeware.
* Any unauthorized use of this software without a valid license
* is a violation of the license agreement and will be prosecuted by
* civil and criminal law.
* http://www.shopmodule.com
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
* @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);
}
}
}

View File

@ -0,0 +1,23 @@
<?php
/**
* This Software is the property of Data Development and is protected
* by copyright law - it is NOT Freeware.
* Any unauthorized use of this software without a valid license
* is a violation of the license agreement and will be prosecuted by
* civil and criminal law.
* http://www.shopmodule.com
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
* @link http://www.oxidmodule.com
*/
namespace D3\DataWizard\Application\Model\ExportRenderer;
interface RendererInterface
{
public function getContent($rows, $fieldnames);
public function getFileExtension();
}

View File

@ -22,7 +22,7 @@ interface QueryBase
/**
* @return string
*/
public function getExportFilename() : string;
public function getExportFilenameBase() : string;
/**
* @return string

View File

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

View File

@ -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"}]</button>
<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('exportformat').value = 'Pretty';
document.getElementById('myedit').submit();
"
>[{oxmultilang ident="D3_DATAWIZARD_EXPORT_FORMAT_PRETTY"}]</button>
</div>
</div>