feat(ws_faqAccordion): Add installer; create entity & db table

This commit is contained in:
2026-02-09 13:53:35 +01:00
parent 644f55dcc0
commit b9cf7e04e1
9 changed files with 294 additions and 2 deletions

View File

@@ -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%"

View File

@@ -0,0 +1,82 @@
<?php
/**
* 2026 De Websmid
*
* @author De Websmid <contact@De Websmid.com>
* @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;
}
}

117
src/Entity/faqAccordion.php Normal file
View File

@@ -0,0 +1,117 @@
<?php
/**
* 2026 De Websmid
*
* @author De Websmid <contact@De Websmid.com>
* @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;
}
}

View File

View File

@@ -2,11 +2,41 @@
namespace Module\WsFaqAccordion\Module;
use Module\WsFaqAccordion\Database\faqInstaller;
trait Install
{
public function install()
{
return parent::install()
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);
}
}

16
src/Module/adminHooks.php Normal file
View File

@@ -0,0 +1,16 @@
<?php
namespace Module\WsFaqAccordion\Module;
trait adminHooks
{
// This function is for the configuraion page on the back office. When you click "configure" this is called.
public function getContent()
{
return $this->renderForm();
}
public function renderForm(){
return("Hello! This is the configuration page in the back office.");
}
}

View File

@@ -0,0 +1,20 @@
<?php
/**
* 2026 De Websmid
*
* @author De Websmid <contact@De Websmid.com>
* @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
{
}

View File

@@ -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;

View File

@@ -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()
{