// Context - first commit

This commit is contained in:
tDidierjean
2011-07-11 14:32:05 +00:00
parent 923269001b
commit 139bb84f87
78 changed files with 783 additions and 631 deletions
+4 -2
View File
@@ -170,7 +170,8 @@ abstract class AdminTabCore
public function __construct()
{
global $cookie;
$context = Context::getContext();
$context->tab = $this;
$this->id = Tab::getCurrentTabId();
$this->_conf = array(
1 => $this->l('Deletion successful'), 2 => $this->l('Selection successfully deleted'),
@@ -480,10 +481,11 @@ abstract class AdminTabCore
* Overload this method for custom checking
*
* @param integer $id Object id used for deleting images
* TODO This function will soon be deprecated. Use ObjectModel->deleteImage instead.
* @deprecated As of 1.5 use ObjectModel->deleteImage instead.
*/
public function deleteImage($id)
{
Tools::displayAsDeprecated();
$dir = null;
/* Deleting object images and thumbnails (cache) */
if (key_exists('dir', $this->fieldImageSettings))
+6 -3
View File
@@ -104,8 +104,11 @@ class CMSCore extends ObjectModel
return false;
}
public static function getLinks($id_lang, $selection = NULL, $active = true, $id_shop = false)
public static function getLinks($id_lang, $selection = NULL, $active = true, $id_shop = false, $context = null)
{
if (!$context)
$context = Context::getContext();
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('
SELECT c.id_cms, cl.link_rewrite, cl.meta_title
FROM '._DB_PREFIX_.'cms c
@@ -114,12 +117,12 @@ class CMSCore extends ObjectModel
'.(($selection !== NULL) ? ' AND c.id_cms IN ('.implode(',', array_map('intval', $selection)).')' : '').
($active ? ' AND c.`active` = 1 ' : '').
'ORDER BY c.`position`');
$link = new Link();
$links = array();
if ($result)
foreach ($result as $row)
{
$row['link'] = $link->getCMSLink((int)($row['id_cms']), $row['link_rewrite']);
$row['link'] = $context->link->getCMSLink((int)($row['id_cms']), $row['link_rewrite']);
$links[] = $row;
}
return $links;
+5 -3
View File
@@ -177,8 +177,11 @@ class CMSCategoryCore extends ObjectModel
);
}
static public function getRecurseCategory($id_lang = _USER_ID_LANG_, $current = 1, $active = 1, $links = 0, $id_shop = false)
static public function getRecurseCategory($id_lang = _USER_ID_LANG_, $current = 1, $active = 1, $links = 0, $id_shop = false, $context = null)
{
if (!$context)
$context = Context::getContext();
$category = Db::getInstance()->getRow('
SELECT c.`id_cms_category`, c.`id_parent`, c.`level_depth`, cl.`name`, cl.`link_rewrite`
FROM `'._DB_PREFIX_.'cms_category` c
@@ -203,10 +206,9 @@ class CMSCategoryCore extends ObjectModel
ORDER BY c.`position`');
if ($links == 1)
{
$link = new Link();
$category['link'] = $link->getCMSCategoryLink($current, $category['link_rewrite']);
foreach($category['cms'] as $key => $cms)
$category['cms'][$key]['link'] = $link->getCMSLink($cms['id_cms'], $cms['link_rewrite']);
$category['cms'][$key]['link'] = $context->link->getCMSLink($cms['id_cms'], $cms['link_rewrite']);
}
return $category;
}
+4
View File
@@ -72,6 +72,10 @@ class CartCore extends ObjectModel
/** @var string Object last modification date */
public $date_upd;
public $checkedTos = false;
public $pictures;
public $textFields;
protected static $_nbProducts = array();
protected static $_isVirtualCart = array();
+95
View File
@@ -0,0 +1,95 @@
<?php
class ContextCore
{
protected static $instance;
public $cart;
public $customer;
public $cookie;
public $link;
public $country;
public $employee;
public $controller;
public $lang;
public $currency;
public $tab;
/**
* Create a context without singleton constraint
* @param array $data
*/
public function __construct($cart = null,
$customer = null,
$cookie = null,
$link = null,
$country = null,
$employee = null,
$lang = null,
$currency = null,
$tab = null)
{
$this->cart = $cart;
$this->customer = $customer;
$this->cookie = $cookie;
$this->link = $link;
$this->country = $country;
$this->employee = $employee;
$this->lang = $lang;
$this->currency = $currency;
$this->tab = $tab;
}
/**
* Get a singleton context
*
* @return Context
*/
public static function getContext()
{
if (!isset(self::$instance))
self::$instance = new self();
return self::$instance;
}
public function setData($cart = null,
$customer = null,
$cookie = null,
$link = null,
$country = null,
$employee = null,
$lang = null,
$currency = null,
$tab = null)
{
$this->cart = $cart;
$this->customer = $customer;
$this->cookie = $cookie;
$this->link = $link;
$this->country = $country;
$this->employee = $employee;
$this->lang = $lang;
$this->currency = $currency;
$this->tab = $tab;
}
/*public function __get($var)
{
return (isset($this->data[$var]) ? $this->data[$var] : null);
}
public function __set($var, $value)
{
$this->data[$var] = $value;
}
public function __isset($var)
{
return isset($this->data[$var]);
}
public function __unset($var)
{
if (isset($this->data[$var]))
unset($this->data[$var]);
}*/
}
+6 -2
View File
@@ -43,7 +43,7 @@ class CustomerCore extends ObjectModel
public $id_gender = 9;
/** @var integer Default group ID */
public $id_default_group;
public $id_default_group = _PS_DEFAULT_CUSTOMER_GROUP_;
/** @var string Lastname */
public $lastname;
@@ -93,7 +93,11 @@ class CustomerCore extends ObjectModel
public $years;
public $days;
public $months;
public $geoloc_id_country;
public $geoloc_id_state;
public $geoloc_postcode;
protected $tables = array ('customer');
protected $fieldsRequired = array('lastname', 'passwd', 'firstname', 'email');
+150 -30
View File
@@ -28,7 +28,7 @@
class FrontControllerCore
{
public $errors = array();
protected static $smarty;
public $smarty;
protected static $cookie;
protected static $link;
protected static $cart;
@@ -52,6 +52,10 @@ class FrontControllerCore
public static $initialized = false;
protected static $currentCustomerGroups;
public $css_files;
public $js_files;
public $nb_items_per_page;
public function __construct()
{
@@ -72,7 +76,7 @@ class FrontControllerCore
public function init()
{
global $cookie, $smarty, $cart, $iso, $defaultCountry, $protocol_link, $protocol_content, $link, $css_files, $js_files;
global $link, $cookie, $cart, $smarty, $iso, $defaultCountry;
if (self::$initialized)
return;
@@ -81,8 +85,8 @@ class FrontControllerCore
$this->id_current_shop = (int)Shop::getCurrentShop();
$this->id_current_group_shop = (int)Shop::getCurrentGroupShop();
$css_files = array();
$js_files = array();
$this->css_files = array();
$this->js_files = array();
if ($this->ssl AND (empty($_SERVER['HTTPS']) OR strtolower($_SERVER['HTTPS']) == 'off') AND Configuration::get('PS_SSL_ENABLED'))
{
@@ -97,7 +101,6 @@ class FrontControllerCore
$defaultCountry = new Country((int)Configuration::get('PS_COUNTRY_DEFAULT'), Configuration::get('PS_LANG_DEFAULT'));
$cookie = new Cookie('ps');
$link = new Link();
if ($this->auth AND !$cookie->isLogged($this->guestAllowed))
Tools::redirect('index.php?controller=authentication'.($this->authRedirection ? '&back='.$this->authRedirection : ''));
@@ -115,7 +118,7 @@ class FrontControllerCore
$_GET['id_lang'] = $id_lang;
Tools::switchLanguage();
Tools::setCookieLanguage();
Tools::setCookieLanguage($cookie);
/* attribute id_lang is often needed, so we create a constant for performance reasons */
if (!defined('_USER_ID_LANG_'))
@@ -133,7 +136,7 @@ class FrontControllerCore
}
global $currency;
$currency = Tools::setCurrency();
$currency = Tools::setCurrency($cookie);
$_MODULES = array();
@@ -224,11 +227,14 @@ class FrontControllerCore
$protocol_link = (Configuration::get('PS_SSL_ENABLED') OR (!empty($_SERVER['HTTPS']) AND strtolower($_SERVER['HTTPS']) != 'off')) ? 'https://' : 'http://';
$protocol_content = ((isset($useSSL) AND $useSSL AND Configuration::get('PS_SSL_ENABLED')) OR (!empty($_SERVER['HTTPS']) AND strtolower($_SERVER['HTTPS']) != 'off')) ? 'https://' : 'http://';
if (!defined('_PS_BASE_URL_'))
define('_PS_BASE_URL_', Tools::getShopDomain(true));
if (!defined('_PS_BASE_URL_SSL_'))
define('_PS_BASE_URL_SSL_', Tools::getShopDomainSsl(true));
$link = new Link($protocol_link, $protocol_content);
$link->preloadPageLinks();
$this->canonicalRedirection();
@@ -305,7 +311,7 @@ class FrontControllerCore
// setting properties from global var
self::$cookie = $cookie;
self::$cart = $cart;
self::$smarty = $smarty;
$this->smarty = $smarty;
self::$link = $link;
if ($this->maintenance)
@@ -321,6 +327,29 @@ class FrontControllerCore
$this->iso = $iso;
$this->setMedia();
//$defaultCountry
if (isset($cookie->id_customer) && (int)$cookie->id_customer)
$customer = new Customer($cookie->id_customer);
else
$customer = new Customer();
if($cookie->id_country)
$customer->geoloc_id_country = (int)$cookie->id_country;
if($cookie->id_state)
$customer->geoloc_id_state = (int)$cookie->id_state;
if($cookie->postcode)
$customer->geoloc_postcode = (int)$cookie->postcode;
$context = Context::getContext();
$context->customer = $customer;
$context->cart = $cart;
$context->link = $link;
$context->cookie = $cookie;
$context->currency = $currency;
$context->controller = $this;
$context->language = $ps_language;
}
/* Display a maintenance page if shop is closed */
@@ -329,7 +358,7 @@ class FrontControllerCore
if (!in_array(Tools::getRemoteAddr(), explode(',', Configuration::get('PS_MAINTENANCE_IP'))))
{
header('HTTP/1.1 503 temporarily overloaded');
self::$smarty->display(_PS_THEME_DIR_.'maintenance.tpl');
$this->smarty->display(_PS_THEME_DIR_.'maintenance.tpl');
exit;
}
}
@@ -431,16 +460,16 @@ class FrontControllerCore
{
global $cookie;
Tools::addCSS(_THEME_CSS_DIR_.'global.css', 'all');
Tools::addJS(array(_PS_JS_DIR_.'jquery/jquery-1.4.4.min.js', _PS_JS_DIR_.'jquery/jquery.easing.1.3.js', _PS_JS_DIR_.'tools.js'));
$this->addCSS(_THEME_CSS_DIR_.'global.css', 'all');
$this->addJS(array(_PS_JS_DIR_.'jquery/jquery-1.4.4.min.js', _PS_JS_DIR_.'jquery/jquery.easing.1.3.js', _PS_JS_DIR_.'tools.js'));
if (Tools::isSubmit('live_edit') AND $ad = Tools::getValue('ad') AND (Tools::getValue('liveToken') == sha1(Tools::getValue('ad')._COOKIE_KEY_)))
{
Tools::addJS(array(
$this->addJS(array(
_PS_JS_DIR_.'jquery/jquery-ui-1.8.10.custom.min.js',
_PS_JS_DIR_.'jquery/jquery.fancybox-1.3.4.js',
_PS_JS_DIR_.'hookLiveEdit.js')
);
Tools::addCSS(_PS_CSS_DIR_.'jquery.fancybox-1.3.4.css');
$this->addCSS(_PS_CSS_DIR_.'jquery.fancybox-1.3.4.css');
}
}
@@ -451,13 +480,11 @@ class FrontControllerCore
public function displayContent()
{
Tools::safePostVars();
self::$smarty->assign('errors', $this->errors);
$this->smarty->assign('errors', $this->errors);
}
public function displayHeader()
{
global $css_files, $js_files;
if (!self::$initialized)
$this->init();
@@ -465,7 +492,7 @@ class FrontControllerCore
header('P3P: CP="IDC DSP COR CURa ADMa OUR IND PHY ONL COM STA"');
/* Hooks are volontary out the initialize array (need those variables already assigned) */
self::$smarty->assign(array(
$this->smarty->assign(array(
'time' => time(),
'img_update_time' => Configuration::get('PS_IMG_UPDATE_TIME'),
'static_token' => Tools::getToken(false),
@@ -475,7 +502,7 @@ class FrontControllerCore
'priceDisplayPrecision' => _PS_PRICE_DISPLAY_PRECISION_,
'content_only' => (int)Tools::getValue('content_only')
));
self::$smarty->assign(array(
$this->smarty->assign(array(
'HOOK_HEADER' => Module::hookExec('header'),
'HOOK_TOP' => Module::hookExec('top'),
'HOOK_LEFT_COLUMN' => Module::hookExec('leftColumn')
@@ -485,16 +512,16 @@ class FrontControllerCore
{
// CSS compressor management
if (Configuration::get('PS_CSS_THEME_CACHE'))
Tools::cccCss();
$this->css_files = Tools::cccCSS($this->css_files);
//JS compressor management
if (Configuration::get('PS_JS_THEME_CACHE'))
Tools::cccJs();
$this->js_files = Tools::cccJs($this->js_files);
}
self::$smarty->assign('css_files', $css_files);
self::$smarty->assign('js_files', array_unique($js_files));
self::$smarty->display(_PS_THEME_DIR_.'header.tpl');
$this->smarty->assign('css_files', $this->css_files);
$this->smarty->assign('js_files', array_unique($this->js_files));
$this->smarty->display(_PS_THEME_DIR_.'header.tpl');
}
public function displayFooter()
@@ -503,16 +530,16 @@ class FrontControllerCore
if (!self::$initialized)
$this->init();
self::$smarty->assign(array(
$this->smarty->assign(array(
'HOOK_RIGHT_COLUMN' => Module::hookExec('rightColumn', array('cart' => self::$cart)),
'HOOK_FOOTER' => Module::hookExec('footer'),
'content_only' => (int)(Tools::getValue('content_only'))));
self::$smarty->display(_PS_THEME_DIR_.'footer.tpl');
$this->smarty->display(_PS_THEME_DIR_.'footer.tpl');
//live edit
if (Tools::isSubmit('live_edit') AND $ad = Tools::getValue('ad') AND (Tools::getValue('liveToken') == sha1(Tools::getValue('ad')._COOKIE_KEY_)))
{
self::$smarty->assign(array('ad' => $ad, 'live_edit' => true));
self::$smarty->display(_PS_ALL_THEMES_DIR_.'live_edit.tpl');
$this->smarty->assign(array('ad' => $ad, 'live_edit' => true));
$this->smarty->display(_PS_ALL_THEMES_DIR_.'live_edit.tpl');
}
else
Tools::displayError();
@@ -539,7 +566,7 @@ class FrontControllerCore
if (!in_array($this->orderWay, $orderWayValues))
$this->orderWay = $orderWayValues[0];
self::$smarty->assign(array(
$this->smarty->assign(array(
'orderby' => $this->orderBy,
'orderway' => $this->orderWay,
'orderbydefault' => $orderByValues[(int)(Configuration::get('PS_PRODUCTS_ORDER_BY'))],
@@ -575,7 +602,7 @@ class FrontControllerCore
$stop = (int)($this->p + $range);
if ($stop > $pages_nb)
$stop = (int)($pages_nb);
self::$smarty->assign('nb_products', $nbProducts);
$this->smarty->assign('nb_products', $nbProducts);
$pagination_infos = array(
'pages_nb' => (int)($pages_nb),
'p' => (int)($this->p),
@@ -585,7 +612,7 @@ class FrontControllerCore
'start' => (int)($start),
'stop' => (int)($stop)
);
self::$smarty->assign($pagination_infos);
$this->smarty->assign($pagination_infos);
}
public static function getCurrentCustomerGroups()
@@ -613,5 +640,98 @@ class FrontControllerCore
$allowed = true;
return $allowed;
}
/**
* addCSS allows you to add stylesheet at any time.
*
* @param mixed $css_uri
* @param string $css_media_type
* @return true
*/
public function addCSS($css_uri, $css_media_type = 'all')
{
if (is_array($css_uri))
{
foreach ($css_uri as $file => $media_type)
$this->addCSS($file, $media_type);
return true;
}
//overriding of modules css files
$different = 0;
$override_path = str_replace(__PS_BASE_URI__.'modules/', _PS_ROOT_DIR_.'/themes/'._THEME_NAME_.'/css/modules/', $css_uri, $different);
if ($different && file_exists($override_path))
$css_uri = str_replace(__PS_BASE_URI__.'modules/', __PS_BASE_URI__.'themes/'._THEME_NAME_.'/css/modules/', $css_uri, $different);
else
{
// remove PS_BASE_URI on _PS_ROOT_DIR_ for the following
$url_data = parse_url($css_uri);
$file_uri = _PS_ROOT_DIR_.Tools::str_replace_once(__PS_BASE_URI__, DIRECTORY_SEPARATOR, $url_data['path']);
// check if css files exists
if (!file_exists($file_uri))
return true;
}
// detect mass add
$css_uri = array($css_uri => $css_media_type);
// adding file to the big array...
if (is_array($this->css_files))
$this->css_files = array_merge($this->css_files, $css_uri);
else
$this->css_files = $css_uri;
return true;
}
/**
* addJS load a javascript file in the header
*
* @param mixed $js_uri
* @return void
*/
public function addJS($js_uri)
{
if(!isset($this->js_files))
$this->js_files = array();
// avoid useless operation...
if (in_array($js_uri, $this->js_files))
return true;
// detect mass add
if (!is_array($js_uri) && !in_array($js_uri, $this->js_files))
$js_uri = array($js_uri);
else
foreach($js_uri as $key => $js)
if (in_array($js, $this->js_files))
unset($js_uri[$key]);
//overriding of modules js files
foreach ($js_uri AS $key => &$file)
{
if (!preg_match('/^http(s?):\/\//i', $file))
{
$different = 0;
$override_path = str_replace(__PS_BASE_URI__.'modules/', _PS_ROOT_DIR_.'/themes/'._THEME_NAME_.'/js/modules/', $file, $different);
if ($different && file_exists($override_path))
$file = str_replace(__PS_BASE_URI__.'modules/', __PS_BASE_URI__.'themes/'._THEME_NAME_.'/js/modules/', $file, $different);
else
{
// remove PS_BASE_URI on _PS_ROOT_DIR_ for the following
$url_data = parse_url($file);
$file_uri = _PS_ROOT_DIR_.Tools::str_replace_once(__PS_BASE_URI__, DIRECTORY_SEPARATOR, $url_data['path']);
// check if js files exists
if (!file_exists($file_uri))
unset($js_uri[$key]);
}
}
}
// adding file to the big array...
$this->js_files = array_merge($this->js_files, $js_uri);
return true;
}
}
+8 -4
View File
@@ -31,14 +31,20 @@ class LinkCore
protected $allow;
protected $url;
public static $cache = array('page' => array());
public $protocol_link;
public $protocol_content;
public $useSSL;
/**
* Constructor (initialization only)
*/
public function __construct()
public function __construct($protocol_link = null, $protocol_content = null)
{
$this->allow = (int)Configuration::get('PS_REWRITING_SETTINGS');
$this->url = $_SERVER['SCRIPT_NAME'];
$this->protocol_link = $protocol_link;
$this->protocol_content = $protocol_content;
}
/**
@@ -146,8 +152,6 @@ class LinkCore
*/
public function getImageLink($name, $ids, $type = NULL)
{
global $protocol_content;
// legacy mode
if (Configuration::get('PS_LEGACY_IMAGES')
&& (file_exists(_PS_PROD_IMG_DIR_.$ids.($type ? '-'.$type : '').'.jpg')))
@@ -168,7 +172,7 @@ class LinkCore
$uri_path = _THEME_PROD_DIR_.Image::getImgFolderStatic($id_image).$id_image.($type ? '-'.$type : '').'.jpg';
}
return $protocol_content.Tools::getMediaServer($uri_path).$uri_path;
return $this->protocol_content.Tools::getMediaServer($uri_path).$uri_path;
}
public function getMediaLink($filepath)
+57 -97
View File
@@ -288,19 +288,20 @@ class ProductCore extends ObjectModel
),
);
public function __construct($id_product = NULL, $full = false, $id_lang = NULL, $id_shop = NULL)
public function __construct($id_product = NULL, $full = false, $id_lang = NULL, $id_shop = NULL, $context = NULL)
{
global $cart;
parent::__construct($id_product, $id_lang, $id_shop);
if (!$context)
$context = Context::getContext();
if ($full AND $this->id)
{
$this->tax_name = 'deprecated'; // The applicable tax may be BOTH the product one AND the state one (moreover this variable is some deadcode)
$this->manufacturer_name = Manufacturer::getNameById((int)$this->id_manufacturer);
$this->supplier_name = Supplier::getNameById((int)$this->id_supplier);
self::$_tax_rules_group[$this->id] = $this->id_tax_rules_group;
if (is_object($cart) AND $cart->{Configuration::get('PS_TAX_ADDRESS_TYPE')} != NULL)
$this->tax_rate = Tax::getProductTaxRate($this->id, $cart->{Configuration::get('PS_TAX_ADDRESS_TYPE')});
if (is_object($context->cart) AND $context->cart->{Configuration::get('PS_TAX_ADDRESS_TYPE')} != NULL)
$this->tax_rate = Tax::getProductTaxRate($this->id, $context->cart->{Configuration::get('PS_TAX_ADDRESS_TYPE')});
else
$this->tax_rate = Tax::getProductTaxRate($this->id, NULL);
$this->new = $this->isNew();
@@ -412,30 +413,22 @@ class ProductCore extends ObjectModel
return $fields;
}
public static function initPricesComputation($id_customer = NULL)
public static function initPricesComputation($customer = NULL)
{
global $cookie;
if ($id_customer)
if ($customer)
{
$customer = new Customer((int)($id_customer));
if (!Validate::isLoadedObject($customer))
die(Tools::displayError());
self::$_taxCalculationMethod = Group::getPriceDisplayMethod((int)($customer->id_default_group));
}
elseif ($cookie->id_customer)
{
$customer = new Customer((int)($cookie->id_customer));
self::$_taxCalculationMethod = Group::getPriceDisplayMethod((int)($customer->id_default_group));
}
else
self::$_taxCalculationMethod = Group::getDefaultPriceDisplayMethod();
}
public static function getTaxCalculationMethod($id_customer = NULL)
public static function getTaxCalculationMethod($customer = NULL)
{
if ($id_customer)
self::initPricesComputation((int)($id_customer));
if ($customer)
self::initPricesComputation((int)($customer));
return (int)(self::$_taxCalculationMethod);
}
@@ -1455,15 +1448,15 @@ class ProductCore extends ObjectModel
return Product::getProductsProperties((int)$id_lang, $result);
}
static protected function _getProductIdByDate($beginning, $ending)
static protected function _getProductIdByDate($beginning, $ending, $context = null)
{
global $cookie, $cart;
if (!$context)
$context = Context::getContext();
$id_group = $cookie->id_customer ? (int)(Customer::getDefaultGroupId((int)($cookie->id_customer))) : _PS_DEFAULT_CUSTOMER_GROUP_;
$id_address = $cart->{Configuration::get('PS_TAX_ADDRESS_TYPE')};
$id_address = $context->cart->{Configuration::get('PS_TAX_ADDRESS_TYPE')};
$ids = Address::getCountryAndState($id_address);
$id_country = (int)($ids['id_country'] ? $ids['id_country'] : Configuration::get('PS_COUNTRY_DEFAULT'));
return SpecificPrice::getProductIdByDate((int)(Shop::getCurrentShop()), (int)($cookie->id_currency), $id_country, $id_group, $beginning, $ending);
return SpecificPrice::getProductIdByDate(Shop::getCurrentShop(), $context->currency->id, $id_country, $context->customer->id_default_group, $beginning, $ending);
}
/**
@@ -1742,10 +1735,13 @@ class ProductCore extends ObjectModel
* @return float Product price
*/
public static function getPriceStatic($id_product, $usetax = true, $id_product_attribute = NULL, $decimals = 6, $divisor = NULL, $only_reduc = false,
$usereduc = true, $quantity = 1, $forceAssociatedTax = false, $id_customer = NULL, $id_cart = NULL, $id_address = NULL, &$specificPriceOutput = NULL, $with_ecotax = TRUE)
$usereduc = true, $quantity = 1, $forceAssociatedTax = false, $id_customer = NULL, $id_cart = NULL, $id_address = NULL, &$specificPriceOutput = NULL,
$with_ecotax = TRUE, $context = null)
{
global $cookie, $cart;
$cur_cart = $cart;
if (!$context)
$context = Context::getContext();
$cur_cart = $context->cart;
if (isset($divisor))
Tools::displayParameterAsDeprecated('divisor');
@@ -1753,18 +1749,17 @@ class ProductCore extends ObjectModel
if (!Validate::isBool($usetax) OR !Validate::isUnsignedId($id_product))
die(Tools::displayError());
// Initializations
if (!$id_customer)
$id_customer = ((Validate::isCookie($cookie) AND isset($cookie->id_customer) AND $cookie->id_customer) ? (int)($cookie->id_customer) : NULL);
$id_group = $id_customer ? (int)(Customer::getDefaultGroupId($id_customer)) : _PS_DEFAULT_CUSTOMER_GROUP_;
$id_group = (isset($context->customer) ? $context->customer->id_default_group : _PS_DEFAULT_CUSTOMER_GROUP_);
if (!is_object($cur_cart) OR (Validate::isUnsignedInt($id_cart) AND $id_cart))
{
/*
* When a user (e.g., guest, customer, Google...) is on PrestaShop, he has already its cart as the global (see /init.php)
* When a non-user calls directly this method (e.g., payment module...) is on PrestaShop, he does not have already it BUT knows the cart ID
* When called from the back office, cart ID can be inexistant
*/
if (!$id_cart AND !Validate::isCookie($cookie))
if (!$id_cart &&!isset($context->tab))
die(Tools::displayError());
$cur_cart = $id_cart ? new Cart((int)($id_cart)) : new Cart((int)($cookie->id_cart));
$cur_cart = new Cart($id_cart);
}
if ((int)($id_cart))
@@ -1778,7 +1773,7 @@ class ProductCore extends ObjectModel
$cart_quantity = self::$_cart_quantity[(int)($id_cart).'_'.(int)($id_product)];
}
$quantity = ($id_cart AND $cart_quantity) ? $cart_quantity : $quantity;
$id_currency = (int)(Validate::isLoadedObject($cur_cart) ? $cur_cart->id_currency : ((isset($cookie->id_currency) AND (int)($cookie->id_currency)) ? $cookie->id_currency : Configuration::get('PS_CURRENCY_DEFAULT')));
$id_currency = (int)(Validate::isLoadedObject($context->currency) ? $context->currency->id : Configuration::get('PS_CURRENCY_DEFAULT'));
// retrieve address informations
$id_country = (int)Country::getDefaultCountryId();
@@ -1800,12 +1795,11 @@ class ProductCore extends ObjectModel
$id_county = (int)County::getIdCountyByZipCode($id_state, $postcode);
}
}
elseif (isset($cookie->id_country))
elseif (isset($context->customer->geoloc_id_country))
{
// fetch address from cookie
$id_country = (int)$cookie->id_country;
$id_state = (int)$cookie->id_state;
$postcode = (int)$cookie->postcode;
$id_country = (int)$context->customer->geoloc_id_country;
$id_state = (int)$context->customer->id_state;
$postcode = (int)$context->customer->postcode;
$id_county = (int)County::getIdCountyByZipCode($id_state, $postcode);
}
@@ -1952,20 +1946,17 @@ class ProductCore extends ObjectModel
return Tools::displayPrice(Tools::convertPrice($price, $currency), $currency);
}
public static function isDiscounted($id_product, $quantity = 1)
public static function isDiscounted($id_product, $quantity = 1, $context = null)
{
global $cookie, $cart;
$id_customer = ((Validate::isCookie($cookie) AND isset($cookie->id_customer) AND $cookie->id_customer) ? (int)($cookie->id_customer) : NULL);
$id_group = $id_customer ? (int)(Customer::getDefaultGroupId($id_customer)) : _PS_DEFAULT_CUSTOMER_GROUP_;
$cart_quantity = !$cart ? 0 : Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('
$id_group = $context->customer->id_default_group;
$cart_quantity = !$context->cart ? 0 : Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('
SELECT SUM(`quantity`)
FROM `'._DB_PREFIX_.'cart_product`
WHERE `id_product` = '.(int)($id_product).' AND `id_cart` = '.(int)($cart->id)
WHERE `id_product` = '.(int)($id_product).' AND `id_cart` = '.(int)($context->cart->id)
);
$quantity = $cart_quantity ? $cart_quantity : $quantity;
$id_currency = (int)(Validate::isLoadedObject($cart) ? $cart->id_currency : ((isset($cookie->id_currency) AND (int)($cookie->id_currency)) ? $cookie->id_currency : Configuration::get('PS_CURRENCY_DEFAULT')));
$ids = Address::getCountryAndState((int)($cart->{Configuration::get('PS_TAX_ADDRESS_TYPE')}));
$id_currency = (int)$context->currency->id;
$ids = Address::getCountryAndState((int)($context->cart->{Configuration::get('PS_TAX_ADDRESS_TYPE')}));
$id_country = (int)($ids['id_country'] ? $ids['id_country'] : Configuration::get('PS_COUNTRY_DEFAULT'));
$id_shop = (int)(Shop::getCurrentShop());
return (bool)SpecificPrice::getSpecificPrice((int)$id_product, $id_shop, $id_currency, $id_country, $id_group, $quantity);
@@ -2203,9 +2194,10 @@ class ProductCore extends ObjectModel
return $productObj->addStockMvt(-(int)$product['cart_quantity'], (int)_STOCK_MOVEMENT_ORDER_REASON_, (int)$product['id_product_attribute'], (int)$id_order, NULL);
}
public static function reinjectQuantities(&$orderDetail, $quantity)
public static function reinjectQuantities(&$orderDetail, $quantity, $context = null)
{
global $cookie;
if (!$context)
$context = Context::getContext();
if (!Validate::isLoadedObject($orderDetail))
die(Tools::displayError());
@@ -2213,12 +2205,12 @@ class ProductCore extends ObjectModel
{
$products_pack = Pack::getItems((int)($orderDetail->product_id), (int)(Configuration::get('PS_LANG_DEFAULT')));
foreach($products_pack AS $product_pack)
if (!$product_pack->addStockMvt((int)($product_pack->pack_quantity * $quantity), _STOCK_MOVEMENT_ORDER_REASON_, (int)$product_pack->id_product_attribute, (int)$orderDetail->id_order, (int)$cookie->id_employee))
if (!$product_pack->addStockMvt((int)($product_pack->pack_quantity * $quantity), _STOCK_MOVEMENT_ORDER_REASON_, (int)$product_pack->id_product_attribute, (int)$orderDetail->id_order, (int)$context->employee->id))
return false;
}
$product = new Product((int)$orderDetail->product_id);
if (!$product->addStockMvt((int)$quantity, _STOCK_MOVEMENT_ORDER_REASON_, (int)$orderDetail->product_attribute_id, (int)$orderDetail->id_order, (int)$cookie->id_employee))
if (!$product->addStockMvt((int)$quantity, _STOCK_MOVEMENT_ORDER_REASON_, (int)$orderDetail->product_attribute_id, (int)$orderDetail->id_order, (int)$context->employee->id))
return false;
$orderDetail->product_quantity_reinjected += (int)($quantity);
@@ -2346,9 +2338,7 @@ class ProductCore extends ObjectModel
* @return array Product accessories
*/
public function getAccessories($id_lang, $active = true, $id_shop = false)
{
global $link, $cookie;
{
if (!$id_shop)
$id_shop_lang = (int)Configuration::get('PS_SHOP_DEFAULT');
else
@@ -2751,10 +2741,11 @@ class ProductCore extends ObjectModel
/**
* Get the link of the product page of this product
*/
public function getLink()
public function getLink($context = null)
{
global $link;
return $link->getProductLink($this);
if (!$context)
$context = Context::getContext();
return $context->link->getProductLink($this);
}
public function getTags($id_lang)
@@ -2774,11 +2765,12 @@ class ProductCore extends ObjectModel
return Language::getIsoById((int)$id_lang).'-default';
}
public static function getProductProperties($id_lang, $row)
public static function getProductProperties($id_lang, $row, $context = null)
{
if (!$row['id_product'])
return false;
$context = Context::getContext();
// Product::getDefaultAttribute is only called if id_product_attribute is missing from the SQL query at the origin of it: consider adding it in order to avoid unnecessary queries
$row['allow_oosp'] = Product::isAvailableWhenOutOfStock($row['out_of_stock']);
if ((!isset($row['id_product_attribute']) OR !$row['id_product_attribute'])
@@ -2797,9 +2789,8 @@ class ProductCore extends ObjectModel
return self::$producPropertiesCache[$cacheKey];
// Datas
$link = new Link();
$row['category'] = Category::getLinkRewrite((int)$row['id_category_default'], (int)($id_lang));
$row['link'] = $link->getProductLink((int)$row['id_product'], $row['link_rewrite'], $row['category'], $row['ean13']);
$row['link'] = $context->link->getProductLink((int)$row['id_product'], $row['link_rewrite'], $row['category'], $row['ean13']);
$row['attribute_price'] = (isset($row['id_product_attribute']) AND $row['id_product_attribute']) ? (float)(Product::getProductAttributePrice($row['id_product_attribute'])) : 0;
$row['price_tax_exc'] = Product::getPriceStatic((int)$row['id_product'], false, ((isset($row['id_product_attribute']) AND !empty($row['id_product_attribute'])) ? (int)($row['id_product_attribute']) : NULL), (self::$_taxCalculationMethod == PS_TAX_EXC ? 2 : 6));
if (self::$_taxCalculationMethod == PS_TAX_EXC)
@@ -2890,19 +2881,16 @@ class ProductCore extends ObjectModel
** Customization management
*/
public static function getAllCustomizedDatas($id_cart, $id_lang = null)
public static function getAllCustomizedDatas($id_cart, $id_lang = null, $context = null)
{
global $cookie;
if (!$context)
$context = Context::getContext();
// No need to query if there isn't any real cart!
if (!$id_cart)
return false;
if (!$id_lang AND !is_null($cookie) AND $cookie->id_lang)
$id_lang = $cookie->id_lang;
else
{
$cart = new Cart((int)($id_cart));
$id_lang = (int)($cart->id_lang);
}
if (!$id_lang)
$id_lang = $context->language->id;
if (!$result = Db::getInstance()->ExecuteS('
SELECT cd.`id_customization`, c.`id_product`, cfl.`id_customization_field`, c.`id_product_attribute`, cd.`type`, cd.`index`, cd.`value`, cfl.`name`
@@ -2926,34 +2914,6 @@ class ProductCore extends ObjectModel
return $customizedDatas;
}
/**
* @param int $id_customization
* @return bool
* @deprecated
*/
public function deleteCustomizedDatas($id_customization)
{
Tools::displayAsDeprecated();
if (Pack::isPack((int)($product['id_product'])))
{
$products_pack = Pack::getItems((int)($product['id_product']), (int)(Configuration::get('PS_LANG_DEFAULT')));
foreach($products_pack AS $product_pack)
{
$tab_product_pack['id_product'] = (int)($product_pack->id);
$tab_product_pack['id_product_attribute'] = self::getDefaultAttribute($tab_product_pack['id_product'], 1);
$tab_product_pack['cart_quantity'] = (int)($product_pack->pack_quantity * $product['cart_quantity']);
self::updateQuantity($tab_product_pack);
}
}
if (($result = Db::getInstance()->ExecuteS('SELECT `value` FROM `'._DB_PREFIX_.'customized_data` WHERE `id_customization` = '.(int)($id_customization).' AND `type` = '._CUSTOMIZE_FILE_)) === false)
return false;
foreach ($result AS $row)
if (!@unlink(_PS_UPLOAD_DIR_.$row['value']) OR !@unlink(_PS_UPLOAD_DIR_.$row['value'].'_small'))
return false;
return (Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'customization` WHERE `id_customization` = '.(int)($id_customization)) AND Db::getInstance()->Execute('DELETE FROM `'._DB_PREFIX_.'customized_data` WHERE `id_customization` = '.(int)($id_customization)));
}
public static function addCustomizationPrice(&$products, &$customizedDatas)
{
foreach ($products AS &$productUpdate)
+25 -92
View File
@@ -268,10 +268,8 @@ class ToolsCore
/**
* Change language in cookie while clicking on a flag
*/
public static function setCookieLanguage()
public static function setCookieLanguage($cookie)
{
global $cookie;
/* If language does not exist or is disabled, erase it */
if ($cookie->id_lang)
{
@@ -309,18 +307,16 @@ class ToolsCore
return $iso;
}
public static function switchLanguage()
public static function switchLanguage($context = null)
{
global $cookie;
if (!$context)
$context = Context::getContext();
if ($id_lang = (int)(self::getValue('id_lang')) AND Validate::isUnsignedId($id_lang))
$cookie->id_lang = $id_lang;
$context->cookie->id_lang = $id_lang;
}
public static function setCurrency()
public static function setCurrency($cookie)
{
global $cookie;
if (self::isSubmit('SubmitCurrency'))
if (isset($_POST['id_currency']) AND is_numeric($_POST['id_currency']))
{
@@ -1387,108 +1383,42 @@ class ToolsCore
return false;
}
/**
/**
* addJS load a javascript file in the header
*
* @deprecated as of 1.5 use FrontController->addJS()
* @param mixed $js_uri
* @return void
*/
public static function addJS($js_uri)
{
global $js_files;
if(!isset($js_files))
$js_files = array();
// avoid useless operation...
if (in_array($js_uri, $js_files))
return true;
// detect mass add
if (!is_array($js_uri) && !in_array($js_uri, $js_files))
$js_uri = array($js_uri);
else
foreach($js_uri as $key => $js)
if (in_array($js, $js_files))
unset($js_uri[$key]);
//overriding of modules js files
foreach ($js_uri AS $key => &$file)
{
if (!preg_match('/^http(s?):\/\//i', $file))
{
$different = 0;
$override_path = str_replace(__PS_BASE_URI__.'modules/', _PS_ROOT_DIR_.'/themes/'._THEME_NAME_.'/js/modules/', $file, $different);
if ($different && file_exists($override_path))
$file = str_replace(__PS_BASE_URI__.'modules/', __PS_BASE_URI__.'themes/'._THEME_NAME_.'/js/modules/', $file, $different);
else
{
// remove PS_BASE_URI on _PS_ROOT_DIR_ for the following
$url_data = parse_url($file);
$file_uri = _PS_ROOT_DIR_.Tools::str_replace_once(__PS_BASE_URI__, DIRECTORY_SEPARATOR, $url_data['path']);
// check if js files exists
if (!file_exists($file_uri))
unset($js_uri[$key]);
}
}
}
// adding file to the big array...
$js_files = array_merge($js_files, $js_uri);
return true;
Tools::displayAsDeprecated();
$context = Context::getContext();
$context->controller->addJs($js_uri);
}
/**
* addCSS allows you to add stylesheet at any time.
*
* @deprecated as of 1.5 use FrontController->addCSS()
* @param mixed $css_uri
* @param string $css_media_type
* @return true
*/
public static function addCSS($css_uri, $css_media_type = 'all')
{
global $css_files;
if (is_array($css_uri))
{
foreach ($css_uri as $file => $media_type)
Tools::addCSS($file, $media_type);
return true;
}
//overriding of modules css files
$different = 0;
$override_path = str_replace(__PS_BASE_URI__.'modules/', _PS_ROOT_DIR_.'/themes/'._THEME_NAME_.'/css/modules/', $css_uri, $different);
if ($different && file_exists($override_path))
$css_uri = str_replace(__PS_BASE_URI__.'modules/', __PS_BASE_URI__.'themes/'._THEME_NAME_.'/css/modules/', $css_uri, $different);
else
{
// remove PS_BASE_URI on _PS_ROOT_DIR_ for the following
$url_data = parse_url($css_uri);
$file_uri = _PS_ROOT_DIR_.Tools::str_replace_once(__PS_BASE_URI__, DIRECTORY_SEPARATOR, $url_data['path']);
// check if css files exists
if (!file_exists($file_uri))
return true;
}
// detect mass add
$css_uri = array($css_uri => $css_media_type);
// adding file to the big array...
if (is_array($css_files))
$css_files = array_merge($css_files, $css_uri);
else
$css_files = $css_uri;
return true;
Tools::displayAsDeprecated();
$context = Context::getContext();
$context->controller->addCSS($css_uri, $css_media_type);
}
/**
* Combine Compress and Cache CSS (ccc) calls
*
* @param array css_files
* @return array processed css_files
*/
public static function cccCss() {
global $css_files;
public static function cccCss($css_files) {
//inits
$css_files_by_media = array();
$compressed_css_files = array();
@@ -1562,14 +1492,17 @@ class ToolsCore
$url = str_replace(_PS_THEME_DIR_, _THEMES_DIR_._THEME_NAME_.'/', $filename);
$css_files[$protocolLink.Tools::getMediaServer($url).$url] = $media;
}
return $css_files;
}
/**
* Combine Compress and Cache (ccc) JS calls
*
* @param array js_files
* @return array processed js_files
*/
public static function cccJS() {
global $js_files;
public static function cccJS($js_files) {
//inits
$compressed_js_files_not_found = array();
$js_files_infos = array();
@@ -1632,7 +1565,7 @@ class ToolsCore
// rebuild the original js_files array
$url = str_replace(_PS_ROOT_DIR_.'/', __PS_BASE_URI__, $compressed_js_path);
$js_files = array_merge(array($protocolLink.Tools::getMediaServer($url).$url), $js_external_files);
return array_merge(array($protocolLink.Tools::getMediaServer($url).$url), $js_external_files);
}