diff --git a/classes/Category.php b/classes/Category.php
index 8dc4f043a..3ccc08776 100644
--- a/classes/Category.php
+++ b/classes/Category.php
@@ -47,6 +47,9 @@ class CategoryCore extends ObjectModel
/** @var integer Parent category ID */
public $id_parent;
+ /** @var integer default Category id */
+ public $id_category_default;
+
/** @var integer Parents number */
public $level_depth;
@@ -95,6 +98,7 @@ class CategoryCore extends ObjectModel
'level_depth' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'),
'active' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool', 'required' => true),
'id_parent' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedInt'),
+ 'id_shop_default' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
'is_root_category' => array('type' => self::TYPE_BOOL, 'validate' => 'isBool'),
'position' => array('type' => self::TYPE_INT),
'date_add' => array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
diff --git a/classes/ObjectModel.php b/classes/ObjectModel.php
index d7f04093d..3c85d68dc 100644
--- a/classes/ObjectModel.php
+++ b/classes/ObjectModel.php
@@ -449,9 +449,19 @@ abstract class ObjectModelCore
if ($autodate && property_exists($this, 'date_upd'))
$this->date_upd = date('Y-m-d H:i:s');
+
+ if (Shop::isTableAssociated($this->def['table']))
+ {
+ $id_shop_list = Shop::getContextListShopID();
+ if (count($this->id_shop_list) > 0)
+ $id_shop_list = $this->id_shop_list;
+ }
+
// Database insertion
if (isset($this->id))
unset($this->id);
+ if (Shop::checkIdShopDefault($this->def['table']))
+ $this->id_shop_default = min($id_shop_list);
if (!$result = ObjectModel::$db->insert($this->def['table'], $this->getFields(), $null_values))
return false;
@@ -461,10 +471,6 @@ abstract class ObjectModelCore
// Database insertion for multishop fields related to the object
if (Shop::isTableAssociated($this->def['table']))
{
- $id_shop_list = Shop::getContextListShopID();
- if (count($this->id_shop_list) > 0)
- $id_shop_list = $this->id_shop_list;
-
$fields = $this->getFieldsShop();
$fields[$this->def['primary']] = (int)$this->id;
@@ -534,7 +540,13 @@ abstract class ObjectModelCore
// Automatically fill dates
if (array_key_exists('date_upd', $this))
$this->date_upd = date('Y-m-d H:i:s');
+
+ $id_shop_list = Shop::getContextListShopID();
+ if (count($this->id_shop_list) > 0)
+ $id_shop_list = $this->id_shop_list;
+ if (Shop::checkIdShopDefault($this->def['table']))
+ $this->id_shop_default = min($id_shop_list);
// Database update
if (!$result = ObjectModel::$db->update($this->def['table'], $this->getFields(), '`'.pSQL($this->def['primary']).'` = '.(int)$this->id, 0, $null_values))
return false;
@@ -555,9 +567,6 @@ abstract class ObjectModelCore
else
$all_fields = $fields;
- $id_shop_list = Shop::getContextListShopID();
- if (count($this->id_shop_list) > 0)
- $id_shop_list = $this->id_shop_list;
foreach ($id_shop_list as $id_shop)
{
diff --git a/classes/Product.php b/classes/Product.php
index 6f3800e31..5a8c7df67 100644
--- a/classes/Product.php
+++ b/classes/Product.php
@@ -51,6 +51,9 @@ class ProductCore extends ObjectModel
/** @var integer default Category id */
public $id_category_default;
+ /** @var integer default Shop id */
+ public $id_shop_default;
+
/** @var string Manufacturer name */
public $manufacturer_name;
@@ -237,6 +240,7 @@ class ProductCore extends ObjectModel
'multilang_shop' => true,
'fields' => array(
// Classic fields
+ 'id_shop_default' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
'id_manufacturer' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
'id_supplier' => array('type' => self::TYPE_INT, 'validate' => 'isUnsignedId'),
'reference' => array('type' => self::TYPE_STRING, 'validate' => 'isReference', 'size' => 32),
@@ -2451,7 +2455,7 @@ class ProductCore extends ObjectModel
LIMIT 1
) as default_on');
$sql->leftJoin('product_attribute', 'pa', 'pa.`id_product` = '.(int)$id_product);
- $sql->join(Shop::addSqlAssociation('product_attribute', 'pa', false));
+ $sql->join(Shop::addSqlAssociation('product_attribute', 'pa', false, 'product_attribute_shop.id_shop = p.id_shop_default'));
}
$res = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
diff --git a/classes/controller/AdminController.php b/classes/controller/AdminController.php
index fafcb5276..ffb192110 100644
--- a/classes/controller/AdminController.php
+++ b/classes/controller/AdminController.php
@@ -109,6 +109,9 @@ class AdminControllerCore extends Controller
protected $shopLink;
+ /** @var string SQL query */
+ protected $_listsql = '';
+
/** @var array Cache for query results */
protected $_list = array();
@@ -248,6 +251,7 @@ class AdminControllerCore extends Controller
public $controller_name;
public $multishop_context = -1;
+ public $multishop_context_group = true;
/**
* Current breadcrumb position as an array of tab names
@@ -1927,6 +1931,9 @@ class AdminControllerCore extends Controller
*/
public function getList($id_lang, $order_by = null, $order_way = null, $start = 0, $limit = null, $id_lang_shop = false)
{
+ if (!empty($this->_listsql))
+ return;
+
/* Manage default params values */
$use_limit = true;
if ($limit === false)
@@ -1997,22 +2004,19 @@ class AdminControllerCore extends Controller
$where_shop = Shop::addSqlRestriction($this->shopShareDatas, 'a', $this->shopLinkType);
}
- $filter_shop = '';
if ($this->multishop_context && Shop::isTableAssociated($this->table) && !empty($this->className))
{
if (Shop::getContext() != Shop::CONTEXT_ALL || Shop::isTableAssociated($this->table) || !$this->context->employee->isSuperAdmin())
{
- $idenfier_shop = Shop::getContextListShopID();
- if (!$this->_group)
- $this->_group = ' GROUP BY a.'.pSQL($this->identifier);
- elseif (!preg_match('#(\s|,)\s*a\.`?'.pSQL($this->identifier).'`?(\s|,|$)#', $this->_group))
- $this->_group .= ', a.'.pSQL($this->identifier);
-
+ $identifier_shop = Shop::getContextListShopID();
$test_join = !preg_match('#`?'.preg_quote(_DB_PREFIX_.$this->table.'_shop').'`? *sa#', $this->_join);
if (Shop::isFeatureActive() && $test_join)
{
- $filter_shop = ' JOIN `'._DB_PREFIX_.$this->table.'_shop` sa ';
- $filter_shop .= 'ON (sa.'.$this->identifier.' = a.'.$this->identifier.' AND sa.id_shop IN ('.implode(', ', $idenfier_shop).'))';
+ $this->_where .= ' AND a.'.$this->identifier.' IN (
+ SELECT sa.'.$this->identifier.'
+ FROM `'._DB_PREFIX_.$this->table.'_shop` sa
+ WHERE sa.id_shop IN ('.implode(', ', $identifier_shop).')
+ )';
}
}
}
@@ -2021,13 +2025,14 @@ class AdminControllerCore extends Controller
$lang_join = '';
if ($this->lang)
{
- $lang_join = 'LEFT JOIN `'._DB_PREFIX_.$this->table.'_lang` b ON (b.`'.$this->identifier.'` = a.`'.$this->identifier.'`';
- $lang_join .= ' AND b.`id_lang` = '.(int)$id_lang;
+ $lang_join = 'LEFT JOIN `'._DB_PREFIX_.$this->table.'_lang` b ON (b.`'.$this->identifier.'` = a.`'.$this->identifier.'` AND b.`id_lang` = '.(int)$id_lang;
if ($id_lang_shop)
+ {
if (Shop::getContext() == Shop::CONTEXT_SHOP)
- $lang_join .= ' AND b.`id_shop`='.(int)$id_lang_shop;
+ $lang_join .= ' AND b.`id_shop` = '.(int)$id_lang_shop;
else
- $lang_join .= ' AND b.`id_shop` IN ('.implode(',', array_map('intval', Shop::getContextListShopID())).')';
+ $lang_join .= ' AND b.`id_shop` = a.id_shop_default';
+ }
$lang_join .= ')';
}
@@ -2047,23 +2052,23 @@ class AdminControllerCore extends Controller
$order_by = pSQL($order_by[0]).'.`'.pSQL($order_by[1]).'`';
}
- $sql = 'SELECT SQL_CALC_FOUND_ROWS
- '.($this->_tmpTableFilter ? ' * FROM (SELECT ' : '').'
- '.($this->lang ? 'b.*, ' : '').'a.*'.(isset($this->_select) ? ', '.$this->_select.' ' : '').$select_shop.'
- FROM `'._DB_PREFIX_.$sql_table.'` a
- '.$filter_shop.'
- '.$lang_join.'
- '.(isset($this->_join) ? $this->_join.' ' : '').'
- '.$join_shop.'
- WHERE 1 '.(isset($this->_where) ? $this->_where.' ' : '').($this->deleted ? 'AND a.`deleted` = 0 ' : '').
- (isset($this->_filter) ? $this->_filter : '').$where_shop.'
- '.(isset($this->_group) ? $this->_group.' ' : '').'
- '.$having_clause.'
- ORDER BY '.(($order_by == $this->identifier) ? 'a.' : '').pSQL($order_by).' '.pSQL($order_way).
- ($this->_tmpTableFilter ? ') tmpTable WHERE 1'.$this->_tmpTableFilter : '').
- (($use_limit === true) ? ' LIMIT '.(int)$start.','.(int)$limit : '');
+ $this->_listsql = '
+ SELECT SQL_CALC_FOUND_ROWS
+ '.($this->_tmpTableFilter ? ' * FROM (SELECT ' : '').'
+ '.($this->lang ? 'b.*, ' : '').'a.*'.(isset($this->_select) ? ', '.$this->_select.' ' : '').$select_shop.'
+ FROM `'._DB_PREFIX_.$sql_table.'` a
+ '.$lang_join.'
+ '.(isset($this->_join) ? $this->_join.' ' : '').'
+ '.$join_shop.'
+ WHERE 1 '.(isset($this->_where) ? $this->_where.' ' : '').($this->deleted ? 'AND a.`deleted` = 0 ' : '').
+ (isset($this->_filter) ? $this->_filter : '').$where_shop.'
+ '.(isset($this->_group) ? $this->_group.' ' : '').'
+ '.$having_clause.'
+ ORDER BY '.(($order_by == $this->identifier) ? 'a.' : '').pSQL($order_by).' '.pSQL($order_way).
+ ($this->_tmpTableFilter ? ') tmpTable WHERE 1'.$this->_tmpTableFilter : '').
+ (($use_limit === true) ? ' LIMIT '.(int)$start.','.(int)$limit : '');
- $this->_list = Db::getInstance()->executeS($sql);
+ $this->_list = Db::getInstance()->executeS($this->_listsql);
$this->_listTotal = Db::getInstance()->getValue('SELECT FOUND_ROWS() AS `'._DB_PREFIX_.$this->table.'`');
}
diff --git a/classes/helper/Helper.php b/classes/helper/Helper.php
index 0fa7e758c..0693a4c9a 100755
--- a/classes/helper/Helper.php
+++ b/classes/helper/Helper.php
@@ -361,9 +361,10 @@ class HelperCore
$context = Context::getContext();
// Get default value
- if (Shop::getContext() == Shop::CONTEXT_ALL)
+ $shop_context = Shop::getContext();
+ if ($shop_context == Shop::CONTEXT_ALL || ($context->controller->multishop_context_group == false && $shop_context == Shop::CONTEXT_GROUP))
$value = '';
- else if (Shop::getContext() == Shop::CONTEXT_GROUP)
+ else if ($shop_context == Shop::CONTEXT_GROUP)
$value = 'g-'.Shop::getContextShopGroupID();
else
$value = 's-'.Shop::getContextShopID();
@@ -376,9 +377,9 @@ class HelperCore
foreach ($tree as $gID => $group_data)
{
if ((!isset($context->controller->multishop_context) || $context->controller->multishop_context & Shop::CONTEXT_GROUP))
- $html .= '';
+ $html .= '';
else
- $html .= '