Location: PHPKode > projects > Dogfood-A Complete Groupware/CRM System > dogfood/calendar/functions00.inc
<?php
if(isset($spers)) $spers = stripslashes($spers);
if($gru) {
	if($mode <= 4) $mode += 4; else if(isset($spers)) $pers = unserialize($spers);
	if($pers[0]) $mode += 4;
	if(isset($gruev)) $gru = $gruev; if($gru) $mode = 5;
	if(isset($persev)) $pers = $persev; if($pers[0]) $mode = 5;
}

function checkanltermin($an) {
	// check whether other events on that day collide
	global $event_ID, $datum, $anfang, $ende, $m1_text1, $DB, $sql_session;
	$result = $sql_session->query("select nachname, vorname, anfang, ende from ".$DB['core'].".users, ".$DB['current'].".termine where users.ID = an and datum = '$datum' AND an='$an' AND anfang <> '----' AND ende <> '----' AND termine.ID <> '$event_ID' AND '$anfang' < ende AND '$ende' > anfang ");
	while($row = db_fetch_row($result)) {
		echo "<tr><td>$m1_text1 $datum, $row[2] - $row[3], $row[0] ".substr($row[1],0,1).".<br>";
		if(!$err) $err=1;
	}
	return $err;
}

function checkanl() {
	global $user_ID, $gruev, $persev, $back, $datum,$anfang,$ende, $ressource,$m1_text1,$m1_text2,$note,$an,$event,$events_par,$event_ID, $DB;
	$errchk=0;
	if($gruev ||(isset($persev) and $persev[0])) {
		if($gruev) {
			$result = db_query("select personen from ".$DB['core'].".profile where ID ='$gruev'");
			$row = db_fetch_row($result);
			$persev = unserialize($row[0]);
		}

		for($a=0; $a < count($persev); $a++) {
			$result = db_query("select ID from ".$DB['core'].".users where kurz ='$persev[$a]'");
			// not in case of anonymous booking
			while($row = db_fetch_row($result)) {if($note <> "99" and !$events_par) {$errchk |= checkanltermin($row[0]); }}
		}
	} else if($note <> "99" and !$events_par) $errchk |= checkanltermin($user_ID);
	
	if($ressource) {
		$a = $anfang; $e = $ende;
		$a == "----"  ?"0000" : $a;
		$e == "----" ? "2400" : $e;
		$result = $sql_session->query("select nachname, vorname, anfang, ende from ".$DB['core'].".users, ".$DB['current'].".termine where users.ID = an AND ressource='$ressource' AND datum = '$datum' AND('$a' < ende AND '$e' > anfang) AND termine.ID <> '$event_ID'");
		while($row = db_fetch_row($result)) {
			echo "<tr><td>$m1_text2 $row[2] - $row[3], $row[0] ".substr($row[1],0,1).".<br>";
			if(!$errchk) $errchk=1;
		}
	}
	if($errchk) system_panic();
}

function anlegendoit($an, $von, $first) {
	global $datum, $event, $note, $anfang, $ende, $project, $ressource, $serie, $m1_text39, $m1_text40, $sms_remind,$smslist, $user_email, $l_text13, $l_text14, $l_text15, $l_text16, $l_text17, $version, $dbIDnull, $dbTSnull, $mail_new_event, $contact, $remark, $visi, $sql_session, $DB;
	
	$result=$sql_session->query("INSERT INTO ".$DB['current'].".termine values($dbIDnull,'$von','$an','$event','$note','$project','$dbTSnull','$datum','$anfang','$ende','$ressource',null,'$contact','$remark',null,null,'$sms_remind','$visi')");

	if($first == 1 and $an <> $von and $mail_new_event) {
		if(!$serie) $ts = $l_text13; else if($serie=="tag") $ts = $l_text14; else if($serie=="woche") $ts = $l_text15; else if($serie=="monat") $ts = $l_text16; else if($serie=="jahr") $ts = $l_text17;

		$v="";
		$res = db_query("select vorname, nachname from ".$DB['core'].".users where ID = '$von'");
		if($r = db_fetch_row($res)) $v = "$r[0] $r[1]";
		if(!$v == "") $v = $von;
		$tn = "";
		if($note) $tn = $tn." ,'$note'"; else if($event) $tn = $tn." ,'$event'";
		$sub=$m1_text39.": ".$datum.", ".$anfang." - ".$ende;
		$msg=$m1_text39.": ".$datum.", ".$anfang." - ".$ende."(".$ts.")".$tn."\n".$m1_text40.": ".$v."\n<".$user_email.">\n\nDogFood ".$version;
		$res = db_query("select email from ".$DB['core'].".users where ID = '$an' and email <> ''");
		if($r = db_fetch_row($res)) mail($r[0], $sub, $msg, "From: $user_email \nReply-To: $user_email");
	}
}

function anlegen($first) {
	global $DB, $user_ID, $gruev, $persev, $event, $note, $contact, $project, $ressource, $remark;
	if($gruev||$persev[0]) {
		if($gruev) {
			$result = db_query("select personen from ".$DB['core'].".profile where ID ='$gruev'");
			$row = db_fetch_row($result);
			$persev = unserialize($row[0]);
		}
		
		for($a=0; $a < count($persev); $a++) {
			$result = db_query("select ID from ".$DB['core'].".users where kurz ='$persev[$a]'");
			while($row = db_fetch_row($result)) anlegendoit($row[0], $user_ID, $first);
		}
	} else {
		if( $note == "99") $user_ID2 = $event; else $user_ID2 = $user_ID;
		anlegendoit($user_ID2, $user_ID2, $first);
	}
}

function loeschendoit($an, $showerr) {
	global $DB, $event_ID, $datum, $day, $month, $year, $anfang, $ende, $m1_text3, $back, $note, $ressource, $user_ID;

	$delok=1;
	// if a event_ID is given, look for this entry, fetch values
	if($event_ID) {
		// check permission
		$result = db_query("select an, von, datum FROM ".$DB['current'].".termine where ID='$event_ID'");
		$row = db_fetch_row($result);
		if(!$row[1]) system_panic("no entry found."); else if($row[0]!=$user_ID&&$row[1]!=$user_ID) system_panic("you are not allowed to do this!");

		$datum = $row[2];
		list($year, $month, $day) = explode("-",$datum);
		// delete record from db
		$result = db_query("delete from ".$DB['current'].".termine where ID='$event_ID'");
	} else {
		// check existence of third party booking event before deleting
		if(isset($ressource) and $note == "99") $result = $sql_session->query("select * from ".$DB['current'].".termine where note='99' AND ressource='$ressource' AND datum='$datum' AND anfang='$anfang' AND ende='$ende'"); else $result = $sql_session->query("select * FROM ".$DB['current'].".termine where an='$an' AND von='$user_ID' AND datum='$datum' AND anfang='$anfang' AND ende='$ende'");

		// if there is no entry -> show error message and turn off the light.
		if(db_fetch_row($result) == 0) {
			if($showerr) system_panic("<tr><td>$datum, $anfang - $ende: $m1_text3");
			$delok=0;
		} else {
			// delete third party booking
			if(isset($ressource)&&$note == "99") $result = db_query("delete from ".$DB['current'].".termine where note='99' AND ressource='$ressource' AND datum='$datum' AND anfang='$anfang' AND ende='$ende'"); else $result = db_query("delete FROM ".$DB['current'].".termine where an='$an' AND von='$user_ID' AND datum='$datum' AND anfang='$anfang' AND ende='$ende'");
		}
	}
	
	return $delok;
}

