<?php
/* $Id: overview.inc.php,v 1.10 2004/05/05 18:19:16 liedekef Exp $ */
error_reporting  (E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
// adding file revision into global array
if(function_exists("revisionInit")) revisionInit("\$Revision: 1.10 $", __FILE__);


// use ROOTPATH so safe_mode doesn't complain when this file gets included
// from the overview module (bugfix #948598)
include(ROOTPATH."/modules/calendar/inc/calendar_func.inc.php");

// =============================================================
// get Appointments from calendar tables
// =============================================================
function getcalendar() {
	global $smarty, $conn, $myEnv;

	// get current date in format 
	//$today = date("Ymd");
	$today_time = time();
	$today_start = $conn->DBTimeStamp(mktime(0,0,1, date("m",$today_time), date("d",$today_time), date("Y",$today_time)) );
	$today_end = $conn->DBTimeStamp(mktime(23,59,59, date("m",$today_time), date("d",$today_time), date("Y",$today_time)) );
	// FIXME SQL
//	$sql = "SELECT * FROM mgw_calendar WHERE userid=". $_SESSION["MGW"]->userid ." AND SUBSTRING(date,1,8) = '$today' ORDER BY date";
	$sql = "SELECT * FROM mgw_calendar 
            WHERE userid=". $_SESSION["MGW"]->userid ." AND 
			      date >= $today_start AND
        		  date <= $today_end 
			ORDER BY date";

	if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
    
	while ($row = $res->FetchRow()){
		$mydatefrom = date($_SESSION["MGW"]->settings["timefmt"], $res->UnixTimeStamp($row["date"]));
		$mydateto = date($_SESSION["MGW"]->settings["timefmt"], $res->UnixTimeStamp($row["date"])+$row["duration"]*60);

		/// NATYS str_decode: ///
		//$termin_list = array("date" => $mydatefrom."&nbsp;-&nbsp;".$mydateto, "subject" => $row["subject"]);
		$termin_list = array("date" => $mydatefrom."&nbsp;-&nbsp;".$mydateto, "subject" => str_decode($row["subject_summ"]));
		/////////////////////////
		$smarty->append("cm" , $termin_list);
	}
	
	$currentuserid = $_SESSION["MGW"]->userid;   
	
	//all appointment subjected by current user and not validated(class 2):
/*	$sql = "SELECT DISTINCT id
			FROM mgw_calendar WHERE inituserid='$currentuserid' AND id_class!='2'
			GROUP BY date
			ORDER BY date";
	*/
	$sql = "SELECT DISTINCT a.id
			FROM mgw_calendar a, mgw_calendar b
			WHERE a.inituserid = '$currentuserid' AND a.id_class != '2' 
			AND a.id = b.id AND b.id_class != '3' AND b.id_class != '2'";

	if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
	if(($n = $res->NumRows())>0){
		//echo getParmLang(getLanguageString("test"), array("TOTO"));
		$appt_subj_by_me = "<a href=\"".ROOTURL."modules/calendar/subj_followup.php?action=view&".SID."\">"
							.Lang::getParmLang(Lang::getLanguageString("you_have_subjected_n_appt"), array($n))
							."</a>";
		$smarty->assign("appt_subj_by_me",$appt_subj_by_me);
	}
//	else $smarty->assign("appt_subj_by_me","");
	
	//all appointment subjected to current user and not answered (class 0):
	/*$sql = "SELECT DISTINCT b.id
			FROM mgw_calendar a, mgw_calendar b WHERE a.userid='$currentuserid' AND a.inituserid!='$currentuserid' AND a.id = b.id and b.id_class='0' 
			GROUP BY b.date
			ORDER BY b.date";		
	*/
	$sql = "SELECT DISTINCT id
			FROM mgw_calendar WHERE userid='$currentuserid' AND inituserid!='$currentuserid' 
			AND id_class='0'";
			
	if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
	if(($n = $res->NumRows())>0){
		
		$appt_subj_to_me = "<a href=\"".ROOTURL."modules/calendar/subj_followup.php?action=view&goto=_2&".SID."\">"
						   .Lang::getParmLang(Lang::getLanguageString("you_have_to_answer_to_n_subjections"), array($n))
						   ."</a>";
	$smarty->assign("appt_subj_to_me",$appt_subj_to_me);
	}
	

	//print_r(dirname(dirname($_SERVER["PHP_SELF"]))."/calendar/subj_followup.php?action=view&\"".SID);
	
	//return $smarty->fetch("file:".ROOTPATH.$folderseperator."modules".$folderseperator."calendar".$folderseperator."templates".$folderseperator."default".$folderseperator.$myEnv["output"].$folderseperator."calendar_overview.tpl");
	return $smarty->fetch("file:".ROOTPATH."/modules/calendar/templates/default/".$myEnv["output"]."/calendar_overview.tpl");
}
?>
