Sprint 1.1: Context.php erweitert - getContext, cloneContext, updateCustomer, getTranslator, Device-Erkennung und Mobile-Erkennung implementiert

This commit is contained in:
thomas 2025-07-06 20:29:40 +02:00
parent 37d18df50c
commit 1f448be833
2 changed files with 310 additions and 21 deletions

View File

@ -2,12 +2,12 @@
## Milestone 1: Core-System Erweiterung (Sprint 1.1-1.3) ## Milestone 1: Core-System Erweiterung (Sprint 1.1-1.3)
### Sprint 1.1: Tools.php Erweiterung (75% abgeschlossen) ### Sprint 1.1: Tools.php Erweiterung (90% abgeschlossen)
- [x] Security-Funktionen (hash, getToken, AdminToken, String-Operationen, Utility-Funktionen) - [x] Security-Funktionen (hash, getToken, AdminToken, String-Operationen, Utility-Funktionen)
- [x] File-Operationen (deleteDirectory, file_get_contents, copy, scandir, etc.) - [x] File-Operationen (deleteDirectory, file_get_contents, copy, scandir, etc.)
- [x] Math-Funktionen (ps_round, math_round, round_helper, ceilf, floorf, spreadAmount) - [x] Math-Funktionen (ps_round, math_round, round_helper, ceilf, floorf, spreadAmount)
- [x] Cache-System Erweiterung (enableCache, restoreCacheSettings, clearCache, clearCompile, clearSmartyCache, clearSf2Cache, clearAllCache, getMemoryLimit, getOctets, isX86_64arch, isPHPCLI, argvToGET, getMaxUploadSize, convertBytes) - [x] Cache-System Erweiterung (enableCache, restoreCacheSettings, clearCache, clearCompile, clearSmartyCache, clearSf2Cache, clearAllCache, getMemoryLimit, getOctets, isX86_64arch, isPHPCLI, argvToGET, getMaxUploadSize, convertBytes)
- [ ] Context.php Erweiterung - [x] Context.php Erweiterung (getContext, cloneContext, updateCustomer, getTranslator, getTranslatorFromLocale, getComputingPrecision, Device-Erkennung, Mobile-Erkennung)
- [ ] Cart.php Erweiterung - [ ] Cart.php Erweiterung
### Sprint 1.2: Datenbank & ORM (0% abgeschlossen) ### Sprint 1.2: Datenbank & ORM (0% abgeschlossen)
@ -122,10 +122,9 @@
- [ ] Monitoring Setup - [ ] Monitoring Setup
- [ ] Backup Setup - [ ] Backup Setup
## Gesamtfortschritt: 11% (1 von 18 Sprints zu 75% abgeschlossen) ## Gesamtfortschritt: 13% (1 von 18 Sprints zu 90% abgeschlossen)
### Nächste Schritte: ### Nächste Schritte:
1. Context.php Erweiterung 1. Cart.php Erweiterung
2. Cart.php Erweiterung 2. Sprint 1.1 abschließen
3. Sprint 1.1 abschließen 3. Sprint 1.2 beginnen (Datenbank & ORM)
4. Sprint 1.2 beginnen (Datenbank & ORM)

View File

