Merge branch 'ticket/r15296-import'
This commit is contained in:
commit
fd6b00ed78
@ -216,95 +216,120 @@ class AdminBulkUpdate extends AdminTab {
|
|||||||
fclose($f);
|
fclose($f);
|
||||||
$output .= '<p class="conf">'.$this->l('Products updated').'</p>';
|
$output .= '<p class="conf">'.$this->l('Products updated').'</p>';
|
||||||
}
|
}
|
||||||
} elseif(Tools::isSubmit('submitUploadCategories')) {
|
}
|
||||||
|
// Update product categories and default category
|
||||||
|
elseif (Tools::isSubmit('submitUploadCategories')) {
|
||||||
|
|
||||||
$f = fopen($_FILES['csvfile']['tmp_name'], 'r');
|
$f = fopen($_FILES['csvfile']['tmp_name'], 'r');
|
||||||
fgetcsv($f, 0, ';');
|
fgetcsv($f, 0, ';');
|
||||||
|
|
||||||
$products = array();
|
// Check
|
||||||
$defaults = array();
|
$error = false;
|
||||||
$positions = array();
|
while ($line = fgetcsv($f, 0, ';')) {
|
||||||
|
// Check id_category_default
|
||||||
while($line = fgetcsv($f, 0, ';')) {
|
if (empty($line[2])) {
|
||||||
if(empty($line[0])) {
|
$error = true;
|
||||||
continue;
|
break;
|
||||||
}
|
|
||||||
|
|
||||||
if(!isset($line[1])) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if(isset($line[2]) && !empty($line[2]) && (int) $line[2] != 0) {
|
|
||||||
$defaults[(int) $line[0]] = (int) $line[2];
|
|
||||||
}
|
|
||||||
|
|
||||||
$categories = array_map('intval', explode(',', $line[1]));
|
|
||||||
$products[(int) $line[0]] = $categories;
|
|
||||||
foreach($categories as $id_category) {
|
|
||||||
$positions[(int) $id_category] = 1;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fclose($f);
|
if ($error == true) {
|
||||||
|
$output .= '<p class="warn">'.$this->l('ID Category default not set, check your file').'</p>';
|
||||||
if(Tools::getValue('category_process') == 1) {
|
|
||||||
Db::getInstance()->ExecuteS('
|
|
||||||
DELETE FROM `'._DB_PREFIX_.'category_product`
|
|
||||||
WHERE `id_product` IN ('.implode(', ', array_keys($products)).')
|
|
||||||
');
|
|
||||||
}
|
}
|
||||||
|
// Import
|
||||||
|
else {
|
||||||
|
$products = array();
|
||||||
|
$defaults = array();
|
||||||
|
$positions = array();
|
||||||
|
|
||||||
foreach(Db::getInstance()->ExecuteS('
|
rewind($f);
|
||||||
SELECT `id_category`, MAX(`position`) AS `position`
|
|
||||||
FROM `'._DB_PREFIX_.'category_product`
|
|
||||||
WHERE `id_category` IN ('.implode(', ', array_keys($positions)).')
|
|
||||||
GROUP BY `id_category`
|
|
||||||
') as $row) {
|
|
||||||
$positions[(int) $row['id_category']] = (int) $row['position'];
|
|
||||||
}
|
|
||||||
|
|
||||||
foreach($products as $id_product => $id_categories) {
|
while ($line = fgetcsv($f, 0, ';')) {
|
||||||
foreach($id_categories as $id_category) {
|
// id_product
|
||||||
|
if (empty($line[0])) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// id_category
|
||||||
|
if (!isset($line[1])) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// id_category_default
|
||||||
|
if (isset($line[2]) && !empty($line[2]) && (int) $line[2] != 0) {
|
||||||
|
$defaults[(int) $line[0]] = (int) $line[2];
|
||||||
|
}
|
||||||
|
|
||||||
|
$categories = array_map('intval', explode(',', $line[1]));
|
||||||
|
$products[(int) $line[0]] = $categories;
|
||||||
|
foreach($categories as $id_category) {
|
||||||
|
$positions[(int) $id_category] = 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fclose($f);
|
||||||
|
|
||||||
|
if(Tools::getValue('category_process') == 1) {
|
||||||
Db::getInstance()->ExecuteS('
|
Db::getInstance()->ExecuteS('
|
||||||
|
DELETE FROM `'._DB_PREFIX_.'category_product`
|
||||||
|
WHERE `id_product` IN ('.implode(', ', array_keys($products)).')
|
||||||
|
');
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach(Db::getInstance()->ExecuteS('
|
||||||
|
SELECT `id_category`, MAX(`position`) AS `position`
|
||||||
|
FROM `'._DB_PREFIX_.'category_product`
|
||||||
|
WHERE `id_category` IN ('.implode(', ', array_keys($positions)).')
|
||||||
|
GROUP BY `id_category`
|
||||||
|
') as $row) {
|
||||||
|
$positions[(int) $row['id_category']] = (int) $row['position'];
|
||||||
|
}
|
||||||
|
|
||||||
|
foreach($products as $id_product => $id_categories) {
|
||||||
|
foreach($id_categories as $id_category) {
|
||||||
|
Db::getInstance()->ExecuteS('
|
||||||
INSERT INTO `'._DB_PREFIX_.'category_product` VALUES (
|
INSERT INTO `'._DB_PREFIX_.'category_product` VALUES (
|
||||||
'.(int) $id_category.',
|
'.(int) $id_category.',
|
||||||
'.(int) $id_product.',
|
'.(int) $id_product.',
|
||||||
'.(int) ++$positions[(int) $id_category].'
|
'.(int) ++$positions[(int) $id_category].'
|
||||||
)
|
)
|
||||||
');
|
');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
foreach($defaults as $id_product => $id_category_default) {
|
foreach($defaults as $id_product => $id_category_default) {
|
||||||
Db::getInstance()->ExecuteS('
|
Db::getInstance()->ExecuteS('
|
||||||
UPDATE `'._DB_PREFIX_.'product`
|
UPDATE `'._DB_PREFIX_.'product`
|
||||||
SET `id_category_default` = '.(int) $id_category_default.'
|
SET `id_category_default` = '.(int) $id_category_default.'
|
||||||
WHERE `id_product` = '.(int) $id_product.'
|
WHERE `id_product` = '.(int) $id_product.'
|
||||||
LIMIT 1
|
LIMIT 1
|
||||||
');
|
|
||||||
|
|
||||||
if(!$row = Db::getInstance()->getRow('
|
|
||||||
SELECT `id_product`
|
|
||||||
FROM `'._DB_PREFIX_.'category_product`
|
|
||||||
WHERE `id_product` = '.(int) $id_product.'
|
|
||||||
AND `id_category` = '.(int) $id_category_default.'
|
|
||||||
')) {
|
|
||||||
$pos = (int) Db::getInstance()->getValue('
|
|
||||||
SELECT MAX(`position`)
|
|
||||||
FROM `'._DB_PREFIX_.'category_product`
|
|
||||||
WHERE `id_category` = '.(int) $id_category_default.'
|
|
||||||
') + 1;
|
|
||||||
Db::getInstance()->ExecuteS('
|
|
||||||
INSERT INTO `'._DB_PREFIX_.'category_product` VALUES (
|
|
||||||
'.(int) $id_category_default.',
|
|
||||||
'.(int) $id_product.',
|
|
||||||
'.(int) $pos.'
|
|
||||||
)
|
|
||||||
');
|
');
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
$output .= '<p class="conf">'.$this->l('Products updated').'</p>';
|
if(!$row = Db::getInstance()->getRow('
|
||||||
} elseif(Tools::isSubmit('submitUploadAccessories')) {
|
SELECT `id_product`
|
||||||
|
FROM `'._DB_PREFIX_.'category_product`
|
||||||
|
WHERE `id_product` = '.(int) $id_product.'
|
||||||
|
AND `id_category` = '.(int) $id_category_default.'
|
||||||
|
')) {
|
||||||
|
$pos = (int) Db::getInstance()->getValue('
|
||||||
|
SELECT MAX(`position`)
|
||||||
|
FROM `'._DB_PREFIX_.'category_product`
|
||||||
|
WHERE `id_category` = '.(int) $id_category_default.'
|
||||||
|
') + 1;
|
||||||
|
Db::getInstance()->ExecuteS('
|
||||||
|
INSERT INTO `'._DB_PREFIX_.'category_product` VALUES (
|
||||||
|
'.(int) $id_category_default.',
|
||||||
|
'.(int) $id_product.',
|
||||||
|
'.(int) $pos.'
|
||||||
|
)
|
||||||
|
');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
$output .= '<p class="conf">'.$this->l('Products updated').'</p>';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
elseif(Tools::isSubmit('submitUploadAccessories')) {
|
||||||
$f = fopen($_FILES['csvfile']['tmp_name'], 'r');
|
$f = fopen($_FILES['csvfile']['tmp_name'], 'r');
|
||||||
fgetcsv($f, 0, ';');
|
fgetcsv($f, 0, ';');
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user