<?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/createCategories.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/>.
*/


$current_path = $current_path."/CATEGORIES[1]";

$moduleInfo = moduleInfo($xml->getData($current_path."/@module"));

function handleCategory(&$xml,$categories_path,$fatherID,$moduleInfo,$path){
	$db_conn = db_connect();
	
	$moduleID = $moduleInfo->ID;
	
	$categories_array = $xml->match($categories_path.'/CATEGORY');
	foreach($categories_array as $current_path){
		$UniqueName = $xml->getData($current_path."/UNIQUENAME");
		
		$sql = "INSERT INTO categories(ModuleID,FatherID,Denomination,Path) VALUES($moduleID,'$fatherID',\"".encode_for_DB($UniqueName)."\",\"".encode_for_DB($path).encode_for_DB($UniqueName)."/\");";
		$db_conn->Execute($sql);
		$ID = $db_conn->Insert_Id();
		
		// category created --> now putting the traductions
		$labels_array = $xml->match($current_path."/LABEL[@languageID!='']");
		$sql="INSERT INTO categorytraductions(CategoryID,LanguageID,`Text`) VALUES";
		$first = true;
		foreach($labels_array as $label_path){
			$languageID=$xml->getData($label_path.'/@languageID');
			$text = $xml->getData($label_path);
			if ($first != true) $sql.=",";
			else $first = false;
			$sql.="($ID,\"$languageID\",\"".encode_for_DB($text)."\")";
		}
		if (sizeof($labels_array)>0){
			$db_conn->Execute($sql);
		}
		handleCategory($xml,$current_path,$ID,$moduleInfo,$path.$UniqueName."/");
	}
}

handleCategory($xml,$current_path,0,$moduleInfo,"/".$moduleInfo->name."/");
return generateMsgXML(0,'Creation of the categories successful.',0,'',$name);
?>
