<?php
/* $Id: collisions.php,v 1.25 2004/07/15 13:24:06 k-fish Exp $ */

$myEnv["module"] = "calendar";
$myEnv["output"] = "html";

header('P3P: CP="NOI ADM DEV PSAi COM NAV OUR OTRo STP IND DEM"'); //MSIE6 P3P header

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

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

// ---------------------------------------------
// making an array of participants ids
// ---------------------------------------------
$participants = explode(".",$_GET["parts"]); 
$participants[] = $_SESSION["MGW"]->userid;

// ---------------------------------------------
// position of uid in timesheet
// ---------------------------------------------
$uids = Array(); 
	
// ---------------------------------------------
// set date to actual timestamp if not set
// ---------------------------------------------
$datum = isset($_GET["datum"]) ? mktime(0,0,0,substr($_GET["datum"],2,2),substr($_GET["datum"],0,2),substr($_GET["datum"],4,4)) : time(); 
	
// ---------------------------------------------
// making where clausule for all participants
// ---------------------------------------------
$where 	= "";
$whereid 	= "";
for($i=0; $i<count($participants); $i++) {
	$where = $where." userid=".$participants[$i]." OR";
	$whereid = $whereid." id=".$participants[$i]." OR";
	$timesheet[$participants[$i]] = array(); //preparing array for future use
	$uids[$i] = $participants[$i];
}
$where = substr($where,0,-3);
$whereid = substr($whereid,0,-3);

// ---------------------------------------------
// getting calendar settings
// ---------------------------------------------
$intval = $_SESSION["MGW"]->settings["calendar_interval"];			// calender interval length in minutes
$min = $_SESSION["MGW"]->settings["calendar_viewfrom"];			// start hour of day-view
$max = $_SESSION["MGW"]->settings["calendar_viewto"];			// last  hour of day-view

// ------------------------------------------
// number of table columns
// ------------------------------------------
$num_of_cells = ($max-$min)/$intval*60;
$smarty->assign("num_of_cells", $num_of_cells+1);
$cols_grp = 60/$intval;
$smarty->assign("col_grp", $cols_grp);
	
// ---------------------------------------------
// creating empty table for selected Participants
// ---------------------------------------------
$sql = "SELECT lastname, firstname, id FROM mgw_users WHERE NOT(level=".UDELETED.") AND ".$whereid;
if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
	
for ($t=0; $t<$num_of_cells; $t++) {
	$cur_time = mktime($min, $t*$intval, 0, date("m", $datum), date("d", $datum), date("Y", $datum));
	$col_arr_hd[] = array(
		"time" => date("H:i", $cur_time),
		"hour" => date("H", $cur_time),
		"mins" => date("i", $cur_time)
	);
	$col_arr_std[date("H:i", $cur_time)] = 0;
} 
		
$ts_hd[] = array(
	"id"    => 0,
	"name"  => Lang::getLanguageString("gentemp_people_list"),
	"dates" => $col_arr_hd
);
                	  	
while ($row = $res->FetchRow()) {
	$timesheet[$row['id']][0] = $row['firstname']." ".$row['lastname'];
	$ts[] = array(
		"id"    => $row['id'],
		"name"  => $row['firstname']." ".$row['lastname'],
		"dates" => NULL
	);
} 

	
// ---------------------------------------------
// creating timetable for selected Participants
// ---------------------------------------------
$ndatum = Date("Ymd",$datum);
$sql = "SELECT date, a.userid, a.duration, b.firstname, b.lastname 
	FROM  mgw_users b LEFT OUTER JOIN mgw_calendar a ON a.userid = b.id
	WHERE (".$where.") 
	AND date>".$ndatum."000000 
	AND date<".$ndatum."235959 
	NOT(b.level=".UDELETED.") AND 
	ORDER BY a.userid, date";
	
if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
	
$old_user=0;
while ($row = $res->FetchRow()) {
	// ---------------------------------------------
	// initializing timetable for new participant
	// ---------------------------------------------
	if ($old_user != $row['userid']) {
		if ($old_user != 0) $col_arr_new[$old_user] =  $col_arr_loc;
		$col_arr_loc  = $col_arr_std;
	}	
			
	$cur_time_loc = $res->UnixTimeStamp($row['date']);
	$dur = ceil($row['duration']/$intval);
	$locmin = mktime(0,0,0, date("m", $datum), date("d", $datum), date("Y", $datum)) + $min * 3600;
	 		
	//echo $dur . " "; echo date("d.m.Y H:I", $cur_time_loc);
	// ---------------------------------------------
	// Marking appointment
	// ---------------------------------------------
	for ($i=0;$i<$dur;$i++){
		$tmp_time = mktime(date("H", $cur_time_loc), $i*$intval + date("i", $cur_time_loc), 0, date("m", $datum), date("d", $datum), date("Y", $datum)); 
		 		
		// ---------------------------------------------
		// Check, wether key exists in timetable
		// ---------------------------------------------
		
		if (!isset($col_arr_std[date("H:i", $tmp_time)])){
			// ---------------------------------------------
			// get greatest key smaller than appointment
			// ---------------------------------------------
			reset($col_arr_std);
			$key_arr = array_keys($col_arr_std);
			for ($i=1;$i<=sizeof($key_arr); $i++) {
				if ($key_arr[$i-1] <= date("H:i", $tmp_time) && $key_arr[$i]   > date("H:i", $tmp_time)) {
					$col_arr_loc[$key_arr[$i-1]] = 1;
				}
			}
		} else {
			$col_arr_loc[date("H:i", $tmp_time)] = 1;
		}
	}	
	$old_user = $row['userid']; 		 
}
	
if ($old_user > 0) {
	$col_arr_new[$old_user] =  $col_arr_loc;
}
	
// ---------------------------------------------
// changing format of timesheet entry
// ---------------------------------------------
while(list($key, $val) = each($ts)) { 
	if (isset($col_arr_new[$val["id"]])) {
		$tmp_cpl = NULL;
		while( list($nkey,$nval) = each($col_arr_new[$val["id"]])) {
			$tmp_cpl[] = array( "time" => $nkey, "value" => $nval);
		}  	
	    	
		$ts_ergeb[] = array(
			"id"    => $val["id"],
			"name"    => $val["name"],
			"dates"    => $tmp_cpl
		);
	} else {   
		$tmp_cpl2 = NULL; 
		reset($col_arr_std);
		while(list($nkey,$nval) = each($col_arr_std)) {
			$tmp_cpl2[] = array( "time" => $nkey, "value" => $nval);
		}    
		$ts_ergeb[] = array(
			"id"    => $val["id"],
			"name"    => $val["name"],
			"dates"    => $tmp_cpl2
		);                	                 
	}				            
}
	
$smarty->assign("rows", $ts_ergeb);
$smarty->assign("rowhead", $ts_hd);
$smarty->assign("datum", date($_SESSION["MGW"]->settings["datefmt"], $datum));

$smarty->assign("temp_title","titre");
$smarty->display("collision2.tpl");
?>