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

// Month
// 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 = "month";
include(CAL2_INC_DIR.'ical_parser.php');
$minical_view = 'month';

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

$date = mktime(0,0,0,"$this_month","$this_day","$this_year");

// find out next month
$next_month_month = ($this_month+1 == '13') ? '1' : ($this_month+1);
$next_month_day = $this_day;
$next_month_year = ($next_month_month == '1') ? ($this_year+1) : $this_year;
while (!checkdate($next_month_month,$next_month_day,$next_month_year)) $next_month_day--;
$next_month_time = mktime(0,0,0,$next_month_month,$next_month_day,$next_month_year);

// find out last month
$prev_month_month = ($this_month-1 == '0') ? '12' : ($this_month-1);
$prev_month_day = $this_day;
$prev_month_year = ($prev_month_month == '12') ? ($this_year-1) : $this_year;
while (!checkdate($prev_month_month,$prev_month_day,$prev_month_year)) $prev_month_day--;
$prev_month_time = mktime(0,0,0,$prev_month_month,$prev_month_day,$prev_month_year);

$next_month = date("Ymd", $next_month_time);
$prev_month = date("Ymd", $prev_month_time);

$display_month = localizeDate ($hashmap["dateFormat_month"], $date);
$parse_month = date ("Ym", $date);
$first_of_month = $this_year.$this_month."01";
$start_month_day = dateOfWeek($first_of_month, $week_start_day);
$num_of_events = 0;

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

