// category import improved

git-svn-id: http://dev.prestashop.com/svn/v1/branches/1.5.x@14298 b9a71923-0436-4b27-9f14-aed3839534dd
This commit is contained in:
vChabot
2012-03-27 13:41:34 +00:00
parent 42385b2cbc
commit 427d139207
2 changed files with 25 additions and 12 deletions
+7 -7
View File
@@ -155,8 +155,8 @@ class CategoryCore extends ObjectModel
if (!isset($this->level_depth)) if (!isset($this->level_depth))
$this->level_depth = $this->calcLevelDepth(); $this->level_depth = $this->calcLevelDepth();
$ret = parent::add($autodate, $null_values); $ret = parent::add($autodate, $null_values);
if (isset($_POST['checkBoxShopAsso_category'])) if (Tools::isSubmit('checkBoxShopAsso_category'))
foreach ($_POST['checkBoxShopAsso_category'] as $row) foreach (Tools::getValue('checkBoxShopAsso_category') as $row)
{ {
foreach ($row as $id_shop => $value) foreach ($row as $id_shop => $value)
{ {
@@ -168,6 +168,8 @@ class CategoryCore extends ObjectModel
foreach (Shop::getShops(true) as $shop) foreach (Shop::getShops(true) as $shop)
{ {
$position = Category::getLastPosition((int)$this->id_parent, $shop['id_shop']); $position = Category::getLastPosition((int)$this->id_parent, $shop['id_shop']);
if (!$position)
$position = 1;
$this->addPosition($position, $shop['id_shop']); $this->addPosition($position, $shop['id_shop']);
} }
if (!isset($this->doNotRegenerateNTree) || !$this->doNotRegenerateNTree) if (!isset($this->doNotRegenerateNTree) || !$this->doNotRegenerateNTree)
@@ -1439,12 +1441,10 @@ class CategoryCore extends ObjectModel
$id = Context::getContext()->shop->id; $id = Context::getContext()->shop->id;
$id_shop = $id ? $id : Configuration::get('PS_SHOP_DEFAULT'); $id_shop = $id ? $id : Configuration::get('PS_SHOP_DEFAULT');
} }
return Db::getInstance()->execute(' return Db::getInstance()->execute('
UPDATE `'._DB_PREFIX_.'category_shop` cs INSERT INTO `'._DB_PREFIX_.'category_shop` (`id_category`, `id_shop`, `position`) VALUES
SET cs.`position` = '.(int)$position.' ('.(int)$this->id.', '.(int)$id_shop.', '.(int)$position.')
WHERE cs.`id_shop` = '.(int)$id_shop.' ON DUPLICATE KEY UPDATE `position` = '.(int)$position);
AND cs.`id_category` = '.(int)$this->id);
} }
public static function getShopsByCategory($id_category) public static function getShopsByCategory($id_category)
+18 -5
View File
@@ -824,12 +824,14 @@ class AdminImportControllerCore extends AdminController
{ {
$category_parent = Category::searchByName($default_language_id, $category->parent, true); $category_parent = Category::searchByName($default_language_id, $category->parent, true);
if ($category_parent['id_category']) if ($category_parent['id_category'])
$category->id_parent = (int)$category_parent['id_category']; $category->id_parent = (int)$category_parent['id_category'];
else else
{ {
$category_to_create = new Category(); $category_to_create = new Category();
$category_to_create->name = AdminImportController::createMultiLangField($category->parent); $category_to_create->name = AdminImportController::createMultiLangField($category->parent);
$category_to_create->active = 1; $category_to_create->active = 1;
$category_link_rewrite = Tools::link_rewrite($category_to_create->name[$default_language_id]);
$category_to_create->link_rewrite = $category_link_rewrite;
$category_to_create->id_parent = Configuration::get('PS_HOME_CATEGORY'); // Default parent is home for unknown category to create $category_to_create->id_parent = Configuration::get('PS_HOME_CATEGORY'); // Default parent is home for unknown category to create
if (($field_error = $category_to_create->validateFields(UNFRIENDLY_ERROR, true)) === true && if (($field_error = $category_to_create->validateFields(UNFRIENDLY_ERROR, true)) === true &&
($lang_field_error = $category_to_create->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true && $category_to_create->add()) ($lang_field_error = $category_to_create->validateFieldsLang(UNFRIENDLY_ERROR, true)) === true && $category_to_create->add())
@@ -884,7 +886,8 @@ class AdminImportControllerCore extends AdminController
$category->doNotRegenerateNTree = true; $category->doNotRegenerateNTree = true;
// If id category AND id category already in base, trying to update // If id category AND id category already in base, trying to update
if ($category->id && $category->categoryExists($category->id) && $category->id != 1) $categories_home_root = array(Configuration::get('PS_ROOT_CATEGORY'), Configuration::get('PS_HOME_CATEGORY'));
if ($category->id && $category->categoryExists($category->id) && !in_array($category->id, $categories_home_root))
$res = $category->update(); $res = $category->update();
if ($category->id == Configuration::get('PS_ROOT_CATEGORY')) if ($category->id == Configuration::get('PS_ROOT_CATEGORY'))
$this->errors[] = Tools::displayError('Root category cannot be modified'); $this->errors[] = Tools::displayError('Root category cannot be modified');
@@ -2114,9 +2117,19 @@ class AdminImportControllerCore extends AdminController
switch ((int)$case) switch ((int)$case)
{ {
case $this->entities[$this->l('Categories')]: case $this->entities[$this->l('Categories')]:
Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'category` WHERE id_category != 1'); Db::getInstance()->execute('
Db::getInstance()->execute('DELETE FROM `'._DB_PREFIX_.'category_lang` WHERE id_category != 1'); DELETE FROM `'._DB_PREFIX_.'category`
Db::getInstance()->execute('ALTER TABLE `'._DB_PREFIX_.'category` AUTO_INCREMENT = 2'); WHERE id_category NOT IN ('.(int)Configuration::get('PS_HOME_CATEGORY').
', '.(int)Configuration::get('PS_ROOT_CATEGORY').')');
Db::getInstance()->execute('
DELETE FROM `'._DB_PREFIX_.'category_lang`
WHERE id_category NOT IN ('.(int)Configuration::get('PS_HOME_CATEGORY').
', '.(int)Configuration::get('PS_ROOT_CATEGORY').')');
Db::getInstance()->execute('
DELETE FROM `'._DB_PREFIX_.'category_shop`
WHERE `id_category` NOT IN ('.(int)Configuration::get('PS_HOME_CATEGORY').
', '.(int)Configuration::get('PS_ROOT_CATEGORY').')');
Db::getInstance()->execute('ALTER TABLE `'._DB_PREFIX_.'category` AUTO_INCREMENT = 3');
foreach (scandir(_PS_CAT_IMG_DIR_) as $d) foreach (scandir(_PS_CAT_IMG_DIR_) as $d)
if (preg_match('/^[0-9]+(\-(.*))?\.jpg$/', $d)) if (preg_match('/^[0-9]+(\-(.*))?\.jpg$/', $d))
unlink(_PS_CAT_IMG_DIR_.$d); unlink(_PS_CAT_IMG_DIR_.$d);