<?php
/*
This file is part of Thomas Bley's moregroupware webmail2/calendar2/admin2 module. (tb-mgw)

More information:
-----------------

Simple Groupware Solutions Thomas Bley
Fichtenstr. 23
Germany, 85640 Putzbrunn

Contact:
--------

info@simple-groupware.de

http://www.simple-groupware.de


Copyright 2002-2003 Thomas Bley
tb-mgw is distributed under the terms of the GNU General Public License 

tb-mgw is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

tb-mgw is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with tb-mgw; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

// Week
// Moregroupware.org

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

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

error_reporting  (E_ERROR | E_WARNING | E_PARSE);

$current_view = "week";
include(CAL2_INC_DIR.'ical_parser.php');
$minical_view = "week";

store_calendar_view($current_view);
store_calendar_date($getdate);

$next_week = date("Ymd", strtotime2("+1 week",  $unix_time));
$prev_week = date("Ymd", strtotime2("-1 week",  $unix_time));
$tomorrows_date = date( "Ymd", strtotime2("+1 day",  $unix_time));
$yesterdays_date = date( "Ymd", strtotime2("-1 day",  $unix_time));
$start_week_time = strtotime2(dateOfWeek($getdate, $week_start_day));
$end_week_time = $start_week_time + (6 * 25 * 60 * 60);
$start_week = localizeDate($hashmap["dateFormat_week"], $start_week_time);
$end_week =  localizeDate($hashmap["dateFormat_week"], $end_week_time);
$display_date = "$start_week - $end_week";
$this_day2 = "";

$dayborder = 0;
$thisdate = $start_week_time;
for ($i=0;$i<7;$i++) {
	$thisday = date("Ymd", $thisdate);
	$nbrGridCols[$thisday] = 1;
	if (isset($master_array[$thisday])) {
		foreach($master_array[($thisday)] as $ovlKey => $ovlValue) {
			if ($ovlKey != "-1") {
				foreach($ovlValue as $ovl2Value) {
					$nbrGridCols[($thisday)] = kgv($nbrGridCols[($thisday)], ($ovl2Value["event_overlap"] + 1));
				}
			}
		} 
	}
	$thisdate = ($thisdate + (25 * 60 * 60));
}
$smarty->display("calendar2_index.tpl");
?>

<center>
<table border="0" cellspacing="0" cellpadding="0">
	<tr>
		<td width="540" valign="top">
			<table width="540" border="0" cellspacing="0" cellpadding="0" class="calborder">
				<tr>
					<td align="center" valign="middle">
						<table width="100%" border="0" cellspacing="0" cellpadding="0">
							<tr>
								<td class="navback">
									<table width="100%" border="0" cellspacing="0" cellpadding="0">
										<tr>
											<td align="right" width="40%" class="navback"><?php echo "<a class=\"psf\" href=\"week.php?cal=$cal&getdate=$prev_week&".SID."\"><img src=\"".IMGPATH."/left_day.gif\" alt=\"[".$hashmap["last_week_lang"]."]\" border=\"0\" align=\"right\"></a>"; ?></td>
											<td align="center" width="20%" class="navback" nowrap valign="middle"><font class="H20"><?php echo $display_date; ?></font></td>
											<td align="left" width="40%" class="navback"><?php echo "<a class=\"psf\" href=\"week.php?cal=$cal&getdate=$next_week&".SID."\"><img src=\"".IMGPATH."/right_day.gif\" alt=\"[".$hashmap["next_week_lang"]."]\" border=\"0\" align=\"left\"></a>"; ?></td>
										</tr>
									</table>
								</td>
							</tr>
						</table>
					</td>
				</tr>
				<tr>
					<td>
						<table width="100%" border="0" cellspacing="0" cellpadding="0" class="G10B">      			
							<tr>
								<td align="center" valign="top">
									<table width="100%" border="0" cellspacing="0" cellpadding="0">
										<tr>
											<td width="60"><img src="templates/default/media/spacer.gif" width="60" height="1" alt=" "></td>
											<td width="1"></td>
											<?php
											$thisdate = $start_week_time;
											$i = 0;
											do {
												$thisday = date("Ymd", $thisdate);
												$colWidth = round(70 / $nbrGridCols[$thisday]);
												for ($j=0;$j < $nbrGridCols[$thisday];$j++) {
													echo "<td width=\"" . $colWidth . "\"><img src=\"templates/default/media/spacer.gif\" width=\"" . $colWidth . "\" height=\"1\" alt=\" \"></td>\n";
												}
												$thisdate = ($thisdate + (25 * 60 * 60));
												$i++;
											} while ($i < 7);
											?>
										</tr>
										<?php
										
										// print out the day names here
										echo "<tr>";
										$thisdate = $start_week_time;
										$i = 0;
										echo "<td class=\"dateback\" width=\"60\"><img src=\"templates/default/media/spacer.gif\" width=\"1\" height=\"12\" alt=\" \"></td>";
										echo "<td class=\"dateback\" width=\"1\"></td>";
										do {
											$thisday = date("Ymd", $thisdate);
											$day = date("d", $thisdate);
											$minical_month = date("m", $thisdate);
											$thisday3 = localizeDate($hashmap["dateFormat_week_list"], $thisdate);
                                            
                                            $theclass = "psf";
                                            if ($today_day == $day and $today_month == $minical_month) {
                                              $theclass = "psf2";
                                            }
                                            
											echo "<td nowrap colspan=\"" . $nbrGridCols[$thisday] . "\" valign=\"top\" align=\"center\" class=\"dateback\">\n";
											echo "<font class=\"V9\"><a class=\"".$theclass."\" href=\"day.php?cal=$cal&getdate=$thisday&".SID."\">$thisday3</a></font>\n";
											echo "</td>\n";
											$thisdate = ($thisdate + (25 * 60 * 60));
											$i++;
										} while ($i < 7);
										echo "</tr>";
										?>
										<tr>
											<td width="60"><img src="templates/default/media/spacer.gif" width="60" height="1" alt=" "></td>
											<td width="1"></td>
											<?php
											$thisdate = $start_week_time;
											$i = 0;
											do {
												$thisday = date("Ymd", $thisdate);
												$colWidth = round(70 / $nbrGridCols[$thisday]);
												for ($j=0;$j < $nbrGridCols[$thisday];$j++) {
													echo "<td width=\"" . $colWidth . "\"><img src=\"templates/default/media/spacer.gif\" width=\"" . $colWidth . "\" height=\"1\" alt=\" \"></td>\n";
												}
												$thisdate = ($thisdate + (25 * 60 * 60));
												$i++;
											} while ($i < 7);
											?>
										</tr>
										<?php
										// The all day events returned here.
										$allday_events_this_week = false;
										$thisdate = $start_week_time;
										for ($i=0;$i<7;$i++) {
											$thisday = date("Ymd", $thisdate);
											if (isset($master_array[($thisday)]["-1"])) {
												$allday_events_this_week = true;
												break;
											}
											$thisdate = ($thisdate + (25 * 60 * 60));
										}
										if ($allday_events_this_week) {
											$thisdate = $start_week_time;
											$i = 0;
											echo "<tr>\n";
											echo "<td class=\"dateback\" colspan=\"2\"></td>";
											do {
												$thisday = date("Ymd", $thisdate);
												echo "<td class=\"dateback\" height=\"20\" colspan=\"" . $nbrGridCols[$thisday] . "\" valign=\"bottom\">\n";
												if (isset($master_array[($thisday)]["-1"])) {
													echo "<table width=\"100%\" border=\"0\" cellspacing=\"1\" cellpadding=\"4\" class=\"V9\">\n";
													foreach($master_array[($thisday)]["-1"] as $my_key => $allday) {
														$all_day_text = word_wrap($all_cals[$my_key]["summary"], 12, $allday_week_lines);
                                                        
                                                        $start_date_full = localizeDate($hashmap["dateFormat_week_list"]." %Y", $thisdate);
                                                        $event_start_full   = $start_date_full;
                                                        $event_end_full     = "";

														$event_start 	= '';
														$event_end		= '';

														$dadd = make_dadd($my_key);
														$dadd .= (isset($allday["recur"])?build_rep_showmenu($allday["recur"],$my_key):"");

														echo "<tr onclick=\"openEventInfo('$my_key','".$all_cals[$my_key]["calendar"]."')\" onmouseover=\"showmenu(event,'".get_calendar_display($all_cals[$my_key]["calendar"])."','".addslashes(htmlentities($all_cals[$my_key]["summary"]))."','".addslashes(htmlentities($all_cals[$my_key]["description"]))."','".addslashes(htmlentities($all_cals[$my_key]["location"]))."','".$all_cals[$my_key]["class"]."','".$all_cals[$my_key]["categories"]."','".$event_start_full."','".$event_end_full."'".$dadd."); return false;\" onmouseout=\"delayhidemenu()\">\n";
														echo "<td valign=\"top\" align=\"center\" class=\"eventbg\" style=\"background-color: ".$all_cals[$my_key]["color"]."\"><a class=\"psf\" href=\"#\"><font color=\"#ffffff\">".$all_day_text.($all_cals[$my_key]["location"]!=""?" ".$all_cals[$my_key]["location"]." ":"")."</font></a> ".make_homepage($my_key,false)."</td>\n";
														echo "</tr>\n";
													}
													echo "</table>\n";
													}
												echo "</td>\n";
												$thisdate = ($thisdate + (25 * 60 * 60));
												$i++;
											} while ($i < 7);
											echo "</tr>\n";
										}
											// $master_array[($getdate)]["$day_time"]
											
											$thisdate = $start_week_time;
											for ($i=0;$i<7;$i++) {
												$thisday = date("Ymd", $thisdate);
												$event_length[$thisday] = array ();
												$thisdate = ($thisdate + (25 * 60 * 60));
											}
											foreach ($day_array as $key) {
												$cal_time = $key;
												ereg('([0-9]{2})([0-9]{2})', $key, $regs_tmp);
												$key = mktime($regs_tmp[1],$regs_tmp[2],0,$this_month,$this_day,$this_year);
												$key = date ($hashmap["timeFormat"], $key);
																					
			
												if (ereg("([0-9]{1,2}):00", $key)) {
													echo "<tr height=\"" . $gridLength . "\">\n";
													echo "<td rowspan=\"" . (60 / $gridLength) . "\" align=\"center\" valign=\"top\" width=\"60\" class=\"timeborder\">$key</td>\n";
													echo "<td width=\"1\" height=\"" . $gridLength . "\"></td>\n";
												} elseif("$cal_time" == "$day_start") {
													$size_tmp = 60 - (int)substr($cal_time,2,2);
													echo "<tr height=\"" . $gridLength . "\">\n";
													echo "<td rowspan=\"" . ($size_tmp / $gridLength) . "\" align=\"center\" valign=\"top\" width=\"60\" class=\"timeborder\">$key</td>\n";
													echo "<td width=\"1\" height=\"" . $gridLength . "\"></td>\n";
												} else {
			
													echo "<tr height=\"" . $gridLength . "\">\n";
													echo "<td width=\"1\" height=\"" . $gridLength . "\"></td>\n";
												}
												
												// initialize $thisdate again
												$thisdate = $start_week_time;
												
												// loop this part 7 times, one for each day
												
												for ($week_loop=0; $week_loop<7; $week_loop++) {
													$thisday = date("Ymd", $thisdate);
                                                    $this__day = date("d", $thisdate);
                                                    $this__month = date("m", $thisdate);
                                                    $this__year = date("Y", $thisdate);
													$dayborder = 0;
													unset($this_time_arr);
													if (isset($master_array[$thisday][$cal_time]) && sizeof($master_array[$thisday][$cal_time]) > 0) {
														$this_time_arr = $master_array[$thisday][$cal_time];
													}
													if ("$day_start" == "$cal_time" && isset($master_array[$thisday]) && is_array($master_array[$thisday])) {
														foreach($master_array[$thisday] as $time_key => $time_arr) {
															if ((int)$time_key < (int)$cal_time && is_array($time_arr) && $time_key != '-1') {
																foreach($time_arr as $my_key => $event_tmp) {
																	// if ((int)$event_tmp['event_end'] > (int)$cal_time) {
																	
                                                                    $this_time_arr[$my_key] = $event_tmp;
																	// }
																}
															} else {
																// break;
															}
														}
													}

	                								if (str_date_to_int($day_end)-$gridLength*60 == str_date_to_int($cal_time) && isset($master_array[$thisday]) && is_array($master_array[$thisday])) {
														foreach($master_array[$thisday] as $time_key => $time_arr) {
															if ((int)$time_key > (int)$cal_time && is_array($time_arr) && $time_key != '-1') {
																foreach($time_arr as $my_key => $event_tmp) {
																		$this_time_arr[$my_key] = $event_tmp;
																}
															} else {
																// break;
															}
														}
													}
													
													
													// check for eventstart 
													if (isset($this_time_arr) && sizeof($this_time_arr) > 0) {
														foreach ($this_time_arr as $eventKey => $loopevent) {
                                                            if ((int)$cal_time <  (int)$loopevent["event_end"]) {
															  $drawEvent = drawEventTimes ($cal_time, $loopevent["event_end"]);
                                                            } else {
															  $drawEvent = drawEventTimes ($cal_time, "");
                                                            }
															$j = 0;
															while (isset($event_length[$thisday][$j])) {
																if ($event_length[$thisday][$j]["state"] == "ended") {
																	$event_length[$thisday][$j] = array ("length" => ($drawEvent["draw_length"] / $gridLength), "key" => $eventKey, "overlap" => $loopevent["event_overlap"],"state" => "begin");
																	break;
																}
																$j++;
															}
															if ($j == sizeof($event_length[$thisday])) {
                                                                array_push ($event_length[$thisday], array ("length" => ($drawEvent["draw_length"] / $gridLength), "key" => $eventKey, "overlap" => $loopevent["event_overlap"],"state" => "begin"));
															}
														}
													}
                                                        
													if (sizeof($event_length[$thisday]) == 0) {
														if ($dayborder == 0) {
															$class = " class=\"weekborder\"";
															$dayborder++;
														} else {
															$class = "";
															$dayborder = 0;
														}
														
														echo "<td bgcolor=\"#ffffff\" colspan=\"" . $nbrGridCols[$thisday] . "\" ".$class." onclick=\"openEventInfo('','','".$cal_time."','".$this__day."','".$this__month."','".$this__year."')\">&nbsp;</td>\n";
														
													} else {
														$emptyWidth = $nbrGridCols[$thisday];
														for ($i=0;$i<sizeof($event_length[$thisday]);$i++) {
														
															$drawWidth = $nbrGridCols[$thisday] / ($event_length[$thisday][$i]["overlap"] + 1);
															$emptyWidth = $emptyWidth - $drawWidth;
															switch ($event_length[$thisday][$i]["state"]) {
																case "begin":
																	$event_length[$thisday][$i]["state"] = "started";
																	$my_key = $event_length[$thisday][$i]["key"];
																			
																	$event_text_short = word_wrap($all_cals[$my_key]["summary"], 25, $week_events_lines);
																	$event_start 	= $this_time_arr[$my_key]["event_start"];
																	$event_end		= $this_time_arr[$my_key]["event_end"];
																	$event_start 	= date ($hashmap["timeFormat"], strtotime2($event_start));
																	$event_end 		= date ($hashmap["timeFormat"], strtotime2($event_end));

                                                                    $start_date_full = localizeDate($hashmap["dateFormat_week_list"]." %Y", $this_time_arr[$my_key]["start_unixtime"]);
                                                                    $end_date_full = localizeDate($hashmap["dateFormat_week_list"]." %Y", $this_time_arr[$my_key]["end_unixtime"]);

                                                                    $event_start_full   = $start_date_full." ".date($hashmap["timeFormat"], $this_time_arr[$my_key]["start_unixtime"]);
                                                                    $event_end_full     = date($hashmap["timeFormat"], $this_time_arr[$my_key]["end_unixtime"]);
                                                                    if ($start_date_full != $end_date_full) { $event_end_full = $end_date_full." ".$event_end_full; }
	                                                    
																	$dadd = make_dadd($my_key);
																	$dadd .= (isset($this_time_arr[$my_key]["recur"])?build_rep_showmenu($this_time_arr[$my_key]["recur"],$my_key):"");

																	echo "<td rowspan=\"" . $event_length[$thisday][$i]["length"] . "\" colspan=\"" . $drawWidth . "\" align=\"left\" valign=\"top\" class=\"eventbg2week\" onclick=\"openEventInfo('".$my_key."','".$all_cals[$my_key]["calendar"]."')\" onmouseover=\"showmenu(event,'".get_calendar_display($all_cals[$my_key]["calendar"])."','".addslashes(htmlentities($all_cals[$my_key]["summary"]))."','".addslashes(htmlentities($all_cals[$my_key]["description"]))."','".addslashes(htmlentities($all_cals[$my_key]["location"]))."','".$all_cals[$my_key]["class"]."','".$all_cals[$my_key]["categories"]."','".$event_start_full."','".$event_end_full."'".$dadd."); return false;\" onmouseout=\"delayhidemenu()\">\n";
																	echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n";
																	echo "<tr>\n";
																	echo "<td class=\"eventborder\"><font class=\"V10WB\"><b>$event_start</b></font></td>\n";
																	echo "</tr>\n";
																	echo "<tr>\n";
																	echo "<td>\n";
																	echo "<table width=\"100%\" border=\"0\" cellpadding=\"1\" cellspacing=\"0\">\n";
																	echo "<tr>\n";
																	echo "<td class=\"eventbg\" style=\"background-color: ".$all_cals[$my_key]["color"]."\"><a class=\"psf\" href=\"#\"><font class=\"V10W\">".$event_text_short.($all_cals[$my_key]["location"]!=""?"<br>".$all_cals[$my_key]["location"]."<br>":"")."</font> ".make_homepage($my_key,true)."</a></td>\n";
																	echo "</tr>\n";
																	echo "</table>\n";
																	echo "</td>\n";           
																	echo "</tr>\n";
																	echo "</table>\n";
																	echo "</td>\n";
																	break;
																case "started":
																	break;
																case "ended":
																	echo "<td bgcolor=\"#ffffff\" colspan=\"" . $drawWidth . "\" $class  onclick=\"openEventInfo('','','".$cal_time."','".$this__day."','".$this__month."','".$this__year."')\">&nbsp;</td>\n";
																	break;
															}
															$event_length[$thisday][$i]["length"]--;
															if ($event_length[$thisday][$i]["length"] == 0) {
																$event_length[$thisday][$i]["state"] = "ended";
															}
														}
// echo $emptyWidth;

														//fill emtpy space on the right
														if ($emptyWidth > 0) {
															echo "<td bgcolor=\"#ffffff\" colspan=\"" . $emptyWidth . "\" ".$class." onclick=\"openEventInfo('','','".$cal_time."','".$this__day."','".$this__month."','".$this__year."')\">&nbsp;</td>\n";
														}
														while (isset($event_length[$thisday][(sizeof($event_length[$thisday]) - 1)]["state"]) && $event_length[$thisday][(sizeof($event_length[$thisday]) - 1)]["state"] == "ended") {
															array_pop($event_length[$thisday]);
														}
													}
													$thisdate = ($thisdate + (25 * 60 * 60));
												}
												echo "</tr>\n";
											}
			
										?>
								</table>
							</td>
						</tr>
						</table>
					</td>
				</tr>
			</table>
		</td>
<?php if (!$print) { ?>
		<td width="20"><img src="templates/default/media/spacer.gif" width="20" height="1" alt=" "></td>
		<td width="160" valign="top">
			<?php include (CAL2_INC_DIR.'sidebar.php'); ?>
			<?php echo $calendar2_footer; ?>
		</td>
<?php } ?>
	</tr>
</table>
<?php if (!$print) $smarty->display($appconf["gentemplates"]."/_footer.tpl"); ?>

