<?php
/**
 * KalenderMx v1.4
 * Copyright (c) 2004 by A.Ellsel (kalender@pragmamx.org)
 * http://www.pragmamx.org & http://www.shiba-design.de
 * 
 * KalenderMx was based on EventCalendar 2.0
 * Copyright (c) 2001 Originally by Rob Sutton
 * 
 * This program 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.
 * 
 * $Source: D:\CVS/dev-Kalender/html/blocks/block-Calendar_combi.php,v $
 * $Revision: 1.8 $
 * $Author: Andreas Ellsel $
 * $Date: 2006/07/21 13:24:09 $
 */

// // !!!! don't change this 3 lines !!!! ////////////////////////////////////////
$thisfile = basename(__file__);
if (eregi($thisfile, $_SERVER["PHP_SELF"])) die ("You can't access this file directly...");
global $bgcolor1, $bgcolor2, $bgcolor3, $bgcolor4, $bgcolor5, $textcolor1, $textcolor2, $dbi;
// ///////////////////////////////////////////////////////////////////////////////////////////

$mxblockcache = false;

// // Einstellungen fuer den Block ////////////////////////////////////////
// CHANGE THIS IF YOU CHANGE THE CALENDAR-MODULES FOLDER NAME
if (!defined("CAL_MODULE_NAME")) define("CAL_MODULE_NAME", "Kalender");

// /////////////////////////////////////////////////////////////////////////////
// verschiedene Einstellungen,
// die Reihenfolge der Teilbereiche kann weiter unten eingestellt werden!!
$sort = "asc";       // Sortierung der Liste:  asc =aufsteigend / desc =absteigend
$scrolling = 1;       // Eintraege scrollen
$scrolldirection = "up";       // Richtung des Scrollens: up / down
$scrollheight = 70;       // Hoehe des scrollenden Bereichs
$scrolldelay = 1;       // Scrollgeschwindigkeit
$scrollamount = 1;       // Scrollgeschwindigkeit
$listcount = 5;       // Anzahl der Termine im Block, 0 hides the List-Block
$listEnddate = 1;       // Enddatum anzeigen: 0=nein / 1=ja
$listEnddate2 = 0;       // Enddatum anzeigen, wenn gleich dem Startdatum ($listEnddate muss 1 sein): 0=nein / 1=ja
$listStarttime = 0;       // Startzeit des Termins anzeigen: 0=nein / 1=ja
$listEndtime = 0;       // Ende-Zeit des Termins anzeigen ($listEnddate muss 1 sein): 0=nein / 1=ja
$showNewlink = 1;       // Link 'Termin vorschlagen'  anzeigen: 0=nein / 1=ja
$showLegend = 1;       // Farblegende anzeigen: 0=nein / 1=ja

// // Einstellungen fuer den Monats-Block  ///////////////////////////////////////////////////

$bordercolor = $bgcolor1;       // Rahmenfarbe um Tage in Monatsblock
$todaycolor = $textcolor1;       // Textfarbe HEUTE
$todaybackground = $bgcolor1;       // Hintergrundfarbe HEUTE
$daycolor = $textcolor2;       // Textfarbe normale Tage
$daybackground = $bgcolor2;       // Hintergrundfarbe normale Tage
$showtodayevents = 1;       // heutige Termine anzeigen
$showsymbols = 0;       // Bildsymbole anzeigen

// // Definition der Farben und Rahmen ///////////////////////////////////////////////////////
$listbgcolor1 = $bgcolor1;       // Hintergrund erste Zeile  ( "" fuer Transparent  )
$listtxtcolor1 = $textcolor1;       // Text erste Zeile
$listbgcolor2 = $bgcolor2;       // Hintergrund naechste Zeile
$listtxtcolor2 = $textcolor2;       // Text naechste Zeile
$listbordercolor = $bgcolor3;       // Rahmenfarbe um Liste
$scrollbgcolor = $bgcolor1;       // Hintergrundfarbe des Marquee-Tags (Zwischenraum Scrollinhalte)
$listtableborder = 0;       // html Rahmen um Liste
$listtablecellspacing = 1;       // abstand zwischen Events (auch Rahmenbreite)
$listtablecellpadding = 2;       // Freiraum innerhalb der Tabellenspalten der einzelnen Termindaten

// // Ende der Einstellungen, ab hier nichts veraendern !! ///////////////////////////////////