function loeschen() {
	global $DB, $user_ID, $gruev, $persev, $event_ID, $datum, $anfang, $ende, $m1_text3, $mode, $make;
	$delok=0;
	// check for goup events
	if(!$event_ID and($gruev or(isset($persev) && $persev[0]))) {
		if($gruev) {
		$result = db_query("select personen from ".$DB['core'].".profile where ID ='$gruev'");
		$row = db_fetch_row($result);
		$persev = unserialize($row[0]);
	}
	for($a=0; $a < count($persev); $a++) {
		$result = db_query("select ID from ".$DB['core'].".users where kurz ='$persev[$a]'");
		while($row = db_fetch_row($result)) $delok |= loeschendoit($row[0], 0);
	}
	if( $delok == 0) system_panic("<tr><td>$datum, $anfang - $ende: $m1_text3"); 
	} else loeschendoit($user_ID, 1);
}

function update() {
	global $DB, $user_ID, $event_ID, $day, $month, $year, $anfang, $ende, $event, $note, $remark, $contact, $project, $ressource, $errchk, $events_par, $visi, $sms_remind, $smslist, $an;

	// check permission
	$result = db_query("select von, an from ".$DB['current'].".termine where ID = '$event_ID'");
	$row = db_fetch_row($result);
	if($row[0] == 0) system_panic("no entry found.");
	if($row[0] <> $user_ID) system_panic("you are not allowed to do this!");
	$an = $row[1];
	$errchk = checkanl();
	// update record
	$datum = $year."-".$month."-".$day;
	$result = db_query("UPDATE ".$DB['current'].".termine set event='$event', note='$note', anfang='$anfang', ende='$ende', datum='$datum', contact='$contact', projekt='$project', ressource='$ressource', note2='$remark', visi='$visi', remind='$sms_remind' where ID = '$event_ID'");
}

//START NO FUNC
if($make=="create" or $make=="delete" or $make=="update") {
	// proof date format, if it is a day evetn without time,. then delete it via the list
	if($make == "delete" && isset($anfang) && $anfang == "" && isset($ende) && $ende == "") {
		$anfang = "";
		$ende = "";
		$make = "";
		echo "$m1_text41";
	}
	if(!($make == "delete" && $event_ID)) {
		if($make != "delete"&&!$event&&!$note) $event="Untitled";
		if((!$anfang or $anfang== "----")&&(!$ende or $ende == "----")) {
			$anfang = '----';
			$ende = '----';
			$sms_remind = "";
		} else {
			//check wether sms or email reminder
			if(!ereg("(^[0-9]*$)",$sms_remind)) { die("<tr><td>$m1_text4");}
			if($smslist=="---" or $smslist=="" or $sms_remind =="") $sms_remind = ""; else {
			if($smslist=="sms") {$sms_remind .= "1";}
			if($smslist=="email") { $sms_remind .="2";}
		}
			$anfang = ereg_replace("[.:,;/]","",$anfang);
			$ende = ereg_replace("[.:,;/]","",$ende);
			if(!ereg("(^[0-9]*$)",$day) or !ereg("(^[0-9]*$)",$anfang) or !ereg("(^[0-9]*$)",$ende)) { die("<tr><td>$m1_text4"); }
			if(!checkdate($month,$day,$year)) { die("<tr><td>$m1_text5"); }
			if($anfang <($tagesanfang*100) OR $anfang >($tagesende*100)) { die("<tr><td>$m1_text6"); }
			if(  $ende <($tagesanfang*100) OR   $ende >($tagesende*100)) { die("<tr><td>$m1_text7"); }
			// begin and end must have 4 digits ...
			$anfang = substr("0000".$anfang,-4);
			$ende = substr("0000".$ende,-4);
			if($anfang && $ende <= $anfang) { die("<tr><td>$m1_text9"); }
		}
	}

	// single event
	if($serie == "") {
		if    ($make == "create") {checkanl(); anlegen(1); }
		elseif($make == "delete") { loeschen();}
		elseif($make == "update") { update();}
		if($mode == 3) $make ="termin";
	} else {
		// check whether end date is valid
		if(!ereg("(^[0-9]*$)",$endday) or !ereg("(^[0-9]*$)",$endmonth) or !ereg("(^[0-9]*$)",$endyear)) { die("<tr><td>$m1_text10"); }
		if(!checkdate($endmonth, $endday, $endyear)) { die("<tr><td>$m1_text11"); }
		$day1=$day;$month1=$month;$year1=$year;  //save actual date
		$first=1;
		while( mktime(0,0,0,$month,$day,$year) <= mktime(0,0,0,$endmonth,$endday,$endyear) ) {
			$datum = "$year-$month-$day";
			if ($make == "create") {
				checkanl();
				anlegen($first);
			} else if($make=="delete") loeschen(); else if($make=="update") update();
			$first=0;
			if($serie == "tag")  {
				if($day == date("t", mktime(0,0,0,($month+1),0,$year))) {
					if($month==12) {
						$day = 1;
						$month = 1;
						$year++;
					} else {
						$day = 1;
						$month++;
					}
				} else $day++;
			}
			if($serie == "woche") {
				if($day >(date("t", mktime(0,0,0,($month+1),0,$year)) - 7)) {
					$day = 7 -( date("t", mktime(0,0,0,($month+1),0,$year)) - $day );
					if($month == 12) {
						$month=1;
						$year++;
					} else $month++;
				} else $day = $day + 7;
			}
			if($serie == "monat"){
				if($month===12) {
					$month=1;
					$year++;
				} else $month++;
			}
			if($serie == "jahr") $year++;
		 }
		if($mode == 3) $make ="termin";
	}
}
//END NO FUNC

// view single day
function text4row($isRess, $row) {
	global $DB, $m1_text40, $ressourcen, $user_ID, $not_self;
	if(isset($isRess) && $isRess) {
		$res = db_query("select vorname,nachname,kurz from ".$DB['core'].".users where ID = '$row[2]'");
		$v = "";
		if($r = db_fetch_row($res)) { $v = "$r[0] $r[1]"; }
		if($v == "") { $v = $row[1]; }
		$text = html_out($v);
	} else {
		$r= db_fetch_row(db_query("select kurz from ".$DB['core'].".users where ID = '$row[2]'"));
		$text=$row[3]."<br> &nbsp; -&nbsp;".$r[0];
		if($row[4] and $row[2] == $user_ID) {
			 $row[4] = html_out($row[4]);
			 $text = "<img src=img/b.gif alt='$row[4]' title='$row[4]' width=7 border=0>&nbsp;".$text;
		}
		if( $row[1] && $row[1] <> $row[2] ) {
			$res = db_query("select vorname, nachname from ".$DB['core'].".users where ID = '$row[1]'");
			$v = "";
			if($r = db_fetch_row($res)) { $v = "$r[0] $r[1]"; }
			if($v == "") { $v = $row[1]; }
			$text = "<img src=img/c.gif alt='$m1_text40: $v' title='$m1_text40: $v' width=7 border=0>&nbsp;".$text;
		}
		if($ressourcen) {
			if($row[10]) {
				$result2 = db_query("select name from ".$DB['current'].".ressourcen where ID = '$row[10]'");
				while($row2 = db_fetch_row($result2)) {$ress2 = $row2[0]; }
				$text = "<img src=img/g.gif alt='$ress2' title='$ress2' width=7 border=0>&nbsp;".$text;
			}
		}
	}
	return $text;
}

// show header for list
function makesort($width, $key, $name) {
	global $DB, $make, $up2, $page, $perpage, $keyword, $filter, $no_r, $all;
	return "<td width=$width%><b><a href='".href("index.php?sys_module=calendar&mode=3&make=$make&sort=$key&up=$up2&page=$page&perpage=$perpage&keyword=$keyword&filter=$filter&no_r=$no_r&all=$all")."'>$name</a></b></td>\n";
}
						

