<?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_list";
include(CAL2_INC_DIR.'ical_parser.php');
$minical_view = 'month_list';

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_list.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_list.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>
</table>
<table border="0" cellspacing="0" cellpadding="0" width="735" bgcolor="#FFFFFF" class="calborder">
	<tr>
		<td align="center" valign="top">
			<table width="100%" border="0" cellspacing="0" cellpadding="0">
				<tr bgcolor="#ffffff">
					<td align="right" valign="top" width="50" class="sideback"><?php echo "<img src=\"templates/default/media/spacer.gif\" alt=\" \" width=\"16\" height=\"1\" border=\"0\" align=\"right\">"; ?></td>
					<td align="left" valign="top" width="270" class="sideback" colspan="3"><?php echo "<img src=\"templates/default/media/spacer.gif\" alt=\" \" width=\"16\" height=\"1\" border=\"0\" align=\"left\">"; ?></td>
					<td align="center" class="sideback" width="200" nowrap></td>
					<td align="right" valign="top" width="70" class="sideback"><?php echo "<img src=\"templates/default/media/spacer.gif\" alt=\" \" width=\"16\" height=\"1\" border=\"0\" align=\"right\"></a>"; ?></td>
					<td align="right" valign="top" width="50" class="sideback"><?php echo "<img src=\"templates/default/media/spacer.gif\" alt=\" \" width=\"16\" height=\"1\" border=\"0\" align=\"right\">"; ?></td>
				</tr>
				<tr bgcolor="#ffffff">
					<td colspan="5" height="1">&nbsp;</td>
				</tr>
				<?php	
					// Iterate the entire master array
					$old_dayofmonth = "";
							
					foreach($master_array as $key => $val) {
						
						// Pull out only this months
						ereg ("([0-9]{6})([0-9]{2})", $key, $regs);
						if ($regs[1] == $parse_month) {
							$dayofmonth = strtotime2 ($key);
							$unix_time = $dayofmonth;
							$dayofmonth = localizeDate ($hashmap["dateFormat_week_list"], $dayofmonth);
							$i = 0;
							if ($today_today == $key) {
								$fontclass="class=\"G10BOLD\"";
							} else {
								$fontclass="class=\"G10B\"";
							}

							// Pull out each day
							foreach ($val as $new_val) {
								
								// Pull out each time
								foreach ($new_val as $my_key => $new_val2) {
									if (isset($new_val2["event_start"])) {
										$event_start 	= date ($hashmap["timeFormat"], strtotime2 ($new_val2["event_start"]));
										$event_end 		= date ($hashmap["timeFormat"], strtotime2 ($new_val2["event_end"]));

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

                                        $event_start_full   = $start_date_full." ".date($hashmap["timeFormat"], $new_val2["start_unixtime"]);
  			                            $event_end_full     = date($hashmap["timeFormat"], $new_val2["end_unixtime"]);
                                        if ($start_date_full != $end_date_full) { $event_end_full = $end_date_full." ".$event_end_full; }

									} else {
										$event_start = $hashmap["all_day_lang"];
										$event_end = "";
										$event_end_full = "";	
                            			$event_start_full = localizeDate($hashmap["dateFormat_week_list"]." %Y", $unix_time);
									}

									$event_text_short = $all_cals[$my_key]["summary"];
									if (strlen($all_cals[$my_key]["summary"]) > 150) {
										$event_text_short = substr($all_cals[$my_key]["summary"], 0, 145) . "...";
									}
									if ($old_dayofmonth != $dayofmonth and $old_dayofmonth!="") {
									  echo "<tr><td></td><td colspan=\"5\" height=\"3\"><img src=\"templates/default/media/spacer.gif\" width=\"1\" height=\"3\" alt=\"\"></td></tr>";  
									  echo "<tr><td></td><td colspan=\"5\" class=\"monthborder\" height=\"4\"><img src=\"templates/default/media/spacer.gif\" width=\"1\" height=\"4\" alt=\"\"></td></tr>";  
									}

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

									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"].'\',\''.$event_start_full.'\',\''.$event_end_full.'\''.$dadd.'); return false;" onmouseout="delayhidemenu()"><td></td>'."\n";
									echo "<td align=\"left\" valign=\"top\" width =\"90\" nowrap><font $fontclass>&nbsp;<a class=\"psf\" href=\"day.php?cal=$cal&getdate=$key&".SID."\">$dayofmonth</a></font></td><td align=\"left\" valign=\"top\" width =\"90\" class=\"montheventline\" nowrap><font class=\"V9G\">($event_start)</font></td><td align=\"left\" valign=\"top\" width =\"90\" class=\"montheventline\" nowrap><font class=\"V9G\">".($event_end!=""?"(".$event_end.")":"")."</font></td>\n";
									echo "<td class=\"eventbg\" align=\"left\" valign=\"top\" colspan=\"2\" style=\"padding-left: 4px; background-color: ".$all_cals[$my_key]["color"]."\" nowrap onclick=\"openEventInfo('$my_key','".$all_cals[$my_key]["calendar"]."')\"><a class=\"psf\" href=\"#\"><font class=\"G10B\" style=\"; color: ".($all_cals[$my_key]["color"]?"#FFFFFF":"")."\">".$all_cals[$my_key]["summary"]."</font></a></td>\n";
									echo "</tr>\n";
									$old_dayofmonth = $dayofmonth;
								}
							}
						}
					}
				
				?>
				<tr>
					<td colspan="5" height="1">&nbsp;</td>
				</tr>
			</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"); } ?>

