<?php

$__g_remapping_g = "unset";
$__g_a_remapping_g = array();

function remap_g( $product ) {
	global $__g_remapping_g;
	global $__g_a_remapping_g;
	if( $__g_remapping_g === false ) { return $product; }
	if( $__g_remapping_g === "unset" ) {
		if( file_exists( "./remapping_g.conf" ) ) {
			$__g_a_remapping_g = file( "./remapping_g.conf" );
			$__g_remapping_g = true;
		} else {
			$__g_remapping_g = false;
			return $product;
		}
	}
	$a_prod = array();
	foreach( $product as $x ) { $a_prod[] = $x; }
	$new_prod = array();
	$c = 0;
	foreach( $__g_a_remapping_g as $key ) {
		if( trim( $key ) ) {
			$new_prod[ trim( $key ) ] = $a_prod[ $c ];
		}
		$c++;
	}
	return $new_prod;
}

$catgroupsys_tpl = preg_replace(
		"~(<(\w+)>)\[!--\\2--\](</\\2>)~e",
		"(strlen(\$general_info[\\2])>0?\"\\1\$general_info[\\2]\\3\":\"\")",
		$catgroupsys_tpl
);

$groupsys = parseCatalog("../tmp/cat_conv.txt");
//echo "<pre>";
//var_dump($groupsys);
//echo "</pre>";
//exit;
//print_r( $groupsys );

makeCatalogGroupSys($groupsys);

function makeCatalogGroupSys($groupsys)
{
	global $catgroupsys_tpl;
	global $fp;
	
	$str = $catgroupsys_tpl;
	reset($groupsys);
	foreach($groupsys as $group)
	{
		$groups .= makeCatgroup($group) . "\n";
	}
	$str = str_replace("[!--CATALOG_STRUCTURE--]",$groups,$str);
	$str = preg_replace("'\n\s*\n'","\n",$str);
	fputs($fp,$str."\n");
}

function makeCatgroup($group,$add=Array())
{
	global $fp;
	global $catstructure_tpl;
	global $bme_groupsys_flds;
	global $aErrors;
	global $mimeinfo_tpl;
	global $mime_tpl;
	
	$ret = $catstructure_tpl;
	while(list($key,$val)=each($group))
	{
//		echo $key .":".$val."<br>";
		if(strlen($val)<1)
		{
//			$str = preg_replace('/^.*\[!--' . $key . '--].*$/m','',$str);
			continue;
		}
		if ($key=='MIME_INFO') {
$mime = $mime_tpl;
			$mime = str_replace("[!--MIME_TYPE--]",getMimeType($val),$mime);
			$mime = str_replace("[!--MIME_SOURCE--]",$val,$mime);
			$mime = str_replace("[!--MIME_DESCR--]","",$mime);
			$mime = str_replace("[!--MIME_ALT--]","",$mime);
			$mime = str_replace("<MIME_PURPOSE></MIME_PURPOSE>","",$mime);
			$mime = str_replace("<MIME_DESCR></MIME_DESCR>","",$mime);
			$mime = str_replace("<MIME_ALT></MIME_ALT>","",$mime);
			$val = $mime; 
			}
						
		     $tag = "[!--" . $key . "--]";
		     $ret = str_replace($tag,$val,$ret);     
	}
	
	if( $group["GROUP_ORDER"] ) {
		$ret = str_replace("<MIME_INFO>","<GROUP_ORDER>" . $group["GROUP_ORDER"] . "</GROUP_ORDER>\n<MIME_INFO>\n",$ret);
	}
	$ret = preg_replace( "#<MIME_INFO>[^<]*\[!--[^<]+<\/MIME_INFO>#is", "", $ret );
	if(preg_match_all("/^.*\[!--(\w+)--].*$/m",$ret,$matches,PREG_SET_ORDER))
	{ 
		foreach($matches as $match)
		{	
			$required = in_array("#" . $match[1],$bme_groupsys_flds);
			if($required)
			{
				$error = 1;
				$aErrors[] = "error: missing required tag $match[1]";
			}
			$ret = str_replace($match[0],"",$ret);
		}
	}
	//print_r( $ret );
	//echo str_repeat( "-", 80 ) . "\n";
	//print_r( $ret );
	return $ret;
	
}