<center>
<table width="735" border="0" cellspacing="0" cellpadding="0" class="calborder">
	<tr>
		<td align="center" valign="middle" bgcolor="white">
			<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=\"month.php?cal=$cal&getdate=$prev_month&".SID."\"><img src=\"".IMGPATH."/left_day.gif\" alt=\"[".$hashmap["last_month_lang"]."]\" border=\"0\" align=\"right\"></a>"; ?></td>
								<td align="center" width="20%" class="navback" nowrap valign="middle"><font class="H20"><?php echo $display_month; ?></font></td>
      							<td align="left" width="40%" class="navback"><?php echo "<a class=\"psf\" href=\"month.php?cal=$cal&getdate=$next_month&".SID."\"><img src=\"".IMGPATH."/right_day.gif\" alt=\"[".$hashmap["next_month_lang"]."]\" border=\"0\" align=\"left\"></a>"; ?></td>
      						</tr>
      					</table>
      				</td>
      			</tr>
      		</table>
		</td>
	</tr>
	<tr>
		<td align="center" valign="top">
			<table width="100%" border="0" cellspacing="0" cellpadding="0">
				<tr>
					<td colspan="7" width="735"><img src="templates/default/media/spacer.gif" width="735" height="1" alt=" "></td>
				</tr>
				<tr>
					<?php
						// loops through 7 times, starts with $week_start_day
						$start_day = strtotime2($week_start_day);
						for ($i=0; $i<7; $i++) {
							$day_num = date("w", $start_day);
							$day = $daysofweek_lang[$day_num];
							echo "<td valign=\"top\" width=\"105\" height=\"12\" class=\"dateback\"><center><font class=\"V9BOLD\">$day</font></center></td>";
							$start_day = strtotime2("+1 day", $start_day);
						}
					?>	
				</tr>
				<tr>
					<td width="105"><img src="templates/default/media/spacer.gif" width="105" height="1" alt=" "></td>
					<td width="105"><img src="templates/default/media/spacer.gif" width="105" height="1" alt=" "></td>
					<td width="105"><img src="templates/default/media/spacer.gif" width="105" height="1" alt=" "></td>
					<td width="105"><img src="templates/default/media/spacer.gif" width="105" height="1" alt=" "></td>
					<td width="105"><img src="templates/default/media/spacer.gif" width="105" height="1" alt=" "></td>
					<td width="105"><img src="templates/default/media/spacer.gif" width="105" height="1" alt=" "></td>
					<td width="105"><img src="templates/default/media/spacer.gif" width="105" height="1" alt=" "></td>
				</tr>
			</table>
		</td>
	</tr>
	<tr>
			<td align="center" valign="top">
			<table width="100%" border="0" cellspacing="1" cellpadding="0" class="monthback">
				<tr>
				<?php 	 
					$sunday = strtotime2("$start_month_day");
					$i = 0;
					$whole_month = TRUE;
					$num_of_events = 0;
					do {
						$day = date ("j", $sunday);
						$daylink = date ("Ymd", $sunday);
						$check_month = date ("m", $sunday);
						$check_week = date ("W", $sunday);
						$check_year = date ("Y", $sunday);
						if ($check_month != $this_month) {
							$day= "<font class=\"G10G\">$day</font>";
							$bgclass="class=\"monthoff\"";
						} else {
							if ($today_today == $daylink) {
								$bgclass="class=\"monthon\"";
							} else {
								$bgclass="class=\"monthreg\"";
							}
						}
						$style = "";
						if ($check_week==$today_week) {
						  $style = 'style="background-color: #C0FFC0"';
						}
						if ($i == 0) echo "<tr height=\"105\"><td class=\"dateback\" ".$style."><a href=\"week.php?cal=".$cal."&getdate=".$daylink."&".SID."\" class=\"psf\">".$check_week."</a></td>\n";

                        $theclass = "psf";
                        if ($today_day == $day and $today_month == $check_month and $today_year == $check_year) {
                          $theclass = "psf2";
                        }

						if (isset($master_array[("$daylink")]) && ($check_month == $this_month)) {
							echo "<td align=\"center\" valign=\"top\" $bgclass width=\"105\" height=\"105\" ".$style." onclick=\"window.location.href='day.php?cal=$cal&getdate=$daylink&".SID."'\">\n";
							echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"1\">\n";
							echo "<tr>\n";
							echo "<td align=\"right\" valign=\"top\" class=\"G10B\">\n";
							echo "<a class=\"".$theclass."\" href=\"day.php?cal=$cal&getdate=$daylink&".SID."\">$day</a>\n";
							echo "</td>\n";
							echo "</tr>\n";
							if ($master_array[("$daylink")]) {
								foreach ($master_array[("$daylink")] as $event_times) {
									foreach ($event_times as $my_key => $val) {
										$num_of_events++;
										
											$event_start 	= @$val["event_start"];
											$event_end 		= @$val["event_end"];
											$event_start 	= date ($hashmap["timeFormat"], @strtotime2 ("$event_start"));
											$event_start2 	= date ($hashmap["timeFormat_small"], @strtotime2 ("$event_start"));
											$event_end 		= date ($hashmap["timeFormat"], @strtotime2 ("$event_end"));
											$event_text_short = word_wrap($all_cals[$my_key]["summary"], 12, $month_event_lines);

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

											echo "<tr>\n";
											echo "<td>\n";
											echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">\n";
											echo "<tr style=\"background-color: ".$all_cals[$my_key]["color"].";\">\n";
											if (!isset($val["event_start"])) {
		                            			$event_start_full = localizeDate($hashmap["dateFormat_week_list"]." %Y", $unix_time);
												$event_end_full = "";	

												echo "<td onclick=\"window.event.cancelBubble=true;openEventInfo('$my_key','".$all_cals[$my_key]["calendar"]."');\"><font class=\"V10\"><center><a class=\"psf\" href=\"#\" style=\"color:".($all_cals[$my_key]["color"]?"#FFFFFF":"")."\" ".'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()"'."><i>".$event_text_short."</i></a></center></font></td>\n";
											} else {	
		                                        $start_date_full = localizeDate($hashmap["dateFormat_week_list"]." %Y", $val["start_unixtime"]);
        		                                $end_date_full = localizeDate($hashmap["dateFormat_week_list"]." %Y", $val["end_unixtime"]);

                		                        $event_start_full   = $start_date_full." ".date($hashmap["timeFormat"], $val["start_unixtime"]);
  			            		                $event_end_full     = date($hashmap["timeFormat"], $val["end_unixtime"]);
                                		        if ($start_date_full != $end_date_full) { $event_end_full = $end_date_full." ".$event_end_full; }
											
												echo "<td onclick=\"window.event.cancelBubble=true;openEventInfo('$my_key','".$all_cals[$my_key]["calendar"]."')\" align=\"left\" valign=\"top\" ".'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()"'."><a class=\"psf\" href=\"#\"><font class=\"V9\" style=\"color:".($all_cals[$my_key]["color"]?"#FFFFFF":"")."\">&nbsp;$event_start2&nbsp;".$event_text_short."</font></a></td>\n";
											}
											echo "</tr>\n";
											echo "</table>\n";
											echo "</td>\n";
											echo "</tr>\n";
									}
								}
							}
							echo "</table>\n";
							echo "</td>\n";
						} else {
							echo "<td align=\"center\" valign=\"top\" $bgclass width=\"105\" height=\"105\" ".$style." onclick=\"window.location.href='day.php?cal=$cal&getdate=$daylink&".SID."'\">\n";
							echo "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"1\">\n";
							echo "<tr>\n";
							echo "<td align=\"right\" valign=\"top\" class=\"G10B\">\n";
							echo "<a class=\"".$theclass."\" href=\"day.php?cal=$cal&getdate=$daylink&".SID."\">$day</a>\n";
							echo "</td>\n";
							echo "</tr>\n";
							echo "</table>\n";
							echo "</td>\n";
						}
						$sunday = strtotime2("+1 day", $sunday); 
						$i++;
						if ($i == 7) { 
							echo "</tr>\n";
							$i = 0;
							$checkagain = date ("m", $sunday);
							if ($checkagain != $this_month) $whole_month = FALSE;	
						}
					} while ($whole_month == TRUE);
				?>
			</table>
		</td>
	</tr>
