826 lines
18 KiB
PHP
826 lines
18 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 ($this->translator === null) {
|
|
$this->translator = new Translator(
|
|
$this->language ? $this->language->locale : 'en-US',
|
|
null,
|
|
$isInstaller
|
|
);
|
|
}
|
|
|
|
return $this->translator;
|
|
}
|
|
|
|
/**
|
|
* Get translator from locale
|
|
*
|
|
* @param string $locale
|
|
* @return Translator
|
|
*/
|
|
public function getTranslatorFromLocale($locale)
|
|
{
|
|
return new Translator($locale);
|
|
}
|
|
|
|
/**
|
|
* Get computing precision
|
|
*
|
|
* @return int
|
|
*/
|
|
public function getComputingPrecision()
|
|
{
|
|
if ($this->priceComputingPrecision === null) {
|
|
$this->priceComputingPrecision = (int) Configuration::get('PS_PRICE_DISPLAY_PRECISION');
|
|
}
|
|
|
|
return $this->priceComputingPrecision;
|
|
}
|
|
|
|
/**
|
|
* Check mobile context
|
|
*/
|
|
protected function checkMobileContext()
|
|
{
|
|
$this->getMobileDetect();
|
|
}
|
|
|
|
/**
|
|
* Initialize context
|
|
*/
|
|
public function initialize()
|
|
{
|
|
$this->checkMobileContext();
|
|
$this->initializeLanguage();
|
|
$this->initializeCurrency();
|
|
$this->initializeShop();
|
|
$this->initializeCountry();
|
|
$this->initializeCustomer();
|
|
$this->initializeCart();
|
|
$this->initializeEmployee();
|
|
$this->initializeController();
|
|
$this->initializeSmarty();
|
|
}
|
|
|
|
/**
|
|
* Initialize language
|
|
*/
|
|
protected function initializeLanguage()
|
|
{
|
|
if (!$this->language) {
|
|
$id_lang = (int) Configuration::get('PS_LANG_DEFAULT');
|
|
$this->language = new Language($id_lang);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Initialize currency
|
|
*/
|
|
protected function initializeCurrency()
|
|
{
|
|
if (!$this->currency) {
|
|
$id_currency = (int) Configuration::get('PS_CURRENCY_DEFAULT');
|
|
$this->currency = new Currency($id_currency);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Initialize shop
|
|
*/
|
|
protected function initializeShop()
|
|
{
|
|
if (!$this->shop) {
|
|
$id_shop = (int) Configuration::get('PS_SHOP_DEFAULT');
|
|
$this->shop = new Shop($id_shop);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Initialize country
|
|
*/
|
|
protected function initializeCountry()
|
|
{
|
|
if (!$this->country) {
|
|
$id_country = (int) Configuration::get('PS_COUNTRY_DEFAULT');
|
|
$this->country = new Country($id_country);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Initialize customer
|
|
*/
|
|
protected function initializeCustomer()
|
|
{
|
|
if (!$this->customer && $this->cookie && $this->cookie->id_customer) {
|
|
$this->customer = new Customer($this->cookie->id_customer);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Initialize cart
|
|
*/
|
|
protected function initializeCart()
|
|
{
|
|
if (!$this->cart && $this->cookie && $this->cookie->id_cart) {
|
|
$this->cart = new Cart($this->cookie->id_cart);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Initialize employee
|
|
*/
|
|
protected function initializeEmployee()
|
|
{
|
|
if (!$this->employee && $this->cookie && $this->cookie->id_employee) {
|
|
$this->employee = new Employee($this->cookie->id_employee);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Initialize controller
|
|
*/
|
|
protected function initializeController()
|
|
{
|
|
if (!$this->controller) {
|
|
$this->controller = new FrontController();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Initialize smarty
|
|
*/
|
|
protected function initializeSmarty()
|
|
{
|
|
if (!$this->smarty) {
|
|
$this->smarty = new Smarty();
|
|
$this->smarty->setTemplateDir(_PS_THEME_DIR_);
|
|
$this->smarty->setCompileDir(_PS_CACHE_DIR_ . 'smarty/compile/');
|
|
$this->smarty->setCacheDir(_PS_CACHE_DIR_ . 'smarty/cache/');
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get shop context
|
|
*
|
|
* @return Shop
|
|
*/
|
|
public function getShop()
|
|
{
|
|
return $this->shop;
|
|
}
|
|
|
|
/**
|
|
* Set shop context
|
|
*
|
|
* @param Shop $shop
|
|
*/
|
|
public function setShop(Shop $shop)
|
|
{
|
|
$this->shop = $shop;
|
|
}
|
|
|
|
/**
|
|
* Get language context
|
|
*
|
|
* @return Language
|
|
*/
|
|
public function getLanguage()
|
|
{
|
|
return $this->language;
|
|
}
|
|
|
|
/**
|
|
* Set language context
|
|
*
|
|
* @param Language $language
|
|
*/
|
|
public function setLanguage(Language $language)
|
|
{
|
|
$this->language = $language;
|
|
}
|
|
|
|
/**
|
|
* Get currency context
|
|
*
|
|
* @return Currency
|
|
*/
|
|
public function getCurrency()
|
|
{
|
|
return $this->currency;
|
|
}
|
|
|
|
/**
|
|
* Set currency context
|
|
*
|
|
* @param Currency $currency
|
|
*/
|
|
public function setCurrency(Currency $currency)
|
|
{
|
|
$this->currency = $currency;
|
|
}
|
|
|
|
/**
|
|
* Get customer context
|
|
*
|
|
* @return Customer|null
|
|
*/
|
|
public function getCustomer()
|
|
{
|
|
return $this->customer;
|
|
}
|
|
|
|
/**
|
|
* Set customer context
|
|
*
|
|
* @param Customer|null $customer
|
|
*/
|
|
public function setCustomer($customer)
|
|
{
|
|
$this->customer = $customer;
|
|
}
|
|
|
|
/**
|
|
* Get cart context
|
|
*
|
|
* @return Cart|null
|
|
*/
|
|
public function getCart()
|
|
{
|
|
return $this->cart;
|
|
}
|
|
|
|
/**
|
|
* Set cart context
|
|
*
|
|
* @param Cart|null $cart
|
|
*/
|
|
public function setCart($cart)
|
|
{
|
|
$this->cart = $cart;
|
|
}
|
|
|
|
/**
|
|
* Get employee context
|
|
*
|
|
* @return Employee|null
|
|
*/
|
|
public function getEmployee()
|
|
{
|
|
return $this->employee;
|
|
}
|
|
|
|
/**
|
|
* Set employee context
|
|
*
|
|
* @param Employee|null $employee
|
|
*/
|
|
public function setEmployee($employee)
|
|
{
|
|
$this->employee = $employee;
|
|
}
|
|
|
|
/**
|
|
* Get country context
|
|
*
|
|
* @return Country
|
|
*/
|
|
public function getCountry()
|
|
{
|
|
return $this->country;
|
|
}
|
|
|
|
/**
|
|
* Set country context
|
|
*
|
|
* @param Country $country
|
|
*/
|
|
public function setCountry(Country $country)
|
|
{
|
|
$this->country = $country;
|
|
}
|
|
|
|
/**
|
|
* Get controller context
|
|
*
|
|
* @return Controller|null
|
|
*/
|
|
public function getController()
|
|
{
|
|
return $this->controller;
|
|
}
|
|
|
|
/**
|
|
* Set controller context
|
|
*
|
|
* @param Controller|null $controller
|
|
*/
|
|
public function setController($controller)
|
|
{
|
|
$this->controller = $controller;
|
|
}
|
|
|
|
/**
|
|
* Get smarty context
|
|
*
|
|
* @return Smarty|null
|
|
*/
|
|
public function getSmarty()
|
|
{
|
|
return $this->smarty;
|
|
}
|
|
|
|
/**
|
|
* Set smarty context
|
|
*
|
|
* @param Smarty|null $smarty
|
|
*/
|
|
public function setSmarty($smarty)
|
|
{
|
|
$this->smarty = $smarty;
|
|
}
|
|
|
|
/**
|
|
* Get link context
|
|
*
|
|
* @return Link|null
|
|
*/
|
|
public function getLink()
|
|
{
|
|
return $this->link;
|
|
}
|
|
|
|
/**
|
|
* Set link context
|
|
*
|
|
* @param Link|null $link
|
|
*/
|
|
public function setLink($link)
|
|
{
|
|
$this->link = $link;
|
|
}
|
|
|
|
/**
|
|
* Get cookie context
|
|
*
|
|
* @return Cookie|null
|
|
*/
|
|
public function getCookie()
|
|
{
|
|
return $this->cookie;
|
|
}
|
|
|
|
/**
|
|
* Set cookie context
|
|
*
|
|
* @param Cookie|null $cookie
|
|
*/
|
|
public function setCookie($cookie)
|
|
{
|
|
$this->cookie = $cookie;
|
|
}
|
|
|
|
/**
|
|
* Get mode
|
|
*
|
|
* @return int
|
|
*/
|
|
public function getMode()
|
|
{
|
|
return $this->mode;
|
|
}
|
|
|
|
/**
|
|
* Set mode
|
|
*
|
|
* @param int $mode
|
|
*/
|
|
public function setMode($mode)
|
|
{
|
|
$this->mode = $mode;
|
|
}
|
|
|
|
/**
|
|
* Get virtual total tax excluded
|
|
*
|
|
* @return float
|
|
*/
|
|
public function getVirtualTotalTaxExcluded()
|
|
{
|
|
return $this->virtualTotalTaxExcluded;
|
|
}
|
|
|
|
/**
|
|
* Set virtual total tax excluded
|
|
*
|
|
* @param float $total
|
|
*/
|
|
public function setVirtualTotalTaxExcluded($total)
|
|
{
|
|
$this->virtualTotalTaxExcluded = $total;
|
|
}
|
|
|
|
/**
|
|
* Get virtual total tax included
|
|
*
|
|
* @return float
|
|
*/
|
|
public function getVirtualTotalTaxIncluded()
|
|
{
|
|
return $this->virtualTotalTaxIncluded;
|
|
}
|
|
|
|
/**
|
|
* Set virtual total tax included
|
|
*
|
|
* @param float $total
|
|
*/
|
|
public function setVirtualTotalTaxIncluded($total)
|
|
{
|
|
$this->virtualTotalTaxIncluded = $total;
|
|
}
|
|
|
|
/**
|
|
* Check if context is initialized
|
|
*
|
|
* @return bool
|
|
*/
|
|
public function isInitialized()
|
|
{
|
|
return $this->shop !== null && $this->language !== null && $this->currency !== null;
|
|
}
|
|
|
|
/**
|
|
* Reset context
|
|
*/
|
|
public function reset()
|
|
{
|
|
$this->cart = null;
|
|
$this->customer = null;
|
|
$this->employee = null;
|
|
$this->controller = null;
|
|
$this->smarty = null;
|
|
$this->link = null;
|
|
$this->cookie = null;
|
|
$this->virtualTotalTaxExcluded = 0;
|
|
$this->virtualTotalTaxIncluded = 0;
|
|
}
|
|
|
|
/**
|
|
* Get context as array
|
|
*
|
|
* @return array
|
|
*/
|
|
public function toArray()
|
|
{
|
|
return [
|
|
'shop' => $this->shop ? $this->shop->id : null,
|
|
'language' => $this->language ? $this->language->id : null,
|
|
'currency' => $this->currency ? $this->currency->id : null,
|
|
'customer' => $this->customer ? $this->customer->id : null,
|
|
'cart' => $this->cart ? $this->cart->id : null,
|
|
'employee' => $this->employee ? $this->employee->id : null,
|
|
'country' => $this->country ? $this->country->id : null,
|
|
'mode' => $this->mode,
|
|
'device' => $this->getDevice(),
|
|
'is_mobile' => $this->isMobile(),
|
|
'is_tablet' => $this->isTablet()
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Load context from array
|
|
*
|
|
* @param array $data
|
|
*/
|
|
public function fromArray($data)
|
|
{
|
|
if (isset($data['shop'])) {
|
|
$this->shop = new Shop($data['shop']);
|
|
}
|
|
if (isset($data['language'])) {
|
|
$this->language = new Language($data['language']);
|
|
}
|
|
if (isset($data['currency'])) {
|
|
$this->currency = new Currency($data['currency']);
|
|
}
|
|
if (isset($data['customer'])) {
|
|
$this->customer = new Customer($data['customer']);
|
|
}
|
|
if (isset($data['cart'])) {
|
|
$this->cart = new Cart($data['cart']);
|
|
}
|
|
if (isset($data['employee'])) {
|
|
$this->employee = new Employee($data['employee']);
|
|
}
|
|
if (isset($data['country'])) {
|
|
$this->country = new Country($data['country']);
|
|
}
|
|
if (isset($data['mode'])) {
|
|
$this->mode = $data['mode'];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get context hash
|
|
*
|
|
* @return string
|
|
*/
|
|
public function getHash()
|
|
{
|
|
return md5(serialize($this->toArray()));
|
|
}
|
|
|
|
/**
|
|
* Check if context has changed
|
|
*
|
|
* @param string $previous_hash
|
|
* @return bool
|
|
*/
|
|
public function hasChanged($previous_hash)
|
|
{
|
|
return $this->getHash() !== $previous_hash;
|
|
}
|
|
}
|