<?php
// $Id: index.php,v 1.1 2004/07/11 10:27:50 Farsus Exp $
// ------------------------------------------------------------------------- //
//               E-Xoops: Content Management for the Masses                  //
//                       < http://www.e-xoops.com >                          //
// ------------------------------------------------------------------------- //
// Original Author: Nobse, Bama
// Author Website : http://www.e-xoops.de, http://service.bama-webdesign.de
// License Type   : GPL: See /manual/LICENSES/GPL.txt
// ------------------------------------------------------------------------- //
include("header.php");
include(XOOPS_ROOT_PATH."/modules/ecal/cache/config.php");

if ($user_remind == 1) {
    include_once("include/remind_mail.php");
	remindmail();
}
$mytree = new XoopsTree($db->prefix("ecal_cat"), "cid", "pid");

if($xoopsConfig['startpage'] == "ecal"){
	$xoopsOption['show_rblock'] =1;
	include(XOOPS_ROOT_PATH."/header.php");
	make_cblock();
	echo "<br />";
}else{
	$xoopsOption['show_rblock'] =0;
	include(XOOPS_ROOT_PATH."/header.php");
}

$month = !empty($HTTP_POST_VARS['month']) ? $HTTP_POST_VARS['month'] : $HTTP_GET_VARS['month'];
$year = !empty($HTTP_POST_VARS['year']) ? $HTTP_POST_VARS['year'] : $HTTP_GET_VARS['year'];

OpenTable();

$useroffset = "";

if ($xoopsUser) {
	$useroffset = $xoopsUser->getVar("timezone_offset");
} else {
	$useroffset = $xoopsConfig['default_TZ'];
}

$usertimevent = (time() + (($useroffset - ($xoopsConfig['server_TZ'] - date('I'))) * 3600));
$currentday = date("j", $usertimevent);
$currentmonth = date("m", $usertimevent);
$currentyear = date("Y", $usertimevent);
$lastday = 1;

if (!$month)  {
	$month = date("m", $usertimevent);
	$year = date("Y", $usertimevent);
}

$Date = date('F', mktime(0,0,0,$month,1,$year));

if ($beginn == 0) {
    $firstday = date( 'w', mktime(0,0,0,$month,1,$year));
}else {
	$firstday = (date( 'w', mktime(0,0,0,$month,1,$year))+6)%7;
}

while (checkdate($month,$lastday,$year))  {
	$lastday++;
}

$nextmonth = $month+1;
$nextyear = $year;

if ($nextmonth == 13) {
	$nextmonth = 1;
    $nextyear = $year + 1;
}

$lastmonth = $month-1;
$lastyear = $year;