</table>
				<?php	
  $fake_getdate_time = strtotime2($this_year.'-'.$this_month.'-15');
?>

	<br>
	<table cellpadding="0" cellspacing="0" border="0" width="250">
		<tr>
			<td valign="middle" align="center">
				<table width="250" border="0" cellpadding="0" cellspacing="0" class="calborder">
					<tr>
						<td align="left" valign="top" width="1%" class="sideback"><img src="templates/default/media/spacer.gif" width="1" height="20" alt=" "></td>
						<td align="center" width="98%" class="sideback"><font class="G10BOLD"><?php echo $hashmap["legend"]; ?></font></td>
						<td align="right" valign="top" width="1%" class="sideback"></td>
					</tr>
					<tr>
						<td colspan="3" bgcolor="#FFFFFF" align="center">
							<table border="0" cellspacing="0" cellpadding="0" bgcolor="#FFFFFF" width="100%">
								<tr>
									<td><img src="templates/default/media/spacer.gif" width="21" height="9" alt=" "></td>
									<td></td>
									<td><img src="templates/default/media/spacer.gif" width="21" height="9" alt=" "></td>
									<td></td>
								</tr>
								<?php
  								  foreach ($cal_legend as $item) {
								    echo '
									<tr>
									  <td></td>
									  <td valign="middle">
									  	<table border="0" cellpadding="0" cellspacing="0" width="21" height="10"><tr><td style="height: 10; width: 21; background-color: '.$item[1].'"><img src="templates/default/media/spacer.gif" width="21" height="10" alt="" border="1"></td></tr></table>
									  </td>
									  <td></td>
									  <td class="G10B">'.htmlspecialchars(ucfirst($item[0])).'</td>
									</tr>
									<tr>
									  <td><img src="templates/default/media/spacer.gif" width="21" height="5" alt=" "></td>
									</tr>
									';
								  }
								?>
							</table>
						</td>
					</tr>
					<tr>
						<td colspan="3" bgcolor="#FFFFFF"><img src="templates/default/media/spacer.gif" width="148" height="6" alt=" "></td>
					</tr>
				</table>
			</td>
		</tr>
	</table>


