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

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_gantt.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_gantt.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>
<?php if (true) { 

  mt_srand((double)microtime()*10000);
  $filename = CACHE_FOLDER."/gantt_".ereg_replace("/","X",md5(mt_rand(1,6000))).".png";

  define('CACHE_DIR',CACHE_FOLDER); // for jpgraph.php itself!
  include (INCLUDEPATH . "jpgraph/jpgraph.php");
  include (INCLUDEPATH . "jpgraph/jpgraph_gantt.php");

  $graph = @new GanttGraph(734,0,"auto");

  $graph->ShowHeaders(GANTT_HDAY | GANTT_HWEEK | GANTT_HMONTH);
  $graph->scale->week->SetStyle(WEEKSTYLE_FIRSTDAY);
  $graph->scale->month->SetStyle(MONTHSTYLE_SHORTNAMEYEAR4);
  
  $graph->scale->month->SetFontColor("white");
  $graph->scale->month->SetBackgroundColor("#6699CC");

  $graph->SetFrame(false);

  $vline = new GanttVLine($this_year."-".$this_month."-01","","white",0);
  $graph->Add($vline);

  $vline = new GanttVLine(date("Y-m-t",$date),"","white",0);
  $graph->Add($vline);

  $counts = array();
  $o = 0;
  
?>	

				<?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($counts[$my_key])) $counts[$my_key] = $o++;

									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; }

										$bar = new GanttBar($counts[$my_key],$all_cals[$my_key]["summary_gantt"],date("Y-m-d",$new_val2["start_unixtime"]),date("Y-m-d",$new_val2["end_unixtime"]));
									    $bar->SetPattern(GANTT_SOLID, $all_cals[$my_key]["color"]);
									    $bar->SetCSIMTarget("javascript:openEventInfo('".$my_key."','".$all_cals[$my_key]["calendar"]."')");
									    $bar->title->SetCSIMTarget("javascript:openEventInfo('".$my_key."','".$all_cals[$my_key]["calendar"]."')");
									    $bar->SetCSIMAlt($event_start_full." - ".$event_end_full.($all_cals[$my_key]["location"]!=""?" / ".$all_cals[$my_key]["location"]:""));
									    $bar->title->SetCSIMAlt($event_start_full." - ".$event_end_full.($all_cals[$my_key]["location"]!=""?" / ".$all_cals[$my_key]["location"]:""));
									    $graph->Add($bar);
									} else {
										$event_start = $hashmap["all_day_lang"];
										$event_end = "";
										$event_end_full = "";	
                            			$event_start_full = localizeDate($hashmap["dateFormat_week_list"]." %Y", $unix_time);

										$bar = new GanttBar($counts[$my_key],">".$all_cals[$my_key]["summary_gantt"],date("Y-m-d",$unix_time),date("Y-m-d",$unix_time));
									    $bar->SetPattern(GANTT_SOLID, $all_cals[$my_key]["color"]);
									    $bar->SetCSIMTarget("javascript:openEventInfo('".$my_key."','".$all_cals[$my_key]["calendar"]."')");
									    $bar->title->SetCSIMTarget("javascript:openEventInfo('".$my_key."','".$all_cals[$my_key]["calendar"]."')");
										// "day.php?cal=$cal&getdate=$key&".SID
									    $bar->SetCSIMAlt($event_start_full.($all_cals[$my_key]["location"]!=""?" - ".$all_cals[$my_key]["location"]:""));
									    $bar->title->SetCSIMAlt($event_start_full.($all_cals[$my_key]["location"]!=""?" - ".$all_cals[$my_key]["location"]:""));
									    $graph->Add($bar);
									}

									$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) . "...";
									}

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

									$old_dayofmonth = $dayofmonth;
								}
							}
						}
					}
				
				?>
<?php 

  $graph->Stroke($filename);
  echo '<img src="'.$filename.'" usemap="#ganttmap" alt="'.$display_month.'" border="0" class="calborder" /><br>';
  echo $graph->GetHTMLImageMap('ganttmap');

} 
  $fake_getdate_time = strtotime2($this_year.'-'.$this_month.'-15');
?>
		</td>
	</tr>
</table>

	<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"); } ?>

