2021-04-16 14:04:30 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
2021-04-20 11:20:34 +02:00
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
2022-01-17 10:59:18 +01:00
|
|
|
*
|
2021-04-20 11:20:34 +02:00
|
|
|
* https://www.d3data.de
|
2021-04-16 14:04:30 +02:00
|
|
|
*
|
|
|
|
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
|
2021-04-20 11:20:34 +02:00
|
|
|
* @author D3 Data Development - Daniel Seifert <info@shopmodule.com>
|
|
|
|
* @link https://www.oxidmodule.com
|
2021-04-16 14:04:30 +02:00
|
|
|
*/
|
|
|
|
|
2021-04-20 09:57:44 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2021-04-16 14:04:30 +02:00
|
|
|
namespace D3\DataWizard\Application\Model;
|
|
|
|
|
2021-07-14 12:21:12 +02:00
|
|
|
use D3\DataWizard\Application\Model\Exceptions\ExportFileException;
|
2021-06-25 15:33:57 +02:00
|
|
|
use D3\DataWizard\Application\Model\Exceptions\InputUnvalidException;
|
2024-03-04 13:57:10 +01:00
|
|
|
use D3\DataWizard\Application\Model\Exceptions\NoSuitableRendererException;
|
2021-04-19 13:30:52 +02:00
|
|
|
use D3\DataWizard\Application\Model\ExportRenderer\RendererBridge;
|
2024-03-04 13:57:10 +01:00
|
|
|
use D3\DataWizard\Application\Model\ExportRenderer\RendererInterface;
|
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;
|
2024-03-04 13:57:10 +01:00
|
|
|
use Doctrine\DBAL\Connection;
|
|
|
|
use Doctrine\DBAL\Driver\Exception;
|
|
|
|
use Doctrine\DBAL\Exception as DBALException;
|
2021-06-25 15:33:57 +02:00
|
|
|
use FormManager\Inputs\Checkbox;
|
|
|
|
use FormManager\Inputs\Input;
|
|
|
|
use FormManager\Inputs\Radio;
|
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;
|
2024-03-04 13:57:10 +01:00
|
|
|
use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory;
|
|
|
|
use OxidEsales\EshopCommunity\Internal\Framework\Database\ConnectionProviderInterface;
|
|
|
|
use Psr\Container\ContainerExceptionInterface;
|
|
|
|
use Psr\Container\NotFoundExceptionInterface;
|
2021-04-16 14:04:30 +02:00
|
|
|
|
|
|
|
abstract class ExportBase implements QueryBase
|
|
|
|
{
|
2024-03-04 13:57:10 +01:00
|
|
|
protected array $formElements = [];
|
2021-06-25 15:33:57 +02:00
|
|
|
|
2021-04-28 13:39:03 +02:00
|
|
|
/**
|
2021-10-31 23:16:03 +01:00
|
|
|
* Ensure that the translations are equally available in the frontend and the backend
|
2021-04-28 13:39:03 +02:00
|
|
|
* @return string
|
|
|
|
*/
|
2022-01-17 10:59:18 +01:00
|
|
|
public function getDescription(): string
|
2021-04-28 13:39:03 +02:00
|
|
|
{
|
|
|
|
return '';
|
|
|
|
}
|
2022-01-17 10:59:18 +01:00
|
|
|
|
2021-04-18 23:17:48 +02:00
|
|
|
/**
|
2021-04-19 13:30:52 +02:00
|
|
|
* @param string $format
|
2024-03-04 13:57:10 +01:00
|
|
|
* @param null $path
|
2021-04-19 13:30:52 +02:00
|
|
|
*
|
2024-03-04 13:57:10 +01:00
|
|
|
* @return string
|
|
|
|
* @throws ContainerExceptionInterface
|
2021-04-19 13:30:52 +02:00
|
|
|
* @throws DBALException
|
2021-04-19 00:27:33 +02:00
|
|
|
* @throws DatabaseConnectionException
|
|
|
|
* @throws DatabaseErrorException
|
2024-03-04 13:57:10 +01:00
|
|
|
* @throws Exception
|
|
|
|
* @throws NoSuitableRendererException
|
|
|
|
* @throws NotFoundExceptionInterface
|
2021-04-18 23:17:48 +02:00
|
|
|
* @throws StandardException
|
|
|
|
* @throws d3ShopCompatibilityAdapterException
|
|
|
|
* @throws d3_cfg_mod_exception
|
|
|
|
*/
|
2022-01-17 10:59:18 +01:00
|
|
|
public function run(string $format = RendererBridge::FORMAT_CSV, $path = null): string
|
2021-04-16 14:04:30 +02:00
|
|
|
{
|
2021-06-25 15:33:57 +02:00
|
|
|
if ($this->hasFormElements()) {
|
|
|
|
/** @var Input $element */
|
|
|
|
foreach ($this->getFormElements() as $element) {
|
|
|
|
if (false === $element->isValid()) {
|
|
|
|
throw oxNew(InputUnvalidException::class, $this, $element);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-28 23:02:15 +01:00
|
|
|
return $this->executeExport($format, $path);
|
2021-04-16 14:04:30 +02:00
|
|
|
}
|
|
|
|
|
2021-04-20 09:50:49 +02:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2022-01-17 10:59:18 +01:00
|
|
|
public function getButtonText(): string
|
2021-04-16 14:04:30 +02:00
|
|
|
{
|
|
|
|
return "D3_DATAWIZARD_EXPORT_SUBMIT";
|
|
|
|
}
|
2021-04-19 00:27:33 +02:00
|
|
|
|
|
|
|
/**
|
2024-03-04 13:57:10 +01:00
|
|
|
* @throws NoSuitableRendererException
|
2021-04-19 00:27:33 +02:00
|
|
|
*/
|
2024-03-04 13:57:10 +01:00
|
|
|
public function getRenderer(string $format): RendererInterface
|
2021-04-19 00:27:33 +02:00
|
|
|
{
|
2021-10-29 11:37:51 +02:00
|
|
|
return $this->getRendererBridge()->getRenderer($format);
|
|
|
|
}
|
|
|
|
|
2021-12-20 13:41:24 +01:00
|
|
|
public function getRendererBridge(): RendererBridge
|
2021-10-29 11:37:51 +02:00
|
|
|
{
|
|
|
|
return oxNew(RendererBridge::class);
|
2021-04-19 13:30:52 +02:00
|
|
|
}
|
2021-04-19 00:27:33 +02:00
|
|
|
|
2021-04-20 09:50:49 +02:00
|
|
|
/**
|
2024-03-04 13:57:10 +01:00
|
|
|
* @throws NoSuitableRendererException
|
2021-04-20 09:50:49 +02:00
|
|
|
*/
|
2024-03-04 13:57:10 +01:00
|
|
|
public function getFileExtension(string $format): string
|
2021-04-19 13:30:52 +02:00
|
|
|
{
|
|
|
|
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
|
|
|
|
*
|
2021-04-20 09:50:49 +02:00
|
|
|
* @return string
|
|
|
|
* @throws Exceptions\NoSuitableRendererException
|
2021-04-19 13:30:52 +02:00
|
|
|
*/
|
2021-04-20 09:50:49 +02:00
|
|
|
public function renderContent($rows, $fieldnames, $format): string
|
2021-04-19 13:30:52 +02:00
|
|
|
{
|
|
|
|
$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
|
|
|
|
2021-04-22 11:30:48 +02:00
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
*/
|
2022-01-17 10:59:18 +01:00
|
|
|
public function getExportFilenameBase(): string
|
2021-04-22 11:30:48 +02:00
|
|
|
{
|
|
|
|
return $this->getTitle();
|
|
|
|
}
|
|
|
|
|
2021-04-19 14:36:25 +02:00
|
|
|
/**
|
|
|
|
* @param $format
|
|
|
|
*
|
|
|
|
* @return string
|
2021-04-20 09:50:49 +02:00
|
|
|
* @throws Exceptions\NoSuitableRendererException
|
2021-04-19 14:36:25 +02:00
|
|
|
*/
|
2022-01-17 10:59:18 +01:00
|
|
|
public function getExportFileName($format): string
|
2021-04-19 14:36:25 +02:00
|
|
|
{
|
|
|
|
return $this->getExportFilenameBase().'_'.date('Y-m-d_H-i-s').'.'.$this->getFileExtension($format);
|
|
|
|
}
|
2021-04-20 09:50:49 +02:00
|
|
|
|
|
|
|
/**
|
2021-04-20 16:18:40 +02:00
|
|
|
* @param array $query
|
2021-04-20 09:50:49 +02:00
|
|
|
*
|
|
|
|
* @return array
|
2024-03-04 13:57:10 +01:00
|
|
|
* @throws DBALException
|
|
|
|
* @throws Exception
|
|
|
|
* @throws ContainerExceptionInterface
|
|
|
|
* @throws NotFoundExceptionInterface
|
2021-04-20 09:50:49 +02:00
|
|
|
*/
|
2022-01-17 10:59:18 +01:00
|
|
|
public function getExportData(array $query): array
|
2021-04-20 09:50:49 +02:00
|
|
|
{
|
2021-04-20 16:18:40 +02:00
|
|
|
[ $queryString, $parameters ] = $query;
|
|
|
|
|
|
|
|
$queryString = trim($queryString);
|
|
|
|
|
2022-01-17 10:59:18 +01:00
|
|
|
if (strtolower(substr($queryString, 0, 6)) !== 'select') {
|
2021-04-20 09:50:49 +02:00
|
|
|
throw oxNew(
|
|
|
|
Exceptions\TaskException::class,
|
|
|
|
$this,
|
2022-01-17 10:59:18 +01:00
|
|
|
Registry::getLang()->translateString('D3_DATAWIZARD_ERR_NOEXPORTSELECT')
|
2021-04-20 09:50:49 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2024-03-04 13:57:10 +01:00
|
|
|
/** @var Connection $connection */
|
|
|
|
$connection = ContainerFactory::getInstance()->getContainer()->get(ConnectionProviderInterface::class)->get();
|
|
|
|
$rows = $connection->executeQuery($queryString, $parameters)->fetchAllAssociative();
|
2021-04-20 09:50:49 +02:00
|
|
|
|
2022-01-17 10:59:18 +01:00
|
|
|
if (count($rows) <= 0) {
|
2021-04-20 09:50:49 +02:00
|
|
|
throw oxNew(
|
|
|
|
Exceptions\TaskException::class,
|
|
|
|
$this,
|
2022-01-17 10:59:18 +01:00
|
|
|
Registry::getLang()->translateString('D3_DATAWIZARD_ERR_NOEXPORTCONTENT', null, true)
|
2021-04-20 09:50:49 +02:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-01-17 10:59:18 +01:00
|
|
|
$fieldNames = array_keys($rows[0]);
|
2021-04-20 09:50:49 +02:00
|
|
|
|
|
|
|
return [ $rows, $fieldNames ];
|
|
|
|
}
|
2021-06-25 15:33:57 +02:00
|
|
|
|
2024-03-04 13:57:10 +01:00
|
|
|
public function registerFormElement(Input $input): void
|
2021-06-25 15:33:57 +02:00
|
|
|
{
|
2021-11-28 23:02:15 +01:00
|
|
|
if ($input instanceof Radio || $input instanceof Checkbox) {
|
|
|
|
$input->setTemplate('<p class="form-check">{{ input }} {{ label }}</p>');
|
|
|
|
$input->setAttribute('class', 'form-check-input');
|
|
|
|
} else {
|
|
|
|
$input->setTemplate('<p class="formElements">{{ label }} {{ input }}</p>');
|
|
|
|
$input->setAttribute('class', 'form-control');
|
2021-06-25 15:33:57 +02:00
|
|
|
}
|
|
|
|
$this->formElements[] = $input;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function hasFormElements(): bool
|
|
|
|
{
|
|
|
|
return (bool) count($this->formElements);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function getFormElements(): array
|
|
|
|
{
|
|
|
|
return $this->formElements;
|
|
|
|
}
|
2021-11-28 23:02:15 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $format
|
2024-03-04 13:57:10 +01:00
|
|
|
* @param $path
|
|
|
|
*
|
2021-11-28 23:02:15 +01:00
|
|
|
* @return string
|
2024-03-04 13:57:10 +01:00
|
|
|
* @throws ContainerExceptionInterface
|
2021-11-28 23:02:15 +01:00
|
|
|
* @throws DBALException
|
|
|
|
* @throws DatabaseConnectionException
|
|
|
|
* @throws DatabaseErrorException
|
2024-03-04 13:57:10 +01:00
|
|
|
* @throws Exception
|
|
|
|
* @throws NoSuitableRendererException
|
|
|
|
* @throws NotFoundExceptionInterface
|
2021-11-28 23:02:15 +01:00
|
|
|
* @throws StandardException
|
|
|
|
* @throws d3ShopCompatibilityAdapterException
|
|
|
|
* @throws d3_cfg_mod_exception
|
|
|
|
*/
|
|
|
|
protected function executeExport(string $format, $path): string
|
|
|
|
{
|
2022-01-17 10:59:18 +01:00
|
|
|
$content = $this->getContent($format);
|
2021-11-28 23:02:15 +01:00
|
|
|
|
|
|
|
$oFS = $this->getFileSystem();
|
|
|
|
if (is_null($path)) {
|
|
|
|
$oFS->startDirectDownload($oFS->filterFilename($this->getExportFileName($format)), $content);
|
|
|
|
} else {
|
|
|
|
$filePath = $oFS->trailingslashit($path) . $oFS->filterFilename($this->getExportFileName($format));
|
2024-03-04 13:57:10 +01:00
|
|
|
if (false === $oFS->createFile($filePath, $content)) {
|
2021-11-28 23:02:15 +01:00
|
|
|
throw oxNew(ExportFileException::class, $filePath);
|
|
|
|
}
|
|
|
|
return $filePath;
|
|
|
|
}
|
|
|
|
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2024-03-04 13:57:10 +01:00
|
|
|
protected function getFileSystem(): d3filesystem
|
2021-11-28 23:02:15 +01:00
|
|
|
{
|
|
|
|
return oxNew(d3filesystem::class);
|
|
|
|
}
|
2021-12-21 13:27:19 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param string $format
|
|
|
|
*
|
|
|
|
* @return string
|
2024-03-04 13:57:10 +01:00
|
|
|
* @throws ContainerExceptionInterface
|
|
|
|
* @throws DBALException
|
|
|
|
* @throws Exception
|
|
|
|
* @throws NoSuitableRendererException
|
|
|
|
* @throws NotFoundExceptionInterface
|
2021-12-21 13:27:19 +01:00
|
|
|
*/
|
2022-01-17 10:59:18 +01:00
|
|
|
public function getContent(string $format): string
|
2021-12-21 13:27:19 +01:00
|
|
|
{
|
2022-01-17 10:59:18 +01:00
|
|
|
[ $rows, $fieldNames ] = $this->getExportData($this->getQuery());
|
2021-12-21 13:27:19 +01:00
|
|
|
|
2024-03-04 13:57:10 +01:00
|
|
|
return $this->renderContent($rows, $fieldNames, $format);
|
2021-12-21 13:27:19 +01:00
|
|
|
}
|
2022-01-17 10:59:18 +01:00
|
|
|
}
|