<?php if (!$print) { ?>
<br>
<table border="0" width="737" cellspacing="0" cellpadding="0" bgcolor="#FFFFFF" class="calborder">
	<tr>
		<td width="50%" valign="top" align="center">
			<table cellpadding="0" cellspacing="0" border="0" width="160">
				<tr>
					<td valign="middle" align="center">
						<table width="160" border="0" cellpadding="0" cellspacing="0">
							<tr>
								<td align="left" valign="top" width="1"><img src="templates/default/media/spacer.gif" width="1" height="20" alt=" "></td>
								<td align="center"><font class="G10BOLD"><?php print (localizeDate ($hashmap["dateFormat_month"], strtotime2("-1 month", $fake_getdate_time))); ?></font></td>
								<td align="right" valign="top" width="1"></td>
							</tr>
							<tr>
								<td colspan="3" bgcolor="#FFFFFF" align="center">
									<table border="0" cellspacing="0" cellpadding="0" bgcolor="#FFFFFF">
										<tr>
											<td><img src="templates/default/media/spacer.gif" width="21" height="3" alt=" "></td>
											<td><img src="templates/default/media/spacer.gif" width="21" height="3" alt=" "></td>
											<td><img src="templates/default/media/spacer.gif" width="21" height="1" alt=" "></td>
											<td><img src="templates/default/media/spacer.gif" width="21" height="1" alt=" "></td>
											<td><img src="templates/default/media/spacer.gif" width="21" height="1" alt=" "></td>
											<td><img src="templates/default/media/spacer.gif" width="21" height="1" alt=" "></td>
											<td><img src="templates/default/media/spacer.gif" width="21" height="1" alt=" "></td>
											<td><img src="templates/default/media/spacer.gif" width="21" height="1" alt=" "></td>
										</tr>
										<tr><td></td>
										<?php
											$start_day = strtotime2($week_start_day);
											for ($i=0; $i<7; $i++) {
												$day_num = date("w", $start_day);
												$day = $daysofweekreallyshort_lang[$day_num];
												print "<td align=\"center\"><font class=\"G10BOLD\">$day</font></td>\n";
												$start_day = strtotime2("+1 day", $start_day); 
											}
										?>
										</tr>
										<tr height="3">
											<td colspan="8"><img src="templates/default/media/spacer.gif" width="1" height="3" alt=" "></td>
										</tr>
										<?php
											$minical_time = strtotime2("-1 month", $fake_getdate_time);
											$minical_month = date("m", $minical_time);
											$minical_year = date("Y", $minical_time);
											$first_of_month = $minical_year.$minical_month."01";
											$start_day = strtotime2(dateOfWeek($first_of_month, $week_start_day));
											$i = 0;
											$whole_month = TRUE;
											do {
												$day = date ("j", $start_day);
												$day2 = date ("d", $start_day);
												$minical_week = date("W", $start_day);
        	                                
												$style = "";
												if ($minical_week==$today_week) {
												  $style='style="background-color: #C0FFC0"';
												}

                                                $theclass = "psf";
                                                if ($today_day == $day and $today_month == $minical_month and $today_year == $minical_year) {
                                                  $theclass = "psf2";
                                                }

												$daylink = date ("Ymd", $start_day);
												$check_month = date ("m", $start_day);
												if ($check_month != $minical_month) $day= "<font class=\"G10G\">$day</font>";
												if ($i == 0) echo "<tr ".$style."><td align='center'><a href='week.php?cal=".$cal."&getdate=".$minical_year.$minical_month.$day2."&".SID."' class='psf'><font class='V9G'>".date ("W", $start_day)."</font></a></td>\n";
												if (isset($master_array[("$daylink")]) && ($check_month == $minical_month)) {
													echo "<td align=\"center\" class=\"G10B\">\n";
													echo "<a class=\"".$theclass."\" href=\"$minical_view.php?cal=$cal&getdate=$daylink&".SID."\" style=\"font-weight: bold; color: #00C000\">$day</a>\n";
													echo "</td>\n";
												} else {
													echo "<td align=\"center\" class=\"G10B\">\n";
													echo "<a class=\"".$theclass."\" href=\"$minical_view.php?cal=$cal&getdate=$daylink&".SID."\">$day</a>\n";
													echo "</td>\n";
												}
												$start_day = strtotime2("+1 day", $start_day); 
												$i++;
												if ($i == 7) { 
													echo "</tr>\n";
													$i = 0;
													$checkagain = date ("m", $start_day);
													if ($checkagain != $minical_month) $whole_month = FALSE;	
												}
											} while ($whole_month == TRUE);
										?>
									</table>
								</td>
							</tr>
							<tr>
								<td colspan="3" bgcolor="#FFFFFF"><img src="templates/default/media/spacer.gif" width="148" height="6" alt=" "></td>
							</tr>
						</table>
					</td>
				</tr>
			</table>
		</td>
		<td width="50%" valign="top" align="center">
	<table cellpadding="0" cellspacing="0" border="0" width="160">
		<tr>
			<td valign="middle" align="center">
				<table width="160" border="0" cellpadding="0" cellspacing="0">
					<tr>
						<td align="left" valign="top" width="1"><img src="templates/default/media/spacer.gif" width="1" height="20" alt=" "></td>
						<td align="center"><font class="G10BOLD"><?php print (localizeDate ($hashmap["dateFormat_month"], strtotime2("+1 month", $fake_getdate_time))); ?></font></td>
						<td align="right" valign="top" width="1"></td>
					</tr>
					<tr>
						<td colspan="3" bgcolor="#FFFFFF" align="center">
							<table border="0" cellspacing="0" cellpadding="0" bgcolor="#FFFFFF">
								<tr>
									<td><img src="templates/default/media/spacer.gif" width="21" height="3" alt=" "></td>
									<td><img src="templates/default/media/spacer.gif" width="21" height="3" alt=" "></td>
									<td><img src="templates/default/media/spacer.gif" width="21" height="1" alt=" "></td>
									<td><img src="templates/default/media/spacer.gif" width="21" height="1" alt=" "></td>
									<td><img src="templates/default/media/spacer.gif" width="21" height="1" alt=" "></td>
									<td><img src="templates/default/media/spacer.gif" width="21" height="1" alt=" "></td>
									<td><img src="templates/default/media/spacer.gif" width="21" height="1" alt=" "></td>
									<td><img src="templates/default/media/spacer.gif" width="21" height="1" alt=" "></td>
								</tr>
								<tr><td></td>
								<?php
									$start_day = strtotime2($week_start_day);
									for ($i=0; $i<7; $i++) {
										$day_num = date("w", $start_day);
										$day = $daysofweekreallyshort_lang[$day_num];
										print "<td align=\"center\"><font class=\"G10BOLD\">$day</font></td>\n";
										$start_day = strtotime2("+1 day", $start_day); 
									}
								?>
								</tr>
								<tr height="3">
									<td colspan="8"><img src="templates/default/media/spacer.gif" width="1" height="3" alt=" "></td>
								</tr>
								<?php
									$minical_time = strtotime2("+1 month", $fake_getdate_time);
									$minical_month = date("m", $minical_time);
									$minical_year = date("Y", $minical_time);
									$first_of_month = $minical_year.$minical_month."01";
									$start_day = strtotime2(dateOfWeek($first_of_month, $week_start_day));
									$i = 0;
									$whole_month = TRUE;
									do {
										$day = date ("j", $start_day);
										$day2 = date ("d", $start_day);
										$minical_week = date("W", $start_day);
        	                                
										$style = "";
										if ($minical_week==$today_week) {
										  $style='style="background-color: #C0FFC0"';
										}

                                        $theclass = "psf";
                                        if ($today_day == $day and $today_month == $minical_month and $today_year == $minical_year) {
                                          $theclass = "psf2";
                                        }

										$daylink = date ("Ymd", $start_day);
										$check_month = date ("m", $start_day);
										if ($check_month != $minical_month) $day= "<font class=\"G10G\">$day</font>";
										if ($i == 0) echo "<tr ".$style."><td align='center'><a href='week.php?cal=".$cal."&getdate=".$minical_year.$minical_month.$day2."&".SID."' class='psf'><font class='V9G'>".date ("W", $start_day)."</font></a></td>\n";
										if (isset($master_array[("$daylink")]) && ($check_month == $minical_month)) {
											echo "<td align=\"center\" class=\"G10B\">\n";
											echo "<a class=\"".$theclass."\" href=\"$minical_view.php?cal=$cal&getdate=$daylink&".SID."\" style=\"font-weight: bold; color: #00C000\">$day</a>\n";
											echo "</td>\n";
										} else {
											echo "<td align=\"center\" class=\"G10B\">\n";
											echo "<a class=\"".$theclass."\" href=\"$minical_view.php?cal=$cal&getdate=$daylink&".SID."\">$day</a>\n";
											echo "</td>\n";
										}
										$start_day = strtotime2("+1 day", $start_day); 
										$i++;
										if ($i == 7) { 
											echo "</tr>\n";
											$i = 0;
											$checkagain = date ("m", $start_day);
											if ($checkagain != $minical_month) $whole_month = FALSE;	
										}
									} while ($whole_month == TRUE);
								?>
							</table>
						</td>
					</tr>
					<tr>
						<td colspan="3" bgcolor="#FFFFFF"><img src="templates/default/media/spacer.gif" width="148" height="6" alt=" "></td>
					</tr>
				</table>
			</td>
		</tr>
	</table>
</td>
</tr>
</table>
</center>
<?php $smarty->display($appconf["gentemplates"]."/_footer.tpl"); } ?>

