[-] BO : Category code cleaning, fixing, moving and multistore-ing

This commit is contained in:
Damien Metzger
2013-11-27 17:40:35 +01:00
parent 8c4e2a9a93
commit 8dc4eb5c03
+135 -188
View File
@@ -191,18 +191,13 @@ class CategoryCore extends ObjectModel
// If the parent category was changed, we don't want to have 2 categories with the same position // If the parent category was changed, we don't want to have 2 categories with the same position
if ($this->getDuplicatePosition()) if ($this->getDuplicatePosition())
{ {
$assos = array();
if (Tools::isSubmit('checkBoxShopAsso_category')) if (Tools::isSubmit('checkBoxShopAsso_category'))
{ foreach (Tools::getValue('checkBoxShopAsso_category') as $id_asso_object => $row)
$check_box = Tools::getValue('checkBoxShopAsso_category');
foreach ($check_box as $id_asso_object => $row)
{
foreach ($row as $id_shop => $value) foreach ($row as $id_shop => $value)
$assos[] = array('id_object' => (int)$id_asso_object, 'id_shop' => (int)$id_shop); $this->addPosition(Category::getLastPosition((int)$this->id_parent, (int)$id_shop), (int)$id_shop);
} else
} foreach (Shop::getShops(true) as $shop)
foreach ($assos as $shop) $this->addPosition(max(1, Category::getLastPosition((int)$this->id_parent, $shop['id_shop'])), $shop['id_shop']);
$this->addPosition(Category::getLastPosition((int)$this->id_parent, $shop['id_shop']), $shop['id_shop']);
} }
$this->cleanPositions((int)$this->id_parent); $this->cleanPositions((int)$this->id_parent);
$ret = parent::update($null_values); $ret = parent::update($null_values);
@@ -382,19 +377,16 @@ class CategoryCore extends ObjectModel
*/ */
public static function regenerateEntireNtree() public static function regenerateEntireNtree()
{ {
$id = Context::getContext()->shop->id;
$id_shop = $id ? $id: Configuration::get('PS_SHOP_DEFAULT');
$categories = Db::getInstance()->executeS(' $categories = Db::getInstance()->executeS('
SELECT c.`id_category`, c.`id_parent` SELECT c.`id_category`, c.`id_parent`
FROM `'._DB_PREFIX_.'category` c FROM `'._DB_PREFIX_.'category` c
LEFT JOIN `'._DB_PREFIX_.'category_shop` cs '.Shop::addSqlAssociation('category', 'c').'
ON (c.`id_category` = cs.`id_category` AND cs.`id_shop` = '.(int)$id_shop.') ORDER BY c.`id_parent`, category_shop.`position` ASC');
ORDER BY c.`id_parent`, cs.`position` ASC');
$categories_array = array(); $categories_array = array();
foreach ($categories as $category) foreach ($categories as $category)
$categories_array[$category['id_parent']]['subcategories'][] = $category['id_category']; $categories_array[$category['id_parent']]['subcategories'][] = $category['id_category'];
$n = 1;
$n = 1;
if (isset($categories_array[0]) && $categories_array[0]['subcategories']) if (isset($categories_array[0]) && $categories_array[0]['subcategories'])
Category::_subTree($categories_array, $categories_array[0]['subcategories'][0], $n); Category::_subTree($categories_array, $categories_array[0]['subcategories'][0], $n);
} }
@@ -408,10 +400,9 @@ class CategoryCore extends ObjectModel
$right = (int)$n++; $right = (int)$n++;
Db::getInstance()->execute(' Db::getInstance()->execute('
UPDATE '._DB_PREFIX_.'category UPDATE '._DB_PREFIX_.'category
SET nleft = '.(int)$left.', nright = '.(int)$right.' SET nleft = '.(int)$left.', nright = '.(int)$right.'
WHERE id_category = '.(int)$id_category.' LIMIT 1 WHERE id_category = '.(int)$id_category.' LIMIT 1');
');
} }
/** /**
@@ -794,24 +785,27 @@ class CategoryCore extends ObjectModel
$id_shop = $shop->id ? $shop->id : Configuration::get('PS_SHOP_DEFAULT'); $id_shop = $shop->id ? $shop->id : Configuration::get('PS_SHOP_DEFAULT');
$selected_cat = explode(',', str_replace(' ', '', $selected_cat)); $selected_cat = explode(',', str_replace(' ', '', $selected_cat));
$sql = 'SELECT c.`id_category`, c.`level_depth`, cl.`name`, IF(( $sql = '
SELECT COUNT(*) SELECT c.`id_category`, c.`level_depth`, cl.`name`,
FROM `'._DB_PREFIX_.'category` c2 IF((
WHERE c2.`id_parent` = c.`id_category` SELECT COUNT(*)
) > 0, 1, 0) AS has_children, '.($selected_cat ? '( FROM `'._DB_PREFIX_.'category` c2
SELECT count(c3.`id_category`) WHERE c2.`id_parent` = c.`id_category`
FROM `'._DB_PREFIX_.'category` c3 ) > 0, 1, 0) AS has_children,
WHERE c3.`nleft` > c.`nleft` '.($selected_cat ? '(
AND c3.`nright` < c.`nright` SELECT count(c3.`id_category`)
FROM `'._DB_PREFIX_.'category` c3
WHERE c3.`nleft` > c.`nleft`
AND c3.`nright` < c.`nright`
AND c3.`id_category` IN ('.implode(',', array_map('intval', $selected_cat)).') AND c3.`id_category` IN ('.implode(',', array_map('intval', $selected_cat)).')
)' : '0').' AS nbSelectedSubCat )' : '0').' AS nbSelectedSubCat
FROM `'._DB_PREFIX_.'category` c FROM `'._DB_PREFIX_.'category` c
LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (c.`id_category` = cl.`id_category` '.Shop::addSqlRestrictionOnLang('cl', $id_shop).')'; LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (c.`id_category` = cl.`id_category` '.Shop::addSqlRestrictionOnLang('cl', $id_shop).')
$sql .= ' LEFT JOIN `'._DB_PREFIX_.'category_shop` cs ON (c.`id_category` = cs.`id_category` AND cs.`id_shop` = '.(int)$id_shop.')'; LEFT JOIN `'._DB_PREFIX_.'category_shop` cs ON (c.`id_category` = cs.`id_category` AND cs.`id_shop` = '.(int)$id_shop.')
$sql .= ' WHERE `id_lang` = '.(int)$id_lang; WHERE `id_lang` = '.(int)$id_lang.'
AND c.`id_parent` = '.(int)$id_parent;
if (Shop::getContext() == Shop::CONTEXT_SHOP && $use_shop_context) if (Shop::getContext() == Shop::CONTEXT_SHOP && $use_shop_context)
$sql .= ' AND cs.`id_shop` = '.(int)$shop->id; $sql .= ' AND cs.`id_shop` = '.(int)$shop->id;
$sql .= ' AND c.`id_parent` = '.(int)$id_parent;
if (!Shop::isFeatureActive() || Shop::getContext() == Shop::CONTEXT_SHOP && $use_shop_context) if (!Shop::isFeatureActive() || Shop::getContext() == Shop::CONTEXT_SHOP && $use_shop_context)
$sql .= ' ORDER BY cs.`position` ASC'; $sql .= ' ORDER BY cs.`position` ASC';
@@ -879,12 +873,11 @@ class CategoryCore extends ObjectModel
if (!isset(self::$_links[$id_category.'-'.$id_lang])) if (!isset(self::$_links[$id_category.'-'.$id_lang]))
self::$_links[$id_category.'-'.$id_lang] = Db::getInstance()->getValue(' self::$_links[$id_category.'-'.$id_lang] = Db::getInstance()->getValue('
SELECT cl.`link_rewrite` SELECT cl.`link_rewrite`
FROM `'._DB_PREFIX_.'category_lang` cl FROM `'._DB_PREFIX_.'category_lang` cl
WHERE `id_lang` = '.(int)$id_lang.' WHERE `id_lang` = '.(int)$id_lang.'
'.Shop::addSqlRestrictionOnLang('cl').' '.Shop::addSqlRestrictionOnLang('cl').'
AND cl.`id_category` = '.(int)$id_category AND cl.`id_category` = '.(int)$id_category);
);
return self::$_links[$id_category.'-'.$id_lang]; return self::$_links[$id_category.'-'.$id_lang];
} }
@@ -919,22 +912,17 @@ class CategoryCore extends ObjectModel
{ {
if ($unrestricted === true) if ($unrestricted === true)
return Db::getInstance()->getRow(' return Db::getInstance()->getRow('
SELECT c.*, cl.* SELECT c.*, cl.*
FROM `'._DB_PREFIX_.'category` c FROM `'._DB_PREFIX_.'category` c
LEFT JOIN `'._DB_PREFIX_.'category_lang` cl LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (c.`id_category` = cl.`id_category` '.Shop::addSqlRestrictionOnLang('cl').')
ON (c.`id_category` = cl.`id_category`'.Shop::addSqlRestrictionOnLang('cl').') WHERE `name` LIKE \''.pSQL($query).'\'');
WHERE `name` LIKE \''.pSQL($query).'\'
');
else else
return Db::getInstance()->executeS(' return Db::getInstance()->executeS('
SELECT c.*, cl.* SELECT c.*, cl.*
FROM `'._DB_PREFIX_.'category` c FROM `'._DB_PREFIX_.'category` c
LEFT JOIN `'._DB_PREFIX_.'category_lang` cl LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (c.`id_category` = cl.`id_category` AND `id_lang` = '.(int)$id_lang.' '.Shop::addSqlRestrictionOnLang('cl').')
ON (c.`id_category` = cl.`id_category` WHERE `name` LIKE \'%'.pSQL($query).'%\'
AND `id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('cl').') AND c.`id_category` != '.(int)Configuration::get('PS_HOME_CATEGORY'));
WHERE `name` LIKE \'%'.pSQL($query).'%\'
AND c.`id_category` != '.(int)Configuration::get('PS_HOME_CATEGORY')
);
} }
/** /**
@@ -948,15 +936,14 @@ class CategoryCore extends ObjectModel
public static function searchByNameAndParentCategoryId($id_lang, $category_name, $id_parent_category) public static function searchByNameAndParentCategoryId($id_lang, $category_name, $id_parent_category)
{ {
return Db::getInstance()->getRow(' return Db::getInstance()->getRow('
SELECT c.*, cl.* SELECT c.*, cl.*
FROM `'._DB_PREFIX_.'category` c FROM `'._DB_PREFIX_.'category` c
LEFT JOIN `'._DB_PREFIX_.'category_lang` cl LEFT JOIN `'._DB_PREFIX_.'category_lang` cl
ON (c.`id_category` = cl.`id_category` ON (c.`id_category` = cl.`id_category`
AND `id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('cl').') AND `id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('cl').')
WHERE `name` LIKE \''.pSQL($category_name).'\' WHERE `name` LIKE \''.pSQL($category_name).'\'
AND c.`id_category` != '.(int)Configuration::get('PS_HOME_CATEGORY').' AND c.`id_category` != '.(int)Configuration::get('PS_HOME_CATEGORY').'
AND c.`id_parent` = '.(int)$id_parent_category AND c.`id_parent` = '.(int)$id_parent_category);
);
} }
/** /**
@@ -989,31 +976,24 @@ class CategoryCore extends ObjectModel
ON (c.`id_category` = cl.`id_category` ON (c.`id_category` = cl.`id_category`
AND `id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('cl').')'; AND `id_lang` = '.(int)$id_lang.Shop::addSqlRestrictionOnLang('cl').')';
if (Shop::isFeatureActive() && Shop::getContext() == Shop::CONTEXT_SHOP) if (Shop::isFeatureActive() && Shop::getContext() == Shop::CONTEXT_SHOP)
$sql .= ' $sql .= ' LEFT JOIN `'._DB_PREFIX_.'category_shop` cs ON (c.`id_category` = cs.`id_category` AND cs.`id_shop` = '.(int)$id_shop.')';
LEFT JOIN `'._DB_PREFIX_.'category_shop` cs $sql .= ' WHERE c.`id_category` = '.(int)$id_current;
ON (c.`id_category` = cs.`id_category` AND cs.`id_shop` = '.(int)$id_shop.')';
$sql .= '
WHERE c.`id_category` = '.(int)$id_current;
if (Shop::isFeatureActive() && Shop::getContext() == Shop::CONTEXT_SHOP) if (Shop::isFeatureActive() && Shop::getContext() == Shop::CONTEXT_SHOP)
$sql .= ' $sql .= ' AND cs.`id_shop` = '.(int)$context->shop->id;
AND cs.`id_shop` = '.(int)$context->shop->id;
$root_category = Category::getRootCategory(); $root_category = Category::getRootCategory();
if (Shop::isFeatureActive() && Shop::getContext() == Shop::CONTEXT_SHOP && if (Shop::isFeatureActive() && Shop::getContext() == Shop::CONTEXT_SHOP
(!Tools::isSubmit('id_category') || && (!Tools::isSubmit('id_category') || (int)Tools::getValue('id_category') == (int)$root_category->id || (int)$root_category->id == (int)$context->shop->id_category))
(int)Tools::getValue('id_category') == (int)$root_category->id || $sql .= ' AND c.`id_parent` != 0';
(int)$root_category->id == (int)$context->shop->id_category))
$sql .= '
AND c.`id_parent` != 0';
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql); $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql);
if (isset($result[0])) if ($result)
$categories[] = $result[0]; $categories[] = $result;
else if (!$categories) elseif (!$categories)
$categories = array(); $categories = array();
if (!$result || ($result[0]['id_category'] == $context->shop->id_category)) if (!$result || ($result['id_category'] == $context->shop->id_category))
return $categories; return $categories;
$id_current = $result[0]['id_parent']; $id_current = $result['id_parent'];
} }
} }
/** /**
@@ -1045,10 +1025,7 @@ class CategoryCore extends ObjectModel
public function addGroups($groups) public function addGroups($groups)
{ {
foreach ($groups as $group) foreach ($groups as $group)
{ Db::getInstance()->insert('category_group', array('id_category' => (int)$this->id, 'id_group' => (int)$group));
$row = array('id_category' => (int)$this->id, 'id_group' => (int)$group);
Db::getInstance()->insert('category_group', $row);
}
} }
public function getGroups() public function getGroups()
@@ -1058,10 +1035,9 @@ class CategoryCore extends ObjectModel
if (!Cache::isStored($cache_id)) if (!Cache::isStored($cache_id))
{ {
$result = Db::getInstance()->executeS(' $result = Db::getInstance()->executeS('
SELECT cg.`id_group` SELECT cg.`id_group`
FROM '._DB_PREFIX_.'category_group cg FROM '._DB_PREFIX_.'category_group cg
WHERE cg.`id_category` = '.(int)$this->id WHERE cg.`id_category` = '.(int)$this->id);
);
$groups = array(); $groups = array();
foreach ($result as $group) foreach ($result as $group)
$groups[] = $group['id_group']; $groups[] = $group['id_group'];
@@ -1075,8 +1051,7 @@ class CategoryCore extends ObjectModel
$groups = $this->getGroups(); $groups = $this->getGroups();
if (!in_array((int)$id_group, $groups)) if (!in_array((int)$id_group, $groups))
return $this->addGroups(array((int)$id_group)); return $this->addGroups(array((int)$id_group));
else return false;
return false;
} }
/** /**
@@ -1115,33 +1090,29 @@ class CategoryCore extends ObjectModel
public function updateGroup($list) public function updateGroup($list)
{ {
$this->cleanGroups(); $this->cleanGroups();
if ($list && !empty($list)) if (empty($list))
$this->addGroups($list); $list = array(Configuration::get('PS_UNIDENTIFIED_GROUP'), Configuration::get('PS_GUEST_GROUP'), Configuration::get('PS_CUSTOMER_GROUP'));
else $this->addGroups($list);
$this->addGroups(array(Configuration::get('PS_UNIDENTIFIED_GROUP'), Configuration::get('PS_GUEST_GROUP'), Configuration::get('PS_CUSTOMER_GROUP')));
} }
public static function setNewGroupForHome($id_group) public static function setNewGroupForHome($id_group)
{ {
if (!(int)$id_group) if (!(int)$id_group)
return false; return false;
return Db::getInstance()->execute(' return Db::getInstance()->execute('
INSERT INTO `'._DB_PREFIX_.'category_group` INSERT INTO `'._DB_PREFIX_.'category_group`
VALUES ('.(int)Context::getContext()->shop->getCategory().', '.(int)$id_group.') VALUES ('.(int)Context::getContext()->shop->getCategory().', '.(int)$id_group.')');
');
} }
public function updatePosition($way, $position) public function updatePosition($way, $position)
{ {
$id = Context::getContext()->shop->id;
$id_shop = $id ? $id: Configuration::get('PS_SHOP_DEFAULT');
if (!$res = Db::getInstance()->executeS(' if (!$res = Db::getInstance()->executeS('
SELECT cp.`id_category`, cs.`position`, cp.`id_parent` SELECT cp.`id_category`, category_shop.`position`, cp.`id_parent`
FROM `'._DB_PREFIX_.'category` cp FROM `'._DB_PREFIX_.'category` cp
LEFT JOIN `'._DB_PREFIX_.'category_shop` cs '.Shop::addSqlAssociation('category', 'c').'
ON (cp.`id_category` = cs.`id_category` AND cs.`id_shop` = '.(int)$id_shop.')
WHERE cp.`id_parent` = '.(int)$this->id_parent.' WHERE cp.`id_parent` = '.(int)$this->id_parent.'
ORDER BY cs.`position` ASC' ORDER BY category_shop.`position` ASC'
)) ))
return false; return false;
@@ -1154,20 +1125,16 @@ class CategoryCore extends ObjectModel
// < and > statements rather than BETWEEN operator // < and > statements rather than BETWEEN operator
// since BETWEEN is treated differently according to databases // since BETWEEN is treated differently according to databases
$result = (Db::getInstance()->execute(' $result = (Db::getInstance()->execute('
UPDATE `'._DB_PREFIX_.'category_shop` cs UPDATE `'._DB_PREFIX_.'category` c '.Shop::addSqlAssociation('category', 'c').'
LEFT JOIN `'._DB_PREFIX_.'category` c SET category_shop.`position`= category_shop.`position` '.($way ? '- 1' : '+ 1').'
ON (c.`id_category` = cs.`id_category` AND cs.`id_shop` = '.(int)$id_shop.') WHERE category_shop.`position`
SET cs.`position`= cs.`position` '.($way ? '- 1' : '+ 1').'
WHERE cs.`position`
'.($way '.($way
? '> '.(int)$moved_category['position'].' AND cs.`position` <= '.(int)$position ? '> '.(int)$moved_category['position'].' AND category_shop.`position` <= '.(int)$position
: '< '.(int)$moved_category['position'].' AND cs.`position` >= '.(int)$position).' : '< '.(int)$moved_category['position'].' AND category_shop.`position` >= '.(int)$position).'
AND c.`id_parent`='.(int)$moved_category['id_parent']) AND c.`id_parent`='.(int)$moved_category['id_parent'])
&& Db::getInstance()->execute(' && Db::getInstance()->execute('
UPDATE `'._DB_PREFIX_.'category_shop` cs UPDATE `'._DB_PREFIX_.'category` c '.Shop::addSqlAssociation('category', 'c').'
LEFT JOIN `'._DB_PREFIX_.'category` c SET category_shop.`position` = '.(int)$position.'
ON (c.`id_category` = cs.`id_category` AND cs.`id_shop` = '.(int)$id_shop.')
SET cs.`position` = '.(int)$position.'
WHERE c.`id_parent` = '.(int)$moved_category['id_parent'].' WHERE c.`id_parent` = '.(int)$moved_category['id_parent'].'
AND c.`id_category`='.(int)$moved_category['id_category'])); AND c.`id_category`='.(int)$moved_category['id_category']));
Hook::exec('actionCategoryUpdate'); Hook::exec('actionCategoryUpdate');
@@ -1186,29 +1153,21 @@ class CategoryCore extends ObjectModel
{ {
if ($id_category_parent === null) if ($id_category_parent === null)
return; return;
$return = true;
$id = Context::getContext()->shop->id; $return = true;
$id_shop = $id ? $id : Configuration::get('PS_SHOP_DEFAULT');
$result = Db::getInstance()->executeS(' $result = Db::getInstance()->executeS('
SELECT c.`id_category` SELECT c.`id_category`
FROM `'._DB_PREFIX_.'category` c FROM `'._DB_PREFIX_.'category` c
LEFT JOIN `'._DB_PREFIX_.'category_shop` cs '.Shop::addSqlAssociation('category', 'c').'
ON (c.`id_category` = cs.`id_category` AND cs.`id_shop` = '.(int)$id_shop.') WHERE c.`id_parent` = '.(int)$id_category_parent.'
WHERE c.`id_parent` = '.(int)$id_category_parent.' ORDER BY category_shop.`position`');
ORDER BY cs.`position`
');
$count = count($result); $count = count($result);
for ($i = 0; $i < $count; $i++) for ($i = 0; $i < $count; $i++)
{ {
$sql = ' $return &= Db::getInstance()->execute('
UPDATE `'._DB_PREFIX_.'category` c UPDATE `'._DB_PREFIX_.'category` c '.Shop::addSqlAssociation('category', 'c').'
LEFT JOIN `'._DB_PREFIX_.'category_shop` cs SET category_shop.`position` = '.(int)($i + 1).'
ON (c.`id_category` = cs.`id_category` AND cs.`id_shop` = '.(int)$id_shop.') WHERE c.`id_parent` = '.(int)$id_category_parent.' AND c.`id_category` = '.(int)$result[$i]['id_category']);
SET cs.`position` = '.(int)($i+1).'
WHERE c.`id_parent` = '.(int)$id_category_parent.'
AND c.`id_category` = '.(int)$result[$i]['id_category'];
$return &= Db::getInstance()->execute($sql);
} }
return $return; return $return;
} }
@@ -1222,12 +1181,11 @@ class CategoryCore extends ObjectModel
*/ */
public static function getLastPosition($id_category_parent, $id_shop) public static function getLastPosition($id_category_parent, $id_shop)
{ {
return (int)(Db::getInstance()->getValue(' return (1 + (int)Db::getInstance()->getValue('
SELECT MAX(cs.`position`) SELECT MAX(cs.`position`)
FROM `'._DB_PREFIX_.'category` c FROM `'._DB_PREFIX_.'category` c
LEFT JOIN `'._DB_PREFIX_.'category_shop` cs LEFT JOIN `'._DB_PREFIX_.'category_shop` cs ON (c.`id_category` = cs.`id_category` AND cs.`id_shop` = '.(int)$id_shop.')
ON (c.`id_category` = cs.`id_category` AND cs.`id_shop` = '.(int)$id_shop.') WHERE c.`id_parent` = '.(int)$id_category_parent));
WHERE c.`id_parent` = '.(int)$id_category_parent) + 1);
} }
public static function getUrlRewriteInformations($id_category) public static function getUrlRewriteInformations($id_category)
@@ -1253,10 +1211,10 @@ class CategoryCore extends ObjectModel
$cache_id = 'Category::getInterval_'.(int)$id; $cache_id = 'Category::getInterval_'.(int)$id;
if (!Cache::isStored($cache_id)) if (!Cache::isStored($cache_id))
{ {
$sql = 'SELECT nleft, nright, level_depth $result = Db::getInstance()->getRow('
FROM '._DB_PREFIX_.'category SELECT nleft, nright, level_depth
WHERE id_category = '.(int)$id; FROM '._DB_PREFIX_.'category
$result = Db::getInstance()->getRow($sql); WHERE id_category = '.(int)$id);
Cache::store($cache_id, $result); Cache::store($cache_id, $result);
} }
return Cache::retrieve($cache_id); return Cache::retrieve($cache_id);
@@ -1292,16 +1250,13 @@ class CategoryCore extends ObjectModel
public function getChildrenWs() public function getChildrenWs()
{ {
$id = Context::getContext()->shop->id;
$id_shop = $id ? $id : Configuration::get('PS_SHOP_DEFAULT');
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(' $result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
SELECT c.`id_category` as id SELECT c.`id_category` as id
FROM `'._DB_PREFIX_.'category` c FROM `'._DB_PREFIX_.'category` c
LEFT JOIN `'._DB_PREFIX_.'category_shop` cs '.Shop::addSqlAssociation('category', 'c').'
ON (c.`id_category` = cs.`id_category` AND cs.`id_shop` = '.(int)$id_shop.')
WHERE c.`id_parent` = '.(int)$this->id.' WHERE c.`id_parent` = '.(int)$this->id.'
AND c.`active` = 1 AND c.`active` = 1
ORDER BY cs.`position` ASC'); ORDER BY category_shop.`position` ASC');
return $result; return $result;
} }
@@ -1322,22 +1277,19 @@ class CategoryCore extends ObjectModel
*/ */
public function getDuplicatePosition() public function getDuplicatePosition()
{ {
$id = Context::getContext()->shop->id; return Db::getInstance()->getValue('
$id_shop = $id ? $id : Configuration::get('PS_SHOP_DEFAULT'); SELECT c.`id_category`
return Db::getInstance()->getRow('
SELECT c.`id_category` as id
FROM `'._DB_PREFIX_.'category` c FROM `'._DB_PREFIX_.'category` c
LEFT JOIN `'._DB_PREFIX_.'category_shop` cs '.Shop::addSqlAssociation('category', 'c').'
ON (c.`id_category` = cs.`id_category` AND cs.`id_shop` = '.(int)$id_shop.')
WHERE c.`id_parent` = '.(int)$this->id_parent.' WHERE c.`id_parent` = '.(int)$this->id_parent.'
AND cs.`position` = '.(int)$this->position.' AND category_shop.`position` = '.(int)$this->position.'
AND c.`id_category` != '.(int)$this->id); AND c.`id_category` != '.(int)$this->id);
} }
public function getWsNbProductsRecursive() public function getWsNbProductsRecursive()
{ {
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(' $nb_product_recursive = Db::getInstance(_PS_USE_SQL_SLAVE_)->getValue('
SELECT COUNT(distinct(id_product)) as nb_product_recursive SELECT COUNT(distinct(id_product))
FROM `'._DB_PREFIX_.'category_product` FROM `'._DB_PREFIX_.'category_product`
WHERE id_category IN ( WHERE id_category IN (
SELECT c2.id_category SELECT c2.id_category
@@ -1349,9 +1301,9 @@ class CategoryCore extends ObjectModel
UNION SELECT '.(int)$this->id.' UNION SELECT '.(int)$this->id.'
) )
'); ');
if (!$result) if (!$nb_product_recursive)
return -1; return -1;
return $result[0]['nb_product_recursive']; return $nb_product_recursive;
} }
/** /**
@@ -1370,13 +1322,12 @@ class CategoryCore extends ObjectModel
$categories = array(); $categories = array();
$results = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(' $results = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
SELECT c.`id_category`, cl.`name`, cl.`link_rewrite`, cl.`id_lang` SELECT c.`id_category`, cl.`name`, cl.`link_rewrite`, cl.`id_lang`
FROM `'._DB_PREFIX_.'category` c FROM `'._DB_PREFIX_.'category` c
LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (c.`id_category` = cl.`id_category`'.Shop::addSqlRestrictionOnLang('cl').') LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (c.`id_category` = cl.`id_category`'.Shop::addSqlRestrictionOnLang('cl').')
'.Shop::addSqlAssociation('category', 'c').' '.Shop::addSqlAssociation('category', 'c').'
WHERE cl.`id_lang` = '.(int)$id_lang.' WHERE cl.`id_lang` = '.(int)$id_lang.'
AND c.`id_category` IN ('.implode(',', array_map('intval', $ids_category)).') AND c.`id_category` IN ('.implode(',', array_map('intval', $ids_category)).')');
');
foreach ($results as $category) foreach ($results as $category)
$categories[$category['id_category']] = $category; $categories[$category['id_category']] = $category;
@@ -1395,9 +1346,8 @@ class CategoryCore extends ObjectModel
return (bool)Db::getInstance()->getValue(' return (bool)Db::getInstance()->getValue('
SELECT c.`id_category` SELECT c.`id_category`
FROM `'._DB_PREFIX_.'category` c FROM `'._DB_PREFIX_.'category` c
LEFT JOIN `'._DB_PREFIX_.'category_shop` cs '.Shop::addSqlAssociation('category', 'c').'
ON (c.`id_category` = cs.`id_category` AND cs.`id_shop` IN ('.implode(', ', Shop::getContextListShopID()).') ) WHERE category_shop.`id_shop` = '.(int)$id_shop.'
WHERE cs.`id_shop` = '.(int)$id_shop.'
AND c.`id_parent` = '.(int)$this->id_parent); AND c.`id_parent` = '.(int)$this->id_parent);
} }
@@ -1418,7 +1368,7 @@ class CategoryCore extends ObjectModel
'id_shop' => (int)$shop['id_shop'], 'id_shop' => (int)$shop['id_shop'],
); );
} }
else if (!$this->existsInShop($id_shop)) elseif (!$this->existsInShop($id_shop))
$data[] = array( $data[] = array(
'id_category' => (int)$this->id, 'id_category' => (int)$this->id,
'id_shop' => (int)$id_shop, 'id_shop' => (int)$id_shop,
@@ -1431,12 +1381,13 @@ class CategoryCore extends ObjectModel
{ {
if (!$id_lang) if (!$id_lang)
$id_lang = Context::getContext()->language->id; $id_lang = Context::getContext()->language->id;
return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS(' return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
SELECT DISTINCT(c.`id_category`), cl.`name` SELECT DISTINCT(c.`id_category`), cl.`name`
FROM `'._DB_PREFIX_.'category` c FROM `'._DB_PREFIX_.'category` c
LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (cl.`id_category` = c.`id_category` AND cl.`id_lang`='.(int)$id_lang.') LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (cl.`id_category` = c.`id_category` AND cl.`id_lang`='.(int)$id_lang.')
WHERE `is_root_category` = 1 WHERE `is_root_category` = 1
'.(($active) ? 'AND `active` = 1': '')); '.($active ? 'AND `active` = 1': ''));
} }
public static function getCategoriesWithoutParent() public static function getCategoriesWithoutParent()
@@ -1516,9 +1467,9 @@ class CategoryCore extends ObjectModel
public static function getShopsByCategory($id_category) public static function getShopsByCategory($id_category)
{ {
return Db::getInstance()->executeS(' return Db::getInstance()->executeS('
SELECT `id_shop` SELECT `id_shop`
FROM `'._DB_PREFIX_.'category_shop` FROM `'._DB_PREFIX_.'category_shop`
WHERE `id_category` = '.(int)$id_category); WHERE `id_category` = '.(int)$id_category);
} }
/** /**
@@ -1549,11 +1500,10 @@ class CategoryCore extends ObjectModel
*/ */
public function deleteFromShop($id_shop) public function deleteFromShop($id_shop)
{ {
return Db::getInstance()->execute( return Db::getInstance()->execute('
'DELETE FROM `'._DB_PREFIX_.'category_shop` DELETE FROM `'._DB_PREFIX_.'category_shop`
WHERE `id_shop` = '.(int)$id_shop.' WHERE `id_shop` = '.(int)$id_shop.'
AND id_category = '.(int)$this->id.'' AND id_category = '.(int)$this->id);
);
} }
/** /**
@@ -1562,9 +1512,7 @@ class CategoryCore extends ObjectModel
*/ */
public static function deleteCategoriesFromShop($id_shop) public static function deleteCategoriesFromShop($id_shop)
{ {
return Db::getInstance()->execute(' return Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'category_shop` WHERE `id_shop` = '.(int)$id_shop);
DELETE FROM `'._DB_PREFIX_.'category_shop` WHERE `id_shop` = '.(int)$id_shop.'
');
} }
/** /**
@@ -1576,8 +1524,7 @@ class CategoryCore extends ObjectModel
{ {
if (!is_array($categories)) if (!is_array($categories))
return false; return false;
$sql = ' $sql = 'INSERT INTO `'._DB_PREFIX_.'category_shop` (`id_category`, `id_shop`) VALUES';
INSERT INTO `'._DB_PREFIX_.'category_shop` (`id_category`, `id_shop`) VALUES';
$tab_categories = array(); $tab_categories = array();
foreach ($categories as $id_category) foreach ($categories as $id_category)
{ {
@@ -1598,9 +1545,9 @@ class CategoryCore extends ObjectModel
public function existsInShop($id_shop) public function existsInShop($id_shop)
{ {
return (bool)Db::getInstance()->getValue(' return (bool)Db::getInstance()->getValue('
SELECT `id_category` SELECT `id_category`
FROM `'._DB_PREFIX_.'category_shop` FROM `'._DB_PREFIX_.'category_shop`
WHERE `id_category` = '.(int)$this->id.' WHERE `id_category` = '.(int)$this->id.'
AND `id_shop` = '.(int)$id_shop); AND `id_shop` = '.(int)$id_shop);
} }
} }