<?php
/* $Id: dayview.inc.php,v 1.22 2006/01/09 22:54:26 leyeti Exp $ */

if(function_exists("revisionInit")) revisionInit("\$Revision: 1.22 $", __FILE__);

	    $r = $t+$interval_sec;
	    $zindex = (($t-$start_time)/$interval_sec)-1;
	    $hits = 0;		// hits
	    $tr_m0 = 0;		// appointments in time slot:  case 1
	    $tr_m1 = 0;		// appointments in time slot:  case 2
	    $tr_10 = 0;		// appointments in time slot:  case 3
	    $tr_11 = 0;		// appointments in time slot:  case 4
	    $tr_lfd = 0;	// appointments in time slot:  case 5

	    // reset result handler
	    $n=0;
			
	    //-------------------------------------------------------------
	    // loop through result
	    //-------------------------------------------------------------
	    $res->Move(0);
	    while ($row = $res->FetchRow()) {

		// compute start/end of appointment in unix-timestamp
		$vmin = $res->UnixTimeStamp($row["date"]);
		$bmin = $vmin + $row["duration"] * 60;
//print_r($row);echo "<br>rowdate= ".$row["date"]." ,vmin = $vmin / ".$res->UnixTimeStamp("20031023080000")/*$res->UnixTimeStamp("2003-10-23 08:00:00")*/."<br>";

		//---- NATYS : 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();
			$appointment_type = array( 	"text" => Lang::getLanguageString("gentemp_".$row_appt_type["type"]),
										"color"=> $row_appt_type["color"]
									 );
		
		//----- location type
			$id_loc = $row["id_location_type"];
			$sql_loc = "SELECT * FROM mgw_calendar_location_type WHERE id=$id_loc";
			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"]);
	
		//-------------
		
		//----- Participants
			
			$sql_part = "SELECT ".$conn->Concat("firstname", "' '", "lastname")." AS name FROM mgw_calendar a, mgw_users b WHERE a.id = ".$row["id"]." AND NOT(b.level=".UDELETED.")  AND a.id_class!='3' AND a.userid = b.id";
			//"SELECT * FROM mgw_calendar WHERE id=".$row["id"];
			if(!$res_part = $conn->Execute($sql_part)) exit(showSQLerror($sql_part, $conn->ErrorMsg(), __LINE__, __FILE__));
			if($n = $res_part->RecordCount()>1)
			{
				$parties = "";
				while($row_part = $res_part->FetchRow()){
					$parties .= $row_part["name"]."<br>";
				}
			}
			else $parties = "";	
		//-------------
			
		//--------------- topic type and color depend on appointment class :
			$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();
				$topic_type = $appointment_type["text"]."&nbsp;:<BR>".Lang::getLanguageString("class_".$row_class["class"]);
				$topic_color = $row_class["color"];
				
				
			}
			else {
				$topic_type = $appointment_type["text"];
				$topic_color = $appointment_type["color"];
			}		
		//-----------------

		//global:
		
		$display_vCal = FALSE;
		$vCallink = "<td><a href=\"index.php?action=vCal&id=".$row["id"]."&".SID."\" class=\"edit\">".$icons["vcal"]."</a></td>";
		//--------
		
		
		//more than one appointment in timeslot 
		// FOR THE MOMENT , appointment_count not used (always = 1)
		//multiple appointments on same slot will be supported later
		if ($row["appointment_count"] > 1 && $vmin >= $t && $vmin < $r) {

		    //is not private (case 1)
		    if ($row["private"] == 0) {
			$sellink  = "<a href=\"index.php?action=sel&calendarid=".$_SESSION['calendarid']."&date=$vmin&ind=1&".SID."\" class=\"edit\">". Lang::getLanguageString("temp_row_label_2") ."</a>";
			$zeile_m0 = array( "start" => "<a class=\"edit\" href=\"index.php?action=new1&date=$t&".SID."\">"."<strong>".date($timemask, $t)   ."</strong>"."</a>",
					   "to"	   => "<a class=\"edit\" href=\"index.php?action=new2&date=$t&".SID."\">"."<strong>".date($timemask, $bmin)."</strong>"."</a>",
					   "topic" => Lang::getLanguageString("temp_row_label_1"),
					   "class" => $rowclass,
					   "location" => "",
					   "rowactions" => $sellink);
			$tr_m0=1;
		    }
		    //is private and is mine (case 2)
		    else if ($row["private"] == 1 && $_SESSION["MGW"]->userid == $_SESSION['calendarid']) {
			$sellink  = "<a href=\"index.php?action=sel&date=$vmin&ind=1&".SID."\" class=\"select\">".Lang::getLanguageString("temp_row_label_2") ."</a>";
			$zeile_m1 = array( "start" 	=> "<strong>".date($timemask, $t)."</strong>",
					   "to" => "<strong>".date($timemask, $bmin)."</strong>",
					   "topic" => Lang::getLanguageString("temp_row_label_1"),
					   "class" => $rowclass,
					   "location" => "",
					   "rowactions" => $sellink);
			$tr_m1=1;
		    }
		} // end more than one appointment

		//******//

		//there is only one appointment in current timeslot
		else if ($row["appointment_count"] == 1 && $vmin >= $t && $vmin < $r) {
		    $myid = $row["id"];

		    //is not private and calendar is mine (case 3)
		    if ($row["private"] == 0 && $_SESSION['calendarid'] == $_SESSION["MGW"]->userid) {
			$detlink  = "<td> <a href=\"details.php?id=$myid&userid=".$_SESSION["MGW"]->userid."&".SID."\" class=\"edit\">". $icons["details"] . "</a> </td>";   
			
			if($id_class==0){ //appointment subjected
				//"validate" button :
				$validlink = "<td> <a href=\"javascript:void(0);\" onclick=\"javascript:if(confirm('".Lang::getLanguageString("query_validate")."')){ window.location.href='index.php?action=validate&id=$myid&date=$vmin&".SID."';}\" class=\"edit\">".$icons["validate"]."</a> </td>";
				//NO "edit" button :
				$editlink = "";
				}
			else { // appointment on standby or validated or aborted
				//NO "validate" button
				$validlink ="";
				//"edit" button :
				if (is_modifiable_by_current_user($myid,$_SESSION['calendarid'])) {
					$editlink = "<td> <a href=\"index.php?action=editform&id=$myid&date=$vmin&".SID."\" class=\"edit\">". $icons["edit"] . "</a> </td>";	    
				}
				else $editlink ="";
			}
						
			
			if($id_class==1 || $id_class==0){  // appointment on standby or subjected
				//abort appointment
				$abortlink  = "<td><a href=\"javascript:void(0);\" onclick=\"javascript:if(confirm('".Lang::getLanguageString("query_abort")."')){ window.location.href='index.php?action=abort&id=$myid&".SID."';}\" class=\"delete\">" . $icons["abort"] . "</a></td>";
				//NO delete button:
				$dellink = "";
			}
			else {  // appointment validated or aborted
				//NO abort button:
				$abortlink = "";
				//delete appointment
				if (is_modifiable_by_current_user($myid,$_SESSION['calendarid'])) {
					$dellink  = "<td><a href=\"index.php?action=del&id=$myid&".SID."\" class=\"delete\">" . $icons["delete"] . "</a></td>";
				}
				else $dellink ="";
			}
			
			//if ($row["dategroup_id"] > 0 && ($row["inituserid"]==$_SESSION["MGW"]->userid || $_SESSION["MGW"]->rights["calendar"]->modify_others)) {
			 if ($row["dategroup_id"] > 0 && ($row["inituserid"]==$_SESSION["MGW"]->userid || queryRights2_bool('root_modules_calendar_modifyothers'))) {
			    $tseditlink = "<td><a class=\"edit\" href=\"index.php?action=editserial&ind=0&id=$myid&".SID."\">". $icons["editserial"] /*. Lang::getLanguageString("temp_row_action_label_serialedit") */. "</a></td>";
			} else {
			    $tseditlink = "";
			}
		
			$zeile_10 = array("start" => "<a class=\"edit\" href=\"index.php?action=new1&date=$t&".SID."\">".date($timemask, $t)."</a>",
					  "to" => "<a class=\"edit\" href=\"index.php?action=new2&date=$t&".SID."\">"."<strong>".date($timemask, $bmin)."</strong>"."</a>",
					  "topic" => str_decode($row["subject_summ"]),
					  "topic_type" => $topic_type,
					  "topic_color" => $topic_color,
					  "availability" => $row["availability"],
					  "telephone" => str_replace("\"","''",$row["telephone"]),
					  "parties" => $parties,
					  "class"	=> $rowclass,
					  "location" => str_decode($row["location"]),
					  "location_type" => $location_type.(str_decode($row["location"]) != "" ? "&nbsp;:" : ""),
					  //"rowactions" => $detlink . $icons["space"] . $editlink . $icons["space"] . $dellink . $icons["space"] . $tseditlink . $icons["space"] . $vCallink );
					  "rowactions" => "<table border=\"0\" cellspacing=\"10\" cellpadding=\"0\"><tr>". $detlink . $validlink . $editlink . $abortlink . $dellink . $tseditlink . ($display_vCal ? $vCallink : "") . "</tr></table>");
			$tr_10=1;
			}

		    //is not private and calendar is not mine (case 3)
		    else if ($row["private"] == 0 && $_SESSION['calendarid'] != $_SESSION["MGW"]->userid) {

			//&& ($_SESSION["MGW"]->userid == $row["inituserid"])
			
			//----------------------
			if($id_class==0 && is_modifiable_by_current_user($myid,$_SESSION['calendarid'])){ //appointment subjected
				//"validate" button :
				$validlink = "<td> <a href=\"javascript:void(0);\" onclick=\"javascript:if(confirm('".Lang::getLanguageString("query_validate")."')){ window.location.href='index.php?action=validate&id=$myid&date=$vmin&".SID."';}\" class=\"edit\">".$icons["validate"]."VALIDATE</a> </td>";
				//NO "edit" button :
				$editlink = "";
				}
			else { // appointment on standby or validated or aborted
				//NO "validate" button
				$validlink ="";
				//"edit" button :
				if (is_modifiable_by_current_user($myid,$_SESSION['calendarid'])) { //if(queryRights2_bool('root_modules_calendar_modifyothers')) {
					$editlink = "<td> <a href=\"index.php?action=editform&id=$myid&date=$vmin&".SID."\" class=\"edit\">". $icons["edit"] . "</a> </td>";	    
				}
				else $editlink ="";
			}
						
			
			if(($id_class==1 || $id_class==0) && is_modifiable_by_current_user($myid,$_SESSION['calendarid'])){  // appointment on standby subjected
				//abort appointment
				$abortlink  = "<td><a href=\"javascript:void(0);\" onclick=\"javascript:if(confirm('".Lang::getLanguageString("query_abort")."')){ window.location.href='index.php?action=abort&id=$myid&".SID."';}\" class=\"delete\">" . $icons["delete"] . "ABORT</a></td>";
				//NO delete button:
				$dellink = "";
			}
			else {  // appointment validated or aborted
				//NO abort button:
				$abortlink = "";
				//delete appointment
				if (is_modifiable_by_current_user($myid,$_SESSION['calendarid'])) { //if(queryRights2_bool('root_modules_calendar_deleteothers')) {
					$dellink  = "<td><a href=\"index.php?action=del&id=$myid&".SID."\" class=\"delete\">" . $icons["delete"] . "</a></td>";
				}
				else $dellink ="";
			}
						
			//----------------------
			if (is_displayable_by_current_user($myid,$_SESSION['calendarid'])) {
				$detlink  = "<td><a href=\"details.php?id=$myid&userid=".$_SESSION["calendarid"]."&".SID."\" class=\"edit\">". $icons["details"] . "</a></td>";
			}
			else $detlink = "";

			//if ($editlink!="" && $row["dategroup_id"] > 0 && ($row["inituserid"]==$_SESSION["MGW"]->userid || $_SESSION["MGW"]->rights["calendar"]->modify_others)) {
			if ($editlink!="" && $row["dategroup_id"] > 0 && ($row["inituserid"]==$_SESSION["MGW"]->userid || queryRights2_bool('root_modules_calendar_modifyothers'))) {
			    $tseditlink = "<td><a class=\"edit\" href=\"index.php?action=editserial&ind=0&id=$myid&".SID."\">". Lang::getLanguageString("temp_row_action_label_serialedit") . "</a></td>";
			} 
			else $tseditlink = "";
			
			
/*		
			if (is_modifiable_by_current_user($myid)) {
			 	$editlink = "<td><a href=\"".$_SERVER['PHP_SELF']."?action=editform&id=$myid&".SID."\" class=\"edit\">". $icons["edit"] . "</a></td>";
				$dellink  = "<td><a href=\"".$_SERVER['PHP_SELF']."?action=del&id=$myid&".SID."\" class=\"delete\">" . $icons["delete"] . "</a></td>";
				$tseditlink = "<td><a class=\"edit\" href=\"".$_SERVER['PHP_SELF']."?action=editserial&ind=0&id=$myid&".SID."\">". Lang::getLanguageString("temp_row_action_label_serialedit") . "</a></td>";
			}
			else $editlink = $dellink = $tseditlink = "";
*/			
			/*
			if($_SESSION["MGW"]->rights["calendar"]->modify_others) {
			    $editlink = "<td><a href=\"".$_SERVER['PHP_SELF']."?action=editform&id=$myid&".SID."\" class=\"edit\">". $icons["edit"] . "</a></td>";
			} else {
			    $editlink = "";
			}
			if($_SESSION["MGW"]->rights["calendar"]->delete_others) {
			    $dellink  = "<td><a href=\"".$_SERVER['PHP_SELF']."?action=del&id=$myid&".SID."\" class=\"delete\">" . $icons["delete"] . "</a></td>";
			} else {
			    $dellink = "";
			}
			if ($row["dategroup_id"] > 0 && ($row["inituserid"]==$_SESSION["MGW"]->userid || $_SESSION["MGW"]->rights["calendar"]->modify_others)) {
			    $tseditlink = "<td><a class=\"edit\" href=\"".$_SERVER['PHP_SELF']."?action=editserial&ind=0&id=$myid&".SID."\">". Lang::getLanguageString("temp_row_action_label_serialedit") . "</a></td>";
			} else {
			    $tseditlink = "";
			}
			*/
			
			
			$zeile_10 = array(  "start" 	=> "<strong>".date( $timemask, $t)."</strong>",
					    "to" => "<strong>".date($timemask, $bmin)."</strong>",
							"inituser" => getRealUsername($row["inituserid"]),
							"topic" => short_string(str_decode($row["subject_summ"]),$max_subject_length),
					    "topic_type" => $topic_type,
					  "topic_color" => $topic_color,
					  "availability" => $row["availability"],
					  "telephone" => $row["telephone"],
						"parties" => $parties,
						"class" => $rowclass,
					    "location"	=> str_decode($row["location"]),
					    "location_type" => $location_type.(str_decode($row["location"]) != "" ? "&nbsp;:" : ""),
						"rowactions" => "<table border=\"0\" cellspacing=\"10\" cellpadding=\"0\"><tr>". $detlink . $editlink . $dellink . $tseditlink . "<tr></table>");
			$tr_10=1;
		    }

		    //is private and is mine (case 4)
		    else if ($row["private"] == 1 && $_SESSION['calendarid'] == $_SESSION["MGW"]->userid) {
			$detlink  = "<a href=\"details.php?id=$myid&userid=".$_SESSION["MGW"]->userid."&".SID."\" class=\"edit\">". $icons["details"] . "</a>";
			$editlink = "<a href=\"index.php?action=editform&id=$myid&".SID."\" class=\"edit\">". $icons["edit"] . "</a>";
			$dellink  = "<a href=\"index.php?action=del&id=$myid&".SID."\" class=\"delete\">" . $icons["delete"] . "</a>";
			//if ($row["dategroup_id"] > 0 && ($row["inituserid"]==$_SESSION["MGW"]->userid || $_SESSION["MGW"]->rights["calendar"]->modify_others)) {
			if ($row["dategroup_id"] > 0 && ($row["inituserid"]==$_SESSION["MGW"]->userid || queryRights2_bool('root_modules_calendar_modifyothers'))) {
			    $tseditlink = "<a class=\"edit\" href=\"index.php?action=editserial&ind=1&id=$myid&".SID."\">". Lang::getLanguageString("temp_row_action_label_serialedit") . "</a>";
			} else {
			    $tseditlink = "";
			}
			//$vCallink = "<a href=\"".$_SERVER['PHP_SELF']."?action=vCal&id=$myid&".SID."\" class=\"edit\">vCal</a>";

			$zeile_11 = array(  "start" => "<a class=\"edit\" href=\"index.php?action=new1&date=$t&".SID."\">"."<strong>".date($timemask, $t)."</strong>"."</a>",
					    "to" => "<a class=\"edit\" href=\"index.php?action=new2&date=$t&".SID."\">"."<strong>".date($timemask, $bmin)."</strong>"."</a>",
					    "topic" => str_decode($row["subject_summ"]),
					    "topic_type" => $topic_type,
						"topic_color" => $topic_color,
						"private"	=>	$row["private"],
					  "availability" => $row["availability"],
					  "telephone" => $row["telephone"],
						"parties" => $parties,
						"class"	=> $rowclass,
					    "location"	=> str_decode($row["location"]),
					    "location_type" => $location_type.(str_decode($row["location"]) != "" ? "&nbsp;:" : ""),
						"rowactions" => "<table border=\"0\" cellspacing=\"10\" cellpadding=\"0\"><tr><td>". $detlink . "</td><td>" /*$icons["space"]*/ . $editlink . "</td><td>" /*$icons["space"]*/ . $dellink . "</td><td>" /*$icons["space"]*/ . $tseditlink . "</td><td>". ($display_vCal ? "<td>" . $vCallink . "</td>" : "") . "<tr></table>");
			$tr_11=1;
		    }

		    //is private and is not mine (case 4)
		    else if ($row["private"] == 1 && $_SESSION['calendarid'] != $_SESSION["MGW"]->userid) {
			$zeile_11 = array(   "start" => "<strong>".date($timemask, $t)."</strong>",
					     "to" => "<strong>".date($timemask, $bmin)."</strong>",
					     "topic" => "", //Lang::getLanguageString("temp_row_action_label_private"),
					     "topic_type" => $topic_type,
						"topic_color" => $topic_color,
						 "private"	=>	$row["private"],
						 "class" => $rowclass,
					     "location" => "",
					     "rowactions" => "");
			$tr_11=1;
		    }
   		}
		else if ($t > $vmin && $t < $bmin) {
		    $zeile_lfd = array("start" => "&nbsp;",
				       "to" => "&nbsp;",
				       "topic" => "...",
 					   "topic_color" => $topic_color,
					   "class" => $rowclass,
				       "location" => "",
				       "rowactions" => "");
		    $tr_lfd=1;
		}
		elseif($t>$end_time) $tr_lfd=0;

	    }
?>
