<?php
/* Modified by NATYS */
$myEnv["module"] = "calendar";
$myEnv["output"] = "html";

include("../../config.inc.php");
include(INCLUDEPATH . "container.inc.php");

// adding file revision into global array
if(function_exists("revisionInit")) revisionInit("\$Revision: 1.28 $", __FILE__);

//// NATYS //// Date langage :
include(CAL1_INC_DIR."date_lang.php");
$_SESSION["date_lang"] = array ("language" => $_SESSION["MGW"]->spkz,
								"lang_dir" => CAL1_DIR."lang/"
								);
///////////////


if (!isset($_GET["id"])) {
    $smarty->display('calendar_date_details.tpl');
    exit();
}

if(isset($_GET["userid"]) || isset($_POST["userid"])) $userid = (isset($_GET["userid"])) ? $_GET["userid"] : $_POST["userid"];
else $userid=$_SESSION["MGW"]->userid;

$id = (int) $_GET["id"];

$timemask = $_SESSION["MGW"]->settings["timefmt"];
$datemask = $_SESSION["MGW"]->settings["datefmt"];

$concat=$conn->Concat("firstname", "' '", "lastname");
$concat_b=$conn->Concat("b.firstname", "' '", "b.lastname");
$concat_c=$conn->Concat("c.firstname", "' '", "c.lastname");


//get the class :
$sql = "SELECT id_class FROM mgw_calendar WHERE id = '$id' AND userid='$userid'";
if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
$row = $res->FetchRow();
$id_class = ($row["id_class"] != "") ? $row["id_class"] : 2;


$sql  = "SELECT a.*, $concat_b AS name, $concat_c AS initname FROM mgw_calendar a, mgw_users b, mgw_users c WHERE a.id_class!='3' AND a.inituserid = c.id AND a.id = $id AND NOT(b.level=".UDELETED.") AND a.userid = b.id";
if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
$row = $res->FetchRow();

$appointment_start = $res->UnixTimeStamp($row["date"]);
$appointment_end   = $appointment_start + $row["duration"] * 60;

///// NATYS  /////
 //----- scheduling type :
$scheduling = $row["id_scheduling"];
 //search string
 $sql_sched = "SELECT * FROM mgw_calendar_scheduling_type WHERE id = $scheduling";
 if(!$res_sched = $conn->Execute($sql_sched)) exit(showSQLerror($sql_sched, $conn->ErrorMsg(), __LINE__, __FILE__));
 $row_sched = $res_sched->FetchRow();
 $scheduling = array("id" 	=> $scheduling,
 					 "type" => Lang::getLanguageString("gentemp_".$row_sched["type"])
					);
 $smarty->assign("scheduling",$scheduling);


 //----- appointment type :
	$id_appt_type = $row["id_appt_type"];
	$sql_appt_type = "SELECT * FROM mgw_calendar_appt_type WHERE id=$id_appt_type";
	if(!$res_appt_type = $conn->Execute($sql_appt_type)) exit(showSQLerror($sql_appt_type, $conn->ErrorMsg(), __LINE__, __FILE__));
	$row_appt_type = $res_appt_type->FetchRow();

			//$id_class = $row["id_class"];
			if ($id_class!=2) {
			    // appointment is subjected or on standby:
				$sql_class = "SELECT * FROM mgw_calendar_class WHERE id='$id_class'";
				if(!$res_class = $conn->Execute($sql_class)) exit(showSQLerror($sql_class, $conn->ErrorMsg(), __LINE__, __FILE__));
				$row_class = $res_class->FetchRow();
				$appointment_type = array( 	"text" => Lang::getLanguageString("gentemp_".$row_appt_type["type"])."&nbsp;:<BR>".Lang::getLanguageString("class_".$row_class["class"]),
										"color"=> $row_class["color"]
									 	 );
			}
			else {
				$appointment_type = array( 	"text" => Lang::getLanguageString("gentemp_".$row_appt_type["type"]),
										"color"=> $row_appt_type["color"]
									 );
			}
	$smarty->assign("appt_type",$appointment_type);


 //----- location type:
 $location_type = $row["id_location_type"];
 //search string
 $sql_loc = "SELECT * FROM mgw_calendar_location_type WHERE id = $location_type";
 if(!$res_loc = $conn->Execute($sql_loc)) exit(showSQLerror($sql_loc, $conn->ErrorMsg(), __LINE__, __FILE__));
 $row_loc = $res_loc->FetchRow();
 $location_type =  Lang::getLanguageString("gentemp_".$row_loc["location_type"]);
 $smarty->assign("location_type",$location_type);

 //----- availability/telephone:
 $availability = $row["availability"];
	if($availability==1)
		{
		$availability = Lang::getLanguageString("gentemp_can_be_disturbed");
		$telephone = $row["telephone"];
		}
	else
		{
		$availability = Lang::getLanguageString("gentemp_can_not_be_disturbed");
		$telephone = "";
		}
 $smarty->assign("availability",$availability);
 $smarty->assign("telephone",$telephone);