function parseCatalog( $file ) {
	global $aErrors;
	global $bme_groupsys_flds;
	$c=0;
	$ret = array();
	$data = file($file);
	foreach( $data as $line ) {
	//	$line = mb_convert_encoding( $line, "UTF-8", "UTF-16" );
		if( ! trim( $line ) ) continue;
		$fields = explode( "\t", trim( $line ) );
		$c = 0;
		$arr = array();
		foreach( $bme_groupsys_flds as $field ) {
			$field = preg_replace("/#/","",$field);
			$arr[ $field ] = fixString( $fields[ $c++ ] );
		}
		$arr = remap_g( $arr );
		$arr[ "type" ] = preg_match("#^prod#i", $arr[ "CATALOG_STRUCTURE" ]) ? "leaf" : "node";
		if( $arr[ "GROUP_ID" ] == 1 ) {
			$arr[ "type" ] = "root";
		}
		$ret[ $arr[ "GROUP_ID" ] ] = $arr;
	}
	if(!isset($ret[1]))
		$aErrors[] = "Es mu� eine Root-Kataloggruppe mit GROUP_ID = 1 existieren";
	foreach($ret as $k=>$v)
	{
		if($v["type"]=="root")continue;
		if(!isset($ret[$v["PARENT_ID"]]))
			$aErrors[] = "Kataloggruppe GROUP_ID=$k: Parent existiert nicht";
		elseif($ret[$v["PARENT_ID"]]["type"]=="leaf")
			$aErrors[] = "Kataloggruppe GROUP_ID=" . $ret[$v["PARENT_ID"]] . ": leaf darf nur Artikel enthalten";		
	}
	reset($ret);
	return $ret;
}

function parseCatalog_old($file)
{
	global $aErrors;
	$c=0;
	$ret = array();
	$data = file($file);
	foreach($data as $line)
	{
		if(preg_match("/^(.+?)\t(.*?)\t(.*?)\t(.*?)\t(.*?)\t(.*?)\t(.*?)$/",$line,$match))
		{
			$c++;
			$id = fixString($match[1]);
			if($id)
			{
				if(isset($ret[$id]))
					$aErrors[] = "Katalogruppe mit doppelter GROUP_ID = $id in Zeile $c";
				$ret[$id] = array(
					//"type"=>(strpos(" ".strtolower($match[4]),"prod"))?"leaf":"node",
					"type"=>$match[4],
					"GROUP_ID"=>$id,
					"GROUP_NAME"=>fixString($match[2]),
					"GROUP_DESCRIPTION"=>fixString($match[3]),
					"PARENT_ID"=>fixString($match[5]),
					"MIME_INFO"=>fixString($match[6]),
				);
				if((string)$id==="1")
				{
					$ret[$id]["type"] = "root";
					$ret[$id]["PARENT_ID"] = 0;
				}
			}
		}
	}
	if(!isset($ret[1]))
		$aErrors[] = "Es mu� eine Root-Kataloggruppe mit GROUP_ID = 1 existieren";
	foreach($ret as $k=>$v)
	{
		if($v["type"]=="root")continue;
		if(!isset($ret[$v["PARENT_ID"]]))
			$aErrors[] = "Kataloggruppe GROUP_ID=$k: Parent existiert nicht";
		elseif($ret[$v["PARENT_ID"]]["type"]=="leaf")
			$aErrors[] = "Kataloggruppe GROUP_ID=" . $ret[$v["PARENT_ID"]] . ": leaf darf nur Artikel enthalten";		
	}
	reset($ret);
	return $ret;
}

?>
