DataWizard/Application/Model/ActionBase.php

152 lines
4.3 KiB
PHP
Raw Permalink Normal View History

2021-05-05 23:20:42 +02:00
<?php
/**
* 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-05-05 23:20:42 +02:00
* https://www.d3data.de
*
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
* @author D3 Data Development - Daniel Seifert <info@shopmodule.com>
* @link https://www.oxidmodule.com
*/
declare(strict_types=1);
namespace D3\DataWizard\Application\Model;
use D3\DataWizard\Application\Model\Exceptions\InputUnvalidException;
2024-03-04 13:57:10 +01:00
use D3\DataWizard\Application\Model\Exceptions\TaskException;
2024-05-13 15:23:47 +02:00
use Doctrine\DBAL\Connection;
2024-03-04 13:57:10 +01:00
use Doctrine\DBAL\Exception as DBALException;
use FormManager\Inputs\Checkbox;
use FormManager\Inputs\Input;
use FormManager\Inputs\Radio;
2021-05-05 23:20:42 +02:00
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-05-05 23:20:42 +02:00
abstract class ActionBase implements QueryBase
{
2024-03-04 13:57:10 +01:00
protected array $formElements = [];
2021-05-05 23:20:42 +02:00
/**
2021-10-31 23:16:03 +01:00
* Ensure that the translations are equally available in the frontend and the backend
2021-05-05 23:20:42 +02:00
* @return string
*/
2022-01-17 10:59:18 +01:00
public function getDescription(): string
2021-05-05 23:20:42 +02:00
{
return '';
}
2021-06-22 13:26:51 +02:00
2021-05-05 23:20:42 +02:00
/**
2024-03-04 13:57:10 +01:00
* @throws ContainerExceptionInterface
* @throws DBALException
* @throws InputUnvalidException
* @throws NotFoundExceptionInterface
* @throws TaskException
2021-05-05 23:20:42 +02:00
*/
2024-03-04 13:57:10 +01:00
public function run(): void
2021-05-05 23:20:42 +02:00
{
if ($this->hasFormElements()) {
/** @var Input $element */
foreach ($this->getFormElements() as $element) {
if (false === $element->isValid()) {
2024-03-04 13:57:10 +01:00
/** @var InputUnvalidException $exception */
$exception = oxNew(InputUnvalidException::class, $this, $element);
throw $exception;
}
}
}
2022-01-17 10:59:18 +01:00
$this->executeAction($this->getQuery());
2021-05-05 23:20:42 +02:00
}
/**
* @param array $query
*
2024-03-04 13:57:10 +01:00
* @return void
* @throws ContainerExceptionInterface
* @throws NotFoundExceptionInterface
* @throws TaskException
* @throws DBALException
2021-05-05 23:20:42 +02:00
*/
2024-03-04 13:57:10 +01:00
public function executeAction(array $query): void
2021-05-05 23:20:42 +02:00
{
[ $queryString, $parameters ] = $query;
$queryString = trim($queryString);
2022-01-17 10:59:18 +01:00
if (strtolower(substr($queryString, 0, 6)) === 'select') {
2024-03-04 13:57:10 +01:00
/** @var TaskException $exception */
$exception = oxNew(
TaskException::class,
2021-05-05 23:20:42 +02:00
$this,
2022-01-17 10:59:18 +01:00
Registry::getLang()->translateString('D3_DATAWIZARD_ERR_ACTIONSELECT')
2021-05-05 23:20:42 +02:00
);
2024-03-04 13:57:10 +01:00
throw $exception;
2021-05-05 23:20:42 +02:00
}
2024-05-13 15:23:47 +02:00
$affected = (int) $this->getConnection()->executeStatement($queryString, $parameters);
2021-06-22 13:26:51 +02:00
2024-03-04 13:57:10 +01:00
/** @var TaskException $exception */
$exception = oxNew(
TaskException::class,
2021-06-22 13:26:51 +02:00
$this,
sprintf(
Registry::getLang()->translateString(
$affected === 1 ? 'D3_DATAWIZARD_ERR_ACTIONRESULT' : 'D3_DATAWIZARD_ERR_ACTIONRESULTS'
),
$affected
)
);
2024-03-04 13:57:10 +01:00
throw $exception;
2021-11-28 01:04:46 +01:00
}
2024-05-13 15:23:47 +02:00
protected function getConnection(): Connection
{
return ContainerFactory::getInstance()->getContainer()->get(ConnectionProviderInterface::class)->get();
}
2021-06-22 13:26:51 +02:00
/**
* @return string
*/
2022-01-17 10:59:18 +01:00
public function getButtonText(): string
2021-06-22 13:26:51 +02:00
{
return "D3_DATAWIZARD_ACTION_SUBMIT";
2021-05-05 23:20:42 +02:00
}
/**
* @param Input $input
*/
2024-03-04 13:57:10 +01:00
public function registerFormElement(Input $input): void
{
2021-11-26 23:16:32 +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');
}
$this->formElements[] = $input;
}
/**
* @return bool
*/
public function hasFormElements(): bool
{
return (bool) count($this->formElements);
}
/**
* @return array
*/
public function getFormElements(): array
{
return $this->formElements;
}
2022-01-17 10:59:18 +01:00
}