<?php
/*
Officity - Web application platform - Version 6.0 - 2011-07-05

François Dispaux, Boris Verdeyen, Thomas Hermant,
Jérémie Roy, Grégory Meurice, Abdelila Harbi, 
Marc Mignonsin, Jonathan Sanchez, Julien Gonzalez, Pierre Fouchez

Sushee and Officity is © Copyright 2011 Nectil SA.

`/sushee-source/sushee/private/updateCategories.inc.php` is part of Officity.

Sushee is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Officity and Sushee are distributed in the hope that they will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Sushee. If not, see <http://www.gnu.org/licenses/>.
*/
function handleCategory(&$xml,$categories_path,$fatherID,$moduleInfo,$path,&$to_keep){
	$db_conn = db_connect();
	if($moduleInfo->loaded)
	$moduleID = $moduleInfo->ID;
	else
	$moduleID = 0;
	$categories_array = $xml->match($categories_path.'/CATEGORY');
	foreach($categories_array as $current_path){
		$UniqueName = $xml->getData($current_path."/UNIQUENAME");
		$ID = $xml->getData($current_path."/@ID");
		$IsAssignable = $xml->getData($current_path."/ISASSIGNABLE");
		if(!$ID){
			$particle = '';
			$success = false;
			while(!$success){
				$sql = "INSERT INTO `categories`(`ModuleID`,`FatherID`,`Denomination`,`Path`,`IsAssignable`) VALUES($moduleID,'$fatherID',\"".encode_for_DB($UniqueName.$particle)."\",\"".encode_for_DB($path).encode_for_DB($UniqueName.$particle)."/\",'".encode_for_DB($IsAssignable)."');";
				$success = $db_conn->Execute($sql);
				$particle++;
			}
			$ID = $db_conn->Insert_Id();
			sql_log($sql);
		}else{
			$sql = "UPDATE `categories` SET `IsAssignable`='".encode_for_DB($IsAssignable)."', `ModuleID`=$moduleID,`FatherID`='$fatherID',`Denomination`=\"".encode_for_DB($UniqueName)."\",`Path`=\"".encode_for_DB($path).encode_for_DB($UniqueName)."/\" WHERE `ID`='$ID';";
			$db_conn->Execute($sql);
			sql_log($sql);
		}
		$to_keep[]=$ID;
		// if there is an ID, we delete the previous categorytraductions
		
		// category created --> now putting the traductions
		$labels_array = $xml->match($current_path."/LABEL[@languageID!='']");
		$sql="INSERT INTO `categorytraductions`(`CategoryID`,`LanguageID`,`Text`) VALUES";
		$first = true;
		$languages = array();
		foreach($labels_array as $label_path){
			$languageID=$xml->getData($label_path.'/@languageID');
			$languages[] = $languageID;
			$text = $xml->getData($label_path);
			if ($first != true) $sql.=",";
			else $first = false;
			$sql.="($ID,\"$languageID\",\"".encode_for_DB($text)."\")";
		}
		$clean_sql = 'DELETE FROM `categorytraductions` WHERE `CategoryID`=\''.$ID.'\' AND `LanguageID` IN ("'.implode('","',$languages).'");';
		$db_conn->Execute($clean_sql);
		if (sizeof($labels_array)>0){
			$db_conn->Execute($sql);
		}
		if($xml->match($current_path.'/CHILDS[1]'))
			handleCategory($xml,$current_path.'/CHILDS[1]',$ID,$moduleInfo,$path.$UniqueName."/",$to_keep);
	}
}
function updateCategories($name,&$xml,$requestName,$current_path,$firstNode,$firstNodePath){
	//$current_path = $current_path."/CATEGORIES[1]";
	$categories_array = $xml->match($current_path.'/CATEGORIES');
	$to_keep = array();
	foreach($categories_array as $path){
		$module = $xml->getData($path."/@module");
		$moduleInfo = moduleInfo($module);
		if($moduleInfo->loaded)
			$start_name = $moduleInfo->name;
		else
			$start_name = $module;
		handleCategory($xml,$path,0,$moduleInfo,"/".$start_name."/",$to_keep);
	}
	// now deleting the absent ones
	$delete_sql1 = 'DELETE FROM `categories` WHERE ';
	$delete_sql2 = 'DELETE FROM `categorylinks` WHERE ';
	$delete_sql3 = 'DELETE FROM `categorytraductions` WHERE ';
	for($i=0;$i<sizeof($to_keep);$i++){
		$IDs_cond='ID!=\''.$to_keep[$i].'\' ';
		$delete_sql1.=$IDs_cond;
		$delete_sql2.='Category'.$IDs_cond;
		$delete_sql3.='Category'.$IDs_cond;
		if($i!=sizeof($to_keep)-1){
			$delete_sql1.='AND ';
			$delete_sql2.='AND ';
			$delete_sql3.='AND ';
		}
	}
	if(sizeof($to_keep)==0){
		$delete_sql1 = 'TRUNCATE TABLE `categories`';
		$delete_sql2 = 'TRUNCATE TABLE `categorylinks`';
		$delete_sql3 = 'TRUNCATE TABLE `categorytraductions`';
	}
	$db_conn = db_connect();
	$db_conn->Execute($delete_sql1);
	$db_conn->Execute($delete_sql2);
	$db_conn->Execute($delete_sql3);
	return generateMsgXML(0,'Creation/Update of the categories successful.',0,'',$name);
}
?>
