<?php
/* $Id: choose_week.php,v 1.4 2003/12/08 16:57:05 leyeti Exp $ */
error_reporting  (E_ERROR | E_WARNING | E_PARSE | E_NOTICE);
/**
 * Calendar : choose a week
 *
 * @version $Id: choose_week.php,v 1.4 2003/12/08 16:57:05 leyeti Exp $
 * @copyright 2003 
 **/
 	
	$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");
	include(INCLUDEPATH . "mSelectBox.class.php");


 	// create monthview-calendar, based on $date
    $calendar = get_monthview_static( $date, $_SESSION['calendar_year'], $_SESSION['calendar_month']);
    

	$target = (isset($_GET["target"])) ? $_GET["target"] : (IsSet($_POST["target"]) ? $_POST["target"] : "");
	if ($target=="") {
	    $p = strpos($_SERVER["HTTP_REFERER"], "?");
		$target = ($p>0) ? substr($_SERVER["HTTP_REFERER"],0,$p) : $_SERVER["HTTP_REFERER"];
	}
	
//echo "###".$_SERVER["HTTP_REFERER"]."#### $target<BR><BR>";	
	
	//$date = (isset($_GET["date"])) ? $_GET["date"] : (IsSet($_POST["date"]) ? $_POST["date"] : "");
	
	/*if ($date=="") {
	 $date = time();   
	}*/


// ===================================
// step one month back in calendar
// ===================================
if (isset($_GET['action']) && $_GET['action']=="mback") {
	if ($_SESSION['calendar_month'] == 1) {
		$_SESSION['calendar_month'] = 12;
		$_SESSION['calendar_year']--;
	} else {
		$_SESSION['calendar_month']--;
	}
	 unset($_GET['action']);
}

// ===================================
// step one month forward in calendar
// ===================================
if (IsSet($_GET['action']) && $_GET['action']=="mforw") {
	if ($_SESSION['calendar_month'] == 12) {
		$_SESSION['calendar_month'] = 1;
		$_SESSION['calendar_year']++;
	} else {
		$_SESSION['calendar_month']++;
	}
	unset($_GET['action']);
}

// ===================================
// step one year back in calendar
// ===================================
if (IsSet($_GET['action']) && $_GET['action']=="yback") {
	$_SESSION['calendar_year']--;
	unset($_GET['action']);
}

// ===================================
// step one year forward in calendar
// ===================================
if (IsSet($_GET['action']) && $_GET['action']=="yforw") {
	$_SESSION['calendar_year']++;
	unset($_GET['action']);
}


	
	///////////////
	
	$year  = $_SESSION['calendar_year'];
	$month = $_SESSION['calendar_month'];