$conftest = @include("modules/" . CAL_MODULE_NAME . "/config/config.php");
if (!$conftest) {
    $content = "<b>Error!</b><br>You must change the constant '\"CAL_MODULE_NAME\"' in file: <br>" . __file__ . "";
    return;
} 
get_lang(CAL_MODULE_NAME);
if (!defined("CAL_MODULE_PATH")) define("CAL_MODULE_PATH", "modules/" . CAL_MODULE_NAME . "/");
if (!defined("CAL_TABLE_EVENTS")) @include_once(CAL_MODULE_PATH . "includes/functions.php");

$showNewlink = (calIsPostAllowed() && $showNewlink) ? 1 : 0;
if (!calDetectGoodBrowser ()) {
    $scrolling = 0;
} 

$curMonth = date("n");
$curDay = date("d");
$curYear = date("Y");

/**
 * *** Get the Day (Integer) for the first day in the month
 */
$First_Day_of_Month_Date = @mktime(0, 0, 0, $curMonth, 1, $curYear);
$Day_of_First_Week = date("w", $First_Day_of_Month_Date) - _CALWEEKBEGINN;
if ($Day_of_First_Week < 0) $Day_of_First_Week = 6;
$day_of_week = $Day_of_First_Week + 1;

/**
 * *** Find the last day of the month
 */
$lastday = date("t", $First_Day_of_Month_Date);
$curMonthName = calGetMonthName($curMonth) . " $curYear";

$whereoption = "categorie in(" . calGetSqlEventpoints() . ") AND activ=1";
$qrydate = "$curYear-$curMonth-$curDay";
$qry = "SELECT count(eid) FROM " . CAL_TABLE_EVENTS . " 
WHERE (startDate>= '$qrydate' or endDate>= '$qrydate') 
and ($whereoption)";
list($icount) = sql_fetch_row(sql_query($qry, $dbi), $dbi);
// print "$icount <br> $qry";
if ($icount > $listcount) $icount = $listcount;

$i = 0;
$ievents = 0;
$content_list = '';
if ($icount) {
    setlocale (LC_TIME, _CALLOCALE);
    $imgprops = "align=\"middle\" hspace=\"1\" vspace=\"2\" width=\"9\" height=\"9\" border=\"0\" alt=\"\"";
    if ($icount < 3) {
        $scrolling = 0;
    } 
    if ($scrolling) {
        srand((double)microtime() * 1000000);
        $randmarq = 'm' . md5(uniqid(rand(), 1));
        $content_list .= "<div style='overflow: hidden; height: ${scrollheight}px;'><marquee id='" . $randmarq . "' behavior='scroll' direction='$scrolldirection' height='$scrollheight' hspace='0' vspace='5' loop='0' scrollamount='$scrollamount' scrolldelay='$scrolldelay' onMouseOver='this.stop()' onMouseOut='this.start()'>";
    } 
    $content_list .= "<table cellspacing=\"$listtablecellspacing\" cellpadding=\"$listtablecellpadding\" style=\"background-color: $listbordercolor; border: ${listtableborder}px solid $listbordercolor;\" border=\"$listtableborder\" width=\"100%\">";
    $qry = "SELECT eid, title, hometext, posteddate, topic, informant, year(startDate), month(startDate), dayofmonth(startDate), hour(startTime), minute(startTime), year(endDate), month(endDate), dayofmonth(endDate), hour(endTime), minute(endTime), alldayevent, categorie FROM " . CAL_TABLE_EVENTS . " 
	WHERE (startDate>= '$qrydate' or endDate>= '$qrydate') 
	and ($whereoption) 
	order by startDate $sort, endDate $sort 
	limit 0,$icount";
    $result = sql_query($qry, $dbi);
    while (list($eid, $title, $hometext, $posteddate, $topic, $informant, $y1, $m1, $d1, $h1, $mi1, $y2, $m2, $d2, $h2, $mi2, $alldayevent, $categorie) = sql_fetch_row($result, $dbi)) {
        $bgcolornew = ($i == 0) ? $listbgcolor1 : $listbgcolor2;
        $txtcolornew = ($i == 0) ? $listtxtcolor1 : $listtxtcolor2; 
        // $listrange = "";
        $i = ($i == 0) ? 1 : 0;
        $title = htmlspecialchars(strip_tags(stripslashes($title)), ENT_QUOTES);
        $dateStart = calStrftime(_CALSHORTDATEFORMAT, @mktime(0, 0, 0, $m1, $d1, $y1));
        if ($listStarttime && !$alldayevent) {
            $dateStart .= ' ' . calStrftime(_CALTIMEFORMAT, @mktime($h1, $mi1, 0, $m1, $d1, $y1));
        } 
        if (!$listEnddate || (!$listEnddate2 && $m1 == $m2 && $d1 == $d2 && $y1 == $y2)) {
            $dateEnd = '';
        } else {
            $dateEnd = calStrftime(_CALSHORTDATEFORMAT, @mktime(0, 0, 0, $m2, $d2, $y2));
        } 
        if ($listEnddate && $listEndtime && !$alldayevent) {
            $dateEnd .= ' ' . calStrftime(_CALTIMEFORMAT, @mktime($h2, $mi2, 0, $m2, $d2, $y2));
        } 
        if ($dateEnd) {
            $dateEnd = ' - ' . $dateEnd;
        } 
        $alttext = ($hometext && !$scrolling) ? substr(htmlentities(strip_tags(stripslashes($hometext))), 0, 70) . '...' : "";
        $content_list .= "<tr style=\"background-color: $bgcolornew;\">
		<td valign=\"top\"><img src=\"" . calGetBarImage("ball", $categorie) . "\" $imgprops></td>\n
		<td><div title=\"$alttext\"><span style=\"color: $txtcolornew;\" class=\"tiny\">" . $dateStart . $dateEnd . "<br>\n
		<a href=\"" . CAL_MODULE_LINK . "&amp;op=view&amp;eid=$eid\" style=\"text-decoration: none;\">$title</a></span></div></td>\n</tr>\n";
        $ievents++;
    } 
    if ($scrolling) {
        // / Mozilla macht Probleme beim scrollen, deshalb hier explizit starten
        $content_list .= "</table></marquee></div>
		<script language='JavaScript1.2'>
		<!--
		if (document.getElementById) { document.getElementById('" . $randmarq . "').start(); }
		//-->
		</script>
		";
    } else {
        $content .= "</table>";
    } 
    setlocale (LC_TIME, $GLOBALS["locale"]);
} 
$bgcolornew = ($i == 1) ? $listbgcolor2 : $listbgcolor1;

