2021-04-16 14:04:30 +02:00
|
|
|
<?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;
|
|
|
|
|
2021-04-19 13:30:52 +02:00
|
|
|
use D3\DataWizard\Application\Model\ExportRenderer\RendererBridge;
|
2021-04-19 00:27:33 +02:00
|
|
|
use D3\ModCfg\Application\Model\d3filesystem;
|
2021-04-18 23:17:48 +02:00
|
|
|
use D3\ModCfg\Application\Model\Exception\d3_cfg_mod_exception;
|
|
|
|
use D3\ModCfg\Application\Model\Exception\d3ShopCompatibilityAdapterException;
|
|
|
|
use Doctrine\DBAL\DBALException;
|
2021-04-19 00:27:33 +02:00
|
|
|
use League\Csv\CannotInsertRecord;
|
|
|
|
use League\Csv\Exception;
|
|
|
|
use OxidEsales\Eshop\Core\DatabaseProvider;
|
2021-04-18 23:17:48 +02:00
|
|
|
use OxidEsales\Eshop\Core\Exception\DatabaseConnectionException;
|
|
|
|
use OxidEsales\Eshop\Core\Exception\DatabaseErrorException;
|
|
|
|
use OxidEsales\Eshop\Core\Exception\StandardException;
|
|
|
|
use OxidEsales\Eshop\Core\Registry;
|
2021-04-16 14:04:30 +02:00
|
|
|
|
|
|
|
abstract class ExportBase implements QueryBase
|
|
|
|
{
|
2021-04-18 23:17:48 +02:00
|
|
|
/**
|
2021-04-19 13:30:52 +02:00
|
|
|
* @param string $format
|
|
|
|
*
|
|
|
|
* @throws DBALException
|
2021-04-19 00:27:33 +02:00
|
|
|
* @throws DatabaseConnectionException
|
|
|
|
* @throws DatabaseErrorException
|
2021-04-18 23:17:48 +02:00
|
|
|
* @throws StandardException
|
|
|
|
* @throws d3ShopCompatibilityAdapterException
|
|
|
|
* @throws d3_cfg_mod_exception
|
|
|
|
*/
|
2021-04-19 13:30:52 +02:00
|
|
|
public function run($format = RendererBridge::FORMAT_CSV)
|
2021-04-16 14:04:30 +02:00
|
|
|
{
|
2021-04-18 23:17:48 +02:00
|
|
|
$query = trim($this->getQuery());
|
|
|
|
|
|
|
|
if (strtolower(substr($query, 0, 6)) !== 'select') {
|
|
|
|
/** @var StandardException $e */
|
|
|
|
throw oxNew(
|
|
|
|
StandardException::class,
|
|
|
|
$this->getTitle().' - '.Registry::getLang()->translateString('D3_DATAWIZARD_EXPORT_NOSELECT')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-04-19 00:27:33 +02:00
|
|
|
$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]);
|
|
|
|
|
2021-04-19 13:30:52 +02:00
|
|
|
$content = $this->renderContent($rows, $fieldNames, $format);
|
2021-04-19 00:27:33 +02:00
|
|
|
|
|
|
|
/** @var $oFS d3filesystem */
|
|
|
|
$oFS = oxNew(d3filesystem::class);
|
2021-04-19 13:30:52 +02:00
|
|
|
$oFS->startDirectDownload(
|
2021-04-19 14:36:25 +02:00
|
|
|
$oFS->filterFilename($this->getExportFileName($format)),
|
2021-04-19 13:30:52 +02:00
|
|
|
$content
|
|
|
|
);
|
2021-04-16 14:04:30 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function getButtonText() : string
|
|
|
|
{
|
|
|
|
return "D3_DATAWIZARD_EXPORT_SUBMIT";
|
|
|
|
}
|
2021-04-19 00:27:33 +02:00
|
|
|
|
|
|
|
/**
|
2021-04-19 13:30:52 +02:00
|
|
|
* @param $format
|
|
|
|
*
|
|
|
|
* @return ExportRenderer\RendererInterface
|
2021-04-19 00:27:33 +02:00
|
|
|
*/
|
2021-04-19 13:30:52 +02:00
|
|
|
public function getRenderer($format): ExportRenderer\RendererInterface
|
2021-04-19 00:27:33 +02:00
|
|
|
{
|
2021-04-19 13:30:52 +02:00
|
|
|
return oxNew(RendererBridge::class)->getRenderer($format);
|
|
|
|
}
|
2021-04-19 00:27:33 +02:00
|
|
|
|
2021-04-19 13:30:52 +02:00
|
|
|
public function getFileExtension($format)
|
|
|
|
{
|
|
|
|
return $this->getRenderer($format)->getFileExtension();
|
|
|
|
}
|
2021-04-19 00:27:33 +02:00
|
|
|
|
2021-04-19 13:30:52 +02:00
|
|
|
/**
|
|
|
|
* @param $rows
|
|
|
|
* @param $fieldnames
|
|
|
|
* @param $format
|
|
|
|
*
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function renderContent($rows, $fieldnames, $format)
|
|
|
|
{
|
|
|
|
$renderer = $this->getRenderer($format);
|
|
|
|
return $renderer->getContent($rows, $fieldnames);
|
2021-04-19 00:27:33 +02:00
|
|
|
}
|
2021-04-19 14:36:25 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $format
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getExportFileName($format) : string
|
|
|
|
{
|
|
|
|
return $this->getExportFilenameBase().'_'.date('Y-m-d_H-i-s').'.'.$this->getFileExtension($format);
|
|
|
|
}
|
2021-04-16 14:04:30 +02:00
|
|
|
}
|