<?
include("../../../framework/framework.php");
include("../functions.php");


// Define page specific text for template
$PAGE[TXT_TITLE]=PRATICHE_MENU_0;
$PAGE[PAGE_INTITLE]=PRATICHE_MENU_0;
$PAGE[PAGE_PICTITLE]="ico_prat_med.gif";
$module="pratiche";

template_init();
ob_start();

//Update last viewed
insert_last_viewed($_GET[id],$module);

if ($_GET[actdone]=="upd")
{
	$rspact[title]=PRATICHE_UPD_DONE;
	print draw_response($rspact);	
} elseif ($_GET[actdone]=="add")
{
	$rspact[title]=PRATICHE_ADD_DONE;
	print draw_response($rspact);	
}


if (check_perm_obj($module,$_GET[id],"r"))
{
	$PAGE_ELEMENT[PAGE][1][0][param]=$_GET[id];
	
	if (!isset($_GET[action]))
	{
		$thisobj= load_fwobject("show","pratiche",0);
		
		if( ($_SESSION[user][admin]==1 || $_SESSION[user][move_prat]==1) && $_SESSION[history]==0)
		{
			$thisobj["Fields"]["button_m"]=make_button("pratiche_show.php?action=hist&id=".$_GET[id],PRATICHE_TOHIST);
		} elseif (($_SESSION[user][admin]==1 || $_SESSION[user][move_prat]==1) && $_SESSION[history]==1)
		{
			$thisobj["Fields"]["button_m"]=make_button("pratiche_show.php?action=rest&id=".$_GET[id],PRATICHE_TOCURR);
		} else
		{$thisobj["Fields"]["button_m"]=""; }
		
		$thisobj["Fields"]["button_d"]="";
		if($_SESSION[history]==0)
		{
			$thisobj["Fields"]["button_w"]=make_button("new_pratiche.php?id=".$_GET[id],FW_MODIFY);
			$thisobj["Fields"]["button_newscad"]=make_button($CONF[url_base].$CONF[dir_modules]."calendar/pages/new_app.php?ref_id=".$_GET[id],PRATICHE_ADD_EVENT);
			$thisobj["Fields"]["button_newpres"]=make_button($CONF[url_base].$CONF[dir_modules]."prestazioni/pages/new_prestazione.php?ref_id=".$_GET[id],PRATICHE_ADD_PREST);
			$thisobj["Fields"]["button_newdoc"]=make_button($CONF[url_base].$CONF[dir_modules]."document/pages/new_document.php?pid=".$_GET[id],PRATICHE_ADD_DOC);
		} else
		{			
			$thisobj["Fields"]["button_w"]=FW_MODIFY;
			$thisobj["Fields"]["button_newscad"]=PRATICHE_ADD_EVENT;
			$thisobj["Fields"]["button_newpres"]=PRATICHE_ADD_PREST;
			$thisobj["Fields"]["button_newdoc"]=PRATICHE_ADD_DOC;
		}
		$thisobj["Fields"]["button_pres"]=make_button($CONF[url_base].$CONF[dir_modules]."prestazioni/pages/prestazioni_view.php?form_id=listprestaz&form_page=1&ref_id[text]=&ref_id[realval][]=".$_GET[id],"8 - ".PRESTAZIONI_TITLE);
		$thisobj["Fields"]["button_scad"]=make_button($CONF[url_base].$CONF[dir_modules]."calendar/pages/app_view.php?form_id=listcont&form_page=1&ref_prat[text]=&ref_prat[realval][]=".$_GET[id],"7 - ".PRATICHE_IMPEGN);
		$thisobj["Fields"]["button_doc"]=make_button($CONF[url_base].$CONF[dir_modules]."document/pages/documents_view.php?form_id=listdoc&form_page=1&ref_prat[text]=&ref_prat[realval][]=".$_GET[id],"9 - ".DOCUMENT_TITLE);
		$thisobj["Fields"]["button_sitcont"]=make_button_clean(PRATICHE_SITCONT,'onClick="loadLayerWindow(\''.$CONF[url_base].$CONF[dir_modules].'pratiche/pages/pratiche_sitcont.php?id='.$_GET[id].'\');"');
		$thislist= load_fwobject("lists","calendar",3);
		$thisobj["Fields"]["scad_list"]=draw_list($thislist,"calendar");
		
		print draw_object($thisobj,intval($_GET[id]),$module);
		
	} elseif ($_GET[action] == "hist")
	{
		$response[title]=PRATICHE_MOVETIT;
		$rsdupe=$DB->Execute("SELECT * FROM calendar WHERE ref_prat=".$_GET[id]);
		if ($rsdupe->RecordCount() > 10)
		{
			$response[text]=PRATICHE_MOVE_ERRCAL."<br><br>".make_button("pratiche_show.php?id=".$_GET[id],PRATICHE_BACK_SHOW);
		} else 
		{
			$response[text]=PRATICHE_MOVE_TXT."<br><br>".make_button("pratiche_show.php?action=histok&id=".$_GET[id],PRATICHE_MOVETIT)." &nbsp;&nbsp;&nbsp;&nbsp".make_button("pratiche_show.php?id=".$_GET[id],PRATICHE_BACK_SHOW);
		}
		print draw_response($response);		

	} elseif ($_GET[action] == "histok")
	{

		$response[title]=PRATICHE_MOVETIT;
		$rsdupe=$DB->Execute("SELECT * FROM calendar WHERE ref_prat=".$_GET[id]);
		if ($rsdupe->RecordCount() > 10)
		{
			$response[text]=PRATICHE_MOVE_ERRCAL."<br><br>".make_button("pratiche_show.php?id=".$_GET[id],PRATICHE_BACK_SHOW);
		} else 
		{
			PratToHistory($_GET[id]);
			$response[text]=PRATICHE_MOVE_DONE."<br><br>".make_button("pratiche_view.php",PRATICHE_BACK_LIST);
		}
		print draw_response($response);		
	
	} elseif ($_GET[action] == "rest")
	{
		$response[title]=PRATICHE_RESTTIT;
		$response[text]=PRATICHE_REST_TXT."<br><br>".make_button("pratiche_show.php?action=restok&id=".$_GET[id],PRATICHE_RESTTIT)." &nbsp;&nbsp;&nbsp;&nbsp".make_button("pratiche_show.php?id=".$_GET[id],PRATICHE_BACK_SHOW);
		print draw_response($response);		

	} elseif ($_GET[action] == "restok")
	{

		$response[title]=PRATICHE_RESTTIT;
		HistoryToPrat($_GET[id]);
		$response[text]=PRATICHE_REST_DONE."<br><br>".make_button("pratiche_view.php",PRATICHE_BACK_LIST);
		print draw_response($response);		
	}


} else {
	$response[title]=FW_ERROR_NO_PERM;
	$response[text]=FW_ERROR_NO_PERM_TXT;
	$iserror=1;
	print draw_response($response);
}
	


$PAGE[PAGE_CONTENT] = ob_get_contents();
ob_end_clean();
template_define_elements();

final_render();

?>