<?php

//ini_set("memory_limit","1024M");
//set_time_limit(60000);

// get data
// connect import database
ini_set('display_errors', 1);
	error_reporting(E_ALL);
echo "start";
// old database
$connection=mysql_connect('localhost', 'root', '1234') or die ("Verbindungsversuch fehlgeschlagen");
mysql_select_db('intelectra', $connection) or die("Konnte die Datenbank nicht waehlen1.");

// get customers
$query = mysql_query('SELECT * FROM `import_structure` order by parentid, rindex') or die("Anfrage nicht erfolgreich");
$data = array();
while($obj = mysql_fetch_object($query)) {
	$data[] = $obj;
}

// imported data
// connect target database

// new database
mysql_select_db('intelectra', $connection) or die("Konnte die Datenbank nicht waehlen2.");

$parent_ids = array();
$parent_ids['oldid_0'] = 357;
$pos = 0;
$parent_id = 0;
foreach ($data as $menu) {
        if ($menu->parentid != $parent_id) {
            $parent_id = $menu->parentid;
            $pos = 0;
        }
	
	$short_uri = $menu->bezeichnung;
	$short_uri = str_replace(array('ä', 'Ä', 'ö', 'Ö', 'ü', 'Ü', 'ß'), array('ae', 'Ae', 'oe', 'Oe', 'ue', 'Ue', 'ss'), $short_uri);
	$short_uri = preg_replace("@[^0-9aA-zZ\-]@si", "-", $short_uri);
	$short_uri = preg_replace("@(-{2,})@si", "-", $short_uri);
	
	$sql = "
			INSERT INTO structure
			SET
				parent_id=".$parent_ids['oldid_'.$menu->parentid].",
				old_id=".$menu->rid.",
				active=1,
				short_uri='".$short_uri."',
                                pos='".$pos."',
                                image='',
				name='".$menu->bezeichnung."'
		";
		
		//echo $sql."<br>";
		$query = mysql_query($sql);
		$last_id = $parent_ids['oldid_'.$menu->rid] = mysql_insert_id($connection);
		$pos++;
		$sql = "
			INSERT INTO `site_content` (`structure_id`, `type`, `position`, `activ`, `content`, `configuration`) VALUES
			(".$last_id.", 'textbox', 1, 0, '', NULL),
			(".$last_id.", 'slider', 2, 0, '', NULL),
			(".$last_id.", 'article_list', 3, 1, '0', NULL),
			(".$last_id.", 'banner', 4, 0, '0', NULL),
			(".$last_id.", 'manufacturers', 5, 0, '', NULL),
			(".$last_id.", 'substructure', 6, 0, '', NULL),
			(".$last_id.", 'popular', 0, 0, '', NULL)
	";
	//$query = mysql_query($sql);
}