// this function handles the list of events
function listview($make, $year, $month, $day) {
	global $DB, $m1_text1a, $m1_text12, $m1_text13, $m1_text14, $m1_text15, $m1_text16, $m1_text17, $m1_text37, $rts_21, $rts_22, $l_text9, $admin_text71, $m1_text36, $rts_23, $all, $up, $sort, $page, $perpage, $filter, $keyword, $previous, $next, $items, $m1_text18, $m1_text19, $no_r, $user_ID;

	// set defaults
	$datum = date("Y-m-d");
	if(!isset($up))$up = 0;
	if(!$sort) { $sort = "datum ASC ,anfang"; $up = 1; }
	$where = $all?"":" and datum >= '$datum'";
	// sort & direction
	if($up == "1") { $direction = "ASC"; $up2 = 0; } else { $direction="DESC"; $up2 = 1; }

	// display title
	 echo "<table width=100%><tr><td>\n";
	if($make == "ress") echo "<h3>$m1_text1a</h3>"; else echo "<h3>$m1_text12</h3> ";

	echo "</td><td><a href='".href("../index.php?year=$year&month=$month&day=$day&mode=3&make=$make&all=$all&gru=$gru&spers=$spers&axis=$axis&view=$view&dist=$dist&no_r=".($no_r?"0":"1")."&isRess=$isRess&u_r_ID=$u_r_ID")."'><img src=img/arrow".($no_r?"left":"right").".gif align=right border=0></a></td></tr></table>";

	echo "<table border=1 cellspacing=0 cellpadding=3>";

	// filter & items per page
	echo "<tr><form action='".href("index.php?sys_module=calendar")."' method=post>\n";
	echo "<input type=hidden name='up' value='$up'>\n";
	echo "<input type=hidden name='sort' value='$sort'>\n";
	echo "<input type=hidden name='page' value='$page'>\n";
	echo "<input type=hidden name='perpage' value='$perpage'>\n";
	echo "<input type=hidden name='filter' value='$filter'>\n";
	echo "<input type=hidden name='keyword' value='$keyword'>\n";
	echo "<input type=hidden name='mode' value='3'>\n";
	echo "<input type=hidden name='make' value='$make'>\n";
	echo "<input type=hidden name='no_r' value='$no_r'>\n";

	// filter options only for event view
	if($make <> "ress") {
		echo "<td>$rts_21:&nbsp;".($no_r?"":"<br>")."<input type=text size=15 name='keyword' value='$keyword'></td>\n";
		echo "<td>$rts_22&nbsp;".($no_r?"":"<br>")."\n";
		echo "<select name='filter'>\n";
		echo "<option value='event'>$l_text9\n";
		echo "<option value='note'>Note\n";
		echo "<option value='note2'>$admin_text71\n";
		echo "</select></td>\n";
	}
	// set default per page
	if(!$perpage) { $perpage = 30; }
	echo "<td> $items:".($no_r?"":"<br>")."<select name='perpage'>\n";
	for($i = 1; $i <= 5; $i++) {
		$j = $i * 10;
		echo "<option value='$j'";
		if($j == $perpage) { echo " selected"; }
		echo ">$j\n";
	}
	echo "</select></td>\n";
	echo "<td><input type=\"image\" class=\"image\" src='img/los.gif' border=0 id=tr></td></tr><tr>\n";
	// end first line of the navigation bar

	// show checkbox to display all events
	echo "<td><input type='Checkbox' name='all' value=1 ".($all?"checked":"").">&nbsp;$m1_text37</td></form>\n";
	echo "<td colspan=4>&nbsp;";
	// keyword
	if($keyword) {
		if($filter == "all") { $where .=  "and(event like '%$keyword%' or note like '%$keyword%' or note2 like '%$keyword%')"; }
		else { $where .= "and $filter like '%$keyword%'"; }
	}

		// build query to view resource booking
	if($make == "ress") { $querystr = "ressource > 0 $where"; }
	// build query to see your own events
	else { $querystr = "an = '$user_ID' $where"; }

	// define 'next' & 'previous' button
	$result = db_query("select count(ID) from ".$DB['current'].".termine where ".$querystr);
	$row = db_fetch_row($result);
	$page_n = $page + 1;
	$page_p = $page - 1;
	if($page) {
		echo "<a href='".href("index.php?sys_module=calcander&mode=3&make=$make&perpage=$perpage&page=$page_p&up=$up&sort=$sort&filter=$filter&keyword=$keyword&no_r=$no_r&all=$all")."'>$previous</a>&nbsp;&nbsp;\n";
	}
	if($row[0] > $page_n*$perpage) {
		echo "<a href='".href("index.php?sys_module=calendar&mode=3&make=$make&perpage=$perpage&page=$page_n&up=$up&sort=$sort&filter=$filter&keyword=$keyword&no_r=$no_r&all=$all")."'>$next</a>\n";
	}
	echo "</td></tr></table>";
	echo "<table cellspacing=0 cellpadding=1 border=1>";

	if($make <> "ress") { echo "<td>&nbsp;</td>"; }
	echo makesort('10', 'datum', $m1_text13);
	echo makesort('7', 'anfang', $m1_text14);
	echo makesort('7', 'ende', $m1_text15);
	// only resource list: name of resource and who booked it
	// else only one
	if($make == "ress") echo "<td>$m1_text16</td><td>$m1_text17</td></tr>"; else {
		echo makesort('30', 'event', $m1_text18);
		echo makesort('40', 'note2', $m1_text36).'</tr>';
	}

	// begin output
	$result = db_query("select * from ".$DB['current'].".termine where $querystr order by $sort $direction");
	while($row = db_fetch_row($result)) {
		if($b >= $page*$perpage and $b <($page+1)*$perpage) {
			// alternate class
			if(($cnr/2) == round($cnr/2)) $class = "inverse"; else $class = "main";
			$cnr++;
			$day = substr($row[7],8,2);
			$month = substr($row[7],5,2);
			$year = substr($row[7],0,4);
			echo "<tr>";

			if($make == "ress") {
				// date, begin and end time time
				echo "<td class=\"inverse\">$day.$month.$year</td><td class=\"inverse\">$row[8]</td><td class=\"inverse\">$row[9]</td>\n";
				// show name of resource
				$result2 = db_query("select name from ".$DB['current'].".ressourcen where ID = '$row[10]'");
				$row2 = db_fetch_row($result2);
				echo "<td>&nbsp;$row2[0]</td>\n";
				// show the name of the user who booked the resource
				$result3 = db_query("select nachname, vorname from ".$DB['core'].".users where ID = '$row[2]'");
				$row3 = db_fetch_row($result3);
				echo "<td>&nbsp;$row3[0]"." ".substr($row3[1],0,1).".</td></tr>\n";
			} else {
				$text = html_out($row[3]);
				$remark = substr(html_out($row[13]),0,180);
				// red button to delete this event
				echo "<td><a href='".href("index.php?sys_module=calendar&mode=3&make=delete&event_ID=$row[0]&sort=$sort&up=$up&page=$page&perpage=$perpage&keyword=$keyword&filter=$filter&no_r=$no_r&all=$all&day=$day&month=$month&year=$year")."'><img src='img/r.gif' alt='$m1_text19' title='$m1_text19' border=0 width=7></a></td>\n";
				// show day, begin and end time
				echo "<td class=\"inverse\">$day.$month.$year</td><td class=\"inverse\">$row[8]</td><td class=\"inverse\">$row[9]</td>\n";
				// add link to edit event
				if($user_ID == $row[1] or $user_ID == $row[2]){
					$text = "<a href='".href("index.php?sys_module=calendar&event_ID=$row[0]&mode=3&sort=$sort&up=$up&page=$page&perpage=$perpage&keyword=$keyword&filter=$filter&no_r=$no_r&all=$all")."'>".$text."</a>";
				}
				// output text and remark
				echo "<td>&nbsp;$text</td><td>$remark&nbsp;</td></tr>\n";
			}
		}
		$b++;
	}
	echo "</table>";
	// print out the view
	echo "&nbsp;<a href='javascript:self.print()'>$print</a>\n";
}

