<?php



require_once("syncinclude.php");

$return=3;
if (auth())
{
	if ((intval($_POST["action"]) == 1) && intval($_POST['KeyKategorie'])>0)
	{
		$return = 0;
		$Kategorie->kKategorie = intval($_POST["KeyKategorie"]);
		$Kategorie->kOberKategorie = intval($_POST["KeyOberKategorie"]);
		$Kategorie->nSort = intval($_POST["Sort"]);
		$Kategorie->cName = realEscape($_POST["KeyName"]);
		$Kategorie->cBeschreibung = realEscape($_POST["KeyBeschreibung"]);
		$Kategorie->parent_id = 0;
		
		//hole einstellungen
		$cur_query = eS_execute_query("select languages_id from eazysales_einstellungen");
		$einstellungen = go_db_fetch_object($cur_query);
		
		if ($Kategorie->kOberKategorie>0)
		{
			//existiert oberkat?
			$categories_id_oberkat = getFremdKategorie($Kategorie->kOberKategorie);
			if (!$categories_id_oberkat)
			{
				eS_execute_query("insert into categories (date_added) values (now())");
				//hole id
				$query = eS_execute_query("select LAST_INSERT_ID()");
				$categories_id_oberkat_arr = go_db_fetch_row($query);
				eS_execute_query("insert into categories_description (categories_id, language_id) values (".$categories_id_oberkat_arr[0].",$einstellungen->languages_id)");
				$Kategorie->parent_id = $categories_id_oberkat_arr[0];
				setMappingKategorie($Kategorie->kOberKategorie, $Kategorie->parent_id);
				
				//erstelle leere description f�r alle anderen Sprachen
				$sonstigeSprachen = getSonstigeSprachen($einstellungen->languages_id);
				if (is_array($sonstigeSprachen))
				{
					foreach ($sonstigeSprachen as $sonstigeSprache)
					{
						eS_execute_query("insert into categories_description (categories_id, language_id) values (".$categories_id_oberkat_arr[0].",$sonstigeSprache)");
					}
				}
			}
			else 
				$Kategorie->parent_id = $categories_id_oberkat;
		}
		//update oder insert?
		$categories_id = getFremdKategorie($_POST['KeyKategorie']);
		if ($categories_id>0)
		{
			//update
			eS_execute_query("update categories set parent_id=$Kategorie->parent_id, sort_order=$Kategorie->nSort where categories_id=".$categories_id);
			eS_execute_query("update categories_description set categories_name=\"$Kategorie->cName\" where categories_id=".$categories_id." and language_id=".$einstellungen->languages_id);
		}
		else 
		{
			//insert
			eS_execute_query("insert into categories (parent_id, date_added, sort_order) values ($Kategorie->parent_id,now(),$Kategorie->nSort)");
			$query = eS_execute_query("select LAST_INSERT_ID()");
			$categories_id_arr = go_db_fetch_row($query);
			eS_execute_query("insert into categories_description (categories_id, language_id, categories_name) values (".$categories_id_arr[0].",$einstellungen->languages_id, \"$Kategorie->cName\")");
			setMappingKategorie($Kategorie->kKategorie, $categories_id_arr[0]);
			
			//erstelle leere description f�r alle anderen Sprachen
			$sonstigeSprachen = getSonstigeSprachen($einstellungen->languages_id);
			if (is_array($sonstigeSprachen))
			{
				foreach ($sonstigeSprachen as $sonstigeSprache)
				{
					eS_execute_query("insert into categories_description (categories_id, language_id, categories_name) values (".$categories_id_arr[0].",$sonstigeSprache, \"$Kategorie->cName\")");
				}
			}
		}
 	}	
	elseif (intval($_POST["action"]) == 3 && intval($_POST['KeyKategorie'])>0)
	{
		$return=0;
		$cat = getFremdKategorie(intval($_POST['KeyKategorie']));
		if ($cat>0)
			eS_execute_query("delete from categories where categories_id=".$cat);
			eS_execute_query("delete from categories_description where categories_id=".$cat);
			eS_execute_query("delete from categories_description where categories_id=".$cat);
	}
	else
		$return=5;
}

echo($return);
logge($return);
?>