<?php
include_once("_dirinfo.php");
include_once(PATH_TO_ROOT."init.php");
include_once(PATH_TO_ROOT."webcati/_class/class.quotas.php");
include_once(PATH_TO_ROOT."webcati/_class/class.webrecode.php");

$db->ChangeDatabase(DB_WARPIT_WEBCATI_BASE);

if(isset($_POST["id_project"]))
	$id_proj=$_POST["id_project"];
else 
	$id_proj=0;

	
$q=new webrecode($id_proj, $db);


if($_POST['multiLoad'])
{
	$tmp = explode(',',$_POST['multiLoad']);
	foreach ($tmp as $grid) {
		if($grid == 1) { $q->get_variables(); $result[]['rows'] = $q->variables_array; }
		else if($grid == 2) $result[]['rows'] = $q->recoded_groups;
		else if($grid == 3){$q->get_answers_to_display($_POST["id_rec_group"]);
				$result[]['rows'] = $q->answers_to_display; }
		else if($grid == 4){ $q->get_categories($_POST["id_rec_group"]); $result[]['rows'] = $q->categories_array;}
		else if($grid == 5){$q->get_answers_in_categories($_POST["id_category"], $_POST["multicode"]); $result[]['rows'] = $q->answers_in_categories; }
		
	}
	echo json_encode($result);
	exit;
}


// edit label text
if($_POST['action'] == "update") {
	unset($_POST["grid_action"]);
	
		$sql = "UPDATE ".$_POST['proj_name']."_labels SET ".$_POST['field']."='".$_POST['value']."' WHERE id=".$_POST['id'];
		if(!$q->datab->SQLexecute($sql)) echo $q->datab->JsonError();
		echo "{success:true,/*$sql*/}";
}


if($_POST["grid_action"]=="get_variables")
{
	$q->get_variables();
	if(count($q->variables_array)>0)
		echo '{success:true, rows:'.json_encode($q->variables_array).'}';
	else
		echo '{success:false, rows:[]}';
} 
else if($_POST["grid_action"]=="get_recoded_groups")
{
	if(count($q->recoded_groups)>0)
		echo '{success:true, rows:'.json_encode($q->recoded_groups).'}';
	else
		echo $q->error;
}
else if($_POST["grid_action"]=="create_recode_group")
{
	$recode=json_decode(stripslashes($_POST["data"]));
	$lab_val=$_POST["podatki"];
	
	if($q->create_recode_group($recode, $lab_val["label"], $lab_val["value"]))
	{
		echo '{success:true, rows:[]}';
	}
	else
		echo $q->error;
}
else if($_POST["grid_action"]=="remove_category_from_group")
{
	$id=array_shift(json_decode(stripslashes($_POST["data"])));
	
	if($q->remove_category_from_group($id))
	{
		echo '{success:true, rows:[]}';
	}
	else
		echo $q->error;
}
else if($_POST["grid_action"]=="get_categories")
{
	if(isset($_POST["id"]))
	{
		$q->get_categories($_POST["id"]);
		echo '{success:true, rows:'.json_encode($q->categories_array).'}'; 
	}
	else
		echo '{success:true, rows:[]}';
}
else if($_POST["grid_action"]=="insert_answers_into_categorie")
{
	/*echo "<pre>";
	print_r($_POST);
	echo "</pre>";*/
	
	$answers=json_decode(stripslashes($_POST["data"]));
	$id_cat=$_POST["id_cat"];
	$multicode=$_POST["multicode"];
	
	if($q->insert_answers_into_categorie($answers, $id_cat, $multicode))
		echo '{success:true}';
	else
		echo $q->error;
	//else
		//echo '{success:false}';
}
else if($_POST["grid_action"]=="remove_answers_from_category")
{
	$ids_to_remove=json_decode(stripslashes($_POST["data"]));
	$recoded_id=$_POST["recoded_id"];
	
	if($q->remove_answers_from_category($ids_to_remove, $_POST["multicode"], $recoded_id))
	{
		echo '{success:true, rows:[]}';
	}
	else
		echo $q->error;
}
else if($_POST["grid_action"]=="get_answers_to_display")
{
	
	if(isset($_POST["id"]))
	{
		$q->get_answers_to_display($_POST["id"]);
		echo '{success:true, rows:'.json_encode($q->answers_to_display).'}'; 
	}
	else
		echo '{success:true, rows:[]}';
	
	
}
else if($_POST["grid_action"]=="get_answers_in_categories")
{
	if(isset($_POST["id"]))
	{
		$q->get_answers_in_categories($_POST["id"], $_POST["multicode"]);
		echo '{success:true, rows:'.json_encode($q->answers_in_categories).'}'; 
	}
	else
		echo '{success:true, rows:[]}';
	
}
else if($_POST["grid_action"]=="create_cat_in_recode_group")
{
	$recoded_group_id=$_POST["id"];
	$data=$_POST["podatki"];
	
	if($q->create_cat_in_recode_group($recoded_group_id, $data["category_name"], $data["category_value"]))
	{
		echo '{success:true, rows:[]}';
	}
	else
		echo $q->error;
	
}
else if($_POST["grid_action"]=="create_statistics")
{	
	if($q->create_statistics())
	{
		echo '{success:true, rows:'.json_encode($q->stat_result).'}'; 
	}
	else
		echo $q->error;
	
}
elseif ($_POST["grid_action"]=="splitLabel")
{
	$idCalc = $_POST['id_calc'];
	$q->splitLabel($idCalc,$_POST['recoded_group'],$_POST['delimiter']);
	
}

?>