Newwebshop/app/Core/Context.php

575 lines
12 KiB
PHP

<?php
/**
* Copyright seit 2024 Webshop System
*
* Context-System für PrestaShop-Modul-Kompatibilität
*
* @author Webshop System
* @license GPL v3
*/
namespace App\Core;
class Context
{
private static $instance = null;
private $shop;
private $language;
private $currency;
private $country;
private $employee;
private $customer;
private $cart;
private $cookie;
private $link;
private $smarty;
private $controller;
private $mobile;
private $mobile_device;
private $mode;
private $override;
private $translator;
private $registry;
private function __construct()
{
$this->initializeContext();
}
/**
* Singleton-Instanz abrufen
*/
public static function getContext()
{
if (self::$instance === null) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Context initialisieren
*/
private function initializeContext()
{
$this->shop = new Shop();
$this->language = new Language();
$this->currency = new Currency();
$this->country = new Country();
$this->employee = null;
$this->customer = null;
$this->cart = null;
$this->cookie = new Cookie();
$this->link = new Link();
$this->smarty = new \Smarty();
$this->controller = null;
$this->mobile = false;
$this->mobile_device = false;
$this->mode = 'front';
$this->override = new Override();
$this->translator = new Translator();
$this->registry = [];
// Smarty-Konfiguration
$this->configureSmarty();
}
/**
* Smarty konfigurieren
*/
private function configureSmarty()
{
$this->smarty->setTemplateDir(__DIR__ . '/../../templates/');
$this->smarty->setCompileDir(__DIR__ . '/../../cache/smarty/compile/');
$this->smarty->setCacheDir(__DIR__ . '/../../cache/smarty/cache/');
$this->smarty->setConfigDir(__DIR__ . '/../../config/');
// Smarty-Plugins registrieren
$this->smarty->registerPlugin('function', 'l', [$this->translator, 'l']);
$this->smarty->registerPlugin('function', 'url', [$this->link, 'getPageLink']);
$this->smarty->registerPlugin('function', 'hook', [$this, 'hook']);
// Globale Variablen setzen
$this->smarty->assign('context', $this);
$this->smarty->assign('shop', $this->shop);
$this->smarty->assign('language', $this->language);
$this->smarty->assign('currency', $this->currency);
}
/**
* Shop-Instanz abrufen
*/
public function getShop()
{
return $this->shop;
}
/**
* Shop-Instanz setzen
*/
public function setShop($shop)
{
$this->shop = $shop;
return $this;
}
/**
* Language-Instanz abrufen
*/
public function getLanguage()
{
return $this->language;
}
/**
* Language-Instanz setzen
*/
public function setLanguage($language)
{
$this->language = $language;
return $this;
}
/**
* Currency-Instanz abrufen
*/
public function getCurrency()
{
return $this->currency;
}
/**
* Currency-Instanz setzen
*/
public function setCurrency($currency)
{
$this->currency = $currency;
return $this;
}
/**
* Country-Instanz abrufen
*/
public function getCountry()
{
return $this->country;
}
/**
* Country-Instanz setzen
*/
public function setCountry($country)
{
$this->country = $country;
return $this;
}
/**
* Employee-Instanz abrufen
*/
public function getEmployee()
{
return $this->employee;
}
/**
* Employee-Instanz setzen
*/
public function setEmployee($employee)
{
$this->employee = $employee;
return $this;
}
/**
* Customer-Instanz abrufen
*/
public function getCustomer()
{
return $this->customer;
}
/**
* Customer-Instanz setzen
*/
public function setCustomer($customer)
{
$this->customer = $customer;
return $this;
}
/**
* Cart-Instanz abrufen
*/
public function getCart()
{
return $this->cart;
}
/**
* Cart-Instanz setzen
*/
public function setCart($cart)
{
$this->cart = $cart;
return $this;
}
/**
* Cookie-Instanz abrufen
*/
public function getCookie()
{
return $this->cookie;
}
/**
* Cookie-Instanz setzen
*/
public function setCookie($cookie)
{
$this->cookie = $cookie;
return $this;
}
/**
* Link-Instanz abrufen
*/
public function getLink()
{
return $this->link;
}
/**
* Link-Instanz setzen
*/
public function setLink($link)
{
$this->link = $link;
return $this;
}
/**
* Smarty-Instanz abrufen
*/
public function getSmarty()
{
return $this->smarty;
}
/**
* Smarty-Instanz setzen
*/
public function setSmarty($smarty)
{
$this->smarty = $smarty;
return $this;
}
/**
* Controller-Instanz abrufen
*/
public function getController()
{
return $this->controller;
}
/**
* Controller-Instanz setzen
*/
public function setController($controller)
{
$this->controller = $controller;
return $this;
}
/**
* Mobile-Status abrufen
*/
public function getMobile()
{
return $this->mobile;
}
/**
* Mobile-Status setzen
*/
public function setMobile($mobile)
{
$this->mobile = $mobile;
return $this;
}
/**
* Mobile-Device-Status abrufen
*/
public function getMobileDevice()
{
return $this->mobile_device;
}
/**
* Mobile-Device-Status setzen
*/
public function setMobileDevice($mobile_device)
{
$this->mobile_device = $mobile_device;
return $this;
}
/**
* Mode abrufen
*/
public function getMode()
{
return $this->mode;
}
/**
* Mode setzen
*/
public function setMode($mode)
{
$this->mode = $mode;
return $this;
}
/**
* Override-Instanz abrufen
*/
public function getOverride()
{
return $this->override;
}
/**
* Translator-Instanz abrufen
*/
public function getTranslator()
{
return $this->translator;
}
/**
* Registry-Wert abrufen
*/
public function get($key, $default = null)
{
return isset($this->registry[$key]) ? $this->registry[$key] : $default;
}
/**
* Registry-Wert setzen
*/
public function set($key, $value)
{
$this->registry[$key] = $value;
return $this;
}
/**
* Registry-Wert entfernen
*/
public function remove($key)
{
unset($this->registry[$key]);
return $this;
}
/**
* Hook ausführen
*/
public function hook($hookName, $params = [])
{
return Hook::exec($hookName, $params);
}
/**
* Admin-Kontext prüfen
*/
public function isAdmin()
{
return $this->mode === 'admin';
}
/**
* Frontend-Kontext prüfen
*/
public function isFront()
{
return $this->mode === 'front';
}
/**
* CLI-Kontext prüfen
*/
public function isCli()
{
return $this->mode === 'cli';
}
/**
* Context zurücksetzen
*/
public function reset()
{
$this->employee = null;
$this->customer = null;
$this->cart = null;
$this->controller = null;
$this->registry = [];
return $this;
}
/**
* Context-Informationen abrufen
*/
public function getContextInfo()
{
return [
'mode' => $this->mode,
'mobile' => $this->mobile,
'mobile_device' => $this->mobile_device,
'shop_id' => $this->shop ? $this->shop->getId() : null,
'language_id' => $this->language ? $this->language->getId() : null,
'currency_id' => $this->currency ? $this->currency->getId() : null,
'employee_id' => $this->employee ? $this->employee->getId() : null,
'customer_id' => $this->customer ? $this->customer->getId() : null,
'cart_id' => $this->cart ? $this->cart->getId() : null
];
}
}
// Hilfsklassen für Context
class Shop
{
private $id;
private $name;
private $url;
public function __construct()
{
$this->id = 1;
$this->name = 'Webshop';
$this->url = $_SERVER['HTTP_HOST'] ?? 'localhost';
}
public function getId() { return $this->id; }
public function getName() { return $this->name; }
public function getUrl() { return $this->url; }
}
class Language
{
private $id;
private $iso_code;
private $name;
public function __construct()
{
$this->id = 1;
$this->iso_code = 'de';
$this->name = 'Deutsch';
}
public function getId() { return $this->id; }
public function getIsoCode() { return $this->iso_code; }
public function getName() { return $this->name; }
}
class Currency
{
private $id;
private $iso_code;
private $symbol;
public function __construct()
{
$this->id = 1;
$this->iso_code = 'EUR';
$this->symbol = '€';
}
public function getId() { return $this->id; }
public function getIsoCode() { return $this->iso_code; }
public function getSymbol() { return $this->symbol; }
}
class Country
{
private $id;
private $iso_code;
private $name;
public function __construct()
{
$this->id = 1;
$this->iso_code = 'DE';
$this->name = 'Deutschland';
}
public function getId() { return $this->id; }
public function getIsoCode() { return $this->iso_code; }
public function getName() { return $this->name; }
}
class Cookie
{
private $data = [];
public function __construct()
{
$this->data = $_COOKIE ?? [];
}
public function get($key, $default = null)
{
return isset($this->data[$key]) ? $this->data[$key] : $default;
}
public function set($key, $value, $expire = 0)
{
$this->data[$key] = $value;
setcookie($key, $value, $expire, '/');
}
public function delete($key)
{
unset($this->data[$key]);
setcookie($key, '', time() - 3600, '/');
}
}
class Link
{
public function getPageLink($controller, $ssl = null, $id_lang = null, $request = null, $request_url_encode = false, $id_shop = null, $relative_protocol = false)
{
$baseUrl = 'http://' . ($_SERVER['HTTP_HOST'] ?? 'localhost');
return $baseUrl . '/' . $controller . '.php';
}
}
class Translator
{
public function l($string, $class = null, $addslashes = false, $htmlentities = true)
{
// Einfache Übersetzung - in echten System würde hier eine Übersetzungsdatei geladen
$translations = [
'Home' => 'Startseite',
'Products' => 'Produkte',
'Cart' => 'Warenkorb',
'Checkout' => 'Kasse',
'Login' => 'Anmelden',
'Register' => 'Registrieren',
'Search' => 'Suche',
'Contact' => 'Kontakt',
'About' => 'Über uns',
'Terms' => 'AGB',
'Privacy' => 'Datenschutz',
'Imprint' => 'Impressum'
];
return isset($translations[$string]) ? $translations[$string] : $string;
}
}