2022-12-18 23:45:14 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*
|
|
|
|
* https://www.d3data.de
|
|
|
|
*
|
|
|
|
* @copyright (C) D3 Data Development (Inh. Thomas Dartsch)
|
|
|
|
* @author D3 Data Development - Daniel Seifert <support@shopmodule.com>
|
|
|
|
* @link https://www.oxidmodule.com
|
|
|
|
*/
|
|
|
|
|
|
|
|
declare(strict_types=1);
|
|
|
|
|
|
|
|
namespace D3\Linkmobility4OXID\Setup;
|
|
|
|
|
|
|
|
use D3\Linkmobility4OXID\Application\Model\MessageTypes\AbstractMessage;
|
|
|
|
use Doctrine\DBAL\Driver\Exception as DoctrineDriverException;
|
2023-01-10 23:33:48 +01:00
|
|
|
use Doctrine\DBAL\Driver\ResultStatement;
|
2022-12-18 23:45:14 +01:00
|
|
|
use Doctrine\DBAL\Exception as DoctrineException;
|
|
|
|
use Doctrine\DBAL\Query\QueryBuilder;
|
2023-01-10 23:33:48 +01:00
|
|
|
use Doctrine\DBAL\Statement;
|
2023-01-09 22:45:53 +01:00
|
|
|
use Monolog\Logger;
|
|
|
|
use OxidEsales\Eshop\Core\Database\Adapter\DatabaseInterface;
|
|
|
|
use OxidEsales\Eshop\Core\Database\Adapter\Doctrine\Database;
|
2022-12-18 23:45:14 +01:00
|
|
|
use OxidEsales\Eshop\Core\DatabaseProvider;
|
2022-12-19 10:59:38 +01:00
|
|
|
use OxidEsales\Eshop\Core\DbMetaDataHandler;
|
2022-12-18 23:45:14 +01:00
|
|
|
use OxidEsales\Eshop\Core\Exception\DatabaseConnectionException;
|
|
|
|
use OxidEsales\Eshop\Core\Exception\DatabaseErrorException;
|
2023-01-09 22:45:53 +01:00
|
|
|
use OxidEsales\Eshop\Core\Exception\StandardException;
|
2022-12-18 23:45:14 +01:00
|
|
|
use OxidEsales\Eshop\Core\Registry;
|
2023-01-09 22:45:53 +01:00
|
|
|
use OxidEsales\Eshop\Core\UtilsView;
|
2022-12-18 23:45:14 +01:00
|
|
|
use OxidEsales\EshopCommunity\Internal\Container\ContainerFactory;
|
2023-01-10 23:33:48 +01:00
|
|
|
use OxidEsales\EshopCommunity\Internal\Framework\Database\QueryBuilderFactory;
|
2022-12-18 23:45:14 +01:00
|
|
|
use OxidEsales\EshopCommunity\Internal\Framework\Database\QueryBuilderFactoryInterface;
|
|
|
|
use Psr\Container\ContainerExceptionInterface;
|
2023-01-09 22:45:53 +01:00
|
|
|
use Psr\Container\ContainerInterface;
|
2022-12-18 23:45:14 +01:00
|
|
|
use Psr\Container\NotFoundExceptionInterface;
|
2023-01-09 22:45:53 +01:00
|
|
|
use Psr\Log\LoggerInterface;
|
2022-12-18 23:45:14 +01:00
|
|
|
|
|
|
|
class Actions
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
* @throws ContainerExceptionInterface
|
|
|
|
* @throws NotFoundExceptionInterface
|
|
|
|
*/
|
|
|
|
public function setupDatabase()
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
if (!$this->hasRemarkTypeEnumValue()) {
|
|
|
|
$this->addRemarkTypeEnumValue();
|
|
|
|
}
|
2023-01-09 22:45:53 +01:00
|
|
|
} catch (StandardException|DoctrineDriverException|DoctrineException $e) {
|
|
|
|
/** @var Logger $logger */
|
|
|
|
$logger = d3GetOxidDIC()->get('d3ox.linkmobility.'.LoggerInterface::class);
|
|
|
|
$logger->error($e->getMessage());
|
|
|
|
/** @var UtilsView $utilsView */
|
|
|
|
$utilsView = d3GetOxidDIC()->get('d3ox.linkmobility.'.UtilsView::class);
|
2023-01-10 23:33:48 +01:00
|
|
|
$utilsView->addErrorToDisplay($e->getMessage());
|
2022-12-18 23:45:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-19 10:59:38 +01:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Regenerate views for changed tables
|
|
|
|
*/
|
2023-01-10 23:33:48 +01:00
|
|
|
public function regenerateViews(): void
|
2022-12-19 10:59:38 +01:00
|
|
|
{
|
2023-01-09 22:45:53 +01:00
|
|
|
/** @var DbMetaDataHandler $oDbMetaDataHandler */
|
|
|
|
$oDbMetaDataHandler = d3GetOxidDIC()->get('d3ox.linkmobility.'.DbMetaDataHandler::class);
|
2022-12-19 10:59:38 +01:00
|
|
|
$oDbMetaDataHandler->updateViews();
|
|
|
|
}
|
|
|
|
|
2022-12-18 23:45:14 +01:00
|
|
|
/**
|
|
|
|
* @return bool
|
|
|
|
* @throws ContainerExceptionInterface
|
|
|
|
* @throws DoctrineDriverException
|
|
|
|
* @throws DoctrineException
|
|
|
|
* @throws NotFoundExceptionInterface
|
|
|
|
*/
|
|
|
|
protected function hasRemarkTypeEnumValue(): bool
|
|
|
|
{
|
|
|
|
$fieldType = $this->getRemarkTypeFieldType();
|
|
|
|
|
|
|
|
$patternEnumCheck = '/^\b(enum)\b/mi';
|
|
|
|
if (!preg_match($patternEnumCheck, $fieldType)) {
|
2023-01-09 22:45:53 +01:00
|
|
|
throw oxNew(StandardException::class, 'remark type field has not the expected enum type');
|
2022-12-18 23:45:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
$patternValueCheck = '/\b('.preg_quote(AbstractMessage::REMARK_IDENT).')\b/mi';
|
|
|
|
|
|
|
|
return (bool) preg_match($patternValueCheck, $fieldType);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return string
|
|
|
|
* @throws DoctrineDriverException
|
|
|
|
* @throws DoctrineException
|
|
|
|
* @throws ContainerExceptionInterface
|
|
|
|
* @throws NotFoundExceptionInterface
|
|
|
|
*/
|
|
|
|
protected function getRemarkTypeFieldType(): string
|
|
|
|
{
|
2023-01-10 23:33:48 +01:00
|
|
|
/** @var QueryBuilderFactory $queryBuilderFactory */
|
|
|
|
$queryBuilderFactory = $this->getContainer()->get(QueryBuilderFactoryInterface::class);
|
2022-12-18 23:45:14 +01:00
|
|
|
/** @var QueryBuilder $qb */
|
2023-01-10 23:33:48 +01:00
|
|
|
$qb = $queryBuilderFactory->create();
|
2022-12-18 23:45:14 +01:00
|
|
|
$qb->select('column_type')
|
|
|
|
->from('INFORMATION_SCHEMA.COLUMNS')
|
|
|
|
->where(
|
|
|
|
$qb->expr()->and(
|
|
|
|
$qb->expr()->eq(
|
|
|
|
'table_schema',
|
|
|
|
$qb->createNamedParameter(Registry::getConfig()->getConfigParam('dbName'))
|
|
|
|
),
|
|
|
|
$qb->expr()->eq(
|
|
|
|
'table_name',
|
|
|
|
$qb->createNamedParameter('oxremark')
|
|
|
|
),
|
|
|
|
$qb->expr()->eq(
|
|
|
|
'COLUMN_NAME',
|
|
|
|
$qb->createNamedParameter('oxtype')
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2023-01-10 23:33:48 +01:00
|
|
|
/** @var Statement $statement */
|
|
|
|
$statement = $qb->execute();
|
|
|
|
|
|
|
|
return (string) $statement->fetchOne();
|
2022-12-18 23:45:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
* @throws DoctrineDriverException
|
|
|
|
* @throws DoctrineException
|
|
|
|
* @throws DatabaseConnectionException
|
|
|
|
* @throws DatabaseErrorException
|
|
|
|
* @throws ContainerExceptionInterface
|
|
|
|
* @throws NotFoundExceptionInterface
|
|
|
|
*/
|
|
|
|
protected function addRemarkTypeEnumValue()
|
2023-01-09 22:45:53 +01:00
|
|
|
{
|
|
|
|
$items = $this->getUniqueFieldTypes();
|
|
|
|
|
|
|
|
/** @var Database $db */
|
|
|
|
$db = d3GetOxidDIC()->get('d3ox.linkmobility.'.DatabaseInterface::class.'.assoc');
|
|
|
|
|
|
|
|
$query = 'ALTER TABLE '.$db->quoteIdentifier('oxremark').
|
|
|
|
' CHANGE '.$db->quoteIdentifier('OXTYPE'). ' '.$db->quoteIdentifier('OXTYPE') .
|
|
|
|
' enum('.implode(',', $db->quoteArray($items)).')'.
|
|
|
|
' COLLATE '.$db->quote('utf8_general_ci').' NOT NULL DEFAULT '.$db->quote('r');
|
|
|
|
|
|
|
|
$db->execute($query);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-01-10 23:33:48 +01:00
|
|
|
* @return string[]
|
2023-01-09 22:45:53 +01:00
|
|
|
* @throws DoctrineDriverException
|
|
|
|
* @throws DoctrineException
|
|
|
|
*/
|
|
|
|
protected function getUniqueFieldTypes(): array
|
2022-12-18 23:45:14 +01:00
|
|
|
{
|
|
|
|
$valuePattern = '/(?<=enum\().*(?=\))/i';
|
|
|
|
preg_match($valuePattern, $this->getRemarkTypeFieldType(), $matches);
|
|
|
|
|
2023-01-09 22:45:53 +01:00
|
|
|
return array_unique(
|
2022-12-18 23:45:14 +01:00
|
|
|
array_merge(
|
|
|
|
str_getcsv($matches[0], ',', "'"),
|
|
|
|
[AbstractMessage::REMARK_IDENT]
|
|
|
|
)
|
|
|
|
);
|
2023-01-09 22:45:53 +01:00
|
|
|
}
|
2022-12-18 23:45:14 +01:00
|
|
|
|
2023-01-09 22:45:53 +01:00
|
|
|
/**
|
|
|
|
* @return ContainerInterface
|
|
|
|
*/
|
|
|
|
public function getContainer(): ContainerInterface
|
|
|
|
{
|
|
|
|
return ContainerFactory::getInstance()->getContainer();
|
2022-12-18 23:45:14 +01:00
|
|
|
}
|
|
|
|
}
|