// ///////////////////////////////////////////////////////////////////////////////////////////
/**
 * *** Build Month
 */
$qry = "SELECT startDate, endDate
FROM " . CAL_TABLE_EVENTS . "
WHERE (((startDate<='$curYear-$curMonth-01') Or (startDate>='$curYear-$curMonth-01' And startDate<='$curYear-$curMonth-$lastday')) AND (endDate>='$curYear-$curMonth-01') 
AND ($whereoption))
ORDER BY startDate, endDate";
$result = sql_query($qry, $dbi);
while (list($startDate, $endDate) = sql_fetch_row($result, $dbi)) {
    $aStartDate = explode("-", $startDate);
    $startDay = (int)$aStartDate[2];
    $startMon = (int)$aStartDate[1];
    $startYea = (int)$aStartDate[0];
    if ($startDate != $endDate) {
        $aEndDate = explode("-", $endDate);
        $endDay = (int)$aEndDate[2];
        $endMon = (int)$aEndDate[1];
        $endYea = (int)$aEndDate[0];
        $lday = ($curMonth < $endMon || $curYear < $endYea) ? $lastday : $endDay;
        $fday = ($curMonth > $startMon || $curYear > $startYea) ? 1 : $startDay;
        for ($countDay = $fday; $countDay <= $lday; $countDay++) {
            $daycount[$countDay] = (empty($daycount[$countDay])) ? 1 : $daycount[$countDay] + 1;
        } 
    } else {
        if ($curYear == $startYea && $curMonth == $startMon) {
            $daycount[$startDay] = (empty($daycount[$startDay])) ? 1 : $daycount[$startDay] + 1;
        } 
    } 
} 

/**
 * *** Build Current Month
 */
$cmonth = "";
for ($day = 1; $day <= $lastday; $day++) {
    if ($day_of_week == 1) {
        $cmonth .= "<tr>\n";
    } 
    $dayrows = (isset($daycount[$day])) ? $daycount[$day] : 0;
    $dayrows = ($dayrows >= 4) ? 4 : $dayrows;
    $bgcolor = ($day == Date("d")) ? $todaybackground : $daybackground;
    $fncolor = ($day == Date("d")) ? $todaycolor : $daycolor;
    $xday = ($day == Date("d")) ? "<b>$day</b>" : $day;
    $imgprops = "width=\"13\" height=\"14\" border=\"0\"";
    $cmonth .= "<td align=\"center\" style=\"background-color: $bgcolor;\">";
    $cmonth .= "<a href=\"" . CAL_MODULE_LINK . "&amp;m=$curMonth&amp;d=$day&amp;y=$curYear&amp;op=day\" style=\"text-decoration: none;\">";
    $cmonth .= "<span style=\"color: $fncolor;\" class=\"tiny\">$xday</span>";
    if ($showsymbols) $cmonth .= "<br><img src=\"" . CAL_IMAGE_PATH . "events" . $dayrows . ".gif\" $imgprops>";
    $cmonth .= "</a></td>\n";
    if ($day_of_week == 7) {
        $day_of_week = 0;
        $cmonth .= "</tr>\n";
    } 
    $day_of_week += 1;
} 