////////////

$rep1 = date($_SESSION["MGW"]->settings["datefmt"], $res->UnixTimeStamp($row["rep_from"]));
$rep2 = date($_SESSION["MGW"]->settings["datefmt"], $res->UnixTimeStamp($row["rep_until"]));

if ($row["repeat_mode"] <= 0 || $row["repeat_mode"] > 4) {
    $smarty->assign("repeater", Lang::getLanguageString("gentemp_repeater_0"));
} else {
    $smarty->assign("repeater", Lang::getLanguageString("gentemp_repeater_".$row["repeat_mode"]).", ".Lang::getLanguageString("gentemp_from")." ".$rep1." ".Lang::getLanguageString("gentemp_to")." ".$rep2);
}

$private = array("id" 	=> $row["private"],
				 "text"	=> ($row["private"] == 0 ) ? Lang::getLanguageString("gentemp_no") : Lang::getLanguageString("gentemp_yes")
				);
/*
if($row["private"]==0) $private = Lang::getLanguageString("gentemp_no");
else $private = Lang::getLanguageString("gentemp_yes");
*/

$smarty->assign('from',date($timemask, $appointment_start));
$smarty->assign('to',date($timemask, $appointment_end));
$smarty->assign('subject_summ', str_decode($row["subject_summ"]));
$smarty->assign('subject', str_decode($row["subject"]));
$smarty->assign('location', str_decode($row["location"]));
$smarty->assign('private', $private);
$smarty->assign('inituser', $row["initname"]);
$smarty->assign('external', str_decode($row["external"]));
$smarty->assign('date', $date);

$smarty->assign('appointment_day', date_lang(Lang::getLanguageString("gentemp_date_format_php"), $appointment_start));

// Show Partipicants
$res->Move(0);
while($row = $res->FetchRow()){
    $smarty->append("partie", $row);
}

//rights for editting/deleting
//if ($row["dategroup_id"] == 0 && $_SESSION["MGW"]->rights["calendar"]->modify && ($row["inituserid"]==$_SESSION["MGW"]->userid || $_SESSION["MGW"]->rights["calendar"]->modify_others)) {
if ($row["dategroup_id"] == 0 && queryRights2_bool('root_modules_calendar_modify') && ($row["inituserid"]==$_SESSION["MGW"]->userid || queryRights2_bool('root_modules_calendar_modifyothers'))) {
    $edit_single_bt = "<input type=\"button\" onclick=\"location='index.php?action=editform&amp;id=".$id."&amp;".SID."'\" class=\"submit\" value=\"".Lang::getLanguageString("temp_row_action_label_edit")."\">";
} else {
    $edit_single_bt = "no";
}

//if ($row["dategroup_id"] > 0 && $_SESSION["MGW"]->rights["calendar"]->modify && ($row["inituserid"]==$_SESSION["MGW"]->userid || $_SESSION["MGW"]->rights["calendar"]->modify_others)) {
if ($row["dategroup_id"] > 0 && queryRights2_bool('root_modules_calendar_modify') && ($row["inituserid"]==$_SESSION["MGW"]->userid || queryRights2_bool('root_modules_calendar_modifyothers'))) {
    $edit_serial_bt = "<input type=\"button\" onclick=\"location='index.php?action=editserial&amp;ind=0&amp;id=".$id."&amp;".SID."'\" class=\"submit\" value=\"".Lang::getLanguageString("temp_row_action_label_serialedit")."\">";
} else {
    $edit_serial_bt = "no";
}

//if ($_SESSION["MGW"]->rights["calendar"]->delete && ($row["inituserid"]==$_SESSION["MGW"]->userid || $_SESSION["MGW"]->rights["calendar"]->delete_others)) {
 if (queryRights2_bool('root_modules_calendar_delete') && ($row["inituserid"]==$_SESSION["MGW"]->userid || queryRights2_bool('root_modules_calendar_deleteothers'))) {
    $delete_bt = "<input type=\"button\" onclick=\"location='index.php?action=del&amp;id=".$id."&amp;".SID."'\" class=\"submit\" value=\"".Lang::getLanguageString("temp_row_action_label_delete")."\">";
} else {
    $delete_bt = "no";
}

$smarty->assign("edit_single_bt", $edit_single_bt);
$smarty->assign("edit_serial_bt", $edit_serial_bt);
$smarty->assign("delete_bt", $delete_bt);
$smarty->assign("referer", $_SERVER["HTTP_REFERER"]);


$smarty->assign("temp_subject_summarized",Lang::getLanguageString("temp_subject_summarized"));
$smarty->assign("temp_subject_detailed",Lang::getLanguageString("temp_subject_detailed"));
$smarty->assign("temp_appt_type",Lang::getLanguageString("temp_appt_type"));
$smarty->assign("temp_availability",Lang::getLanguageString("temp_availability"));




$smarty->display('calendar_date_details.tpl');
?>
