// Context in modules + Shop::sql* methods

git-svn-id: http://dev.prestashop.com/svn/v1/branches/1.5.x@7648 b9a71923-0436-4b27-9f14-aed3839534dd
This commit is contained in:
rMalie
2011-07-19 09:23:10 +00:00
parent 2c493137c3
commit 8a0a62ae03
25 changed files with 127 additions and 263 deletions
+14 -17
View File
@@ -1171,28 +1171,25 @@ abstract class AdminTabCore
$whereShop = Shop::sqlRestriction($this->shopShareDatas, 'a', null, null, $this->shopLinkType);
}
$filterShop = '';
if (Context::shop() != Shop::CONTEXT_ALL)
$assos = Shop::getAssoTables();
if (isset($assos[$this->table]) && $assos[$this->table]['type'] == 'shop')
{
$assos = Shop::getAssoTables();
if (isset($assos[$this->table]) && $assos[$this->table]['type'] == 'shop')
$filterKey = $assos[$this->table]['type'];
$idenfierShop = Shop::getListFromContext();
}
else if (Context::shop() == Shop::CONTEXT_GROUP)
{
$assos = GroupShop::getAssoTables();
if (isset($assos[$this->table]) AND $assos[$this->table]['type'] == 'group_shop')
{
$filterKey = $assos[$this->table]['type'];
$idenfierShop = Shop::getListFromContext();
$idenfierShop = array($context->shop->getGroupID());
}
else if (Context::shop() == Shop::CONTEXT_GROUP)
{
$assos = GroupShop::getAssoTables();
if (isset($assos[$this->table]) AND $assos[$this->table]['type'] == 'group_shop')
{
$filterKey = $assos[$this->table]['type'];
$idenfierShop = array($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.' IN ('.implode(', ', $idenfierShop).'))';
}
$filterShop = '';
if (isset($filterKey))
$filterShop = 'JOIN `'._DB_PREFIX_.$this->table.'_'.$filterKey.'` sa ON (sa.'.$this->identifier.' = a.'.$this->identifier.' AND sa.id_'.$filterKey.' IN ('.implode(', ', $idenfierShop).'))';
/* Query in order to get results with all fields */
$sql = 'SELECT SQL_CALC_FOUND_ROWS
+2 -2
View File
@@ -493,7 +493,7 @@ class CategoryCore extends ObjectModel
{
$sql = 'SELECT COUNT(cp.`id_product`) AS total
FROM `'._DB_PREFIX_.'product` p
'.Shop::sqlAsso('product', 'p', $context).'
'.Shop::sqlAsso('product', 'p', true, $context).'
LEFT JOIN `'._DB_PREFIX_.'category_product` cp ON p.`id_product` = cp.`id_product`
WHERE cp.`id_category` = '.(int)($this->id).
($active ? ' AND p.`active` = 1' : '').
@@ -506,7 +506,7 @@ class CategoryCore extends ObjectModel
FROM `'._DB_PREFIX_.'category_product` cp
LEFT JOIN `'._DB_PREFIX_.'product` p ON p.`id_product` = cp.`id_product`
LEFT JOIN `'._DB_PREFIX_.'product_attribute` pa ON (p.`id_product` = pa.`id_product` AND default_on = 1)
'.Shop::sqlAsso('product', 'p', $context).'
'.Shop::sqlAsso('product', 'p', true, $context).'
'.Product::sqlStock('p', 'pa', false, $context).'
LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (p.`id_category_default` = cl.`id_category` AND cl.`id_lang` = '.(int)$id_lang.Shop::sqlLang('cl', $context).')
LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = '.(int)$id_lang.Shop::sqlLang('pl', $context).')
+12 -33
View File
@@ -90,24 +90,19 @@ abstract class ModuleCore
* @var array used by AdminTab to determine which lang file to use (admin.php or module lang file)
*/
public static $classInModule = array();
/** @var int */
protected $shopID;
/** @var int */
protected $shopGroupID;
/** @var Context */
protected $context;
/**
* Constructor
*
* @param string $name Module unique name
* @param Context $context
*/
public function __construct($name = null)
public function __construct($name = null, Context $context = null)
{
// Search the module shop context
list($shopID, $shopGroupID) = Shop::getContext();
$this->setShopID($shopID);
$this->setShopGroupID($shopGroupID);
$this->context = ($context) ? $context : Context::getContext();
if ($this->name == NULL)
$this->name = $this->id;
@@ -115,7 +110,7 @@ abstract class ModuleCore
{
if (self::$modulesCache == NULL AND !is_array(self::$modulesCache))
{
$list = Shop::getListOfID($this->shopID, $this->shopGroupID);
$list = Shop::getListOfID($this->context);
// Join clause is done to check if the module is activated in current shop context
$sql = 'SELECT m.id_module, m.name, (
@@ -142,26 +137,10 @@ abstract class ModuleCore
}
}
}
/**
* @param int $id Set shopID property
*/
public function setShopID($id)
{
$this->shopID = (int)$id;
}
/**
* @param int $id Set shopGroupID property
*/
public function setShopGroupID($id)
{
$this->shopGroupID = (int)$id;
}
protected function sqlShopRestriction($share = false, $alias = null)
{
return Shop::sqlRestriction($share, $alias, $this->shopID, $this->shopGroupID, 'shop');
return Shop::sqlRestriction($share, $alias, $this->context, 'shop');
}
/**
@@ -240,7 +219,7 @@ abstract class ModuleCore
*/
public function enable($forceAll = false)
{
$list = Shop::getListOfID($this->shopID, $this->shopGroupID);
$list = Shop::getListOfID($this->context);
$sql = 'SELECT id_shop
FROM '._DB_PREFIX_.'module_shop
WHERE id_module = '.$this->id.'
@@ -288,7 +267,7 @@ abstract class ModuleCore
{
$sql = 'DELETE FROM '._DB_PREFIX_.'module_shop
WHERE id_module = '.$this->id.'
'.((!$forceAll) ? ' AND id_shop IN('.implode(', ', Shop::getListOfID($this->shopID, $this->shopGroupID)).')' : '');
'.((!$forceAll) ? ' AND id_shop IN('.implode(', ', Shop::getListOfID($this->context)).')' : '');
Db::getInstance()->execute($sql);
}
@@ -916,7 +895,7 @@ abstract class ModuleCore
*/
public function updatePosition($id_hook, $way, $position = NULL)
{
$list = ShopCore::getListOfID($this->shopID, $this->shopGroupID);
$list = ShopCore::getListOfID($this->context);
foreach ($list as $shopID)
{
$sql = 'SELECT hm.`id_module`, hm.`position`, hm.`id_hook`
+2 -2
View File
@@ -1517,7 +1517,7 @@ class ProductCore extends ObjectModel
{
$sql = 'SELECT COUNT(DISTINCT p.`id_product`) AS nb
FROM `'._DB_PREFIX_.'product` p
'.Shop::sqlAsso('product', 'p', $context).'
'.Shop::sqlAsso('product', 'p', true, $context).'
WHERE p.`active` = 1
AND p.`show_price` = 1
'.((!$beginning AND !$ending) ? ' AND p.`id_product` IN('.((is_array($ids_product) AND sizeof($ids_product)) ? implode(', ', $ids_product) : 0).')' : '').'
@@ -1535,7 +1535,7 @@ class ProductCore extends ObjectModel
pl.`name`, i.`id_image`, il.`legend`, t.`rate`, m.`name` AS manufacturer_name,
DATEDIFF(p.`date_add`, DATE_SUB(NOW(), INTERVAL '.(Validate::isUnsignedInt(Configuration::get('PS_NB_DAYS_NEW_PRODUCT')) ? Configuration::get('PS_NB_DAYS_NEW_PRODUCT') : 20).' DAY)) > 0 AS new
FROM `'._DB_PREFIX_.'product` p
'.Shop::sqlAsso('product', 'p', $context).'
'.Shop::sqlAsso('product', 'p', true, $context).'
'.Product::sqlStock('p', 0, false, $context).'
LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (p.`id_product` = pl.`id_product` AND pl.`id_lang` = '.(int)$id_lang.Shop::sqlLang('pl', $context).')
LEFT JOIN `'._DB_PREFIX_.'image` i ON (i.`id_product` = p.`id_product` AND i.`cover` = 1)
+21 -16
View File
@@ -449,8 +449,11 @@ class ShopCore extends ObjectModel
* @param bool $share If true and if $shopID is set, list shops with share data too
* @return array
*/
public static function getListOfID($shopID, $shopGroupID, $share = false)
public static function getListOfID(Context $context, $share = false)
{
$shopID = $context->shop->getID();
$shopGroupID = $context->shop->getGroupID();
if ($shopID)
$list = (!$share) ? array($shopID) : Shop::getSharedShops($shopID);
else if ($shopGroupID)
@@ -531,14 +534,11 @@ class ShopCore extends ObjectModel
/**
* Get a list of ID concerned by the shop context (E.g. if context is shop group, get list of children shop ID)
*
* @param Context $context
* @return array
*/
public static function getListFromContext(Context $context = null)
public static function getListFromContext()
{
if (!$context)
$context = Context::getContext();
return Shop::getListOfID($context->shop->getID(), $context->shop->getGroupID());
return Shop::getListOfID(Context::getContext());
}
/**
@@ -556,7 +556,7 @@ class ShopCore extends ObjectModel
return Shop::CONTEXT_ALL;
}
public static function sqlRestriction($share = false, $alias = null, $shopID = null, $shopGroupID = null, $type = 'shop')
public static function sqlRestriction($share = false, $alias = null, Context $context = null, $type = 'shop')
{
if ($type != 'shop' && $type != 'group_shop')
$type = 'shop';
@@ -565,10 +565,10 @@ class ShopCore extends ObjectModel
$alias .= '.';
$restriction = '';
if (is_null($shopID))
$shopID = Shop::getContext('shop');
if (is_null($shopGroupID))
$shopGroupID = Shop::getContext('group');
if (!$context)
$context = Context::getContext();
$shopID = $context->shop->getID();
$shopGroupID = $context->shop->getGroupID();
if ($type == 'group_shop')
{
@@ -580,7 +580,7 @@ class ShopCore extends ObjectModel
else
{
if ($shopID || $shopGroupID)
$restriction = ' AND '.$alias.'id_shop IN ('.implode(', ', Shop::getListOfID($shopID, $shopGroupID, $share)).')';
$restriction = ' AND '.$alias.'id_shop IN ('.implode(', ', Shop::getListOfID($context, $share)).')';
}
return $restriction;
@@ -610,21 +610,26 @@ class ShopCore extends ObjectModel
*
* @param string $table Table name (E.g. product, module, etc.)
* @param string $alias Alias of table
* @param bool $innerJoin Use or not INNER JOIN
* @param Context $context
* @return string
*/
public static function sqlAsso($table, $alias, Context $context = null)
public static function sqlAsso($table, $alias, $innerJoin = false, Context $context = null)
{
if (!$context)
$context = Context::getContext();
$tableAlias = ' asso_shop_'.$table;
if (strpos($table, '.') !== false)
list($tableAlias, $table) = explode('.', $table);
$assoTables = Shop::getAssoTables();
if (!isset($assoTables[$table]) || $assoTables[$table]['type'] != 'shop')
return ;
$sql = ' LEFT JOIN '._DB_PREFIX_.$table.'_shop asso_shop_'.$table.'
ON asso_shop_'.$table.'.id_'.$table.' = '.$alias.'.id_'.$table.'
AND asso_shop_'.$table.'.id_shop IN('.implode(', ', Shop::getListFromContext($context)).') ';
$sql = (($innerJoin) ? ' INNER' : ' LEFT').' JOIN '._DB_PREFIX_.$table.'_shop '.$tableAlias.'
ON '.$tableAlias.'.id_'.$table.' = '.$alias.'.id_'.$table.'
AND '.$tableAlias.'.id_shop IN('.implode(', ', Shop::getListFromContext($context)).') ';
return $sql;
}