//debug ECHO "***** year = $year, month = $month<br>";

	$today = date("Ymd", time());
	
	if ($_SESSION['calendarid']=="all") $sql_owner_part="";
	else $sql_owner_part="userid = ".$_SESSION['calendarid']." AND";

	$zeit = getdate($date);
	$day  = $zeit["mday"];						// Date im aktuellen Monat ?
	/// NATYS ///
	// no difference :
	/*
	if ($zeit["mon"] == $month  &&	$zeit["year"] == $year) {
		$header_class = "navlinkon"; // "stand";
	}	else {
		$header_class = "edit";
	}
	*/
	$header_class = "navlinkon";
	//// /NATYS ////
	
	// compute array for actual month holding the weekdays	
	$num_days = date("t",strtotime($month . "/1/". $year));

	$starts_at = 1; //0=su, 1=mo, ... from settings
	$trim_month = true; // from settings
	
	$days_before =  $starts_at-date("w",strtotime($month . "/1/". $year))+1;
	if($days_before>1) $days_before -= 7; 

	if($starts_at > 0) $ends_at = $starts_at - 1;
	else $ends_at = 6; 

	$days_after = $ends_at-date("w",strtotime("$month/$num_days/$year"));
	if($days_after < 0) $days_after += 7;

	$month_days = array();
	for($o=$days_before; $o<=$num_days+$days_after; $o++) {
		$day = array();
		$day["timestamp"] =  mktime(0,0,1,$month,$o,$year);
//		$day["sql_tag"] =  date("Ymd", $day["timestamp"]);
		$day["sql_tag"] =  $conn->DBTimeStamp($day["timestamp"]);
		$day["day_number"] =  date("j", $day["timestamp"]);
		$day["day_of_week"] =  date("w", $day["timestamp"]);

		$month_days[] = $day;
	}

    $mymonth = $month;
    if ($mymonth < 10 and strlen($mymonth) == 2) { $mymonth = substr($mymonth,1); }

	// HTML-Tabelle Überschriften Monat / Jahr
	$out = "<table border=\"0\" width=\"180\" cellpadding=\"0\" cellspacing=\"0\">\n";
	$out .= "<tr><td class=\"lines\" width=\"1\" height=\"1\" colspan=\"10\"><img src=\"". $appconf["imgpathgeneral"] ."/space.gif\" alt=\"\" width=\"1\" height=\"1\" /></td></tr>";
	$out .= "<tr class=\"stand\" height=\"20\">";
	$out .= "	<td class=\"lines\" width=\"1\" height=\"1\"><img src=\"". $appconf["imgpathgeneral"] ."/space.gif\" alt=\"\" width=\"1\" height=\"1\" /></td>";
	$out .= "	<td class=\"subnavback\">&nbsp;<a class=\"$header_class\" href=\"choose_week.php?action=mback&date=$date&".SID."\"><strong>&lt;&lt;&lt;</strong></a></td>";
	$out .= "	<td class=\"subnavback\" colspan=\"6\" align=\"center\">";
	$out .= "		<!-- <a class=\"navlink\" href=\"month.php?date=".$date."&".SID."\"> --> <div class=\"classicfont\"><strong>".Lang::getLanguageString("gentemp_month_name_".$mymonth)."</strong></div> <!-- </a> -->";
	$out .= "	</td>";
	$out .= "	<td class=\"subnavback\"><a class=\"$header_class\" href=\"choose_week.php?action=mforw&date=$date&".SID."\"> <strong>&gt;&gt;&gt;</strong></a>&nbsp;</td>";
	$out .= "	<td  class=\"lines\" width=\"1\" height=\"1\"><img src=\"". $appconf["imgpathgeneral"] ."/space.gif\" alt=\"\" width=\"1\" height=\"1\" /></td>";
	$out .= "</tr>\n";
	$out .= "<tr class=\"stand\" height=\"20\">";
	$out .= "	<td class=\"lines\" width=\"1\" height=\"1\"><img src=\"". $appconf["imgpathgeneral"] ."/space.gif\" alt=\"\" width=\"1\" height=\"1\" /></td>";
	$out .= "	<td class=\"subnavback\">&nbsp;<a class=\"$header_class\" href=\"choose_week.php?action=yback&date=$date&".SID."\"><strong>&lt;&lt;&lt;</strong></a></td>";
	$out .= "	<td class=\"subnavback\" colspan=\"6\" align=\"center\"><strong>$year</strong></td>";
	$out .= "	<td class=\"subnavback\"><a class=\"$header_class\" href=\"choose_week.php?action=yforw&date=$date&".SID."\"> <strong>&gt;&gt;&gt;</strong></a>&nbsp;</td>";
	$out .= "	<td class=\"lines\" width=\"1\" height=\"1\"><img src=\"". $appconf["imgpathgeneral"] ."/space.gif\" alt=\"\" width=\"1\" height=\"1\" /></td>";
	$out .= "</tr>\n";
	$out .= "<tr><td class=\"lines\" width=\"1\" height=\"1\" colspan=\"10\"><img src=\"". $appconf["imgpathgeneral"] ."/space.gif\" alt=\"\" width=\"1\" height=\"1\" /></td></tr>";

	$out .= "<tr class=\"stand\" height=\"20\">";
	$out .= "	<td class=\"lines\" width=\"1\" height=\"1\"><img src=\"". $appconf["imgpathgeneral"] ."/space.gif\" alt=\"\" width=\"1\" height=\"1\" /></td>";
	$out .= "	<td class=\"category\" align=\"center\">&nbsp;" . Lang::getLanguageString("calweek") . "&nbsp;</td>";

	for($i=$starts_at; $i<$starts_at+7; $i++) // prints names of days
		$out .= "<td class=\"category\" align=\"center\">&nbsp;". Lang::getLanguageString("temp_weekday_abbr_".$i%7) . "&nbsp;</td>";

	$out .="	<td class=\"lines\" width=\"1\" height=\"1\"><img src=\"". $appconf["imgpathgeneral"] ."/space.gif\" alt=\"\" width=\"1\" height=\"1\" /></td>
	</tr>\n";

	$out .= "<tr><td class=\"lines\" width=\"1\" height=\"1\" colspan=\"10\"><img src=\"". $appconf["imgpathgeneral"] ."/space.gif\" alt=\"\" width=\"1\" height=\"1\" /></td></tr>";
	
	for($i=0;$i<count($month_days);$i++) {
		// today
		$day_tag = date("Y",$month_days[$i]["timestamp"]).date("m",$month_days[$i]["timestamp"]).date("d",$month_days[$i]["timestamp"]);
		if($day_tag == $today) $klasse = "heute";
		//weekend
		elseif ($month_days[$i]["day_of_week"]==6 || $month_days[$i]["day_of_week"]==0) $klasse = "wochenende";
		//end of last/beginning of next month
		elseif ($i<(abs($days_before)+1) || $i>abs($days_before)+$num_days) $klasse = "tab";
		//holiday
		elseif (get_celebrate_days($month_days[$i]["timestamp"], $_SESSION["MGW"]->settings["calendar_holiday_system"]) != "") $klasse = "feiertag";
		//absolutely ordinary day :))
		else $klasse = "tab";

		if($i%7==0) {
			$week_timestamp = $month_days[$i]["timestamp"];
			$out .= "<tr class=\"tab\"  height=20 bgcolor=\"#c8c9d4\">\n";
			$out .= "<td class=\"lines\" width=\"1\" height=\"1\"><img src=\"". $appconf["imgpathgeneral"] ."/space.gif\" alt=\"\" width=\"1\" height=\"1\" /></td>";
			$out .= "<td bgcolor=\"#838899\" align=\"center\"><a class=\"subnavlink\" href=\"javascript:void(0);\" onclick=\"javascript:copy_year(".date("Y",$week_timestamp).");copy_week(".get_weeknumber($week_timestamp).");window.close();\">" . get_weeknumber($week_timestamp) . "</a></td>\n";
		}
		
//		if($trim_month &&)
//		$sqldate = $conn->DBTimeStamp(strtotime($month_days[$i]["sql_tag"]));
		$sqldate = $month_days[$i]["sql_tag"];
		$sqldate_end = $conn->DBTimeStamp(mktime(23,59,59, date("m",$month_days[$i]["timestamp"]), date("d",$month_days[$i]["timestamp"]), date("Y",$month_days[$i]["timestamp"])) );
//		$sql = "SELECT COUNT(*) AS appointment_count FROM mgw_calendar WHERE ".$sql_owner_part." SUBSTRING(date,1,8) = $sqldate GROUP BY date ORDER BY date";
		$sql = "SELECT COUNT(*) AS appointment_count 
        		FROM mgw_calendar 
				WHERE ".$sql_owner_part." date >= $sqldate AND
        				date <= $sqldate_end
				GROUP BY date 
				ORDER BY date";
		if(!$res = $conn->Execute($sql)) exit(showSQLerror($sql, $conn->ErrorMsg(), __LINE__, __FILE__));
		$row = $res->FetchRow();
		if($row["appointment_count"]>0) $markday = " style=\"font-weight:bold;\" title=\"".$row["appointment_count"]."\" alt=\"".$row["appointment_count"]."\"";
		else $markday = "";

		/***************************/
		/* *** OLD :
		if(!$trim_month || ($trim_month && ($i>0 && $i<=$num_days+abs($days_before)))) {		
			$out .= "<td class=\"$klasse\" align=\"center\"$markday>";
			$out .= "<a class=\"subnavlink\" href=\"week.php?date=".$week_timestamp."&".SID."\">".$month_days[$i]["day_number"]."<!-- </a> --></td>\n";
		} else {
			$out .= "<td class=\"stand\"></td>";
		}
		*** */
		
		/* *** NEW : */
		//timestamp of 1st day of current month :
		$ts_curr_month = mktime(0, 0, 0, $month , 1 , $year );
		//timestamp of 1st day of next month :
		$next_month = ($month==12) ? 1 : $month+1;
		$next_year = ($month==12) ? $year+1 : $year;
		$ts_next_month = mktime(0, 0, 0, $next_month , 1 , $next_year );
		
		//debug echo "curr : $ts_curr_month, next : $ts_next_month<BR>";
		
		if(!$trim_month || ($trim_month && ( ($i>0 or $month_days[$i]["timestamp"]==$ts_curr_month+1) && $i<=$num_days+abs($days_before)) && $month_days[$i]["timestamp"]<$ts_next_month )) {
			//the day don't belong to the month -> italic
			if ($month_days[$i]["timestamp"]<$ts_curr_month) {
		    	//debug echo $month_days[$i]["timestamp"]."<BR>";
				$out .= "<td class=\"stand\">";
				$out .= "&nbsp;&nbsp;<a class=\"subnavlink\"  href=\"javascript:void(0);\" onclick=\"javascript:copy_year(".date("Y",$week_timestamp).");copy_week(".get_weeknumber($week_timestamp).");window.close();\" ><I>".$month_days[$i]["day_number"]."</I></a></td>\n";
			}
			else {
			$out .= "<td class=\"$klasse\" align=\"center\"$markday>";
			$out .= "<a class=\"subnavlink\"  href=\"javascript:void(0);\" onclick=\"javascript:copy_year(".date("Y",$week_timestamp).");copy_week(".get_weeknumber($week_timestamp).");window.close();\" " /*$target?date=".$week_timestamp."&week=".get_weeknumber($week_timestamp)."&".SID*/.">".$month_days[$i]["day_number"]."</a></td>\n";
			}
		} 
		else {
			$out .= "<td class=\"stand\"></td>";
		}
		
		/***************************/
			
		if(($i+1)%7==0) {
			$out .= "<td class=\"lines\" width=\"1\" height=\"1\"><img src=\"". $appconf["imgpathgeneral"] ."/space.gif\" alt=\"\" width=\"1\" height=\"1\" /></td>\n";
			$out .= "</tr>\n";
		}
	}

	//$out .= "<td class=\"lines\" width=\"1\" height=\"1\"><img src=\"". $appconf["imgpathgeneral"] ."/space.gif\" alt=\"\" width=\"1\" height=\"1\" /></td>";
	//$out .= "</tr>\n";
	$out .= "<tr><td class=\"lines\" width=\"1\" height=\"1\" colspan=\"10\"><img src=\"". $appconf["imgpathgeneral"] ."/space.gif\" alt=\"\" width=\"1\" height=\"1\" /></td></tr>";
	$out .= "</table>\n";
	
	
	
	
	//////////////
	
	$calendar = $out;
	
    // assign variables to template
	//$smarty->assign("target",$target);
    $smarty->assign('month_calendar', 	$calendar);
	$smarty->display("choose_week.tpl");
?>