<?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
*/

// Day
// 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);

if (isset($_GET['jumpto_day'])) {
	$jumpto_day_time = strtotime2($_GET['jumpto_day']);
	if ($jumpto_day_time == -1) {
		$getdate = date('Ymd');
	} else {
		$getdate = date('Ymd', $jumpto_day_time);
	}
}

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

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

$this_day2 = $this_day;

$parse_month = date ('Ym', $unix_time);
$start_week_time = strtotime2(dateOfWeek($getdate, $week_start_day));

$dayborder = 0;

$nbrGridCols = 1;
if (isset($master_array[($getdate)]) and is_array($master_array[($getdate)])) {
	foreach($master_array[($getdate)] as $ovlKey => $ovlValue) {
		if ($ovlKey != '-1') {
			foreach($ovlValue as $ovl2Value) {
				$nbrGridCols = kgv($nbrGridCols, ($ovl2Value['event_overlap'] + 1));
			}
		}
	} 
}

$smarty->display("calendar2_index.tpl");
?>

<center>

<table border="0" cellspacing="0" cellpadding="0">
	<tr>
		<td width="520" valign="top">
<table width="520" 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="day.php?cal='.$cal.'&getdate='.$yesterdays_date.'&'.SID.'"><img src="'.IMGPATH.'/left_day.gif" alt="['.$hashmap["last_day_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="day.php?cal='.$cal.'&getdate='.$tomorrows_date.'&'.SID.'"><img src="'.IMGPATH.'/right_day.gif" alt="['.$hashmap["next_day_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">	
				<?php
					// The all day events returned here.
					if (isset($master_array[($getdate)]) and isset($master_array[($getdate)]['-1']) and $master_array[($getdate)]['-1']) {
						echo '<tr>'."\n";
						echo '<td colspan="3" height="24">'."\n";
						echo '<table width="100%" border="0" cellspacing="1" cellpadding="4">'."\n";
						foreach($master_array[($getdate)]['-1'] as $my_key => $allday) {
                            $start_date_full = localizeDate($hashmap["dateFormat_week_list"]." %Y", $unix_time);

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

							if ($all_cals[$my_key]["summary2"]!="") {
							  echo '<tr 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"].'\',\''.$start_date_full.'\',\'\''.$dadd.'); return false;" onmouseout="delayhidemenu()">'."\n";
							} else {
							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"].'\',\''.$start_date_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"><i>'.$all_cals[$my_key]["summary"].($all_cals[$my_key]["location"]!=""?" / ".$all_cals[$my_key]["location"]:"").'</i></font> '.make_homepage($my_key,false);

							if ($all_cals[$my_key]["summary2"]!="") {
							  if ($all_cals[$my_key]["summary2"]=="delete") {
							    echo '<br><br><table bgcolor="#ffffff" width="100%"><tr><td align="center" class="G10BOLD" colspan="2">'.$hashmap["really_delete"].'</td></tr><tr><td align="center"><a href="#" onclick="document.location=\'day.php?action=accept&'.SID.'\';" style="text-decoration: none"><img src="templates/default/media/accept.gif" border="0" title="" align="absmiddle"> '.
							  		 $hashmap["yes"].'</a></td><td align="center"><a href="../webmail2/index.php?'.SID.'" style="text-decoration: none"><img src="templates/default/media/decline.gif" border="0" title="" align="absmiddle"> '.$hashmap["no"].'</a></td></tr></table>';
							  } else {
							    echo '<br><br><table bgcolor="#ffffff" width="100%"><tr><td align="center"><a href="#" onclick="document.location=\'day.php?action=accept&'.SID.'\';" style="text-decoration: none"><img src="templates/default/media/accept.gif" border="0" title="" align="absmiddle">'.$hashmap["accept"].'</a>'.
							      '</td><td align="center"><a href="#" onclick="document.location=\'day.php?action=tentative&'.SID.'\';" style="text-decoration: none"><img src="templates/default/media/tentative.gif" border="0" title="" align="absmiddle">'.$hashmap["tentative"].
								  '</a></td><td align="center"><a href="#" onclick="document.location=\'day.php?action=decline&'.SID.'\';" style="text-decoration: none"><img src="templates/default/media/decline.gif" border="0" title="" align="absmiddle">'.$hashmap["decline"].'</a></td></tr></table>';
							  }
							}

							echo '</a></td>'."\n";
							echo '</tr>'."\n";
						}
						echo '</table>'."\n";
						echo '</td>'."\n";
						echo '</tr>'."\n";
					}
      				?>
      			<tr>
					<td align="center" valign="top" colspan="3">
						<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 for ($m=0;$m < $nbrGridCols;$m++) { 
									echo '<td><img src="templates/default/media/spacer.gif" width="' . (520 / $nbrGridCols) . '" height="1" alt=" "></td>';
								} ?>
							</tr>
							<?php
								// $master_array[($getdate)][$day_time]
								$event_length = array ();
								$border = 0;
                                
								foreach ($day_array as $key) {
									ereg('([0-9]{2})([0-9]{2})', $key, $regs_tmp);
									$cal_time = $key;
									$key = mktime($regs_tmp[1],$regs_tmp[2],0,$this_month,$this_day,$this_year);
									$key = date ($hashmap["timeFormat"], $key);
									unset($this_time_arr);
									
									// add events that overlap the start time
									if (isset($master_array[$getdate][$cal_time]) && sizeof($master_array[$getdate][$cal_time]) > 0) {
										$this_time_arr = $master_array[$getdate][$cal_time];
									}
									
									// add events that overlap $day_start instead of cutting them out completely
									if ("$day_start" == "$cal_time" && isset($master_array[$getdate]) && is_array($master_array[$getdate])) {
										foreach($master_array[$getdate] 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[$getdate]) && is_array($master_array[$getdate])) {
										foreach($master_array[$getdate] 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, "0");
                                            }
											$j = 0;
											while (isset($event_length[$j])) {
												if ($event_length[$j]['state'] == 'ended') {
													$event_length[$j] = array ('length' => ($drawEvent['draw_length'] / $gridLength), 'key' => $eventKey, 'overlap' => $loopevent['event_overlap'],'state' => 'begin');
													break;
												}
												$j++;
											}
											if ($j == sizeof($event_length)) {
												array_push ($event_length, array ('length' => ($drawEvent['draw_length'] / $gridLength), 'key' => $eventKey, 'overlap' => $loopevent['event_overlap'],'state' => 'begin'));
											}
										}
									}
									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";
									}
									if ($dayborder == 0) {
										$class = ' class="dayborder"';
										$dayborder++;
									} else {
										$class = ' class="dayborder2"';
										$dayborder = 0;
									}
									if (sizeof($event_length) == 0) {
										echo '<td bgcolor="#ffffff" colspan="' . $nbrGridCols . '" '.$class.' onclick="openEventInfo(\'\',\'\',\''.$cal_time.'\',\''.$this_day.'\',\''.$this_month.'\',\''.$this_year.'\')">&nbsp;</td>'."\n";
									} else {
										$emptyWidth = $nbrGridCols;
										for ($i=0;$i<sizeof($event_length);$i++) {
											$drawWidth = $nbrGridCols / ($event_length[$i]['overlap'] + 1);
											$emptyWidth = $emptyWidth - $drawWidth;
											switch ($event_length[$i]['state']) {
												case 'begin':
													$event_length[$i]['state'] = 'started';
													$my_key = $event_length[$i]['key'];
													$event_start 	= strtotime2 ($this_time_arr[$my_key]['event_start']);
													$event_end		= strtotime2 ($this_time_arr[$my_key]['event_end']);
													$event_start 	= date ($hashmap["timeFormat"], $event_start);
													$event_end 		= date ($hashmap["timeFormat"], $event_end);

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

                                                    $event_start_full   = $start_date_full." ".date($hashmap["timeFormat"], $this_time_arr[($event_length[$i]["key"])]["start_unixtime"]);
                                                    $event_end_full     = date($hashmap["timeFormat"], $this_time_arr[($event_length[$i]["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):"");
                                                    
                                                    if ($all_cals[$my_key]["summary2"]!="") {
													  echo '<td rowspan="' . $event_length[$i]['length'] . '" colspan="' . $drawWidth . '" align="left" valign="top" class="eventbg2" 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";
													} else {
													echo '<td rowspan="' . $event_length[$i]['length'] . '" colspan="' . $drawWidth . '" align="left" valign="top" class="eventbg2" 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()" onclick="openEventInfo(\''.$my_key.'\', \''.$all_cals[$my_key]["calendar"].'\')">'."\n";
													}
													echo '<table width="100%" border="0" cellspacing="0" cellpadding="2">'."\n";
													echo '<tr>'."\n";
													echo '<td class="eventborder"><font class="eventfont"><b>'.$event_start.'</b> - '.$event_end.'</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="eventfont">'.$all_cals[$my_key]["summary"].
													  ($all_cals[$my_key]["location"]!=""?" / ".$all_cals[$my_key]["location"]:"").'</font> '.make_homepage($my_key,true).
													  ($all_cals[$my_key]["summary2"]!=""?$all_cals[$my_key]["summary2"]=="delete"?'<br><br><table bgcolor="#ffffff" width="100%"><tr><td align="center" class="G10BOLD" colspan="2">'.$hashmap["really_delete"].'</td></tr><tr><td align="center"><a href="#" onclick="document.location=\'day.php?action=accept&'.SID.'\';" style="text-decoration: none"><img src="templates/default/media/accept.gif" border="0" title="" align="absmiddle"> '.
													  $hashmap["yes"].'</a></td><td align="center"><a href="#" onclick="window.close();" style="text-decoration: none"><img src="templates/default/media/decline.gif" border="0" title="" align="absmiddle"> '.$hashmap["no"].
													  '</a></td></tr></table>':'<br><br><table bgcolor="#ffffff" width="100%"><tr><td align="center"><a href="#" onclick="document.location=\'day.php?action=accept&'.SID.'\'"; style="text-decoration: none"><img src="templates/default/media/accept.gif" border="0" title="" align="absmiddle">'.
													  $hashmap["accept"].'</a></td><td align="center"><a href="#" onclick="document.location=\'day.php?action=tentative&'.SID.'\';" style="text-decoration: none"><img src="templates/default/media/tentative.gif" border="0" title="" align="absmiddle">'.$hashmap["tentative"].
													  '</a></td><td align="center"><a href="#" onclick="document.location=\'day.php?action=decline&'.SID.'\';" style="text-decoration: none"><img src="templates/default/media/decline.gif" border="0" title="" align="absmiddle">'.$hashmap["decline"].'</a></td></tr></table></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[$i]['length']--;
											if ($event_length[$i]['length'] == 0) {
												$event_length[$i]['state'] = 'ended';
											}
										}
										//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[(sizeof($event_length) - 1)]) && $event_length[(sizeof($event_length) - 1)]['state'] == 'ended') {
											array_pop($event_length);
										}
										
									}
									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>
</center>
<?php if (!$print) $smarty->display($appconf["gentemplates"]."/_footer.tpl"); ?>

