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

// Year
// 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 = 'year_gantt';
include(CAL2_INC_DIR.'ical_parser.php');

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

$this_year2 = date("Y",strtotime2($getdate));

$startYear = strtotime2 ($this_year."0101");

$checkad = date ("Ymd", $startYear);

$next_year = strtotime2 ("+1 year", strtotime2($getdate));
$next_year = date ("Ymd", $next_year);
$prev_year = strtotime2 ("-1 year", strtotime2($getdate));
$prev_year = date ("Ymd", $prev_year);

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


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(0,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_year2."-01-01","","white",0);
$graph->Add($vline);

$vline = new GanttVLine($this_year2."-12-31","","white",0);
$graph->Add($vline);

echo "<center>";
$counts = array();
$o = 0;

$m=0;	
$n=0;
do {
		
  $minical_time = $startYear;
  $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;
  $num_of_events = 0;
  do {
	$day = date ("j", $start_day);
	$daylink = date ("Ymd", $start_day);
	$check_week = date ("W", $start_day);
	$check_month = date ("m", $start_day);

	if (isset($master_array[("$daylink")]) && ($check_month == $minical_month)) {
	  if (isset($master_array[$daylink])) {
		foreach ($master_array[$daylink] as $item) {
		  if (is_array($item) and count($item)>0) {
			foreach ($item as $my_key=>$new_val2) {
			  $unix_time = strtotime2($daylink);
			  if (isset($new_val2["event_start"])) {
			    if (!isset($counts[$my_key])) $counts[$my_key] = $o++;
				
				$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 {
			    if (!isset($counts[$my_key])) $counts[$my_key] = $o++;

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

				$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);
			  }
			} 
		  }
	    }
	  }
	}
	
	$start_day = strtotime2("+1 day", $start_day); 
	$i++;
	if ($i == 7) { 
  	  $i = 0;
  	  $checkagain = date ("m", $start_day);
	  if ($checkagain != $minical_month) $whole_month = FALSE;	
	}
  } while ($whole_month == TRUE);
  $startYear = strtotime2 ("+1 month", $startYear);

  $m++;
  $n++;
  if (($m == 3) && ($n < 12)) $m = 0;
} while (($m < 3) && ($n < 12)); 

  $graph->Stroke($filename);
?>
<table border="0" cellspacing="0" cellpadding="0">
	<tr>
		<td align="center">
			<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="45%" class="navback"><?php echo "<a class=\"psf\" href=\"year_gantt.php?cal=$cal&getdate=$prev_year&".SID."\"><img src=\"".IMGPATH."/left_day.gif\" alt=\"[".$hashmap["last_year_lang"]."]\" border=\"0\" align=\"right\"></a>"; ?></td>
								<td align="center" width="10%" class="navback" nowrap valign="middle"><font class="H20"><?php echo $this_year2; ?></font></td>
      							<td align="left" width="45%" class="navback"><?php echo "<a class=\"psf\" href=\"year_gantt.php?cal=$cal&getdate=$next_year&".SID."\"><img src=\"".IMGPATH."/right_day.gif\" alt=\"[".$hashmap["next_year_lang"]."]\" border=\"0\" align=\"left\"></a>"; ?></td>
      						</tr>
      					</table>
      				</td>
      			</tr>
      		</table>
		</td>
	</tr>
	<tr><td>
<?php
  echo '<img src="'.$filename.'" usemap="#ganttmap" alt="'.$this_year2.'" border="0" class="calborder" />';
  echo $graph->GetHTMLImageMap('ganttmap');
  echo "</td></tr><tr><td align='center'>";
?>

	<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
  echo "</td></tr></table></center>";
  
  if (!$print) $smarty->display($appconf["gentemplates"]."/_footer.tpl");
?>

