[*] BO : Adding category management with multishop
This commit is contained in:
+119
-18
@@ -74,6 +74,9 @@ class CategoryCore extends ObjectModel
|
||||
/** @var string Object last modification date */
|
||||
public $date_upd;
|
||||
|
||||
/** @var boolean is Category Root */
|
||||
public $is_root_category;
|
||||
|
||||
public $groupBox;
|
||||
|
||||
protected static $_links = array();
|
||||
@@ -92,6 +95,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'),
|
||||
'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'),
|
||||
'date_upd' => array('type' => self::TYPE_DATE, 'validate' => 'isDate'),
|
||||
@@ -639,12 +643,24 @@ class CategoryCore extends ObjectModel
|
||||
|
||||
public static function getRootCategory($id_lang = null, Shop $shop = null)
|
||||
{
|
||||
$context = Context::getContext();
|
||||
if (is_null($id_lang))
|
||||
$id_lang = Context::getContext()->language->id;
|
||||
$id_lang = $context->language->id;
|
||||
if (!$shop)
|
||||
$shop = Context::getContext()->shop;
|
||||
$shop = $context->shop;
|
||||
|
||||
return new Category($shop->getCategory(), $id_lang);
|
||||
// context : no multishop
|
||||
if (count(Shop::getShops()) == 1)
|
||||
if (count(Category::getCategoriesWithoutParent()) > 1)
|
||||
$category = new Category();
|
||||
else
|
||||
$category = new Category($shop->getCategory(), $id_lang);
|
||||
else // context : multishop
|
||||
if (count(Category::getCategoriesWithoutParent()) > 1 && $context->shop() != Shop::CONTEXT_SHOP)
|
||||
$category = new Category();
|
||||
else
|
||||
$category = new Category($shop->getCategory(), $id_lang);
|
||||
return $category;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -663,7 +679,9 @@ class CategoryCore extends ObjectModel
|
||||
SELECT c.`id_category`, cl.`name`, cl.`link_rewrite`
|
||||
FROM `'._DB_PREFIX_.'category` c
|
||||
LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON c.`id_category` = cl.`id_category`'.Context::getContext()->shop->addSqlRestrictionOnLang('cl').'
|
||||
LEFT JOIN `'._DB_PREFIX_.'category_shop` cs ON c.`id_category` = cs.`id_category`
|
||||
WHERE `id_lang` = '.(int)$id_lang.'
|
||||
AND cs.`id_shop` = '.(int)Context::getContext()->shop->getID(true).'
|
||||
AND c.`id_parent` = '.(int)$id_parent.'
|
||||
'.($active ? 'AND `active` = 1' : '').'
|
||||
ORDER BY `position` ASC');
|
||||
@@ -694,7 +712,7 @@ class CategoryCore extends ObjectModel
|
||||
* @param int $id_lang
|
||||
* @return array
|
||||
*/
|
||||
public static function getChildrenWithNbSelectedSubCat($id_parent, $selected_cat, $id_lang, Shop $shop = null)
|
||||
public static function getChildrenWithNbSelectedSubCat($id_parent, $selected_cat, $id_lang, Shop $shop = null, $use_shop_context = true)
|
||||
{
|
||||
if (!$shop)
|
||||
$shop = Context::getContext()->shop;
|
||||
@@ -712,8 +730,16 @@ class CategoryCore extends ObjectModel
|
||||
AND c3.`id_category` IN ('.implode(',', array_map('intval', $selected_cat)).')
|
||||
)' : '0').' AS nbSelectedSubCat
|
||||
FROM `'._DB_PREFIX_.'category` c
|
||||
LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON c.`id_category` = cl.`id_category`'.$shop->addSqlRestrictionOnLang('cl').'
|
||||
WHERE `id_lang` = '.(int)$id_lang.'
|
||||
LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON c.`id_category` = cl.`id_category`'.$shop->addSqlRestrictionOnLang('cl');
|
||||
if (Context::getContext()->shop() == Shop::CONTEXT_SHOP && $use_shop_context)
|
||||
$sql .= '
|
||||
LEFT JOIN `'._DB_PREFIX_.'category_shop` cs ON c.`id_category` = cs.`id_category`';
|
||||
$sql .= '
|
||||
WHERE `id_lang` = '.(int)$id_lang;
|
||||
if (Context::getContext()->shop() == Shop::CONTEXT_SHOP && $use_shop_context)
|
||||
$sql .= '
|
||||
AND cs.`id_shop` = '.(int)$shop->getID(true);
|
||||
$sql .= '
|
||||
AND c.`id_parent` = '.(int)$id_parent.'
|
||||
ORDER BY `position` ASC';
|
||||
return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
|
||||
@@ -870,25 +896,41 @@ class CategoryCore extends ObjectModel
|
||||
*/
|
||||
public function getParentsCategories($id_lang = null)
|
||||
{
|
||||
$context = Context::getContext();
|
||||
if (is_null($id_lang))
|
||||
$id_lang = Context::getContext()->language->id;
|
||||
$id_lang = $context->language->id;
|
||||
|
||||
$categories = null;
|
||||
$id_current = $this->id;
|
||||
while (true)
|
||||
{
|
||||
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
|
||||
SELECT c.*, cl.*
|
||||
FROM `'._DB_PREFIX_.'category` c
|
||||
LEFT JOIN `'._DB_PREFIX_.'category_lang` cl
|
||||
ON (c.`id_category` = cl.`id_category`
|
||||
AND `id_lang` = '.(int)$id_lang.Context::getContext()->shop->addSqlRestrictionOnLang('cl').')
|
||||
WHERE c.`id_category` = '.(int)$id_current.'
|
||||
AND c.`id_parent` != 0
|
||||
');
|
||||
$sql = '
|
||||
SELECT c.*, cl.*
|
||||
FROM `'._DB_PREFIX_.'category` c
|
||||
LEFT JOIN `'._DB_PREFIX_.'category_lang` cl
|
||||
ON (c.`id_category` = cl.`id_category`
|
||||
AND `id_lang` = '.(int)$id_lang.$context->shop->addSqlRestrictionOnLang('cl').')';
|
||||
if (count(Shop::getShops()) > 1 && $context->shop() == Shop::CONTEXT_SHOP)
|
||||
$sql .= '
|
||||
LEFT JOIN `'._DB_PREFIX_.'category_shop` cs
|
||||
ON c.`id_category` = cs.`id_category`';
|
||||
$sql .= '
|
||||
WHERE c.`id_category` = '.(int)$id_current;
|
||||
if (count(Shop::getShops()) > 1 && $context->shop() == Shop::CONTEXT_SHOP)
|
||||
$sql .= '
|
||||
AND cs.`id_shop` = '.(int)$context->shop->getID(true);
|
||||
$root_category = Category::getRootCategory();
|
||||
if ($context->shop() == Shop::CONTEXT_SHOP && (!Tools::isSubmit('id_category') || (int)Tools::getValue('id_category') == (int)$root_category->id_category || (int)$root_category->id_category == (int)$context->shop->id_category))
|
||||
$sql .= '
|
||||
AND c.`id_parent` != 0';
|
||||
|
||||
isset($result[0]) ? $categories[] = $result[0] : $categories = array();
|
||||
if (!$result || $result[0]['id_parent'] == 1)
|
||||
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($sql);
|
||||
|
||||
if (isset($result[0]))
|
||||
$categories[] = $result[0];
|
||||
else if (!$categories)
|
||||
$categories = array();
|
||||
if (!$result || $result[0]['id_category'] == $context->shop->id_category)
|
||||
return $categories;
|
||||
$id_current = $result[0]['id_parent'];
|
||||
}
|
||||
@@ -1197,5 +1239,64 @@ class CategoryCore extends ObjectModel
|
||||
|
||||
return $categories;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $id_shop
|
||||
* @return bool
|
||||
*/
|
||||
public function isParentCategoryAvailable($id_shop)
|
||||
{
|
||||
return (bool)Db::getInstance()->getValue('
|
||||
SELECT c.`id_category`
|
||||
FROM `'._DB_PREFIX_.'category` c
|
||||
LEFT JOIN `'._DB_PREFIX_.'category_shop` cs
|
||||
ON c.`id_category` = cs.`id_category`
|
||||
WHERE cs.`id_shop` = '.(int)$id_shop.'
|
||||
AND c.`id_parent` = '.(int)$this->id_parent);
|
||||
}
|
||||
|
||||
/**
|
||||
* Add association between shop and cateogries
|
||||
* @param int $id_shop
|
||||
* @return bool
|
||||
*/
|
||||
public function addShop($id_shop)
|
||||
{
|
||||
$sql = '';
|
||||
if (!$id_shop)
|
||||
{
|
||||
foreach (Shop::getShops(true) as $shop)
|
||||
$sql .= '('.(int)$this->id.', '.(int)$shop['id_shop'].'),';
|
||||
// removing last comma to avoid SQL error
|
||||
$sql = substr($sql, 0, strlen($sql) - 1);
|
||||
} else
|
||||
$sql .= '('.(int)$this->id.', '.(int)$id_shop.')';
|
||||
|
||||
return Db::getInstance()->execute('
|
||||
INSERT INTO `'._DB_PREFIX_.'category_shop` (`id_category`, `id_shop`) VALUES
|
||||
'.$sql);
|
||||
}
|
||||
|
||||
public function getRootCategories($id_lang = null, $active = true)
|
||||
{
|
||||
if (!$id_lang)
|
||||
$id_lang = Context::getContext()->language->id;
|
||||
return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
|
||||
SELECT DISTINCT(c.`id_category`), cl.`name`
|
||||
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.')
|
||||
WHERE `is_root_category` = 1
|
||||
'.(($active) ? 'AND `active` = 1': ''));
|
||||
}
|
||||
|
||||
public function getCategoriesWithoutParent()
|
||||
{
|
||||
return Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS('
|
||||
SELECT c.*
|
||||
FROM `'._DB_PREFIX_.'category` c
|
||||
LEFT JOIN `'._DB_PREFIX_.'category_lang` cl ON (c.`id_category` = cl.`id_category` AND cl.`id_lang` = '.(int)Context::getContext()->language->id.')
|
||||
WHERE `id_parent` = 0
|
||||
');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -598,8 +598,10 @@ class SearchCore
|
||||
LEFT JOIN `'._DB_PREFIX_.'product_tag` pt ON (p.`id_product` = pt.`id_product`)
|
||||
LEFT JOIN `'._DB_PREFIX_.'tag` t ON (pt.`id_tag` = t.`id_tag` AND t.`id_lang` = '.(int)$id_lang.')
|
||||
LEFT JOIN `'._DB_PREFIX_.'category_product` cp ON (cp.`id_product` = p.`id_product`)
|
||||
LEFT JOIN `'._DB_PREFIX_.'category_shop` cs ON (cg.`id_category` = cs.`id_category`)
|
||||
LEFT JOIN `'._DB_PREFIX_.'category_group` cg ON (cg.`id_category` = cp.`id_category`)
|
||||
WHERE p.`active` = 1
|
||||
AND cs.`id_shop` = '.(int)Context::getContext()->shop->getID().'
|
||||
AND cg.`id_group` '.(!$id_customer ? '= 1' : 'IN (
|
||||
SELECT id_group FROM '._DB_PREFIX_.'customer_group
|
||||
WHERE id_customer = '.(int)$id_customer.')').'
|
||||
@@ -633,8 +635,10 @@ class SearchCore
|
||||
LEFT JOIN `'._DB_PREFIX_.'tag` t ON (pt.`id_tag` = t.`id_tag` AND t.`id_lang` = '.(int)$id_lang.')
|
||||
LEFT JOIN `'._DB_PREFIX_.'category_product` cp ON (cp.`id_product` = p.`id_product`)
|
||||
LEFT JOIN `'._DB_PREFIX_.'category_group` cg ON (cg.`id_category` = cp.`id_category`)
|
||||
LEFT JOIN `'._DB_PREFIX_.'category_shop` cs ON (cg.`id_category` = cs.`id_category`)
|
||||
'.Product::sqlStock('p', 0).'
|
||||
WHERE p.`active` = 1
|
||||
AND cs.`id_shop` = '.(int)Context::getContext()->shop->getID().'
|
||||
AND cg.`id_group` '.(!$id_customer ? '= 1' : 'IN (
|
||||
SELECT id_group FROM '._DB_PREFIX_.'customer_group
|
||||
WHERE id_customer = '.(int)$id_customer.')').'
|
||||
|
||||
@@ -142,7 +142,6 @@ class HelperCore
|
||||
$html = '
|
||||
<script type="text/javascript">
|
||||
var inputName = "'.$input_name.'";
|
||||
var use_radio = '.($use_radio ? '1' : '0').';
|
||||
';
|
||||
if (sizeof($selected_cat) > 0)
|
||||
{
|
||||
@@ -155,7 +154,7 @@ class HelperCore
|
||||
$html .= 'var selectedCat = "";';
|
||||
$html .= '
|
||||
var selectedLabel = \''.$trads['selected'].'\';
|
||||
var home = \''.$trads['Home'].'\';
|
||||
var home = \''.$trads['Root']['name'].'\';
|
||||
var use_radio = '.(int)$use_radio.';';
|
||||
if (!$use_in_popup)
|
||||
$html .= '
|
||||
@@ -187,7 +186,7 @@ class HelperCore
|
||||
if (is_array($cat))
|
||||
{
|
||||
$disabled = in_array($cat['id_category'], $disabled_categories);
|
||||
if ($cat['id_category'] != 1)
|
||||
if ($cat['id_category'] != $trads['Root']['id_category'])
|
||||
$html .= '<input '.($disabled?'disabled="disabled"':'').' type="hidden" name="'.$input_name.'" value="'.$cat['id_category'].'" >';
|
||||
else
|
||||
$home_is_selected = true;
|
||||
@@ -195,7 +194,7 @@ class HelperCore
|
||||
else
|
||||
{
|
||||
$disabled = in_array($cat, $disabled_categories);
|
||||
if ($cat != 1)
|
||||
if ($cat != $trads['Root']['id_category'])
|
||||
$html .= '<input '.($disabled?'disabled="disabled"':'').' type="hidden" name="'.$input_name.'" value="'.$cat.'" >';
|
||||
else
|
||||
$home_is_selected = true;
|
||||
@@ -203,8 +202,8 @@ class HelperCore
|
||||
}
|
||||
$html .= '
|
||||
<ul id="categories-treeview" class="filetree">
|
||||
<li id="1" class="hasChildren">
|
||||
<span class="folder"> <input type="'.(!$use_radio ? 'checkbox' : 'radio').'" name="'.$input_name.'" value="1" '.($home_is_selected ? 'checked' : '').' onclick="clickOnCategoryBox($(this));" /> '.$trads['Home'].'</span>
|
||||
<li id="'.$trads['Root']['id_category'].'" class="hasChildren">
|
||||
<span class="folder"> <input type="'.(!$use_radio ? 'checkbox' : 'radio').'" name="'.$input_name.'" value="1" '.($home_is_selected ? 'checked' : '').' onclick="clickOnCategoryBox($(this));" /> '.$trads['Root']['name'].'</span>
|
||||
<ul>
|
||||
<li><span class="placeholder"> </span></li>
|
||||
</ul>
|
||||
|
||||
@@ -71,6 +71,7 @@ class ShopCore extends ObjectModel
|
||||
private static $asso_tables = array(
|
||||
'carrier' => array('type' => 'shop'),
|
||||
'carrier_lang' => array('type' => 'fk_shop'),
|
||||
'category' => array('type' => 'shop'),
|
||||
'category_lang' => array('type' => 'fk_shop'),
|
||||
'cms' => array('type' => 'shop'),
|
||||
'contact' => array('type' => 'shop'),
|
||||
@@ -909,4 +910,130 @@ class ShopCore extends ObjectModel
|
||||
{
|
||||
return Context::getContext()->shop->getID(true);
|
||||
}
|
||||
|
||||
/**
|
||||
* @static
|
||||
* @param int $id
|
||||
* @return array
|
||||
*/
|
||||
public static function getCategories($id = 0, $only_id = true)
|
||||
{
|
||||
// build query
|
||||
$query = new DbQuery();
|
||||
if ($only_id)
|
||||
$query->select('cs.`id_category`');
|
||||
else
|
||||
$query->select('DISTINCT cs.`id_category`, cl.`name`, cl.`link_rewrite`');
|
||||
$query->from('category_shop', 'cs')
|
||||
->leftJoin('category_lang', 'cl', 'cl.`id_category` = cs.`id_category` AND cl.`id_lang` = '.(int)Context::getContext()->language->id)
|
||||
->where('cs.`id_shop` = '.(int)$id);//d($query->__toString());
|
||||
$result = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query);
|
||||
|
||||
if ($only_id)
|
||||
{
|
||||
$array = array();
|
||||
foreach ($result as $row)
|
||||
$array[] = $row['id_category'];
|
||||
$array = array_unique($array);
|
||||
}
|
||||
else
|
||||
return $result;
|
||||
|
||||
return $array;
|
||||
}
|
||||
|
||||
/**
|
||||
* Update categories for a shop
|
||||
*
|
||||
* @param string $productCategories Categories list to associate a shop
|
||||
* @return array Update/insertion result
|
||||
*/
|
||||
public function updateCategories($categories)
|
||||
{
|
||||
// if array is empty or if the default category is not selected, return false
|
||||
if (empty($categories) || !in_array($this->id_category, $categories))
|
||||
return false;
|
||||
|
||||
// delete categories for this shop
|
||||
$this->deleteCategories();
|
||||
|
||||
// and add $categories to this shop
|
||||
return $this->addToCategories($categories);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete shop from category $id_category
|
||||
* @param int $id_category
|
||||
* @return bool
|
||||
*/
|
||||
public function deleteCategory($id_category)
|
||||
{
|
||||
return Db::getInstance()->execute(
|
||||
'DELETE FROM `'._DB_PREFIX_.'category_shop`
|
||||
WHERE `id_shop` = '.(int)$this->id.'
|
||||
AND id_category = '.(int)$id_category.''
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete every categories
|
||||
* @return bool
|
||||
*/
|
||||
public function deleteCategories()
|
||||
{
|
||||
return Db::getInstance()->execute('
|
||||
DELETE FROM `'._DB_PREFIX_.'category_shop` WHERE `id_shop` = '.(int)$this->id.'
|
||||
');
|
||||
}
|
||||
|
||||
/**
|
||||
* Add some categories to a shop
|
||||
* @param array $categories
|
||||
* @return bool
|
||||
*/
|
||||
public function addToCategories($categories)
|
||||
{
|
||||
if (!is_array($categories))
|
||||
return false;
|
||||
$sql = '
|
||||
INSERT INTO `'._DB_PREFIX_.'category_shop` (`id_category`, `id_shop`) VALUES';
|
||||
foreach ($categories as $c)
|
||||
$sql .= '("'.(int)$c.'", "'.(int)$this->id.'"),';
|
||||
// removing last comma to avoid SQL error
|
||||
$sql = substr($sql, 0, strlen($sql) - 1);
|
||||
|
||||
return Db::getInstance()->execute($sql);
|
||||
}
|
||||
|
||||
/**
|
||||
* @static
|
||||
* @param $id_category
|
||||
* @return bool
|
||||
*/
|
||||
public static function isCategoryAvailable($id_category)
|
||||
{
|
||||
return (bool)Db::getInstance()->getValue('
|
||||
SELECT `id_category`
|
||||
FROM `'._DB_PREFIX_.'category_shop`
|
||||
WHERE `id_category` = '.(int)$id_category.'
|
||||
AND `id_shop` = '.(int)Context::getContext()->shop->getID(true));
|
||||
}
|
||||
|
||||
/**
|
||||
* @static
|
||||
* @param $id_product
|
||||
* @return bool
|
||||
*/
|
||||
public static function isProductAvailable($id_product)
|
||||
{
|
||||
return (bool)Db::getInstance()->getValue('
|
||||
SELECT p.`id_product`
|
||||
FROM `'._DB_PREFIX_.'product` p
|
||||
LEFT JOIN `'._DB_PREFIX_.'category_product` cp
|
||||
ON p.`id_product` = cp.`id_product`
|
||||
LEFT JOIN `'._DB_PREFIX_.'category_shop` cs
|
||||
ON cp.`id_category` = cs.`id_category`
|
||||
WHERE p.`id_product` = '.(int)$id_product.'
|
||||
AND cs.`id_shop` = '.(int)Context::getContext()->shop->getID(true));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user