addChild($nodename,'',$namespace); //Added a nodename to create inside the function
$node1 = dom_import_simplexml($node);
$no = $node1->ownerDocument;
$node1->appendChild($no->createCDATASection($cdata_text));
return $node;
}
}
header ('Content-type: text/html; charset=utf-8');
function leerFichero($archivo) {
if (!$muebles_xml = simplexml_load_file($archivo)) {
trigger_error('Error reading XML file',E_USER_ERROR);
}
$muebles = array();
$cont = 0;
foreach($muebles_xml as $mueble_xml) {
$muebles[$cont]['titulo'] = trim($mueble_xml->titulo);
$muebles[$cont]['slug'] = preg_replace("![^a-z0-9]+!i", "-", strtoupper(trim($muebles[$cont]['titulo'])));
$muebles[$cont]['precio'] = trim($mueble_xml->precio);
$muebles[$cont]['categoria_name'] = $mueble_xml->categoria_name;
$muebles[$cont]['categoria'] = $mueble_xml->categoria;
$muebles[$cont]['descripcion'] = '';
if (strlen($mueble_xml->medidas) > 0) {
$muebles[$cont]['descripcion'] .= trim($mueble_xml->medidas) . 'cm';
}
if (strlen($mueble_xml->obs) > 0) {
$muebles[$cont]['descripcion'] .= ' ' . trim($mueble_xml->obs);
}
if (strlen($mueble_xml->Obs) > 0) {
$muebles[$cont]['descripcion'] .= ' ' . trim($mueble_xml->Obs);
}
$cont++;
}
return $muebles;
}
function escribirFicheroXML($muebles, $archivo_salida) {
include "cabecera.php";
$xml = new SimpleXMLExtend($xmlstr);
$raiz = $xml->channel;
foreach($muebles as $mueble) {
$item = $raiz->addChild('item');
$item->addChild('title', $mueble['titulo']);
$item->addChild('link', 'http://localhost/originalhouse/?tcp_product='.$mueble['slug']);
$item->addChild('pubDate', 'Wed, 15 Jul 2011 00:00:00 +0000');
$item->addChild('dc:creator', 'rodax');
$guid = $item->addChild('guid', 'http://localhost/originalhouse/?tcp_product&p='.$mueble['slug']);
$guid->addAttribute('isPermaLink' , 'false');
$item->addChild('description', '');
$item->addCData('content:encoded', $mueble['descripcion'], 'http://purl.org/rss/1.0/modules/content/');
$item->addCData('excerpt:encoded', '', 'http://purl.org/rss/1.0/modules/excerpt/');
//227
$item->addChild('post_date', '2011-07-15 17:09:59', 'http://wordpress.org/export/1.1/');
$item->addChild('post_date_gmt', '2011-07-15 17:09:59', 'http://wordpress.org/export/1.1/');
$item->addChild('post_name', $mueble['slug'], 'http://wordpress.org/export/1.1/');
$item->addChild('status', 'publish', 'http://wordpress.org/export/1.1/');
$item->addChild('post_type', 'tcp_product', 'http://wordpress.org/export/1.1/');
$categoria = $item->addCData('category', $mueble['categoria']);
$categoria->addAttribute('domain', 'tcp_product_category');
$categoria->addAttribute('nicename', $mueble['categoria_name']);
$meta = $item->addChild('wp:postmeta', '', 'http://wordpress.org/export/1.1/');
$meta->addChild('wp:meta_key', 'tcp_price', 'http://wordpress.org/export/1.1/');
$meta->addCData('wp:meta_value', $mueble['precio'], 'http://wordpress.org/export/1.1/');
}
return $xml->asXML($archivo_salida);
}
/* INSERT INTO `wp_posts` (`post_author`, `post_date`, `post_date_gmt`, `post_content`, `post_title`, `post_status`, `comment_status`, `ping_status`, `post_name`, `post_modified`, `post_modified_gmt`, `post_parent`, `guid`, `menu_order`, `post_type`, `comment_count`) VALUES
(1, now(), now(), '85x130x45 cm', 'ACRO 66ML', 'publish', 'closed', 'open', 'ACRO 66ML', now(), now(), 0, 'http://localhost/originalhouse/?post_type=tcp_product&p=214', 0, 'tcp_product', 0),
* insert into wp_postmeta (post_id, meta_key, meta_value) values (227, 'tcp_price', 697);
*/
function escribirFichero($muebles, $categoria, $archivo_salida) {
$file = fopen($archivo_salida, "w+");
if (!$file) {
trigger_error('Error creando fichero de salida', E_USER_ERROR);
}
$cont = 0;
foreach($muebles as $mueble) {
generarRegistrosMueble($mueble, $cont, $categoria, $file);
$cont++;
}
fclose($file);
}
function generarRegistrosMueble($mueble, $orden, $categoria, $file) {
/* INSERT INTO `wp_posts` (`post_author`, `post_date`, `post_date_gmt`, `post_content`, `post_title`, `post_status`, `comment_status`, `ping_status`, `post_name`,
* `post_modified`, `post_modified_gmt`, `post_parent`, `guid`, `menu_order`, `post_type`, `comment_count`) VALUES
(1, now(), now(), '85x130x45 cm', 'ACRO 66ML', 'publish', 'closed', 'open', 'ACRO 66ML', now(), now(), 0, 'http://localhost/originalhouse/?post_type=tcp_product&p=214', 0, 'tcp_product', 0),
* insert into wp_postmeta (post_id, meta_key, meta_value) values (227, 'tcp_price', 697);
*/
$output = "INSERT INTO `wp_posts` (`post_author`, `post_date`, `post_date_gmt`, `post_content`, `post_title`, `post_status`, `comment_status`, `ping_status`, `post_name`, `post_modified`, `post_modified_gmt`, `post_parent`, `guid`, `menu_order`, `post_type`, `comment_count`) VALUES \n";
$output .= "(";
$output .= "1, now(), now(), ";
$output .= "'" . $mueble['descripcion'] . "', "; // post_content
$output .= "'" . $mueble['titulo'] . "', "; // post_title
$output .= "'publish', 'closed', 'open', ";
$output .= "'" . $mueble['slug'] . "', "; // post_name
$output .= "now(), now(), 0, ";
$output .= "'http://localhost/originalhouse/?tcp_product&p=".$mueble['slug']."', ";
$output .= "0, 'tcp_product', 0";
$output .= ");";
$output .= "\n";
fwrite($file, $output);
//
$output = "SET @last_id := LAST_INSERT_ID();\n";
fwrite($file, $output);
//INSERT INTO `wp_term_relationships` (`object_id`, `term_taxonomy_id`, `term_order`) VALUES
$output = "INSERT INTO `wp_term_relationships` (`object_id`, `term_taxonomy_id`, `term_order`) VALUES \n";
$output .= "(@last_id, ". $categoria . ", 0);\n";
fwrite($file, $output);
// metadatos
$output = "insert into wp_postmeta (post_id, meta_key, meta_value) values \n";
// precio
$output .= "(@last_id, 'tcp_price', '" . $mueble['precio'] . "'),\n";
//stock
$output .= "(@last_id, 'tcp_stock', -1),\n";
//tipo
$output .= "(@last_id, 'tcp_type', 'SIMPLE'),\n";
//order
$output .= "(@last_id, 'tcp_order', '". $orden . "'),\n";
//sku
$output .= "(@last_id, 'tcp_sku', '".$mueble['slug']."'),\n";
$output .= "(@last_id, 'tcp_tax_id', '0'),\n";
$output .= "(@last_id, 'tcp_back_end_label', ''),\n";
$output .= "(@last_id, 'tcp_hide_buy_button', ''),\n";
$output .= "(@last_id, 'tcp_exclude_range', ''),\n";
$output .= "(@last_id, 'tcp_is_downloadable', ''),\n";
$output .= "(@last_id, 'tcp_max_downloads', '0'),\n";
$output .= "(@last_id, 'tcp_days_to_expire', '0'),\n";
$output .= "(@last_id, 'tcp_is_visible', '1'),\n";
$output .= "(@last_id, 'tcp_weight', '0');\n";
fwrite($file, $output);
//SELECT * FROM `wp_posts` WHERE `post_type` = 'attachment' and `post_mime_type` = 'image/jpeg' and `post_name` = $mueble['slug']
//imagen
$output = "insert into wp_postmeta (post_id, meta_key, meta_value) \n";
$output .= "SELECT @last_id, '_thumbnail_id', wp_posts.id FROM `wp_posts` WHERE `post_type` = 'attachment' and `post_mime_type` = 'image/jpeg' and ";
$output .= "`post_title` = '" . $mueble['slug'] . "';\n\n\n";
fwrite($file, $output);
}
/*
*
ACRO 66ML
http://localhost/originalhouse/?tcp_product=acro-66ml
Wed, 06 Jul 2011 17:09:59 +0000
rodax
http://localhost/originalhouse/?post_type=tcp_product&p=214
227
2011-07-06 17:09:59
2011-07-06 17:09:59
closed
closed
acro-66ml
publish
0
0
tcp_product
0
tcp_price
*
*/
$archivo = 'armarios-cajoneras.xml';
$archivo_salida = 'salida-armarios-cajoneras.sql';
$muebles = leerFichero($archivo);
echo escribirFichero($muebles, 48, $archivo_salida);
$archivo = 'consolas.xml';
$archivo_salida = 'salida-consolas.sql';
$muebles = leerFichero($archivo);
echo escribirFichero($muebles, 50, $archivo_salida);
$archivo = 'estanterias.xml';
$archivo_salida = 'salida-estanterias.sql';
$muebles = leerFichero($archivo);
echo escribirFichero($muebles, 45, $archivo_salida);
$archivo = 'vitrinas.xml';
$archivo_salida = 'salida-vitrinas.sql';
$muebles = leerFichero($archivo);
echo escribirFichero($muebles, 46, $archivo_salida);
$archivo = 'mesas-centro.xml';
$archivo_salida = 'salida-mesas-centro.sql';
$muebles = leerFichero($archivo);
echo escribirFichero($muebles, 57, $archivo_salida);
$archivo = 'mesas-comedor.xml';
$archivo_salida = 'salida-mesas-comedor.sql';
$muebles = leerFichero($archivo);
echo escribirFichero($muebles, 56, $archivo_salida);
$archivo = 'mesas-rincon.xml';
$archivo_salida = 'salida-mesas-rincon.sql';
$muebles = leerFichero($archivo);
echo escribirFichero($muebles, 58, $archivo_salida);
$archivo = 'muebles-tv.xml';
$archivo_salida = 'salida-muebles-tv.sql';
$muebles = leerFichero($archivo);
echo escribirFichero($muebles, 49, $archivo_salida);
$archivo = 'sillas.xml';
$archivo_salida = 'salida-sillas.sql';
$muebles = leerFichero($archivo);
echo escribirFichero($muebles, 52, $archivo_salida);
$archivo = 'sofas.xml';
$archivo_salida = 'salida-sofas.sql';
$muebles = leerFichero($archivo);
echo escribirFichero($muebles, 51, $archivo_salida);
$archivo = 'sofas-cama.xml';
$archivo_salida = 'salida-sofas-cama.sql';
$muebles = leerFichero($archivo);
echo escribirFichero($muebles, 51, $archivo_salida);
/*if ($muebles_xml) {
foreach($muebles_xml as $mueble_xml) {
$muebles['id'] = $mueble_xml->Id;
$muebles['titulo'] = $mueble_xml->titulo;
$muebles['slug'] = preg_replace("![^a-z0-9]+!i", "-", strtolower($muebles[titulo]));
print_r($muebles);
}*/
/*foreach($muebles->children() as $child) {
echo $child->getName().":
";
foreach($child->children() as $subchild) {
echo "--->".$subchild->getName().": ".$subchild."
";
}
}*/
?>