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

$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.49 $", __FILE__);
		
$concat=$conn->Concat("lastname", "', '", "firstname");
	  
$hol_monat = (isset($_GET["hol_monat"])) ? $_GET["hol_monat"] : (IsSet($_POST["hol_monat"]) ? $_POST["hol_monat"] : "");
$hol_jahr = (isset($_GET["hol_jahr"])) ? $_GET["hol_jahr"] : (IsSet($_POST["hol_jahr"]) ? $_POST["hol_jahr"] : "");

//  monthview back
if (isset($_GET["action"]) && $_GET["action"]=="mback") {
    if ($monat == 1) {  
	$monat = 12;
	$jahr--;
    }
    else $monat--;
    unset($_GET["action"]);
}			     


// monthview next
if (isset($_GET["action"]) && $_GET["action"]=="mforw") {
    if ($monat == 12) {  
	$monat = 1;
	$jahr++;
    }
    else $monat++;
    unset($_GET["action"]);
}

//  year back
if (isset($_GET["action"]) && $_GET["action"]=="yback") {
    $jahr--;
    unset($_GET["action"]);
}			     


//  year next
if (isset($_GET["action"]) && $_GET["action"]=="yforw") {
    $jahr++;
    unset($_GET["action"]);
}


// entry vacation view
if(!isset($_GET["action"])) {
    
    // entry dayview : set calendarid
    if (!session_is_registered('calendarid')) {
	$sql = "SELECT $concat AS name
		           	FROM mgw_users 
		          	WHERE NOT(level=".UDELETED.") AND a.id = $MGW->userid";
	
	if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
	$row = $res->FetchRow();
	
	$calendarid	 = $MGW->userid;
	$calendarname = $row["name"];
	session_register('calendarid');
	session_register('calendarname');
    }
    
    
    // Festlegung Monatskalender (falls nicht in Session gespeichert)
    if (!session_is_registered('calendar_month') && !session_is_registered('calendar_year')) {
	$calendar_month = date("m", $date);
	$calendar_year = date("Y", $date);
	session_register('calendar_month');
	session_register('calendar_year');
    }
    
    // Auswahlliste Kalenderinhaber
    $sql = "SELECT id, $concat AS name
		   			FROM mgw_users WHERE NOT(level=".UDELETED.")
					ORDER BY 2";
    
    if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
    while ($row = $res->FetchRow()) {
	$smarty->append("td2", $row);
    } 
    
    $smarty->assign('action', Lang::getLanguageString("action"));
    
    // Namensliste mit Urlaub
    $sql = "SELECT userid, $concat AS name, '25' as days, sum(CASE WHEN holiday_id=1 THEN 1 ELSE 0 END) AS used, 25-sum(CASE WHEN holiday_id=1 THEN 1 ELSE 0 END) as rest 
					FROM mgw_users a, mgw_calendar b 
					WHERE NOT(a.level=".UDELETED.") AND a.id = b.userid 
					group by 1,2
					order by 2";
    
    if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
    
    while ($row = $res->FetchRow()) {
	$myid = $row["userid"];
	$detlink  = "<a href=\"javascript:show_holi($myid,'".SID."')\" CLASS=\"edit\">Details</a>";
	$row["urls"] = $detlink;
	$smarty->append("td", $row);
    } 
    
    $kalender  = get_monthview_static( $date, $calendar_year, $calendar_month);
    $kalendername = "<FONT COLOR=#000000>$calendarname</FONT>";
    $feiertag  = "(" . get_celebrate_days($date, $MGW->holitemp) .	")";
    if ($feiertag == "()") $feiertag = "";
    
    $smarty->assign('SID', SID);
    $smarty->assign('header_date', $text);
    $smarty->assign('kalender', $kalender);
    $smarty->assign('nav_links', $zurueck);
    $smarty->assign('nav_rechts', $weiter);
    $smarty->assign('calendar_owner', $kalendername);
    $smarty->assign('date', $date);
    $smarty->assign('inh', "urlaub.php?action=inh&date=$date&".SID);
    $smarty->assign("holidayoverview", Lang::getParmLang(Lang::getLanguageString("holidayoverview"), array("<a class=\"edit\" href=\"urlaub.php?action=holiday_monat&".SID."\">","</a>")));
    
    $smarty->display('holiday_list.tpl');
}

