<?php
include_once ("_dirinfo.php");
include_once (PATH_TO_ROOT."mystery/init.php");

if ($_POST['action'] == "deleteSession") {
	unset($_SESSION[$_POST['uniqeID']]);
} elseif ($_POST['action'] == "update") {

		// uniqe ID 
		// type - is table or graph? TableStatus; GraphStatus
		// node - id of folder 
		
		if ( !$_POST['checked'] ) 	
				$_SESSION[$_POST['uniqeID']][$_POST['type']][$_POST['node']] = $_POST['node'];
			else
				unset($_SESSION[$_POST['uniqeID']][$_POST['type']][$_POST['node']]);
		
} elseif ($_POST['action'] == "weightUpdate") {

	$tmp = explode("_",$_POST['weightName']);
	$idFolder = $tmp[1];
	
	$sql = "SELECT id FROM ".DB_WARPIT_WEBCATI.".".$_POST['projectName']."_mystery 
				WHERE id_folder = ".$idFolder;
	
	$res = $db->SQLexecute($sql);
	if ($db->NumRows($res)) {
		
		$sql = "UPDATE ".DB_WARPIT_WEBCATI.".".$_POST['projectName']."_mystery
					 SET sc_value = ".$_POST['weightValue']." 
					WHERE id_folder = ".$idFolder;		
	} else {
		$sql = "INSERT INTO  ".DB_WARPIT_WEBCATI.".".$_POST['projectName']."_mystery
					 SET sc_value = ".$_POST['weightValue'].", id_folder = ".$idFolder;
	}
	
	$db->SQLexecute($sql);
	
} else {

	if ($_POST['node']) $nodeID = $_POST['node']; else $nodeID = 0;
	
	$db->SQLexecute("SET @cnt:=0");
	
	$sql = "SELECT @cnt := @cnt+1 as idCnt, a.id,a.idLab,IF(b.id IS NULL, false, true ) as is_leaf, if (c.sc_value,c.sc_value,'')  as folderWeight  
				FROM ".DB_WARPIT_WEBCATI.".".$_POST['projectName']."_tree as a \
					LEFT JOIN ".DB_WARPIT_WEBCATI.".".$_POST['projectName']."_tree as b ON a.id = b.id_sup	\	
					LEFT JOIN ".DB_WARPIT_WEBCATI.".".$_POST['projectName']."_mystery as c ON a.id = c.id_folder \
				WHERE a.is_leaf = 0 AND a.id_sup = ".$nodeID."  GROUP BY a.id ORDER BY a.orderBy";	
	
	echo "/*".$sql."*/";
	
	$res = $db->SQLexecute($sql);
	while ($zad = $db->fetchAssoc($res)) {
		unset($path);
		$path['id']					= $zad['id'];
		$path['idCnt']				= "";
		$path['groupName']			= "[".$zad['id']."] - ".$zad['idLab'];
		$path['groupName']			= $zad['idLab'];
		$path['folderWeight']		= $zad['folderWeight'];

		// check if we have values on this folder... if not we disable this checkbox because their is no data ...
		if (!$_SESSION[$_GET['uniqeID']]['TableStatus'][$path['id']]) {
			$path['TableStatus']	= "checked ";
			$path['GraphStatus']	= "checked ";
		} else {
			$path['TableStatus']	= "disabled ";
			$path['GraphStatus']	= "disabled ";
		}
		
		if ($nodeID > 0) $path['GraphStatus']	= " disabled ";
		$path['leaf']			= $zad['is_leaf'] ? false : true;
		$nodes[] = $path;
	}
		
	 	echo json_encode($nodes);
}
?>