//START NO FUNC
switch ($mode) {
	case 1:
		// chnage here the timecale of the daily view
		$time_step=15;
		for($i = 0; $i <(60/$time_step *($tagesende-$tagesanfang)); $i++) {
			$htmtab[$i] = "&nbsp;";
			$isset[$i] = 0;
			$repeat[$i] = 1;
		}
		$querystr="";
		if($isRess) {
			define("MULTI", TRUE);
			//Resource View
			$result2 = db_query("select ".$DB['current'].".ressourcen.name from ".$DB['current'].".ressourcen where ".$DB['current'].".ressourcen.ID = '$u_r_ID'");
			while($row2 = db_fetch_row($result2)) $ress2 = $row2[0];
			$user_ress = "$m1_text16-$o_calendar: <b>$ress2</b>($m1_text17: $user_firstname $user_name)";
			$querystr = "select * from ".$DB['current'].".termine where ".$DB['current'].".termine.datum = '$datum' and ".$DB['current'].".termine.ressource='$u_r_ID'";
		} else if($spers) {
			define("MULTI", TRUE);
			//Group View
			$tmp_spers=ereg_replace("^a:[0-9]:{", "", ereg_replace("}$", "", ereg_replace("i:[0-9];s:[0-9]:\"([[:alpha:]]+)\";", "\\1:", $spers)));
			$sel_users=explode(":", $tmp_spers);
			$query="";
			for($i=0; $i<count($sel_users); $i++) {
				if($sel_users[$i]) {
					if($query) $query.=" or ";
					$query.="(".$DB['core'].".users.kurz='".$sel_users[$i]."' and ".$DB['current'].".termine.an=".$DB['core'].".users.ID)";
				}
			}
			$querystr = "select ".$DB['current'].".termine.*,".$DB['core'].".users.kurz from ".$DB['current'].".termine,".$DB['core'].".users where ".$DB['current'].".termine.datum = '$datum' and(".$query.")";
		} else {
			//Single User View
			$user_ress = "$user_firstname $user_name";
			$querystr = "select * from ".$DB['current'].".termine where ".$DB['current'].".termine.datum = '$datum' and an='$user_ID'";
		}
		unset($event_ID); // initialize array
		$result = db_query($querystr." AND ".$DB['current'].".termine.anfang <> '----' AND ".$DB['current'].".termine.ende <> '----' order by ".$DB['current'].".termine.anfang");
	
		while($row = db_fetch_row($result)) {
			$ha = substr($row[8], 0, 2); $ma = substr($row[8], 2, 2);
			$he = substr($row[9], 0, 2); $me = substr($row[9], 2, 2);
			if($ha < $tagesanfang) {
				$ha = 0;
				$ma = 0;
			} else $ha -= $tagesanfang;
				if($he < $tagesanfang) {
					$he = $tagesanfang;
					$me = 0;
				}
				if($he == $tagesende)  $me = 0; else if($he > $tagesende) {
					$he = $tagesende;
					$me = 0;
				}
				$he-=$tagesanfang;
				$i1 =(int)floor(($ha*60+$ma)/$time_step);
				$i2 =(int)floor(($he*60+$me-1)/$time_step);
				$text = "";
				if(!$not_self or $row[17]<>1)$text = text4row($isRess, $row);
				$repeat[$i1] = 0; $repeat[$i2+1] = 0;
				for($i = $i1; $i <= $i2; $i++) {
					$event_ID[$i] = $row[0]; // assign ID for link to edit event
					if($user_ID == $row[1] || $user_ID == $row[2]) {
						$a_href = "<a href='".href("index.php?sys_module=calendar&event_ID=$event_ID[$i]&mode=$mode")."'>";
						$a_close= "</a>";
					} else {
						$a_href ="";
						$a_close="";
					}
					if($isset[$i]) $htmtab[$i] .= "<BR>".$a_href.$text.$a_close; else $htmtab[$i] = $a_href.$text.$a_close;
					$isset[$i] = 1;
				}
			}
			for($i = 0; $i <(60 / $time_step *($tagesende-$tagesanfang)); $i++) if($isset[$i] == 0) $repeat[$i] = 0;
		
			// fetch week day
			$wo_tag = date('w', mktime(0,0,0,$month,$day,$year));
			echo "<center><table border=0><tr>";
			echo "<td colspan=2 class=mo><b>$name_day[$wo_tag], $day.$month.$year</b> - ";
			echo "$user_ress";
			if($groups) echo "/ $group_name";
			echo "<a href='".href("index.php?year=$year&month=$month&day=$day&mode=$mode&gru=$gru&spers=$spers&axis=$axis&view=$view&dist=$dist&no_r=".($no_r?"0":"1")."&isRess=$isRess&u_r_ID=$u_r_ID")."'>\n<img src=img/arrow".($no_r?"left":"right").".gif align=right border=0></a><br>";
			echo "</td></tr>\n";

			$result = db_query($querystr." AND anfang = '----' AND ende = '----'");
			echo "<tr><td colspan=2 valign=top><table width='100%' border=1 cellspacing=0 cellpadding=1>\n";
			while($row = db_fetch_row($result)) {
				echo "<tr><td>\n";
				if(!$not_self or $row[17]<>1)$text = text4row($isRess, $row); else $text = "*****";
				if($user_ID == $row[1] || $user_ID == $row[2]) $text="<a href='".href("index.php?sys_module=calendar&event_ID=$row[0]&mode=$mode")."'>$text</a>";
				echo "$text</td></tr>\n";
			}
			echo "</table></td></tr>\n";

			echo "<tr><td valign=top><table border=1 cellspacing=0 cellpadding=1>\n";
			$i = 0;
			$a1 = $tagesanfang;
			$a2 = 0;
			$first = 1;
			while($a1 < $tagesende) {
				if(strlen($a1) == 1) {$a1 = "0".$a1;}
				if(strlen($a2) == 1) {$a2 = "0".$a2;}
				if((($a1+$a2/60) >=(($tagesende-$tagesanfang)/2+$tagesanfang)) and !$reihe2) {
					echo "</table></td><td><table border=1 cellspacing=0 cellpadding=1>\n";
					$reihe2=1;
					$first=1;
				}
				$a3 = $a2 + $time_step - 1;
				$tanfang = $a1.$a2;
				$tende = $a1.$a3;
				$r = 1;
				while($repeat[$i+$r]) $r++;
				if($isset[$i]) $class = "inverse"; else $class = "main";
				// display day time and link with javascript for automatic insert into form
				if($first) {
					echo "<tr><td><a href=\"javascript:time('$a1$a2')\">$a1:$a2</a></td>\n";
					echo "<td width=200".($r>1?" rowspan=$r":"")." class=\"".$class."\">$htmtab[$i]</td></tr>\n";
				} else echo "<tr><td><a href=\"javascript:time('$a1$a2')\">$a1:$a2</a></td></tr>\n";
				$first = 0; $r--;
				if($r == 0) $first = 1;
				$i++; $a2 += $time_step;
				if($a2 == 60) { 
					$a1 += 1;
					$a2 = 0;
				}
			}
		echo "</table></td></table>";
		echo "&nbsp;<a href='javascript:self.print()'><img src=\"img/print.png\" border=\"0\" height=\"16\" width=\"16\" alt=\"Print\" title=\"Print\"></a></center>";
		break;

	case 3:
		// show list of events
		listview($make, $year, $month, $day);
		break;

	case 4:
		if(isset($isRess) && $isRess != "") {
			if($isRess) {
				$result2 = db_query("select name from ".$DB['current'].".ressourcen where ID = '$u_r_ID'");
				while($row2 = db_fetch_row($result2)) $ress2 = $row2[0];
				echo "$m1_text16-$o_calendar: <b>$ress2</b>($m1_text17: $user_firstname $user_name)";
			 } else {
				$not_self = 1;
				$result2 = db_query("select nachname,vorname from ".$DB['core'].".users where ID = '$u_r_ID'");
				while($row2 = db_fetch_row($result2)) $ress2 = $row2[1]." ".$row2[0];
				echo "$m1_text17-$o_calendar: <b>$ress2</b>($m1_text17: $user_firstname $user_name)";
			 }
		} else echo "<b>$user_firstname $user_name</b>\n";
		echo "<a href='".href("index.php?year=$year&month=$month&day=$day&mode=$mode&gru=$gru&spers=$spers&axis=$axis&view=$view&dist=$dist&no_r=".($no_r?"0":"1")."&isRess=$isRess&u_r_ID=$u_r_ID")."'><img src=img/arrow".($no_r?"left":"right").".gif align=right border=0></a><br><br>";
		echo "<center><table cellspacing=0 cellpadding=1 border=1><tr>\n";
		for($i = 0; $i<7; $i++) { echo "<td class=mo>$name_day2[$i]</td>\n"; }
		echo "</tr><tr>\n";
		// days of the previous month
		// sunday? -> other value
		if(date("w", mktime(0,0,0,$month,1,$year)) == "0") $b = 5; else $b = date("w", mktime(0,0,0,$month,1,$year)) - 2;
		for($a = $b; $a>=0; $a--) {
			$d = date("t", mktime(0,0,0,($month),0,$year)) - $a;
			echo"<td id=vn width=70 height=80>$d &nbsp; <br>\n";
		}

		// actual month
		for($d=1; $d <= date("t", mktime(0,0,0,($month+1),0,$year)); $d++) {
			$args = "?sys_module=calendar&mode=1&year=$year&month=$month&day=$d&no_r=$no_r&isRess=$isRess&u_r_ID=$u_r_ID";
			echo "<td width=70 height=80><a href='".href("index.php$args")."'>$d</a> &nbsp; <br>";
			if(strlen($d) == 1) $d = "0".$d;
			$datum = "$year-$month-$d";
			if(isset($isRess) && $isRess != "") {
					if($isRess) $querystr = "select * from ".$DB['current'].".termine where datum = '$datum' and ressource='$u_r_ID' order by anfang"; else $querystr = "select * from ".$DB['current'].".termine where datum = '$datum' and an='$u_r_ID' order by anfang";
			} else $querystr = "select * from ".$DB['current'].".termine where datum = '$datum' and an='$user_ID' order by anfang";
			$result = db_query($querystr);
			while($row = db_fetch_row($result)) {
				$termin = 1;
				if(isset($isRess) && $isRess) {
					$res = db_query("select vorname, nachname from ".$DB['core'].".users where ID = '$row[2]'");
					$v = "";
					if($r = db_fetch_row($res)) { $v = "$r[0] $r[1]"; }
					if($v == "") $v = $row[1];
					$text = html_out($v);
				} else {
					$text = substr($row[3],0,8);
					if(strlen($row[3]) > 8) { $text = $text."..";}
					$text = html_out($text);
			// add link to edit event
					 if($user_ID == $row[1] || $user_ID == $row[2]) $text = "<a href='".href("index.php?sys_module=calendar&event_ID=$row[0]&mode=$mode")."'>".$text."</a>";
				}
				if($not_self and $row[17]==1) echo "<img src='img/b.gif' alt='$m1_text14: $row[8] - $m1_text15: $row[9]' title='$m1_text14: $row[8] - $m1_text15: $row[9]' width=7>&nbsp;<br>\n"; else echo "<img src='img/b.gif' alt='$m1_text14: $row[8] - $m1_text15: $row[9]' title='$m1_text14: $row[8] - $m1_text15: $row[9]' width=7>&nbsp;$text<br>";
			}

			if(!$termin) echo "&nbsp;";
			echo "</td>\n";
			if(date("w", mktime(0,0,0,$month,$d,$year)) == 0 && date("t", mktime(0,0,0,($month+1),0,$year)) > $d ) echo "</tr><tr>";
		}

		// next month
		if($month == 12) {
			$year++;
			$month = 0;
		}
		if(date("w", mktime(0,0,0,$month+1,1,$year))!=1) {
			$d=1;
			while(date("w", mktime(0,0,0,($month+1),$d,$year)) <> 1) {
				$m2 = $month + 1;
				echo"<td id=vn width=70 height=80>$d &nbsp;</td>\n";
				$d++;
			}
		}
		echo "</tr></table>";
		echo "&nbsp;<a href='javascript:self.print()'>$print</a><br></center>";

		// if the month view is chosen, offer an additional print view for the whole year
		echo "&nbsp;<a href='".href("index.php?sys_module=calendar&mode=year&year=$year")."'>$m1_text43</a>";

		break;

	case $mode==2||($mode>4&&$mode<9):
		//Weekly view(2), group view(5,6,8), Not implemented(7)
		if($day < 0) $day += 1;
		$daylen = 60 *($tagesende - $tagesanfang);
		$cselect = "datum, anfang, ende, von, an, event, note, ressource, termine.ID, visi";
		if($mode == 2) {
			if(isset($isRess) && $isRess != "") {
				if($isRess) {
					$result2 = db_query("select name from ".$DB['current'].".ressourcen where ID = '$u_r_ID'");
					while($row2 = db_fetch_row($result2)) $ress2 = $row2[0];
					echo "$m1_text16-$o_calendar: <b>$ress2</b>($m1_text17: $user_firstname $user_name)";
				} else {
					$not_self =1;
					$result2 = db_query("select nachname, vorname from ".$DB['core'].".users where ID = '$u_r_ID'");
					while($row2 = db_fetch_row($result2)) $ress2 = $row2[1]." ".$row2[0];
					echo "$m1_text17-$o_calendar: <b>$ress2</b>($m1_text17: $user_firstname $user_name)";
				}
			} else echo "<b>$user_firstname $user_name</b>";
		}
		echo "<a href='".href("index.php?year=$year&month=$month&day=".($day>0?$day:$day-1)."&mode=".($mode>=5?$mode-4:$mode)."&gru=$gru&spers=$spers&axis=$axis&view=$view&dist=$dist&no_r=".($no_r?"0":"1")."&isRess=$isRess&u_r_ID=$u_r_ID")."'><img src=img/arrow".($no_r?"left":"right").".gif align=right border=0></a><br><br>";

		// Initialize Groups or Ressources
		if($mode>=5&&$mode<=8) {
			$tinterval = $dist;
			if($gru) {
				unset($pers);
				if($gru == "ress") {
					$i = 0;
					$result = db_query("select ID from ".$DB['current'].".ressourcen");
						while($row = db_fetch_row($result)) $pers[$i++] = $row[0];
				}
			else {
				$result = db_query("select personen from ".$DB['current'].".profile where ID ='$gru'");
				$row = db_fetch_row($result);
				$pers = unserialize($row[0]);
				if(count($pers)==0) $pers[] = $user_kurz;
			}
		}
		// Initialize additional tables and where statements
		$vallist = "";
		if($gru == "ress") {
			$jointab = "ressourcen";
			$presel  = "$jointab.name";
			$joincon = "termine.ressource = $jointab.ID";
		} else {
			$jointab = "users";
			$presel  = "$jointab.nachname, $jointab.vorname, $jointab.kurz, $jointab.loginname, $jointab.acc";
			$joincon = "termine.an = $jointab.ID";
			$prefix  = "AND kurz IN(";
			for($a = 0; $a < count($pers); $a++) {
				$vallist .= "$prefix'$pers[$a]'";
				$prefix = ", ";
			}
			$vallist .= ") AND NOT(acc LIKE '%n%')";
		}
		break;
	}

	// Initialize PHP-array-"headers" depends on "mode"
	$lastday = date("t", mktime(0,0,0,$month,1,$year));
	switch($mode) {
		case 2:
			$axis = "v"; // Be sure: on 2 always vertical!
			$coloffset = 1;
			$tinterval = 30;
			$tsanf = mktime(0,0,0,$month,$day,$year); $danf = date("Y-m-d", $tsanf);
			$tsend = $tsanf + 604800; $dend = date("Y-m-d", $tsend); // add 7 days
			$nrofcols = 7;
			$nrofrows =(int)ceil($daylen / $tinterval);
			for($d = 0; $d < 7; $d++) {
				$ts = $tsanf + $d * 86400;
				$colcmp[$d] = date("Y-m-d", $ts); // add $d days
				$shwtxt[$d] = 2;
				$devent[$d] = "";
				$args="?sys_module=calendar&mode=1&year=".date("Y", $ts)."&month=".date("m", $ts)."&day=".date("d", $ts)."&no_r=$no_r&isRess=$isRess&u_r_ID=$u_r_ID";
				$htmtab[0][$coloffset+$d] ="<a href='".href("index.php$args")."'>".date("d.m.Y", $ts)."</a>";

				$isset[0][$coloffset+$d] = 0; $repeat[0][$coloffset+$d] = 0;
			}
			$base = mktime($tagesanfang,0,0,$month,$day,$year);
			for($a = 0; $a < $nrofrows; $a++) {
				$htmtab[1+$a][0] = date("H:i", $base + $a*$tinterval * 60); // $tagesanfang + $a*$tinterval min.
				$isset[1+$a][0] = 0; $repeat[1+$a][0] = 0;
			}
			if(isset($isRess) && $isRess != "") {
					 if($isRess) {
							$m1stmt = "SELECT datum AS dummy, $cselect FROM ".$DB['current'].".termine WHERE datum >= '$danf' AND datum < '$dend' AND ressource = '$u_r_ID' ORDER BY datum, anfang";
					 }
					 else {
						$m1stmt = "SELECT datum AS dummy, $cselect FROM ".$DB['current'].".termine WHERE datum >= '$danf' AND datum < '$dend' AND an = '$u_r_ID' ORDER BY datum, anfang";
						for($d = 0; $d < 7; $d++) {$shwtxt[$d] = 1;}
					 }
			} else {
				$m1stmt = "SELECT datum AS dummy, $cselect FROM ".$DB['current'].".termine WHERE datum >= '$danf' AND datum < '$dend' AND an = '$user_ID' ORDER BY datum, anfang";
			}
			break;
		case 5:
			$coloffset = 1;
			if($tinterval == 0) $tinterval = 30;
			$tsanf = mktime(0,0,0,$month,$day,$year); $danf = date("Y-m-d", $tsanf);
			$nrofrows =(int)ceil($daylen / $tinterval);
			$m1stmt = "SELECT $jointab.ID, $cselect FROM ".$DB['current'].".termine, $jointab WHERE $joincon AND datum = '$danf' $vallist ORDER BY datum, anfang";
			$htmtab[0][0] = date("d.m.Y", $tsanf);
			$base = mktime($tagesanfang,0,0,$month,$day,$year);
			for($a = 0; $a < $nrofrows; $a++) {
				if($axis == 'x') {
					if(date("i", $base + $a*$tinterval * 60) == 0)
						$htmtab[1+$a][0] = date("H", $base + $a*$tinterval * 60);
					else
						$htmtab[1+$a][0] = "&nbsp;-&nbsp;";
				} else
					$htmtab[1+$a][0] = "<a href=\"javascript:time('".date("Hi", $base + $a*$tinterval * 60)."')\">".date("H:i", $base + $a*$tinterval * 60)."</a>";
				$isset[1+$a][0] = 0; $repeat[1+$a][0] = 0;
			}
			break;
		case 6:
			$coloffset = 2;
			if($tinterval == 0) $tinterval =(($axis=='x')?2:4)*60;
			$tsanf = mktime(0,0,0,$month,$day,$year); $danf = date("Y-m-d", $tsanf);
			$tsend = $tsanf + 604800; $dend = date("Y-m-d", $tsend); // add 7 days
			$nrofrows = 7 *(int)ceil($daylen / $tinterval);
			$m1stmt = "SELECT $jointab.ID, $cselect FROM ".$DB['current'].".termine, $jointab WHERE $joincon AND datum >= '$danf' AND datum < '$dend' $vallist  ORDER BY datum, anfang";
			$i = 1;
			$base = mktime($tagesanfang,0,0,$month,$day,$year);
			for($d = 0; $d < 7; $d++) {
				$isrept = 0;
				$ts = $tsanf + $d * 86400;
				for($a = 0; $a < $nrofrows/7; $a++) {
					$args = html_out("?sys_module=calendar&mode=1&year=".date("Y", $ts)."&month=".date("m", $ts)."&day=".date("d", $ts)."&view=$view&gru=$gru&spers=$spers&axis=$axis&dist=$dist&no_r=$no_r&isRess=$isRess&u_r_ID=$u_r_ID");
					$htmtab[$i][0] = "<a href='".href("index.php$args")."'>".date("d.m.Y", $ts)."</a>";
					if($axis == 'x') {
						if(date("i", $base + $a*$tinterval * 60 + $d * 86400) == 0)
							$htmtab[$i][1] = date("H", $base + $a*$tinterval * 60 + $d * 86400);
						else
							$htmtab[$i][1] = "&nbsp;-&nbsp;";
					} else
						$htmtab[$i][1] = date("H:i", $base + $a*$tinterval * 60 + $d * 86400);
					$isset[$i][0] = 0; $repeat[$i][0] = $isrept;
					$isset[$i][1] = 0; $repeat[$i][1] = 0; $i++;
					$isrept = 1;
				}
			}
			break;
		case 7:
			die("<h2>Not implemented yet!</h2></body></html>");
		case 8:
			 function get_week_nr($day){
					global $DB, $year, $month;
					$t_stamp = mktime(1,0,0,$month,$day,$year);
					while($nr < 01){
						 if($t_stamp > 1104101999 and $t_stamp < 1104534000){return 53;}
						 if($t_stamp > 1009666799 and $t_stamp < 1104102000){$t_stamp += 604800;}
						 $nr = strftime("%W", $t_stamp);
						 $t_stamp -= 86400;
					}
					return $nr;
			 }
			$coloffset = 3;
			if($tinterval == 0) $tinterval =($axis=='x')?4*60:$daylen*60;
			$tsanf = mktime(0,0,0,$month,1,$year); $danf = date("Y-m-d", $tsanf);
			$tsend = mktime(0,0,0,$month+1,1,$year); $dend = date("Y-m-d", $tsend);
			$dayofmon = date("t", $tsanf);
			$nrofrows = $dayofmon *(int)ceil($daylen / $tinterval);
			$m1stmt = "SELECT $jointab.ID, $cselect FROM termine, $jointab WHERE $joincon AND datum >= '$danf' AND datum < '$dend' $vallist  ORDER BY datum, anfang";
			$i = 1;
			$lastweek=-99;
			$base = mktime($tagesanfang,0,0,$month,1,$year);
			for($d = 0; $d < $dayofmon; $d++) {
				$isrept = 0;
				$ts = $base + $d * 86400;
				$week = get_week_nr($d+1);
				$startweek=date("w", $ts);
				$startweek=date("d", $ts) -($startweek?$startweek:7) + 1;
				if($startweek <= 0) { $startweek -= 1; } // be compatible with l.php!
				for($a = 0; $a < $nrofrows/$dayofmon; $a++) {
					$htmtab[$i][0] = "<a href='".href("index.php?sys_module=calendar&mode=2&year=".date("Y", $ts)."&month=".date("m", $ts)."&day=$startweek&view=$view&gru=$gru&spers=$spers&axis=$axis&dist=0&no_r=$no_r&isRess=$isRess&u_r_ID=$u_r_ID")."'>$week</a>";
					$args = htmlspecialchars("?sys_module=calendar&mode=1&year=".date("Y", $ts)."&month=".date("m", $ts)."&day=".date("d", $ts)."&view=$view&gru=$gru&spers=$spers&axis=$axis&dist=0&no_r=$no_r&isRess=$isRess&u_r_ID=$u_r_ID");
					$htmtab[$i][1] = "<a href='".href("index.php$args")."'>".date("d.m.Y", $ts)."</a>";
					if($axis == 'x') {
						if(date("i", $base + $a*$tinterval * 60 + $d * 86400) == 0)
							$htmtab[$i][2] = date("H", $base + $a*$tinterval * 60 + $d * 86400);
						else
							$htmtab[$i][2] = "&nbsp;-&nbsp;";
					} else
						$htmtab[$i][2] = date("H:i", $base + $a*$tinterval * 60 + $d * 86400);
					$isset[$i][0] = 0; $repeat[$i][0] = $startweek==$lastweek?1:0;
					$isset[$i][1] = 0; $repeat[$i][1] = $isrept;
					$isset[$i][2] = 0; $repeat[$i][2] = 0; $i++;
					$lastweek = $startweek;
					$isrept = 1;
				}
			}
			break;
		}
	}
	// Initialize PHP-array columns-headers on group-views
	if($mode >= 5 && $mode <= 8) {
		$a = 0;
		$qry = "SELECT ID, $presel FROM ".$DB['current'].".$jointab WHERE ID IS NOT NULL $vallist";
		if($gru == "ress") $qry .= "order by $presel"; //resource view
		else{
				switch($groupviewuserheader) {
					case 2: // loginname
						$qry .= "order by $jointab.loginname";
						break;
					case 1: // shortname
						$qry .= "order by $jointab.kurz";
						break;
					case 0: // name
					default: // default
						$qry .= "order by $jointab.nachname, $jointab.vorname";
						break;
				}
		}
		$result = db_query($qry);
		while($row = db_fetch_row($result)) {
			$colcmp[$a] = $row[0];
			$devent[$a] = "";
			if($gru == "ress") {
				$shwtxt[$a] = 2;
				$htmrow[$coloffset+$a] = $row[1];
				$htmtab[0][$coloffset+$a] = "<a href='".href("index.php?sys_module=calendar&year=$year&month=$month&day=".($day>0?$day:$day-1)."&mode=".($mode-4)."&no_r=$no_r&u_r_ID=".$row[0]."&isRess=1")."'>".$row[1]."</a>";
			} else {
				$shwtxt[$a] = 0;
				if($user_ID == $row[0])
					$shwtxt[$a] = 2;
				else {
					if(stristr($row[5],"y")) $shwtxt[$a] = 1;
				}
				switch($groupviewuserheader) {
					case 2: // loginname
						$n = $row[4];
						break;
					case 1: // shortname
						$n = $row[3];
						break;
					case 0: // name
					default: // default
						$n = $row[1]." ".substr($row[2],0,1).".";
						break;
				}
				$htmrow[$coloffset+$a] = html_out($n);
				$htm = "";
				if($shwtxt[$a]) $htm .= "<a href='".href("index.php?sys_module=calendar&year=$year&month=$month&day=".($day>0?$day:$day-1)."&mode=".($mode-4)."&no_r=$no_r&u_r_ID=".$row[0]."&isRess=0")."'>";
				$htm .= $n;
				if($shwtxt[$a]) $htm .= "</a>";
				$htmtab[0][$coloffset+$a] = $htm;
			}
			$isset[0][$coloffset+$a] = 0;
			$repeat[0][$coloffset+$a] = 0;
			$a++;
		}
		$nrofcols = $a;
	}

	// Initialize PHP-array "inside"-fields
	for($i = 0; $i < $nrofrows; $i++) {
		for($a = 0; $a < $nrofcols; $a++) {
			$htmtab[1+$i][$coloffset+$a] = "&nbsp;";
			$isset[1+$i][$coloffset+$a] = 0;
			$repeat[1+$i][$coloffset+$a] = 1;
		}
	}

	// select database and fill fields
	$result = db_query($m1stmt);
	while($row = db_fetch_row($result)) {
		// calculate the row in table
		if($mode == 2)
			$daydiff = 0;
		else {
			$ary = explode("-", $row[1]);
			$daydiff =(mktime(0,0,0, $ary[1], $ary[2], $ary[0]) - $tsanf) / 86400;
		}
		$ha = substr($row[2], 0, 2); $ma = substr($row[2], 2, 2);
		$he = substr($row[3], 0, 2); $me = substr($row[3], 2, 2);
		if($ha < $tagesanfang) { $ha = 0; $ma = 0; } else { $ha -= $tagesanfang; }
		if($he < $tagesanfang) { $he = $tagesanfang; $me = 0; }
		if($he == $tagesende)  { $me = 0; }
		if($he > $tagesende)   { $he = $tagesende; $me = 0; }
		$he -= $tagesanfang;
		$i1 = $daydiff*(int)ceil($daylen/$tinterval) +(int)floor(($ha*60+$ma)/$tinterval); // $i1: first row
		$i2 = $daydiff*(int)ceil($daylen/$tinterval) +(int)floor(($he*60+$me-1)/$tinterval); // $i2: last row
		// search the column in table
		for($a = 0; $a < $nrofcols; $a++) if($colcmp[$a] == $row[0]) break; // $a is the number of dest-column
		// get the value for the field(s)
		if($gru == "ress") {
			// 99? -> anonymous ressource booking
			if($row[7] == "99") {
				$t = $row[4];
			} else {
				$result2 = db_query("select nachname, vorname from ".$DB['core'].".users where ID = '$row[5]' ORDER BY nachname");
				$row2 = db_fetch_row($result2);
				$t = $row2[0]." ".substr($row2[1],0,1).".";
			}
		} else {
			if(isset($isRess) && $isRess) {
				$res = db_query("select vorname, nachname from ".$DB['core'].".users where ID = '$row[5]'");
				$v = "";
				if($r = db_fetch_row($res)) { $v = "$r[0] $r[1]"; }
				if($v == "") { $v = $row[1]; }
				$t = htmlspecialchars($v);
			} else {
				$t = htmlspecialchars($row[6]);
			}
		}

		if($shwtxt[$a] == 2) $shwtxt2 = 1;
		elseif($row[10] == 2) $shwtxt2 = 1;
		elseif($row[10] == 0 and $shwtxt[$a] == 1) $shwtxt2 = 1;
		elseif($user_ID == $row[4]) $shwtxt2 = 1;
		else $shwtxt2 = 0;


		if($shwtxt2) {
			$text = substr($t,0,9); $addinfo = 0; $tiptext = "";

			// add link to edit event
			if($user_ID == $row[4] || $user_ID == $row[5]){
			$text = "<a href='".href("index.php?sys_module=calendar&event_ID=$row[9]&mode=$mode")."'>".$text."</a>";
			}
			if(strlen($t) > 9) { $addinfo = 1; $tiptext .= $t; }
			$t = "$row[2] - $row[3]: \n$t";
			if(isset($isRess) == 0 || $isRess == 0) {
				if(strlen($row[7]) > 0 and $user_ID==$row[5]) { if($addinfo) $tiptext .= " \n"; $addinfo = 1; $tiptext .= $row[7]; $t .= " \n".$row[7]; }
				if(strlen($row[8]) > 0) {
					$result2 = db_query("select name from ".$DB['current'].".ressourcen where ID = '$row[8]'");
					if($row2 = db_fetch_row($result2)) { if($addinfo) $tiptext .= " \n"; $addinfo = 1; $tiptext .= $row2[0]; $t .= " \n".$row2[0]; }
				}
			}
			if($addinfo) $text .= "<img src='img/dots.gif' align=bottom alt='$tiptext' title='$tiptext'>";
					if($row[2] == '----' && $row[3] == '----') {
						$ary = explode("-", $row[1]);
						$d = $ary[2].".".$ary[1].".".$ary[0];
						if($mode == 2 or $mode == 5) { // weekly view
							if($devent[$a] == "") $devent[$a] = "$t";
							else $devent[$a] .= "\n$t";
						}
						else {
							if($devent[$a] == "") $devent[$a] = "$d $t";
							else $devent[$a] .= "\n$d $t";
						}
					}
					if(!$text) $text = "&nbsp;";
		} else {
					if($row[2] == '----' && $row[3] == '----') {
								  $ary = explode("-", $row[1]);
								  $d = $ary[2].".".$ary[1].".".$ary[0];
						if($devent[$a] == "") $devent[$a] = "$d"; else $devent[$a] .= "\n$d";
					}
			$text = "&nbsp;";
		}

		// the first row cannot be a repeatation
		$repeat[1+$i1][$coloffset+$a] = 0;
		// set the value and flag
		for($i = $i1; $i <= $i2; $i++) {
			if($axis == 'x') {
				if($isset[1+$i][$coloffset+$a])
					$htmtab[1+$i][$coloffset+$a] .= "$t";
				else
					$htmtab[1+$i][$coloffset+$a] = "$t";
			} else {
				if($isset[1+$i][$coloffset+$a]) {
					if($shwtxt[$a]) $htmtab[1+$i][$coloffset+$a] .= "<BR>$text";
					else $htmtab[1+$i][$coloffset+$a] = $text;
				} else {
					$htmtab[1+$i][$coloffset+$a] = $text;
				}
			}
			$isset[1+$i][$coloffset+$a] = 1;
		}
		// the row below the last row cannot be a repeatation
		if($i < $nrofrows) $repeat[1+$i][$coloffset+$a] = 0;
	}

	// reset repeat, if isset == 0
	for($i = 0; $i < $nrofrows; $i++)
		for($a = 0; $a < $nrofcols; $a++)
			if($isset[1+$i][$coloffset+$a] == 0) $repeat[1+$i][$coloffset+$a] = 0;

	// output PHP-array to HTML
	echo "<table border=1 cellspacing=0 cellpadding=1>\n";
	if($axis == "h" || $axis == "x") {
		for($a = 0; $a < $coloffset+$nrofcols; $a++) {
			echo "  <tr>\n    ";
			for($i = 0; $i < $nrofrows+1; $i++) {
				if($repeat[$i][$a] == 0) {
					for($r = 1; $r < $nrofrows+1-$i && $repeat[$i+$r][$a]; $r++);
					if($isset[$i][$a]) $class="inverse"; else $class="main";
					if($i >= 1 && $a >= $coloffset-1) $wd=" width=80"; else $wd="";
					if($axis == 'x' && $i >= 1 && $a >= $coloffset) {
						$w=17+21*($r-1);
						echo "<td$wd".($r>1?" colspan=$r":"")."><img src=img/".($isset[$i][$a]?"b":"t").".gif alt='$htmrow[$a]' title='$htmrow[$a]'";
						if($htmtab[$i][$a] <> "&nbsp;") echo "\n".$htmtab[$i][$a];
						echo "' height=17 width=$w></td>";
					} else {
						if($i == 0 && $a >= $coloffset && $devent[$a-$coloffset] != "") {
							$devent2 = trim(ereg_replace("---- - ----:","",$devent[$a-$coloffset]));
							echo "<td$wd class=\"".$class."\"".($r>1?" colspan=$r":"")."><img src='img/b.gif' width=5 alt='".$devent2."' title='".$devent2."' align=top>".$htmtab[$i][$a]."</td>";
						} else echo "<td$wd class=\"".$class."\"".($r>1?" colspan=$r":"").">".$htmtab[$i][$a]."</td>";
					}
				}
			}
			echo "\n  </tr>\n";
		}
	} else {
		for($i = 0; $i < $nrofrows+1; $i++) {
			echo "  <tr>\n    ";
			for($a = 0; $a < $coloffset+$nrofcols; $a++) {
				if($repeat[$i][$a] == 0) {
					for($r = 1; $r < $nrofrows+1-$i && $repeat[$i+$r][$a]; $r++);
					if($isset[$i][$a]) $class="inverse"; else $class="main";
					if($a >= $coloffset) $wd=" width=20"; else $wd="";
					if($i == 0 && $a >= $coloffset && $devent[$a-$coloffset] != "") {
						$devent2 = trim(ereg_replace("---- - ----:","",$devent[$a-$coloffset]));
						echo "<td$wd class=\"".$class."\"".($r>1?" rowspan=$r":"")."><img src='img/b.gif' width=5 alt='".$devent2."' title='".$devent2."' align=top>".$htmtab[$i][$a]."</td>";
					} else echo "<td$wd class=\"".$class."\"".($r>1?" rowspan=$r":"").">".$htmtab[$i][$a]."</td>";
				}
			}
			echo "\n  </tr>\n";
		}
	}
	echo "</table>";

	// trailer of each page
	echo "&nbsp;<a href='javascript:self.print()'>$print</a><br>";

	// if the week view is chosen, offer an additional print view
	if($mode == 2) echo "&nbsp;<a href='".href("misc/print.php?module=calendar&mode=2&ID=$row[0]&year=$year&month=$month&day=$day")."' target=_blank>$printable_view</a>"; elseif($mode == "year") {
		echo "<table cellpadding=0 cellspacing=0 border=1><tr><td valign=top>\n";
		for($i = 1; $i <= 12; $i++) {
			$treffer = 0;
			if($i < 10) { $e = "0".$i; } else { $e = $i; }
			$thismonth = $year."-".$e;
			// print table for this day
			echo "<table border=0 cellspacing=1 cellpadding=1>\n";
			// show name of month and link to month view
			echo "<tr><td colspan=4 align=left valign=top><b>";
			echo "<a href='".href("index.php?sys_module=calendar&year=$year&month=$i&mode=4")."' target='_top'>$name_month[$i]</a>";
			echo "</b></td></tr>\n";
			// fetch records which begin with $year-$month in the datum field
			$result = db_query("select ID, anfang, ende, event, datum from ".$DB['current'].".termine where datum like '$thismonth%' and an = $user_ID order by datum, anfang");
			while($row = db_fetch_row($result)) {
				$datum = substr($row[4],8,2).".".substr($row[4],5,2);
				$anfang = substr($row[1],0,2).":".substr($row[1],2,2);
				$ende = substr($row[2],0,2).":".substr($row[2],2,2);
				$event = html_out($row[3]);
				// show blue button with start and end time and link to edit event
				// show date
				echo "<tr id=small><td width=40 valign=top><img src='img/b.gif' alt='$anfang - $ende' title='$anfang - $ende' border=0 width=7> $datum</td>\n";
				echo "<td width=150 valign=top><a href='".href("index.php?sys_module=calendar&event_ID=$row[0]&mode=3&no_r=$no_r&all=$all")."'>$event</a></td></tr>\n";
				$treffer++;
			}
			// fill in the table with blank lines
			for($e = $treffer; $e < 10; $e++) { echo "<tr><td colspan=4 width=250>&nbsp;</td></tr>\n"; }
			// close table
			echo "</table>\n";

			if($i == 6) {echo "</td><td valign=top>\n"; }
			elseif($i <> 12) {echo "<img src='img/s.gif' width=250 height=1 vspace=2>\n"; }
		}
		echo "</td></tr></table>\n";

		// if the month view is chosen, offer an additional print view for the whole year
		echo "&nbsp;<a href='".href("misc/print.php?module=calendar&mode=year&ID=$row[0]&year=$year&month=$month&day=$day")."' target=_blank>$m1_text43 $printable_view</a>";
} // end year view
?>
Return current item: Dogfood-A Complete Groupware/CRM System