407 lines
11 KiB
PHP
407 lines
11 KiB
PHP
<?php
|
|
/**
|
|
* Copyright seit 2024 Webshop System
|
|
*
|
|
* Module-Base-Class für PrestaShop-Modul-Kompatibilität
|
|
*
|
|
* @author Webshop System
|
|
* @license GPL v3
|
|
*/
|
|
|
|
namespace App\Core;
|
|
|
|
use Doctrine\DBAL\DriverManager;
|
|
use Doctrine\DBAL\Exception;
|
|
|
|
abstract class Module
|
|
{
|
|
public $name;
|
|
public $tab;
|
|
public $version;
|
|
public $author;
|
|
public $need_instance;
|
|
public $ps_versions_compliancy;
|
|
public $bootstrap;
|
|
public $displayName;
|
|
public $description;
|
|
public $confirmUninstall;
|
|
public $limited_countries;
|
|
public $module_key;
|
|
|
|
protected $context;
|
|
protected $shop;
|
|
protected $config;
|
|
|
|
public function __construct()
|
|
{
|
|
$this->context = Context::getContext();
|
|
$this->shop = new Shop();
|
|
$this->config = new Configuration();
|
|
|
|
// Standardwerte setzen
|
|
$this->need_instance = 0;
|
|
$this->bootstrap = false;
|
|
$this->ps_versions_compliancy = ['min' => '1.7.0.0', 'max' => _PS_VERSION_];
|
|
}
|
|
|
|
/**
|
|
* Modul installieren
|
|
*/
|
|
public function install()
|
|
{
|
|
try {
|
|
// Modul in Datenbank eintragen
|
|
$this->registerModule();
|
|
|
|
// Modul-Konfiguration installieren
|
|
$this->installConfiguration();
|
|
|
|
// Datenbank-Tabellen erstellen
|
|
$this->installDatabase();
|
|
|
|
// Hooks registrieren
|
|
$this->registerHooks();
|
|
|
|
return true;
|
|
|
|
} catch (\Exception $e) {
|
|
error_log("Modul-Installation Fehler: " . $e->getMessage());
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Modul deinstallieren
|
|
*/
|
|
public function uninstall()
|
|
{
|
|
try {
|
|
// Hooks entfernen
|
|
$this->unregisterHooks();
|
|
|
|
// Datenbank-Tabellen entfernen
|
|
$this->uninstallDatabase();
|
|
|
|
// Modul-Konfiguration entfernen
|
|
$this->uninstallConfiguration();
|
|
|
|
// Modul aus Datenbank entfernen
|
|
$this->unregisterModule();
|
|
|
|
return true;
|
|
|
|
} catch (\Exception $e) {
|
|
error_log("Modul-Deinstallation Fehler: " . $e->getMessage());
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Modul in Datenbank registrieren
|
|
*/
|
|
protected function registerModule()
|
|
{
|
|
try {
|
|
$conn = DriverManager::getConnection([
|
|
'url' => getenv('DATABASE_URL') ?: 'mysql://root:password@localhost/webshop'
|
|
]);
|
|
|
|
$stmt = $conn->prepare('
|
|
INSERT INTO ws_module (
|
|
name, display_name, description, version, author,
|
|
tab, need_instance, bootstrap, active, created_at
|
|
) VALUES (?, ?, ?, ?, ?, ?, ?, ?, 1, NOW())
|
|
ON DUPLICATE KEY UPDATE
|
|
display_name = ?, description = ?, version = ?,
|
|
author = ?, tab = ?, need_instance = ?, bootstrap = ?,
|
|
updated_at = NOW()
|
|
');
|
|
|
|
$stmt->execute([
|
|
$this->name,
|
|
$this->displayName ?: $this->name,
|
|
$this->description ?: '',
|
|
$this->version ?: '1.0.0',
|
|
$this->author ?: 'Unknown',
|
|
$this->tab ?: 'administration',
|
|
$this->need_instance,
|
|
$this->bootstrap ? 1 : 0,
|
|
$this->displayName ?: $this->name,
|
|
$this->description ?: '',
|
|
$this->version ?: '1.0.0',
|
|
$this->author ?: 'Unknown',
|
|
$this->tab ?: 'administration',
|
|
$this->need_instance,
|
|
$this->bootstrap ? 1 : 0
|
|
]);
|
|
|
|
} catch (Exception $e) {
|
|
throw new \Exception('Modul-Registrierung Fehler: ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Modul aus Datenbank entfernen
|
|
*/
|
|
protected function unregisterModule()
|
|
{
|
|
try {
|
|
$conn = DriverManager::getConnection([
|
|
'url' => getenv('DATABASE_URL') ?: 'mysql://root:password@localhost/webshop'
|
|
]);
|
|
|
|
$stmt = $conn->prepare('
|
|
UPDATE ws_module
|
|
SET active = 0, updated_at = NOW()
|
|
WHERE name = ?
|
|
');
|
|
$stmt->execute([$this->name]);
|
|
|
|
} catch (Exception $e) {
|
|
error_log('Modul-Entfernung Fehler: ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Modul-Konfiguration installieren
|
|
*/
|
|
protected function installConfiguration()
|
|
{
|
|
// Standard-Konfiguration installieren
|
|
$this->config->set('MODULE_' . strtoupper($this->name) . '_ENABLED', true);
|
|
}
|
|
|
|
/**
|
|
* Modul-Konfiguration entfernen
|
|
*/
|
|
protected function uninstallConfiguration()
|
|
{
|
|
// Modul-Konfiguration entfernen
|
|
$this->config->delete('MODULE_' . strtoupper($this->name) . '_ENABLED');
|
|
}
|
|
|
|
/**
|
|
* Datenbank-Tabellen erstellen
|
|
*/
|
|
protected function installDatabase()
|
|
{
|
|
// Kann von Modulen überschrieben werden
|
|
}
|
|
|
|
/**
|
|
* Datenbank-Tabellen entfernen
|
|
*/
|
|
protected function uninstallDatabase()
|
|
{
|
|
// Kann von Modulen überschrieben werden
|
|
}
|
|
|
|
/**
|
|
* Hooks registrieren
|
|
*/
|
|
protected function registerHooks()
|
|
{
|
|
// Standard-Hooks können hier registriert werden
|
|
// Hook::register('displayHeader', $this->name, [$this, 'hookDisplayHeader']);
|
|
}
|
|
|
|
/**
|
|
* Hooks entfernen
|
|
*/
|
|
protected function unregisterHooks()
|
|
{
|
|
// Alle Hooks für dieses Modul entfernen
|
|
try {
|
|
$conn = DriverManager::getConnection([
|
|
'url' => getenv('DATABASE_URL') ?: 'mysql://root:password@localhost/webshop'
|
|
]);
|
|
|
|
$stmt = $conn->prepare('
|
|
UPDATE ws_hook_module
|
|
SET active = 0, updated_at = NOW()
|
|
WHERE module_name = ?
|
|
');
|
|
$stmt->execute([$this->name]);
|
|
|
|
} catch (Exception $e) {
|
|
error_log('Hook-Entfernung Fehler: ' . $e->getMessage());
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Hook registrieren
|
|
*/
|
|
public function registerHook($hookName)
|
|
{
|
|
try {
|
|
$conn = DriverManager::getConnection([
|
|
'url' => getenv('DATABASE_URL') ?: 'mysql://root:password@localhost/webshop'
|
|
]);
|
|
|
|
$stmt = $conn->prepare('
|
|
INSERT INTO ws_hook_module (hook_name, module_name, position, active, created_at)
|
|
VALUES (?, ?, 0, 1, NOW())
|
|
ON DUPLICATE KEY UPDATE active = 1, updated_at = NOW()
|
|
');
|
|
$stmt->execute([$hookName, $this->name]);
|
|
|
|
return true;
|
|
|
|
} catch (Exception $e) {
|
|
error_log('Hook-Registrierung Fehler: ' . $e->getMessage());
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Hook entfernen
|
|
*/
|
|
public function unregisterHook($hookName)
|
|
{
|
|
try {
|
|
$conn = DriverManager::getConnection([
|
|
'url' => getenv('DATABASE_URL') ?: 'mysql://root:password@localhost/webshop'
|
|
]);
|
|
|
|
$stmt = $conn->prepare('
|
|
UPDATE ws_hook_module
|
|
SET active = 0, updated_at = NOW()
|
|
WHERE hook_name = ? AND module_name = ?
|
|
');
|
|
$stmt->execute([$hookName, $this->name]);
|
|
|
|
return true;
|
|
|
|
} catch (Exception $e) {
|
|
error_log('Hook-Entfernung Fehler: ' . $e->getMessage());
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Modul-Konfiguration abrufen
|
|
*/
|
|
public function getConfig($key, $default = null)
|
|
{
|
|
return $this->config->get('MODULE_' . strtoupper($this->name) . '_' . strtoupper($key), $default);
|
|
}
|
|
|
|
/**
|
|
* Modul-Konfiguration setzen
|
|
*/
|
|
public function setConfig($key, $value)
|
|
{
|
|
return $this->config->set('MODULE_' . strtoupper($this->name) . '_' . strtoupper($key), $value);
|
|
}
|
|
|
|
/**
|
|
* Modul-Konfiguration entfernen
|
|
*/
|
|
public function deleteConfig($key)
|
|
{
|
|
return $this->config->delete('MODULE_' . strtoupper($this->name) . '_' . strtoupper($key));
|
|
}
|
|
|
|
/**
|
|
* Modul-URL generieren
|
|
*/
|
|
public function getModuleUrl($controller = null, $params = [])
|
|
{
|
|
$url = '/admin/module/' . $this->name;
|
|
|
|
if ($controller) {
|
|
$url .= '/' . $controller;
|
|
}
|
|
|
|
if (!empty($params)) {
|
|
$url .= '?' . http_build_query($params);
|
|
}
|
|
|
|
return $url;
|
|
}
|
|
|
|
/**
|
|
* Modul-Pfad abrufen
|
|
*/
|
|
public function getModulePath()
|
|
{
|
|
return __DIR__ . '/../../modules/' . $this->name . '/';
|
|
}
|
|
|
|
/**
|
|
* Modul-Template-Pfad abrufen
|
|
*/
|
|
public function getTemplatePath()
|
|
{
|
|
return $this->getModulePath() . 'views/templates/';
|
|
}
|
|
|
|
/**
|
|
* Modul-Asset-Pfad abrufen
|
|
*/
|
|
public function getAssetPath()
|
|
{
|
|
return $this->getModulePath() . 'views/assets/';
|
|
}
|
|
|
|
/**
|
|
* Modul-Admin-Controller erstellen
|
|
*/
|
|
public function getContent()
|
|
{
|
|
// Kann von Modulen überschrieben werden
|
|
return '';
|
|
}
|
|
|
|
/**
|
|
* Modul-Admin-Formular verarbeiten
|
|
*/
|
|
public function postProcess()
|
|
{
|
|
// Kann von Modulen überschrieben werden
|
|
return true;
|
|
}
|
|
|
|
/**
|
|
* Modul-Status prüfen
|
|
*/
|
|
public function isEnabled()
|
|
{
|
|
return $this->getConfig('ENABLED', false);
|
|
}
|
|
|
|
/**
|
|
* Modul aktivieren
|
|
*/
|
|
public function enable()
|
|
{
|
|
return $this->setConfig('ENABLED', true);
|
|
}
|
|
|
|
/**
|
|
* Modul deaktivieren
|
|
*/
|
|
public function disable()
|
|
{
|
|
return $this->setConfig('ENABLED', false);
|
|
}
|
|
|
|
/**
|
|
* Modul-Informationen abrufen
|
|
*/
|
|
public function getModuleInfo()
|
|
{
|
|
return [
|
|
'name' => $this->name,
|
|
'display_name' => $this->displayName ?: $this->name,
|
|
'description' => $this->description ?: '',
|
|
'version' => $this->version ?: '1.0.0',
|
|
'author' => $this->author ?: 'Unknown',
|
|
'tab' => $this->tab ?: 'administration',
|
|
'need_instance' => $this->need_instance,
|
|
'bootstrap' => $this->bootstrap,
|
|
'enabled' => $this->isEnabled(),
|
|
'path' => $this->getModulePath()
|
|
];
|
|
}
|
|
}
|