337 lines
8.1 KiB
PHP
337 lines
8.1 KiB
PHP
<?php
|
|
/**
|
|
* Copyright since 2024 Webshop System
|
|
*
|
|
* Zentrale Registry für den aktuellen Webshop-Kontext (User, Shop, Sprache, etc.)
|
|
*
|
|
* @author Webshop System
|
|
* @license GPL v3
|
|
*/
|
|
|
|
class Context
|
|
{
|
|
/** @var Context|null */
|
|
protected static $instance;
|
|
|
|
/** @var Cart|null */
|
|
public $cart;
|
|
|
|
/** @var Customer|null */
|
|
public $customer;
|
|
|
|
/** @var Cookie|null */
|
|
public $cookie;
|
|
|
|
/** @var Link|null */
|
|
public $link;
|
|
|
|
/** @var Country|null */
|
|
public $country;
|
|
|
|
/** @var Employee|null */
|
|
public $employee;
|
|
|
|
/** @var Controller|null */
|
|
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()
|
|
{
|
|
// Initialisierung erfolgt später
|
|
}
|
|
|
|
/**
|
|
* Singleton-Instanz holen
|
|
* @return Context|null
|
|
*/
|
|
public static function getContext()
|
|
{
|
|
if (!isset(self::$instance)) {
|
|
self::$instance = new Context();
|
|
}
|
|
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;
|
|
}
|
|
}
|
|
}
|