if ($lastmonth == 0) {
	$lastmonth = 12;
    $lastyear = $year-1;
}
// Array Termine ***********
$result1=$db->query("SELECT subject,stamp,privat,username FROM ".$db->prefix("ecal")." WHERE stamp >= \"$year-$month-01 00:00:00\" AND stamp <=\"$year-$month-31 23:59:00\" AND valid='yes' order by stamp DESC");
while   ( list($subject,$stamp,$privat,$username) = $db->fetch_row($result1) ){
$jahr = substr($stamp,0,4);
$monat = substr($stamp,5,2);

 if (substr($stamp,8,1) == 0) {
	    $tag = substr($stamp,9,1);
	} else {
		$tag = substr($stamp,8,2);
	}
	
if ($zaehler[$tag] < 4) {
    

	if ($xoopsUser) {
		if ($privat == 1 and $username == $xoopsUser->uname()) {
			$text[$tag] .= "<a href='display.php?day=$tag&month=$monat&year=$jahr'>";
			$text[$tag] .= "<img src='images/mini_calendar.gif' alt='"._CAL_TERMIN."'> ".$subject."</a><br>";
		}
	}
	if($privat == 0) {
		$text[$tag] .= "<a href='display.php?day=$tag&month=$monat&year=$jahr'>";
		$text[$tag] .= "<img src='images/mini_calendar.gif' alt='"._CAL_TERMIN."'> ".$subject."</a><br>";
	}
$zaehler[$tag] = $zaehler[$tag]+1;
} else {
	$zaehler[$tag] = $zaehler[$tag]+1;
}

if ($zaehler[$tag] == 5) {
    $text[$tag] .= "<a href='display.php?day=$tag&month=$monat&year=$jahr'>";
	$text[$tag] .= _CAL_WEITER."</a>";
	$zaehler[$tag] = $zaehler[$tag]+1;
}
}
//End Array *****************
// NEWS Array **************
if ($shownews ==1) {
	$result=$db->query("SELECT storyid, published, title FROM ".$db->prefix("stories")." WHERE published >= ".strtotime("$year/$month/1")." and published <= ".strtotime("$year/$month/31 23:59:59")." and published <= ". time()."");
		while (list( $storyid, $published, $title) = $db->fetch_row($result)){
			$ist = date("j",$published);
			//$newsid[$ist] .= $storyid;
			$text[$ist] .= 	"<a href=".$xoopsConfig['xoops_url']."/modules/news/article.php?storyid=$storyid><img src='images/mini_news.gif' alt='"._CAL_NEWS."'> $title</a><br>";		}
}
//****************************
define("_NAVPAGE","MON");
include ("include/nav.php");
echo "<B><center>"._CALMONTH."</center></B><P>";

    echo "<table border=0 width=100% class='bg3'><tr><td width='25%' align='left' valign='middle'>";
    form_last_month($lastmonth, $lastyear, $pid);
    echo "</td><td width='35%' align='right' valign='middle'><form method='post' action='index.php'>";
    echo "<select class='select' size='1' name='month'>";
    buildMonthSelect($month);
    echo "</select>&nbsp;<select class='select' size='1' name='year'>";
    buildYearSelect($year);
    echo "</select></td><td width='15%' align='left' valign='bottom'>&nbsp;<input type='image' src='".XOOPS_URL."/modules/ecal/images/ok.gif'></td></form>";
    echo "<td width='25%' align=right valign='middle'>";
    form_next_month($nextmonth, $nextyear, $pid);
    echo "</td></tr></table>";

echo "<table width=100% cellpadding=5  CELLSPACING=1 border=0  class='bg2'>";

