versionecologique/scripts/import_categories.php
2017-07-24 10:04:12 +02:00

149 lines
3.8 KiB
PHP

<?php
ini_set('display_errors', 1);
ini_set('display_startup_errors', 1);
error_reporting(E_ALL);
require '../www/config/config.inc.php';
fwrite(STDOUT, 'START IMPORTING CATEGORIES'.PHP_EOL);
/* Connexion à une base ODBC avec l'invocation de pilote */
$dsn = 'mysql:dbname=versionecologique_old;host=192.168.0.41';
$user = 'root';
$password = '';
$old_bdd = new PDO($dsn, $user, $password);
$categories = array(
340,
341,
342,
343,
344,
345,
346,
347,
348,
350,
351
);
$stmt = $old_bdd->query('
SELECT c.id_category, cl.name, cl.description, cl.link_rewrite, cl.meta_title, cl.meta_keywords, cl.meta_description, c.id_parent
FROM ps_category c
LEFT JOIN ps_category_lang cl ON (cl.id_category = c.id_category)
WHERE c.id_category IN (' .implode(',', $categories) .')
', PDO::FETCH_OBJ);
$results = $stmt->fetchAll();
$data_insert = array();
foreach ($results as $key => $result) {
$data_insert[] = array(
'id' => $result->id_category,
'name' => $result->name,
'description' => $result->description,
'link_rewrite' => $result->link_rewrite,
'meta_title' => $result->meta_title,
'meta_keywords' => $result->meta_keywords,
'meta_description' => $result->meta_description,
'id_parent' => $result->id_parent
);
$childs = $old_bdd->query('
SELECT c.id_category, cl.name, cl.description, cl.link_rewrite, cl.meta_title, cl.meta_keywords, cl.meta_description, c.id_parent
FROM ps_category c
LEFT JOIN ps_category_lang cl ON (cl.id_category = c.id_category)
WHERE c.id_parent = ' .$result->id_category
, PDO::FETCH_OBJ);
foreach ($childs as $child) {
$data_insert[] = array(
'id' => $child->id_category,
'name' => $child->name,
'description' => $child->description,
'link_rewrite' => $child->link_rewrite,
'meta_title' => $child->meta_title,
'meta_keywords' => $child->meta_keywords,
'meta_description' => $child->meta_description,
'id_parent' => $child->id_parent
);
}
}
$query = 'INSERT INTO `ps_category` VALUES';
$query_lang = 'INSERT INTO `ps_category_lang` VALUES';
$query_shop = 'INSERT INTO `ps_category_shop` VALUES';
$query_groups = 'INSERT INTO `ps_category_group` VALUES';
$langs = Language::getLanguages();
$shops = Shop::getShops(false);
foreach ($data_insert as $key => $data) {
$query.= '(
'.(int) $data['id'].',
'.(int) $data['id_parent'].',
1,
1,
2,
3,
1,
"'.date('Y-m-d H:i:s').'",
"'.date('Y-m-d H:i:s').'",
'.Category::getLastPosition($data['id_parent'],1).',
0
),';
foreach ($shops as $shop) {
foreach ($langs as $key => $lang) {
$query_lang.= '(
'.(int) $data['id'].',
'.(int) $shop['id_shop'].',
'.(int) $lang['id_lang'].',
"'.pSQL($data['name']).'",
"'.pSQL($data['description']).'",
"",
"'.pSQL($data['link_rewrite']).'",
"'.pSQL($data['meta_title']).'",
"'.pSQL($data['meta_keywords']).'",
"'.pSQL($data['meta_description']).'"
),';
}
$query_shop.= '('.(int) $data['id'].', '.(int) $shop['id_shop'].', 0),';
}
$query_groups.=
'('.(int) $data['id'].', 1),
('.(int) $data['id'].', 2),
('.(int) $data['id'].', 3),'
;
}
$query = rtrim($query,',');
$query_lang = rtrim($query_lang,',');
$query_shop = rtrim($query_shop,',');
$query_groups = rtrim($query_groups,',');
Db::getInstance()->execute('DELETE FROM ps_category WHERE id_category > 2');
Db::getInstance()->execute('DELETE FROM ps_category_lang WHERE id_category > 2');
Db::getInstance()->execute('DELETE FROM ps_category_group WHERE id_category > 2');
Db::getInstance()->execute('DELETE FROM ps_category_shop WHERE id_category > 2');
Db::getInstance()->execute($query);
Db::getInstance()->execute($query_lang);
Db::getInstance()->execute($query_shop);
Db::getInstance()->execute($query_groups);
fwrite(STDOUT, 'FIN IMPORT CATEGORIES'.PHP_EOL);
// Category::regenerateEntireNtree();
fwrite(STDOUT, 'START IMPORTING CATEGORIES'.PHP_EOL);