/**
 * *** Build Month
 */
$content_month = "\n<table border=\"0\" cellspacing=\"1\" cellpadding=\"2\" style=\"background-color: $bordercolor;\" width=\"100%\">";
$content_month .= "\n<tr>\n<th colspan=\"7\" style=\"background-color: $bgcolor2;\">";
$content_month .= "<span class=\"tiny\"><a href=\"" . CAL_MODULE_LINK . "&amp;m=$curMonth&amp;d=1&amp;y=$curYear&amp;op=month\">";
$content_month .= "$curMonthName</a></span></th>\n</tr>\n";
$content_month .= "\n<tr>";
/**
 * *** Previous Greyed month days
 */
if ($Day_of_First_Week != 0) {
    $content_month .= "\n<td colspan=\"$Day_of_First_Week\" style=\"background-color: $daybackground;\">&nbsp;</td>\n";
} 
$content_month .= $cmonth;
/**
 * *** Next Greyed month days
 */
// $day = 1;
if ($day_of_week != 1) {
    $tmp = 8 - $day_of_week;
    $content_month .= "<td colspan=\"$tmp\" style=\"background-color: $daybackground;\">&nbsp;</td>\n</tr>\n";
} 
$content_month .= "</table>\n";

$content = "\n<!-- $thisfile output start -->\n";
// //////////////////////////////////////////////////////////////////////////////
// / ab hier die eigentliche Ausgabe.
// / um die Reihenfolge zu verändern, einfach die einzelen Teilblöcke verschieben
// //////////////////////////////////////////////////////////////////////////////
// / Ausgabe des Teilblocks 'Monat' /////////////////////////////////////////////
$content .= $content_month;
// //////////////////////////////////////////////////////////////////////////////
// / Trennlinie zwischen Monat und Liste ////////////////////////////////////////
if (!$scrolling && $content_list) {
    $content .= "<hr size=\"1\" style=\"color: $listbgcolor2;\" width=\"100%\" noshade>";
} 
// / Ausgabe des Teilblocks 'Liste' /////////////////////////////////////////////
$content .= $content_list;
// //////////////////////////////////////////////////////////////////////////////
// / Trennlinie zwischen Teilblock und Kategorieliste ///////////////////////////
if ($showLegend) {
    $content .= "<hr size=\"1\" style=\"color: $listbgcolor2;\" width=\"100%\" noshade>";
} 
// //////////////////////////////////////////////////////////////////////////////
// / Kategorieliste /////////////////////////////////////////////////////////////
if ($showLegend) {
    $imgprops = "align=\"middle\" hspace=\"6\" vspace=\"3\" width=\"9\" height=\"9\" border=\"0\" alt=\"\"";
    $content .= calBuildColorLegendSideBlocks();
} 
// //////////////////////////////////////////////////////////////////////////////
// / Link zum Termin hinzufügen /////////////////////////////////////////////////
if ($showNewlink) {
    // $content_month.="<hr size=\"1\" style=\"color: $listbgcolor2;\" width=\"100%\" noshade>";
    $content .= "<table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td><a href=\"" . CAL_MODULE_LINK . "&amp;file=submit\"><img src=\"" . CAL_IMAGE_PATH . "sign.gif\" alt=\"" . _CALSUBMITEVENT . "\" width=\"16\" height=\"16\" border=\"0\" align=\"middle\" hspace=\"0\" vspace=\"1\"></a></td><td valign=\"middle\"><span class=\"tiny\">&nbsp;<a href=\"" . CAL_MODULE_LINK . "&amp;file=submit\">" . _CALSUBMITEVENT . "</a></span></td></tr></table>";
} 
// //////////////////////////////////////////////////////////////////////////////
// //////////////////////////////////////////////////////////////////////////////
$content .= "\n<!-- $thisfile output end -->\n<!-- KalenderMx © by shiba-design.de -->\n";
$content = ereg_replace("\t", "", $content);
// / Der Blocktitel wird über die Sprachdatei des Kalenders gesteuert,
// / soll statt dessen die Einstellung des Adminmenüs verwendet werden,
// / einfach die folgenden Zeilen löschen oder auskommentieren
if ($ievents == 0) {
    $blockfiletitle = _CALNAME;
} else {
    if (empty($icount)) {
        $icount = "";
    } 
    $blockfiletitle = _CALNEXT . " $icount " . _CALLISTDESCRIPTION2;
} 

?>
