From b9cf7e04e1f3a6ffd4b9e49b1d6ff58af9f20864 Mon Sep 17 00:00:00 2001 From: Isabelle Date: Mon, 9 Feb 2026 13:53:35 +0100 Subject: [PATCH] feat(ws_faqAccordion): Add installer; create entity & db table --- config/services.yml | 18 ++++ src/Database/faqInstaller.php | 82 +++++++++++++++ src/Entity/faqAccordion.php | 117 ++++++++++++++++++++++ src/Form/formController.php | 0 src/Module/Install.php | 34 ++++++- src/Module/adminHooks.php | 16 +++ src/Repository/faqAccordionRepository.php | 20 ++++ src/Resources/data/install.sql | 7 ++ ws_faqAccordion.php | 2 + 9 files changed, 294 insertions(+), 2 deletions(-) create mode 100644 src/Database/faqInstaller.php create mode 100644 src/Entity/faqAccordion.php create mode 100644 src/Form/formController.php create mode 100644 src/Module/adminHooks.php create mode 100644 src/Repository/faqAccordionRepository.php create mode 100644 src/Resources/data/install.sql diff --git a/config/services.yml b/config/services.yml index e69de29..0bc6108 100644 --- a/config/services.yml +++ b/config/services.yml @@ -0,0 +1,18 @@ +services: + _defaults: + autowire: true + autoconfigure: true + + # Repository - this makes it available via dependency injection + Module\WsFaqAccordion\Repository\FaqAccordionRepository: + public: true + factory: ["@doctrine.orm.default_entity_manager", getRepository] + arguments: + - Module\WsFaqAccordion\Entity\FaqAccordion + + # Database installer + Module\WsFaqAccordion\Database\faqInstaller: + public: true + arguments: + - "@doctrine.dbal.default_connection" + - "%database_prefix%" diff --git a/src/Database/faqInstaller.php b/src/Database/faqInstaller.php new file mode 100644 index 0000000..6711f84 --- /dev/null +++ b/src/Database/faqInstaller.php @@ -0,0 +1,82 @@ + + * @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 Module\WsFaqAccordion\Database; + +use Doctrine\DBAL\Connection; +use Doctrine\DBAL\Exception as DBALException; + +class faqInstaller +{ + 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_faqaccordion', + ]; + 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/Entity/faqAccordion.php b/src/Entity/faqAccordion.php new file mode 100644 index 0000000..0a86521 --- /dev/null +++ b/src/Entity/faqAccordion.php @@ -0,0 +1,117 @@ + + * @copyright 2026 De Websmid + * @license Academic Free License 3.0 (AFL-3.0) + */ + +declare(strict_types=1); + +namespace Module\WsFaqAccordion\Entity; + +use Doctrine\ORM\Mapping as ORM; + +/** + * @ORM\Table() + * @ORM\Entity(repositoryClass="Module\WsFaqAccordion\Repository\FaqAccordionRepository") + */ +class faqAccordion +{ + /** + * @var int + * + * @ORM\Id + * @ORM\Column(name="id", type="integer") + * @ORM\GeneratedValue(strategy="AUTO") + */ + private $id; + + /** + * @var string + * + * @ORM\Column(name="title", type="string", length=255) + */ + private $title; + + /** + * @var string + * + * @ORM\Column(name="description", type="text") + */ + private $description; + + /** + * @var int + * + * @ORM\Column(name="position", type="integer") + */ + private $position; + + /** + * @return int|null + */ + public function getId(): ?int + { + return $this->id; + } + + /** + * @return string|null + */ + public function getTitle(): ?string + { + return $this->title; + } + + /** + * @param string $title + * @return $this + */ + public function setTitle(string $title): self + { + $this->title = $title; + + return $this; + } + + /** + * @return string|null + */ + public function getDescription(): ?string + { + return $this->description; + } + + /** + * @param string $description + * @return $this + */ + public function setDescription(string $description): self + { + $this->description = $description; + + return $this; + } + + /** + * @return int|null + */ + public function getPosition(): ?int + { + return $this->position; + } + + /** + * @param int $position + * @return $this + */ + public function setPosition(int $position): self + { + $this->position = $position; + + return $this; + } +} \ No newline at end of file diff --git a/src/Form/formController.php b/src/Form/formController.php new file mode 100644 index 0000000..e69de29 diff --git a/src/Module/Install.php b/src/Module/Install.php index 48e97e1..dae0a6c 100644 --- a/src/Module/Install.php +++ b/src/Module/Install.php @@ -2,11 +2,41 @@ namespace Module\WsFaqAccordion\Module; +use Module\WsFaqAccordion\Database\faqInstaller; + trait Install { public function install() { - return parent::install() - && $this->registerHook('displayHomeAccordion'); + return $this->installTables() + && parent::install() + && $this->registerHook('displayHomeAccordion'); + } + + private function getInstaller(): faqInstaller + { + try { + $installer = $this->get(faqInstaller::class); + } catch (\Throwable $e) { + $installer = null; + } + + if (!$installer) { + $installer = new faqInstaller( + $this->get('doctrine.dbal.default_connection'), + $this->getContainer()->getParameter('database_prefix') + ); + } + + return $installer; + } + + private function installTables(): bool + { + /** @var faqInstaller $installer */ + $installer = $this->getInstaller(); + $errors = $installer->createTables(); + + return empty($errors); } } \ No newline at end of file diff --git a/src/Module/adminHooks.php b/src/Module/adminHooks.php new file mode 100644 index 0000000..21e6fec --- /dev/null +++ b/src/Module/adminHooks.php @@ -0,0 +1,16 @@ +renderForm(); + } + + public function renderForm(){ + return("Hello! This is the configuration page in the back office."); + } +} \ No newline at end of file diff --git a/src/Repository/faqAccordionRepository.php b/src/Repository/faqAccordionRepository.php new file mode 100644 index 0000000..2797f28 --- /dev/null +++ b/src/Repository/faqAccordionRepository.php @@ -0,0 +1,20 @@ + + * @copyright 2026 De Websmid + * @license Academic Free License 3.0 (AFL-3.0) + */ + +declare(strict_types=1); + +namespace Module\WsFaqAccordion\Repository; + +use Doctrine\ORM\EntityRepository; +use Module\WsFaqAccordion\Entity\FaqAccordion; + +class faqAccordionRepository extends EntityRepository +{ + +} \ No newline at end of file diff --git a/src/Resources/data/install.sql b/src/Resources/data/install.sql new file mode 100644 index 0000000..80fc393 --- /dev/null +++ b/src/Resources/data/install.sql @@ -0,0 +1,7 @@ +CREATE TABLE IF NOT EXISTS `PREFIX_faqaccordion` ( + `id` int(11) NOT NULL AUTO_INCREMENT, + `title` varchar(255) NOT NULL, + `description` text NOT NULL, + `position` int(11) NOT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; diff --git a/ws_faqAccordion.php b/ws_faqAccordion.php index dd59dbc..89b9def 100644 --- a/ws_faqAccordion.php +++ b/ws_faqAccordion.php @@ -21,6 +21,7 @@ */ use Module\WsFaqAccordion\Module\Install; use Module\WsFaqAccordion\Module\displayHooks; +use Module\WsFaqAccordion\Module\adminHooks; if (!defined('_PS_VERSION_')) { @@ -31,6 +32,7 @@ class ws_faqAccordion extends Module { use Install; use displayHooks; + use adminHooks; public function __construct() {