// ==============================
// vacationsview per month
// ==============================
if(isset($_GET["action"]) && $_GET["action"] == "holiday_monat") {
    if (!isset($hol_monat) || $hol_monat == "") { $hol_monat = $monat; $hol_jahr = $jahr; }
    $vmonat = date("n", mktime(0,0,0,$hol_monat-1, 1, $hol_jahr)); 
    $vjahr  = date("Y", mktime(0,0,0,$hol_monat-1, 1, $hol_jahr));
    $nmonat = date("n", mktime(0,0,0,$hol_monat+1, 1, $hol_jahr)); 
    $njahr  = date("Y", mktime(0,0,0,$hol_monat+1, 1, $hol_jahr));
    
    $sql = "SELECT a.id, $concat AS name, b.date
					FROM mgw_users a, mgw_calendar b 
					WHERE NOT(a.level=".UDELETED.") AND a.id = b.userid and
						(substring(b.date, 5,2) = $hol_monat or substring(b.date,6,1) = $hol_monat) and
						substring(b.date, 1,4) = $hol_jahr and
						holiday_id = 1 
					ORDER BY 2, date";
    
    if(!$res = $conn->Execute($sql)) die(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
    
    $sql2 = "SELECT id, $concat AS name
					FROM mgw_users WHERE NOT(level=".UDELETED.")
					order by 2";
    
    if(!$res2 = $conn->Execute($sql2)) die(showSQLerror($sql2, $conn->ErrorMsg(), __LINE__, __FILE__));
    
    while ($row = $res->FetchRow()){
	$myid = $row["id"];
	$tag  = date("j", $res->UnixTimeStamp($row["date"]));  
	$arr_monat[$myid][$tag] = 1;
    }  
    
    $tage = date("t", mktime(0,0,0, $hol_monat, 1 ,$hol_jahr));
    $spalten = $tage+1;
    $hh = monthname( mktime(0,0,0, $hol_monat, 1 ,$hol_jahr));
    
    
    $out = "<table border=1 FRAME=BOX bgcolor=\"#838899\" cellspacing=0 cellpadding=0>";	   	
    $out .= "<tr height=20>";
    $out .= "<td align=\"center\" class=\"tab\" colspan=$spalten><a class=\"edit\" href=\"urlaub.php?action=holiday_monat&hol_monat=$vmonat&hol_jahr=$vjahr&".SID."\"><<</a>&nbsp;&nbsp;$hh&nbsp;&nbsp;<a class=\"edit\" href=\"urlaub.php?action=holiday_monat&hol_monat=$nmonat&hol_jahr=$njahr&".SID."\">>></a></td></tr>";
    $out .= "<tr height=20><td class=\"tab\" width=\"150\">Name</td>"; 
    for ($i=1;$i<=$tage;$i++) $out.="<td class=\"tab\" align=\"center\">&nbsp;$i&nbsp;</td>";
    $out .= "</tr>"; 
    
    while ($user = $res2->FetchRow()){
	$out .= "<tr height=20><td class=\"tab\"><a href=\"javascript:show_holi(".$user["id"].",'".SID."')\" CLASS=\"edit\">".$user["name"]."</a></td>\n";
	for ($i=1;$i<=$tage;$i++) {
	    $uid = $user["id"];
	    if (isset($arr_monat[$uid][$i])) $out.="<td width=\"25\" class=\"edit\" valign=center align=center><table height=12 width=20 border=0 cellspacing=0 cellpadding=0><tr><td class=\"edit\"></td></tr></table></td>\n";
	    else if (date("w", mktime(0,0,0,$hol_monat,$i,$hol_jahr)) == 0 || date("w", mktime(0,0,0,$hol_monat, $i, $hol_jahr))== 6) $out.="<td width=\"25\" class=\"wochenende\">&nbsp;</td>\n";
	    else if (get_celebrate_days(mktime(0,0,0,$hol_monat,$i,$hol_jahr), $MGW->holitemp) != "") $out.="<td width=\"25\" class=\"feiertag\">&nbsp;</td>\n";
	    else if (mktime(0,0,0,$hol_monat,$i,$hol_jahr) == mktime(0,0,0, date("w",  time()), date("j", time()), date("Y", time()))) $out.="<td width=\"25\" class=\"heute\">&nbsp;</td>\n";
	    else $out.="<td width=\"25\" class=\"tab\">&nbsp;</td>\n";
	}
	$out.="</tr>";
    }
    $out.="</table>";
    
    $kalender  = get_monthview_static( $date, $jahr, $monat);
    $kalendername = "<FONT COLOR=#000000>$calendarname</FONT>";
    $feiertag  = "(" . get_celebrate_days($date, $MGW->holitemp) .	")";
    if ($feiertag == "()") $feiertag = "";
    
    $smarty->assign('SID', SID);
    $smarty->assign('header_date', $text);
    $smarty->assign('kalender', $kalender);
    $smarty->assign('nav_links', $zurueck);
    $smarty->assign('nav_rechts', $weiter);
    $smarty->assign('kalendername', $kalendername);
    $smarty->assign('holiday_kal', $out);
    $smarty->assign('date', $date);
    $smarty->assign('inh', "urlaub.php?action=inh&date=$date&".SID);
    
    $smarty->assign("holidayoverviewmonth", Lang::getParmLang(Lang::getLanguageString("holidayoverviewmonth"),array("<a class=\"edit\" href=\"urlaub.php?".SID."\">", "</a>")));
    
    $smarty->display('holiday_monat.tpl');
}

// ==============================
// vacationview details
// ==============================	

if(isset($_GET["action"]) && $_GET["action"] == "details") {
    
    $sql0 = "SELECT a.id, $concat AS name, 
						'25' as days, sum(CASE WHEN holiday_id=1 THEN 1 ELSE 0 END) AS used, 25-sum(CASE WHEN holiday_id=1 THEN 1 ELSE 0 END) as rest 
					FROM mgw_users a left outer join mgw_calendar b 
		    		ON a.id = b.userid and holiday_id=1
	         		WHERE NOT(a.level=".UDELETED.") AND a.id = $uid 
					GROUP BY 1,2";
    
    $sql1 = "SELECT a.id, $concat AS name, b.date 
					FROM mgw_users a, mgw_calendar b 
		  			WHERE NOT(a.level=".UDELETED.") AND a.id = b.userid and
		        		a.id = $uid and	
		        		holiday_id=1 
					ORDER by date";
    if(!$res = $conn->Execute($sql0)) die(showSQLerror($sql0));
    if(!$res1 = $conn->Execute($sql1)) die(showSQLerror($sql1));
    
    $row = $res->FetchRow();
    
    while ($row1 = $res1->FetchRow()){
	$holday["wochentag"] = date("D", $res->UnixTimeStamp($row1["date"]));
	$holday["datum"]     = date("d.m.Y", $res->UnixTimeStamp($row1["date"]));  
	$smarty->append("holday", $holday);
    }  
    
    $smarty->assign("name", $row["name"]);
    $smarty->assign("days", $row["days"]);
    $smarty->assign("used", $row["used"]);
    $smarty->assign("rest", $row["rest"]);
    $smarty->assign('SID', SID);
    $smarty->assign('date', $date);
    
    $smarty->display('holiday_details.tpl');
} 
?>