@ -10,24 +10,80 @@
class Context class Context
{ {
/** @var Context */ /** @var Context|null */
protected static $instance; protected static $instance;
/** @var Shop */ /** @var Cart|null */
public $shop; public $cart;
/** @var Language */
public $language; /** @var Customer|null */
/** @var Country */ public $customer;
public $country;
/** @var Configuration */ /** @var Cookie|null */
public $configuration;
/** @var Cookie */
public $cookie; public $cookie;
/** @var Session */
public $session; /** @var Link|null */
/** @var array */ public $link;
/** @var Country|null */
public $country;
/** @var Employee|null */
public $employee;
/** @var Controller|null */
public $controller; public $controller;
/** @var Language|null */
public $language;
/** @var Currency|null */
public $currency;
/** @var Shop|null */
public $shop;
/** @var Smarty|null */
public $smarty;
/** @var int */
public $mode;
/** @var float */
public $virtualTotalTaxExcluded = 0;
/** @var float */
public $virtualTotalTaxIncluded = 0;
/** @var Translator|null */
protected $translator = null;
/** @var int */
protected $priceComputingPrecision = null;
/** @var bool|null */
protected $mobile_device = null;
/** @var bool|null */
protected $is_mobile = null;
/** @var bool|null */
protected $is_tablet = null;
/** @var MobileDetect|null */
public $mobile_detect = null;
// Device constants
public const DEVICE_COMPUTER = 1;
public const DEVICE_TABLET = 2;
public const DEVICE_MOBILE = 4;
// Mode constants
public const MODE_STD = 1;
public const MODE_STD_CONTRIB = 2;
public const MODE_HOST_CONTRIB = 4;
public const MODE_HOST = 8;
private function __construct() private function __construct()
{ {
// Initialisierung erfolgt später // Initialisierung erfolgt später
@ -35,7 +91,7 @@ class Context
/** /**
* Singleton-Instanz holen * Singleton-Instanz holen
* @return Context * @return Context|null
*/ */
public static function getContext() public static function getContext()
{ {
@ -44,4 +100,238 @@ class Context
} }
return self::$instance; return self::$instance;
} }
/**
* Set instance for testing
*
* @param Context $testInstance
*/
public static function setInstanceForTesting($testInstance)
{
self::$instance = $testInstance;
}
/**
* Delete testing instance
*/
public static function deleteTestingInstance()
{
self::$instance = null;
}
/**
* Clone current context object
*
* @return static
*/
public function cloneContext()
{
return clone $this;
}
/**
* Get MobileDetect tool object
*
* @return MobileDetect
*/
public function getMobileDetect(): MobileDetect
{
if ($this->mobile_detect === null) {
$this->mobile_detect = new MobileDetect();
}
return $this->mobile_detect;
}
/**
* Check if visitor's device is a mobile device
*
* @return bool
*/
public function isMobile(): bool
{
if ($this->is_mobile === null) {
$mobileDetect = $this->getMobileDetect();
$this->is_mobile = $mobileDetect->isMobile();
}
return $this->is_mobile;
}
/**
* Check if visitor's device is a tablet device
*
* @return bool
*/
public function isTablet(): bool
{
if ($this->is_tablet === null) {
$mobileDetect = $this->getMobileDetect();
$this->is_tablet = $mobileDetect->isTablet();
}
return $this->is_tablet;
}
/**
* Get mobile device
*
* @return bool
*/
public function getMobileDevice(): bool
{
if ($this->mobile_device === null) {
$this->mobile_device = $this->isMobile() || $this->isTablet();
}
return $this->mobile_device;
}
/**
* Get device type
*
* @return int
*/
public function getDevice(): int
{
if ($this->isMobile()) {
return self::DEVICE_MOBILE;
}
if ($this->isTablet()) {
return self::DEVICE_TABLET;
}
return self::DEVICE_COMPUTER;
}
/**
* Get current locale
*
* @return string
*/
public function getCurrentLocale()
{
if ($this->language) {
return $this->language->locale;
}
return 'en-US';
}
/**
* Update customer in context
*
* @param Customer $customer
*/
public function updateCustomer(Customer $customer)
{
// Update the customer in context object
$this->customer = $customer;
// Update basic information in the cookie
if ($this->cookie) {
$this->cookie->id_customer = (int) $customer->id;
$this->cookie->customer_lastname = $customer->lastname;
$this->cookie->customer_firstname = $customer->firstname;
$this->cookie->passwd = $customer->passwd;
$this->cookie->logged = true;
$customer->logged = true;
$this->cookie->email = $customer->email;
$this->cookie->is_guest = $customer->isGuest();
// Handle cart following
if (Configuration::get('PS_CART_FOLLOWING')
&& (empty($this->cookie->id_cart) || Cart::getNbProducts((int) $this->cookie->id_cart) == 0)
&& $idCart = (int) Cart::lastNoneOrderedCart($this->customer->id)
) {
$this->cart = new Cart($idCart);
$this->cart->secure_key = $customer->secure_key;
$this->cookie->id_guest = (int) $this->cart->id_guest;
} else {
// Initialize new visit
if (!$this->cookie->id_guest) {
Guest::setNewGuest($this->cookie);
}
// Update cart if exists
if (Validate::isLoadedObject($this->cart)) {
$this->cart->secure_key = $customer->secure_key;
$this->cart->id_guest = (int) $this->cookie->id_guest;
$this->cart->id_customer = (int) $customer->id;
$this->cart->updateAddressId($this->cart->id_address_delivery, (int) Address::getFirstCustomerAddressId((int) $customer->id));
$this->cart->id_address_delivery = (int) Address::getFirstCustomerAddressId((int) $customer->id);
$this->cart->id_address_invoice = (int) Address::getFirstCustomerAddressId((int) $customer->id);
}
}
// Save cart and update cookie
if (Validate::isLoadedObject($this->cart)) {
$this->cart->save();
$this->cookie->id_cart = (int) $this->cart->id;
}
// Save cookie
$this->cookie->write();
}
}
/**
* Get translator
*
* @param bool $isInstaller
* @return Translator
*/
public function getTranslator($isInstaller = false)
{
if (null !== $this->translator && $this->language && $this->language->locale === $this->translator->getLocale()) {
return $this->translator;
}
if ($isInstaller || !$this->language) {
$this->translator = $this->getTranslatorFromLocale('en-US');
} else {
$this->translator = $this->getTranslatorFromLocale($this->language->locale);
}
return $this->translator;
}
/**
* Get translator from locale
*
* @param string $locale
* @return Translator
*/
public function getTranslatorFromLocale($locale)
{
$cacheDir = _PS_CACHE_DIR_ . 'translations';
$translator = new Translator($locale, null, $cacheDir, false);
return $translator;
}
/**
* Get computing precision
*
* @return int
*/
public function getComputingPrecision()
{
if ($this->priceComputingPrecision === null) {
$this->priceComputingPrecision = (int) Configuration::get('PS_PRICE_COMPUTE_PRECISION');
}
return $this->priceComputingPrecision;
}
/**
* Check mobile context
*/
protected function checkMobileContext()
{
if ($this->getMobileDevice()) {
$this->mode = self::MODE_STD;
}
}
} }