// Refactoring of shop class with new context

This commit is contained in:
rMalie
2011-07-12 16:28:20 +00:00
parent e7ea230525
commit 2d77925e7d
28 changed files with 216 additions and 185 deletions
+1 -1
View File
@@ -1190,7 +1190,7 @@ abstract class AdminTabCore
$assos = GroupShop::getAssoTables();
if (isset($assos[$this->table]) AND $assos[$this->table]['type'] == 'group_shop')
$filterKey = $assos[$this->table]['type'];
$idenfierShop = $context->shop->getGroup();
$idenfierShop = $context->shop->getGroupID();
}
if (isset($filterKey))
$filterShop = 'JOIN `'._DB_PREFIX_.$this->table.'_'.$filterKey.'` sa ON (sa.'.$this->identifier.' = a.'.$this->identifier.' AND sa.id_'.$filterKey.'='.(int)$idenfierShop.')';
+1 -1
View File
@@ -535,7 +535,7 @@ class CartCore extends ObjectModel
*/
public function updateQty($quantity, $id_product, $id_product_attribute = NULL, $id_customization = false, $operator = 'up', $id_shop = NULL, $id_group_shop = NULL)
{
if (is_null($id_shop)) $id_shop = Shop::getCurrentShop(true);
if (is_null($id_shop)) $id_shop = Context::getContext()->shop->getID();
$quantity = (int)$quantity;
$id_product = (int)$id_product;
$id_product_attribute = (int)$id_product_attribute;
+1 -1
View File
@@ -728,7 +728,7 @@ class CategoryCore extends ObjectModel
public function getName($id_lang = NULL, $id_shop = false, $context = null)
{
if (!$id_shop)
$id_shop = (int)Shop::getCurrentShop(true);
$id_shop = Context::getContext()->shop->getID();
if (!$id_lang)
{
if (!$context)
+12 -12
View File
@@ -97,11 +97,11 @@ class ConnectionCore extends ObjectModel
AND preg_match('/BotLink|ahoy|AlkalineBOT|anthill|appie|arale|araneo|AraybOt|ariadne|arks|ATN_Worldwide|Atomz|bbot|Bjaaland|Ukonline|borg\-bot\/0\.9|boxseabot|bspider|calif|christcrawler|CMC\/0\.01|combine|confuzzledbot|CoolBot|cosmos|Internet Cruiser Robot|cusco|cyberspyder|cydralspider|desertrealm, desert realm|digger|DIIbot|grabber|downloadexpress|DragonBot|dwcp|ecollector|ebiness|elfinbot|esculapio|esther|fastcrawler|FDSE|FELIX IDE|ESI|fido|Hmhkki|KIT\-Fireball|fouineur|Freecrawl|gammaSpider|gazz|gcreep|golem|googlebot|griffon|Gromit|gulliver|gulper|hambot|havIndex|hotwired|htdig|iajabot|INGRID\/0\.1|Informant|InfoSpiders|inspectorwww|irobot|Iron33|JBot|jcrawler|Teoma|Jeeves|jobo|image\.kapsi\.net|KDD\-Explorer|ko_yappo_robot|label\-grabber|larbin|legs|Linkidator|linkwalker|Lockon|logo_gif_crawler|marvin|mattie|mediafox|MerzScope|NEC\-MeshExplorer|MindCrawler|udmsearch|moget|Motor|msnbot|muncher|muninn|MuscatFerret|MwdSearch|sharp\-info\-agent|WebMechanic|NetScoop|newscan\-online|ObjectsSearch|Occam|Orbsearch\/1\.0|packrat|pageboy|ParaSite|patric|pegasus|perlcrawler|phpdig|piltdownman|Pimptrain|pjspider|PlumtreeWebAccessor|PortalBSpider|psbot|Getterrobo\-Plus|Raven|RHCS|RixBot|roadrunner|Robbie|robi|RoboCrawl|robofox|Scooter|Search\-AU|searchprocess|Senrigan|Shagseeker|sift|SimBot|Site Valet|skymob|SLCrawler\/2\.0|slurp|ESI|snooper|solbot|speedy|spider_monkey|SpiderBot\/1\.0|spiderline|nil|suke|http:\/\/www\.sygol\.com|tach_bw|TechBOT|templeton|titin|topiclink|UdmSearch|urlck|Valkyrie libwww\-perl|verticrawl|Victoria|void\-bot|Voyager|VWbot_K|crawlpaper|wapspider|WebBandit\/1\.0|webcatcher|T\-H\-U\-N\-D\-E\-R\-S\-T\-O\-N\-E|WebMoose|webquest|webreaper|webs|webspider|WebWalker|wget|winona|whowhere|wlm|WOLP|WWWC|none|XGET|Nederland\.zoek/i', $_SERVER['HTTP_USER_AGENT']))
{
// This is a bot and we have to retrieve its connection ID
$sql = 'SELECT `id_connections` FROM `'._DB_PREFIX_.'connections` c
$sql = 'SELECT `id_connections` FROM `'._DB_PREFIX_.'connections`
WHERE ip_address = '.ip2long(Tools::getRemoteAddr()).'
AND DATE_ADD(c.`date_add`, INTERVAL 30 MINUTE) > \''.pSQL(date('Y-m-d H:i:00')).'\'
AND id_shop = '.Shop::getCurrentShop().'
ORDER BY c.`date_add` DESC';
AND DATE_ADD(`date_add`, INTERVAL 30 MINUTE) > \''.pSQL(date('Y-m-d H:i:00')).'\'
AND id_shop = '.Shop::sqlRestriction().'
ORDER BY `date_add` DESC';
if ($id_connections = Db::getInstance()->getValue($sql))
{
$cookie->id_connections = (int)$id_connections;
@@ -110,12 +110,12 @@ class ConnectionCore extends ObjectModel
}
// A new connection is created if the guest made no actions during 30 minutes
$sql = 'SELECT c.`id_guest`
FROM `'._DB_PREFIX_.'connections` c
WHERE c.`id_guest` = '.(int)($cookie->id_guest).'
AND DATE_ADD(c.`date_add`, INTERVAL 30 MINUTE) > \''.pSQL(date('Y-m-d H:i:00')).'\'
AND id_shop = '.Shop::getCurrentShop().'
ORDER BY c.`date_add` DESC';
$sql = 'SELECT `id_guest`
FROM `'._DB_PREFIX_.'connections`
WHERE `id_guest` = '.(int)($cookie->id_guest).'
AND DATE_ADD(`date_add`, INTERVAL 30 MINUTE) > \''.pSQL(date('Y-m-d H:i:00')).'\'
AND id_shop = '.Shop::sqlRestriction().'
ORDER BY `date_add` DESC';
$result = Db::getInstance()->getRow($sql);
if (!$result['id_guest'] AND (int)($cookie->id_guest))
{
@@ -130,8 +130,8 @@ class ConnectionCore extends ObjectModel
$connection->id_guest = (int)($cookie->id_guest);
$connection->id_page = Page::getCurrentId();
$connection->ip_address = Tools::getRemoteAddr() ? ip2long(Tools::getRemoteAddr()) : '';
$connection->id_shop = Shop::getCurrentShop();
$connection->id_group_shop = Shop::getCurrentGroupShop();
$connection->id_shop = Context::getContext()->shop->getID();
$connection->id_group_shop = Context::getContext()->shop->getGroupID();
if (Validate::isAbsoluteUrl($referer))
$connection->http_referer = $referer;
$connection->add();
+54 -3
View File
@@ -24,21 +24,72 @@
* @license http://opensource.org/licenses/osl-3.0.php Open Software License (OSL 3.0)
* International Registered Trademark & Property of PrestaShop SA
*/
/**
* @since 1.5.0
*/
class ContextCore
{
/**
* @var Context
*/
protected static $instance;
/**
* @var Cart
*/
public $cart;
/**
* @var Customer
*/
public $customer;
/**
* @var Cookie
*/
public $cookie;
/**
* @var Link
*/
public $link;
/**
* @var Country
*/
public $country;
/**
* @var Employee
*/
public $employee;
/**
* @var Controller
*/
public $controller;
public $lang;
/**
* @var Language
*/
public $language;
/**
* @var Currency
*/
public $currency;
/**
* @var AdminTab
*/
public $tab;
/**
* @var Shop
*/
public $shop;
/**
* Create a context without singleton constraint
*/
@@ -48,7 +99,7 @@ class ContextCore
$link = null,
$country = null,
$employee = null,
$lang = null,
$language = null,
$currency = null,
$tab = null)
{
@@ -58,7 +109,7 @@ class ContextCore
$this->link = $link;
$this->country = $country;
$this->employee = $employee;
$this->lang = $lang;
$this->language = $language;
$this->currency = $currency;
$this->tab = $tab;
}
+3 -3
View File
@@ -198,7 +198,7 @@ class CurrencyCore extends ObjectModel
static public function getPaymentCurrenciesSpecial($id_module, $id_shop = null)
{
if (is_null($id_shop))
$id_shop = Shop::getCurrentShop(true);
$id_shop = Context::getContext()->shop->getID();
$sql = 'SELECT *
FROM '._DB_PREFIX_.'module_currency
@@ -210,7 +210,7 @@ class CurrencyCore extends ObjectModel
static public function getPaymentCurrencies($id_module, $id_shop = null)
{
if (is_null($id_shop))
$id_shop = Shop::getCurrentShop(true);
$id_shop = Context::getContext()->shop->getID();
$sql = 'SELECT c.*
FROM `'._DB_PREFIX_.'module_currency` mc
@@ -226,7 +226,7 @@ class CurrencyCore extends ObjectModel
static public function checkPaymentCurrencies($id_module, $id_shop = null)
{
if (is_null($id_shop))
$id_shop = Shop::getCurrentShop(true);
$id_shop = Context::getContext()->shop->getID();
return Db::getInstance(_PS_USE_SQL_SLAVE_)->ExecuteS('
SELECT mc.*
+2 -2
View File
@@ -155,8 +155,8 @@ class CustomerCore extends ObjectModel
public function add($autodate = true, $nullValues = true)
{
$this->id_shop = ($this->id_shop) ? $this->id_shop : Shop::getCurrentShop();
$this->id_group_shop = ($this->id_group_shop) ? $this->id_group_shop : Shop::getCurrentGroupShop();
$this->id_shop = ($this->id_shop) ? $this->id_shop : Context::getContext()->shop->getID();
$this->id_group_shop = ($this->id_group_shop) ? $this->id_group_shop : Context::getContext()->shop->getGroupID();
$this->birthday = (empty($this->years) ? $this->birthday : (int)($this->years).'-'.(int)($this->months).'-'.(int)($this->days));
$this->secure_key = md5(uniqid(rand(), true));
$this->last_passwd_gen = date('Y-m-d H:i:s', strtotime('-'.Configuration::get('PS_PASSWD_TIME_FRONT').'minutes'));
+2 -2
View File
@@ -82,8 +82,8 @@ class FrontControllerCore
return;
self::$initialized = true;
$this->id_current_shop = (int)Shop::getCurrentShop();
$this->id_current_group_shop = (int)Shop::getCurrentGroupShop();
$this->id_current_shop = Context::getContext()->shop->getID();
$this->id_current_group_shop = Context::getContext()->shop->getGroupID();
$this->css_files = array();
$this->js_files = array();
+5 -5
View File
@@ -240,10 +240,10 @@ abstract class ObjectModelCore
if (!Tools::isMultishopActivated())
{
if (isset($assos[$this->table]) && $assos[$this->table]['type'] == 'shop')
$result &= $this->associateTo(Shop::getCurrentShop(true), 'shop');
$result &= $this->associateTo(Context::getContext()->shop->getID(), 'shop');
$assos = GroupShop::getAssoTables();
if (isset($assos[$this->table]) && $assos[$this->table]['type'] == 'group_shop')
$result &= $this->associateTo(Shop::getCurrentGroupShop(), 'group_shop');
$result &= $this->associateTo(Context::getContext()->shop->getGroupID(), 'group_shop');
}
return $result;
}
@@ -284,7 +284,7 @@ abstract class ObjectModelCore
die(Tools::displayError());
if ($this->langMultiShop)
$field['id_shop'] = ($this->id_shop ? $this->id_shop : Shop::getCurrentShop(true));
$field['id_shop'] = ($this->id_shop ? $this->id_shop : Context::getContext()->shop->getID());
// used to insert missing lang entries
$where_lang = '`'.pSQL($this->identifier).'` = '.(int)$this->id.
@@ -739,7 +739,7 @@ abstract class ObjectModelCore
public function isAssociatedToShop($id_shop = null)
{
if (is_null($id_shop))
$id_shop = Shop::getCurrentShop();
$id_shop = Context::getContext()->shop->getID();
$sql = 'SELECT id_shop
FROM `'.pSQL(_DB_PREFIX_.$this->table).'_shop`
@@ -782,7 +782,7 @@ abstract class ObjectModelCore
public function isAssociatedToGroupShop($id_group_shop = null)
{
if (is_null($id_group_shop))
$id_shop = Shop::getCurrentGroupShop();
$id_group_shop = Context::getContext()->shop->getGroupID();
$sql = 'SELECT id_group_shop
FROM `'.pSQL(_DB_PREFIX_.$this->table).'_group_shop`
+1 -1
View File
@@ -125,7 +125,7 @@ class PageCore extends ObjectModel
'id_page' => (int)$id_page,
'counter' => 1,
'id_shop' => $context->shop->getID(),
'id_group_shop' => $context->shop->getGroup(),
'id_group_shop' => $context->shop->getGroupID(),
), 'INSERT');
}
}
+1 -1
View File
@@ -217,7 +217,7 @@ abstract class PaymentModuleCore extends Module
if (!empty($product['ecotax']))
$ecotaxTaxRate = Tax::getProductEcotaxRate($order->{Configuration::get('PS_TAX_ADDRESS_TYPE')});
$quantityDiscount = SpecificPrice::getQuantityDiscount((int)$product['id_product'], Shop::getCurrentShop(), (int)$cart->id_currency, (int)$vat_address->id_country, (int)$customer->id_default_group, (int)$product['cart_quantity']);
$quantityDiscount = SpecificPrice::getQuantityDiscount((int)$product['id_product'], $this->shopID, (int)$cart->id_currency, (int)$vat_address->id_country, (int)$customer->id_default_group, (int)$product['cart_quantity']);
$unitPrice = Product::getPriceStatic((int)$product['id_product'], true, ($product['id_product_attribute'] ? intval($product['id_product_attribute']) : NULL), 2, NULL, false, true, 1, false, (int)$order->id_customer, NULL, (int)$order->{Configuration::get('PS_TAX_ADDRESS_TYPE')});
$quantityDiscountValue = $quantityDiscount ? ((Product::getTaxCalculationMethod((int)$order->id_customer) == PS_TAX_EXC ? Tools::ps_round($unitPrice, 2) : $unitPrice) - $quantityDiscount['price'] * (1 + $tax_rate / 100)) : 0.00;
$query .= '('.(int)($order->id).',
+10 -10
View File
@@ -1375,7 +1375,7 @@ class ProductCore extends ObjectModel
public static function getNewProducts($id_lang, $pageNumber = 0, $nbProducts = 10, $count = false, $orderBy = NULL, $orderWay = NULL, $id_shop = null)
{
if (is_null($id_shop))
$id_shop = Shop::getCurrentShop();
$id_shop = Context::getContext()->shop->getID();
if ($pageNumber < 0) $pageNumber = 0;
if ($nbProducts < 1) $nbProducts = 10;
if (empty($orderBy) || $orderBy == 'position') $orderBy = 'date_add';
@@ -1456,7 +1456,7 @@ class ProductCore extends ObjectModel
$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(Shop::getCurrentShop(), $context->currency->id, $id_country, $context->customer->id_default_group, $beginning, $ending);
return SpecificPrice::getProductIdByDate($context->shop->getID(), $context->currency->id, $id_country, $context->customer->id_default_group, $beginning, $ending);
}
/**
@@ -1523,7 +1523,7 @@ class ProductCore extends ObjectModel
if (!Validate::isBool($count))
die(Tools::displayError());
if (is_null($id_shop)) $id_shop = Shop::getCurrentShop(true);
if (is_null($id_shop)) $id_shop = Context::getContext()->shop->getID();
if ($pageNumber < 0) $pageNumber = 0;
if ($nbProducts < 1) $nbProducts = 10;
if (empty($orderBy) || $orderBy == 'position') $orderBy = 'price';
@@ -1810,9 +1810,7 @@ class ProductCore extends ObjectModel
if ($usetax != false AND !empty($address_infos['vat_number']) AND $address_infos['id_country'] != Configuration::get('VATNUMBER_COUNTRY') AND Configuration::get('VATNUMBER_MANAGEMENT'))
$usetax = false;
$id_shop = (int)(Shop::getCurrentShop());
return Product::priceCalculation($id_shop, $id_product, $id_product_attribute, $id_country, $id_state, $id_county, $id_currency, $id_group, $quantity, $usetax, $decimals, $only_reduc,
return Product::priceCalculation($context->shop->getID(), $id_product, $id_product_attribute, $id_country, $id_state, $id_county, $id_currency, $id_group, $quantity, $usetax, $decimals, $only_reduc,
$usereduc, $with_ecotax, $specificPriceOutput);
}
@@ -1950,6 +1948,9 @@ class ProductCore extends ObjectModel
public static function isDiscounted($id_product, $quantity = 1, $context = null)
{
if (!$context)
$context = Context::getContext();
$id_group = $context->customer->id_default_group;
$cart_quantity = !$context->cart ? 0 : Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('
SELECT SUM(`quantity`)
@@ -1960,8 +1961,7 @@ class ProductCore extends ObjectModel
$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);
return (bool)SpecificPrice::getSpecificPrice((int)$id_product, $context->shop->getID(), $id_currency, $id_country, $id_group, $quantity);
}
/**
@@ -2084,7 +2084,7 @@ class ProductCore extends ObjectModel
{
if (!$this->id)
return ;
$shop = Shop::getCurrentShop(true);
$shop = Context::getContext()->shop->getID();
// For retrocompatibility
$id_product_attribute = (int)$id_product_attribute;
@@ -2157,7 +2157,7 @@ class ProductCore extends ObjectModel
if (!$this->id)
return 0;
$shop = Shop::getCurrentShop(true);
$shop = Context::getContext()->shop->getID();
$sql = 'SELECT quantity
FROM '._DB_PREFIX_.'stock
WHERE id_product = '.$this->id.'
+5 -5
View File
@@ -116,13 +116,13 @@ class ProductSaleCore
** @param integer $nbProducts Number of products to return (optional)
** @return array keys : id_product, link_rewrite, name, id_image, legend, sales, ean13, upc, link
*/
static public function getBestSalesLight($id_lang, $pageNumber = 0, $nbProducts = 10, $id_shop = null)
static public function getBestSalesLight($id_lang, $pageNumber = 0, $nbProducts = 10, $id_shop = null, $context = null)
{
global $link;
if (!$context)
$context = Context::getContext();
if ($pageNumber < 0) $pageNumber = 0;
if ($nbProducts < 1) $nbProducts = 10;
if (is_null($id_shop)) $id_shop = Shop::getCurrentShop();
if (is_null($id_shop)) $id_shop = $context->shop->getID();
$groups = FrontController::getCurrentCustomerGroups();
$sqlGroups = (count($groups) ? 'IN ('.implode(',', $groups).')' : '= 1');
@@ -150,7 +150,7 @@ class ProductSaleCore
foreach ($result AS &$row)
{
$row['link'] = $link->getProductLink($row['id_product'], $row['link_rewrite'], $row['category'], $row['ean13']);
$row['link'] = $context->link->getProductLink($row['id_product'], $row['link_rewrite'], $row['category'], $row['ean13']);
$row['id_image'] = Product::defineProductImage($row, $id_lang);
}
return $result;
+3 -4
View File
@@ -147,19 +147,18 @@ class SearchCore
public static function find($id_lang, $expr, $pageNumber = 1, $pageSize = 1, $orderBy = 'position', $orderWay = 'desc', $ajax = false, $useCookie = true, $id_shop = false, $context = null)
{
global $cookie;
if (!$context)
$context = Context::getContext();
$db = Db::getInstance(_PS_USE_SQL_SLAVE_);
// Only use cookie if id_customer is not present
if ($useCookie)
$id_customer = $cookie->id_customer;
$id_customer = $context->customer->id;
else
$id_customer = 0;
if (!$id_shop)
$id_shop = Shop::getCurrentShop();
$id_shop = $context->shop->getID();
// TODO : smart page management
if ($pageNumber < 1) $pageNumber = 1;
@@ -541,7 +540,7 @@ class SearchCore
$shops = array($context->shop->getID());
if (!is_array($shops))
$shops = array($shops);
// Only use cookie if id_customer is not present
if ($useCookie)
$id_customer = (int)$context->customer->id;
+13 -10
View File
@@ -253,31 +253,34 @@ class ShopCore extends ObjectModel
/**
* Get group of current shop
*
* @param bool $asObject If false, return only the ID of the group
* @return GroupShop
*/
public function getGroup($asObject = false)
{
if (!$asObject)
return $this->id_group_shop;
if (!$this->group)
$this->group = new GroupShop($this->id_group_shop);
return $this->group;
}
/**
* Get current shop ID.
* Get current shop ID
*
* @param bool $forceContext If true and if no shop is selected in context, get default shop ID
* @return int
*/
public static function getID($forceContext = false)
public function getID()
{
if (!$this->id && $forceContext)
return (int)Configuration::get('PS_SHOP_DEFAULT');
return (int)$this->id;
}
/**
* Get current shop group ID
*
* @return int
*/
public function getGroupID()
{
return (int)$this->id_group_shop;
}
/**
* Get root category of current shop
@@ -571,7 +574,7 @@ class ShopCore extends ObjectModel
$alias .= '.';
if (is_null($shopID))
$shopID = Shop::getCurrentShop(true);
$shopID = Context::getContext()->shop->getID();
if (!$shopID)
return ($shopGroupID) ? ' AND '.$alias.'id_group_shop = '.(int)$shopGroupID : '';
+2 -2
View File
@@ -87,7 +87,7 @@ class ShopUrlCore extends ObjectModel
if (!self::$main_domain)
self::$main_domain = Db::getInstance()->getValue('SELECT domain
FROM '._DB_PREFIX_.'shop_url
WHERE main=1 AND id_shop='.(int)Shop::getCurrentShop());
WHERE main=1 AND id_shop='.Context::getContext()->shop->getID());
return self::$main_domain;
}
@@ -96,7 +96,7 @@ class ShopUrlCore extends ObjectModel
if (!self::$main_domain)
self::$main_domain = Db::getInstance()->getValue('SELECT domain
FROM '._DB_PREFIX_.'shop_url
WHERE main=1 AND id_shop='.(int)Shop::getCurrentShop());
WHERE main=1 AND id_shop='.Context::getContext()->shop->getID());
return self::$main_domain_ssl;
}
}