diff --git a/src/Database/ReferenceInstaller.php b/src/Database/ReferenceInstaller.php deleted file mode 100644 index b10f753..0000000 --- a/src/Database/ReferenceInstaller.php +++ /dev/null @@ -1,82 +0,0 @@ - - * @copyright 2026 De Websmid - * @license De Websmid - * - * NOTICE OF LICENSE - * - * This source file is subject to the Academic Free License 3.0 (AFL-3.0). - * It is also available through the world-wide-web at this URL: https://opensource.org/licenses/AFL-3.0 - */ - -declare(strict_types=1); - -namespace Websmid\WsOrderReference\Database; - -use Doctrine\DBAL\Connection; -use Doctrine\DBAL\Exception as DBALException; - -class ReferenceInstaller -{ - private Connection $connection; - private string $dbPrefix; - - public function __construct(Connection $connection, string $dbPrefix) - { - $this->connection = $connection; - $this->dbPrefix = $dbPrefix; - } - - public function createTables(): array - { - $errors = []; - $this->dropTables(); - $sqlInstallFile = dirname(__DIR__) . '/Resources/data/install.sql'; - - $sqlContent = file_get_contents($sqlInstallFile); - $sqlContent = str_replace('PREFIX_', $this->dbPrefix, $sqlContent); - - $sqlQueries = array_filter(array_map('trim', explode(';', $sqlContent))); - - foreach ($sqlQueries as $query) { - if (empty($query)) { - continue; - } - - try { - $this->connection->executeQuery($query); - } catch (DBALException $e) { - $errors[] = [ - 'key' => $e->getMessage(), - 'parameters' => [], - 'domain' => 'Admin.Modules.Notification', - ]; - } - } - return $errors; - } - - public function dropTables(): array - { - $errors = []; - $tableNames = [ - 'ws_orderreference', - ]; - foreach ($tableNames as $tableName) { - $sql = 'DROP TABLE IF EXISTS ' . $this->dbPrefix . $tableName; - try { - $this->connection->executeQuery($sql); - } catch (DBALException $e) { - $errors[] = [ - 'key' => $e->getMessage(), - 'parameters' => [], - 'domain' => 'Admin.Modules.Notification', - ]; - } - } - return $errors; - } -} \ No newline at end of file diff --git a/src/Module/Install.php b/src/Module/Install.php index dd44f13..58c5f7a 100644 --- a/src/Module/Install.php +++ b/src/Module/Install.php @@ -11,7 +11,7 @@ trait Install if (!parent::install()) { return false; } - if (!$this->installTables()){ + if (!$this->createTables()){ return false; } if (!$this->registerHooks()){ @@ -21,6 +21,19 @@ trait Install return true; } + private function createTables(): bool + { + $sql = 'CREATE TABLE IF NOT EXISTS `' . _DB_PREFIX_ . 'order_reference` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `id_cart` int(11) NOT NULL, + `order_reference` varchar(255) NOT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `id_cart` (`id_cart`) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8;'; + + return \Db::getInstance()->execute($sql); + } + public function registerHooks(){ return $this->registerHook('displayShoppingCart') && $this->registerHook('displayPDFDeliverySlip') @@ -28,31 +41,4 @@ trait Install && $this->registerHook('actionFrontControllerSetMedia'); } - private function getInstaller(): ReferenceInstaller - { - try { - $installer = $this->get(ReferenceInstaller::class); - } catch (\Throwable $e) { - $installer = null; - } - - if (!$installer) { - $installer = new ReferenceInstaller( - $this->get('doctrine.dbal.default_connection'), - $this->getContainer()->getParameter('database_prefix') - ); - } - - return $installer; - } - - private function installTables(): bool - { - /** @var ReferenceInstaller $installer */ - $installer = $this->getInstaller(); - $errors = $installer->createTables(); - - return empty($errors); - } - } diff --git a/src/Resources/data/install.sql b/src/Resources/data/install.sql deleted file mode 100644 index 61de717..0000000 --- a/src/Resources/data/install.sql +++ /dev/null @@ -1,7 +0,0 @@ -CREATE TABLE IF NOT EXISTS `PREFIX_order_reference` ( - `id` int(11) NOT NULL AUTO_INCREMENT, - `id_cart` int(11) NOT NULL, - `order_reference` varchar(255) NOT NULL, - PRIMARY KEY (`id`), - UNIQUE KEY `id_cart` (`id_cart`) -) ENGINE=InnoDB DEFAULT CHARSET=utf8; \ No newline at end of file