if ($beginn == 0) {
    echo "<tr class='bg1'><td width=14%><b>"._SUN."</td><td width=14%><b>"._MON."</td>
      <td width=14%><b>"._TUE."</td><td width=14%><b>"._WED."</td>
      <td width=14%><b>"._THU."</td><td width=14%><b>"._FRI."</td>
      <td width=14%><b>"._SAT."</td></tr>";
}else {
	echo "<tr class='bg1'><td width=14%><b>"._MON."</td><td width=14%><b>"._TUE."</td>
      <td width=14%><b>"._WED."</td><td width=14%><b>"._THU."</td>
      <td width=14%><b>"._FRI."</td><td width=14%><b>"._SAT."</td>
      <td width=14%><b>"._SUN."</td></tr>";
}


      for ($i=0; $i<7; $i++) {
	      if ($i < $firstday) {
      		echo "<td></td>";
          } else {
            $thisday = ($i+1)-$firstday;
			//$h11 = $thisday - $firstday;
			 $xy = strtotime("$year/$month/$thisday");
			  $wochentag = date("l", $xy);
				if ($currentyear > $year)  {
					if ($wochentag == "Saturday" or $wochentag == "Sunday") {
				    	echo "<td valign=top bgcolor=\"$weekcolor\">";
					}else{
						echo "<td valign=top class='bg3'";
                	}
				}
                else if ($currentmonth > $month && $currentyear == $year) {
					if ($wochentag == "Saturday" or $wochentag == "Sunday") {
				    	echo "<td valign=top bgcolor=\"$weekcolor\">";
					}else{
						echo "<td valign=top class='bg3'>";
                	}
				}
                else if ($currentmonth == $month && $currentday > $thisday && $currentyear == $year) {
			        if ($wochentag == "Saturday" or $wochentag == "Sunday") {
				   		echo "<td valign=top bgcolor=\"$weekcolor\">";
					}else{
						echo "<td valign=top class='bg3'>";
                	}
				} 
				else if ($currentmonth == $month && $currentday == $thisday && $currentyear == $year) {
					echo "<td valign=top bgcolor=\"$daycolor\">";
				}
				else if ($wochentag == "Saturday" or $wochentag == "Sunday") {
				    echo "<td valign=top bgcolor=\"$weekcolor\">";   
				}else {
					echo "<td valign=top class='bg3'>";
                }
							
			echo "<a href=display.php?day=$thisday&month=$month&year=$year><B>$thisday</B></a><br><hr>";
			
			
			if ($text[$thisday] == "") {
			    echo "<br><br><br><br><br>";
			} else {
				echo $text[$thisday];
			}
		echo "</td>";
        }
     }

     echo "</tr>\n";
     $nextday = ($i+1)-$firstday;

      for ($j = 0; $j<5; $j++) {
		//echo "<tr>";
           for ($k = 0; $k<7; $k++) {
               if ($nextday < $lastday) {
			   $xy = strtotime("$year/$month/$nextday");
			   $wochentag = date("l", $xy);
	            if ($currentyear > $year) {
					if ($wochentag == "Saturday" or $wochentag == "Sunday") {
						echo "<td valign=top bgcolor=\"$weekcolor\">"; 
					}else{
						echo "<td valign=top class='bg3'>";
                    }
				   }
				else if ($currentmonth > $month && $currentyear == $year) {
          			if ($wochentag == "Saturday" or $wochentag == "Sunday") {
						echo "<td valign=top bgcolor=\"$weekcolor\">"; 
					}else{
						echo "<td valign=top class='bg3'>";
                	}
				}
                else if ($currentmonth == $month && $currentday > $nextday && $currentyear == $year) {
					if ($wochentag == "Saturday" or $wochentag == "Sunday") {
						echo "<td valign=top bgcolor=\"$weekcolor\">"; 
					}else{
						echo "<td valign=top class='bg3'>";
					}
				} 
				else if ($currentmonth == $month && $currentday == $nextday && $currentyear == $year) {
					echo "<td valign=top bgcolor=\"$daycolor\">";
				} 
				else if ($wochentag == "Saturday" or $wochentag == "Sunday") {
				         echo "<td valign=top bgcolor=\"$weekcolor\">";  
				}else  {
					echo "<td valign=top class='bg3'>";
                }

   			echo "<a href=display.php?day=$nextday&month=$month&year=$year><B>$nextday</B></a><br><hr>";
	        if ($text[$nextday] == "") {
			    echo "<br><br><br><br><br>";
			} else {
				 echo $text[$nextday];
			}
		echo "</td>";
		$nextday++;
        }
   }

		echo "</tr>\n";
}

	echo "</table>";
	
echo "<table border='0' width='100%' class='bg3'><tr><td align=left>";
    form_last_month($lastmonth, $lastyear, $pid);
	if ($xoopsUser) {
		$group = $xoopsUser->groups();
    }else{
		$group[] = 3;
	}
	$anno = explode("|", $anoevent);
	$is = array_intersect ($group, $anno);
	if (count($is) > 0) {
        echo "<td align=center>";
        form_add_event($month, $year);
        echo "</td>";
    } else {
        echo "<td>&nbsp;</td>";
	}	
    echo "<td align=right>";
    form_next_month($nextmonth, $nextyear, $pid);
    echo "</td></tr></table>";

CloseTable();
include(XOOPS_ROOT_PATH."/footer.php");
?>