<?php
//=================================================================
// KOOBI R7 - CMS - PORTALSOFTWARE(c) DREAM4(r) 
// HTTP://WWW.DREAM4.DE
//=================================================================
// ACHTUNG!
// DIESER PROGRAMMCODE IST URHERRECHTLICH GESCHUETZT
// DIE URHEBERRECHTE LIEGEN BEI DEN JEWEILIGEN AUTOREN.
// AENDERUNGEN IN DIESER DATEI ERFOLGEN AUF EIGENE GEFAHR!
// ES IST NICHT GESTATTET, DEN COPYRIGHTHINWEIS ZU AENDERN ODER ZU
// ENTFERNEN. NUR WENN SIE EINE COPYRIGHTHINWEIS-ENTFERNUNG BESTELLT
// HABEN, DUERFEN SIE DIESEN ENTFERNEN.
//================================================================
// BEI FRAGEN WENDEN SIE SICH BITTE AN: KONTAKT@DREAM4.DE
//=================================================================
function CalendarBirthdays($tag='',$monat='',$jahr='',$nolimit='')
{
	global $Icon;

	$user_birth_today = '';
	$de_d = $tag;
	$de_b = $monat;
	$de_y = $jahr;
	$m = $de_d;
	
	if(is_array($tag)) 
	{
		$de_d = $tag['tag'];
		$de_b = $tag['monat'];
		$de_y = $tag['jahr'];
		$jahr = $tag['jahr'];
		$m = $de_d;
	}
	
	$day_query = (substr($m,0,1)=='0')  
		? 
		"
			((LEFT(Geburtstag,1) - " . substr($m,1) /* z.B.: 8.10.1980 */ . ") = 0 AND (RIGHT(LEFT(Geburtstag,4), 2) - " . $de_b . ") = 0) 
		OR 
			((LEFT(Geburtstag,2)) = '0" . substr($m,1) /* z.B.: 08.10.1980 */ . "'  AND (RIGHT(LEFT(Geburtstag,5), 2) - " . $de_b . ") = 0)
		" 
		: 
		"
			((LEFT(Geburtstag,1) - " . $m /* z.B.: 11.10.1980 */ . ") = 0 AND (RIGHT(LEFT(Geburtstag,4), 2) - " . $de_b . ") = 0)
		OR
			((LEFT(Geburtstag,2) - " . $m /* z.B.: 11.10.1980 */ . ") = 0 AND (RIGHT(LEFT(Geburtstag,5), 2) - " . $de_b . ") = 0)
		";
	
	$q = "SELECT 
		Id,Benutzername,Geburtstag, $jahr - RIGHT(Geburtstag, 4) AS age 
	FROM 
		" . PREFIX . "_benutzer AS u 
	WHERE u.Aktiv='1' 
	HAVING 
		$day_query 
	ORDER BY Benutzername ASC";
	
	if(!empty($m) && $m >= 1)
	{
		$sql = $GLOBALS['db']->Query($q);
		$num = $sql->numrows();
		if($nolimit==1) $num=0;
		
		if($num <= 1)
		{
			while ($row = $sql->fetchrow())
			{
				
				if($nolimit==1)
				{
					$row->Benutzername = (strlen($row->Benutzername) > 20) ? substr($row->Benutzername,0,20) . '...' : $row->Benutzername;
					$user_birth_today[] = "<img class=\"absmiddle\" src=\"theme/" . $GLOBALS['theme'] . "/images/calendar/birthday.png\" alt=\"\" /> <a href=\"index.php?p=user&amp;id=$row->Id&amp;area=" . AREA . "\">" . $row->Benutzername . "</a> <span class=\"calendarNorm\">(" . $row->age . ")</span><br />";
				} else {
					if(isset($_REQUEST['action']) && $_REQUEST['action']=='week') 
					{
						$row->Benutzername = (strlen($row->Benutzername) > 20) ? substr($row->Benutzername,0,20) . '...' : $row->Benutzername;
						$user_birth_today .= "<img class=\"absmiddle\" src=\"theme/" . $GLOBALS['theme'] . "/images/calendar/birthday.png\" alt=\"\" /> <a href=\"index.php?p=user&amp;id=$row->Id&amp;area=" . AREA . "\">" . $row->Benutzername . "</a> <span class=\"calendarNorm\">(" . ($row->age) . ")</span><br />";
						
					} else {
						$row->Benutzername = (strlen($row->Benutzername) > 20) ? substr($row->Benutzername,0,20) . '...' : $row->Benutzername;
						$user_birth_today[] = "<img class=\"absmiddle\" src=\"theme/" . $GLOBALS['theme'] . "/images/calendar/birthday.png\" alt=\"\" /> <a href=\"index.php?p=user&amp;id=$row->Id&amp;area=" . AREA . "\">" . $row->Benutzername . "</a> <span class=\"calendarNorm\">(" . $row->age . ")</span><br />";
						
					}
				}
			} 
			
			return $user_birth_today;	
			
		} else {
			$user_birth_today = "<img class=\"absmiddle\" src=\"theme/" . $GLOBALS['theme'] . "/images/calendar/birthday.png\" alt=\"\" /> <a href=\"index.php?p=calendar&amp;action=birthdays&amp;show=public&amp;koobi_monat=$de_b&amp;koobi_jahr=" . ((isset($_REQUEST['koobi_jahr']) && is_numeric($_REQUEST['koobi_jahr']) && strlen($_REQUEST['koobi_jahr'])==4 ) ? $_REQUEST['koobi_jahr'] : date("Y")) . "&amp;koobi_tag=$de_d&amp;area=" . AREA . "\">$num " . $GLOBALS['config_vars']['Calendar_Birthdays'] ."</a>";
			return $user_birth_today;
		}
	} else {
		$user_birth_today = '';
		return $user_birth_today;
	}
} 

	
//============================================
// Koobi Kalenderklasse
//============================================
class KoobiCalendar extends Calendar 
{ 
	
	function getCalendarLink($month, $year) 
	{ 
		
		switch ($_REQUEST['p']) 
		{
   			case "index" :
    		case "" :
        	$sep = "index.php?area=" . AREA . "&amp;";
        break;
    	default :
       	$sep = redir() . "&amp;";
		} 

		return "index.php?koobi_monat=$month&amp;koobi_jahr=$year&amp;area=" . AREA; 
	}
	
	
	function getCalendarLinkAjax($month, $year) 
	{ 
		
		switch ($_REQUEST['p']) 
		{
   			case "index" :
    		case "" :
        	$sep = "index.php?area=" . AREA . "&amp;";
        break;
    	default :
       	$sep = redir() . "&amp;";
		} 

		return "index.php?koobi_monat=$month&amp;koobi_jahr=$year&amp;area=" . AREA; 
	}
	
}


class Calendar
{
	var $icons;
	var $startYear = '';
	var $endYear = '';
	var $startDay = 1;
    var $startMonth = 1;
	var $daysInMonth = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
	
	
	
    function Calendar()
    {
    }
	
	function dayNames()
	{
		$dn = explode(',', $GLOBALS['config_vars']['Calendar_Days']);
		return $dn;
	}
	
	function dayNames_week()
	{
		$dnw = explode(',', $GLOBALS['config_vars']['Calendar_DaysWeek']);
		return $dnw;
	}
	
	function tMonths()
	{
		$Months = explode(',', $GLOBALS['config_vars']['Calendar_Months']);
		return $Months;
	}
    
	
	function getDayCount()
	{
		return $this->daysInMonth;
	}
	
	function dispWeight($weight)
	{
		$ws = @explode(",", $GLOBALS['config_vars']['Calendar_Weight']);
		return $ws[$weight-1];
	}
	
	function getMonthNameSimple($month)
	{
		#global $monthNames;
		$this->monthNames = $this->tMonths();
		return $this->monthNames[$month-1];
	}
	
	
	function dispType()
	{
		$dispType = ($_REQUEST['show']=='private') ? $GLOBALS['config_vars']['Calendar_private'] : $GLOBALS['config_vars']['Calendar_public'];
		return $dispType;
	}
	
    function displayMonth()
	{
		$this->monthNames = $this->tMonths();
		return $this->monthNames;
	}
	
	function startYear()
	{
		return date("Y")-5;
	}
	
	function endYear()
	{
		return date("Y")+5;
	}
	
	function getTemplate()
	{
		return $this->Template = $Template;
	}
	
    function getDayNames()
    {
		$this->dayNames = $this->dayNames();
        return $this->dayNames;
    }
    
	function getDayNamesWeek()
    {
       $this->dayNames_week = $this->dayNames_week();
	    return $this->dayNames_week;
    }
	
	function privatePublic()
	{
		$pri_pub = (@$_REQUEST['show']=='private') ? 'private' : 'public';
		return $pri_pub;
	}
	
    
    function setDayNames($names)
    {
        $this->dayNames = $names;
    }
    
    
    function getMonthNames()
    {
       $this->monthNames = $this->tMonths();
	   return $this->monthNames;
    }
    
    function setMonthNames($names)
    {
        $this->monthNames = $names;
    }
    
    
     function getStartDay()
    {
        return $this->startDay;
    }
    
    // Start der Woche
    function setStartDay($day)
    {
        $this->startDay = $day;
    }
    
    
    // Anfangsmonat auslesen
    function getStartMonth()
    {
        return $this->startMonth;
    }
    
    // Anfangsmonat setzen
    function setStartMonth($month)
    {
        $this->startMonth = $month;
    }
    
    
    // Ungenutzt
    function getCalendarLink($month, $year)
    {
        return "";
    }
    
    // Ungenutzt
    function getDateLink($day, $month, $year)
    {
        return "";
    }


    // Ausgabe für den aktuellen Monat
    function getCurrentMonthView()
    {
        $d = getdate(time());
        return $this->getMonthView($d["mon"], $d["year"]);
    }
    

    // Ausgabe für das aktuelle Jahr
    function getCurrentYearView()
    {
        $d = getdate(time());
        return $this->getYearView($d["year"]);
    }
    
    // Ausgabe für 1 Monat
    function getMonthView($month, $year, $template, $showYear, $SmallBig, $Ajax='')
    {
		return $this->getMonthHTML($month, $year, $template, $showYear, $SmallBig, $Ajax);
    }
    

    // Ausgabe für 1 Jahr
    function getYearView($year)
    {
        return $this->getYearHTML($year);
    }
    
	// Geburtstage
	function Birthdays($d_b, $m_b, $year, $echo, $detail, $maxbd)
	{
		return $this->CalendarBirthdays($d_b, $m_b, $year, $echo, $detail, $maxbd);
	}
	
    function getDaysInMonth($month, $year)
    {
        if ($month < 1 || $month > 12)
        {
            return 0;
        }
   
        $d = $this->daysInMonth[$month - 1];
   
        if ($month == 2)
        {
            // Schaltjahr
        	if ($year%4 == 0)
            {
                if ($year%100 == 0)
                {
                    if ($year%400 == 0)
                    {
                        $d = 29;
                    }
                }
                else
                {
                    $d = 29;
                }
            }
        }
    
        return $d;
    }



    //============================================
	// Code für Smarty erzeugen
	//============================================
    function getMonthHTML($m, $y, $template, $showYear, $SmallBig, $Ajax='')
    {
       	$db_end = "";
		$link = "";
		$birthdays = "";
		$items = "";
		
        $a = $this->adjustDate($m, $y);
        $month = $a[0];
        $year = $a[1];        
        
    	$daysInMonth = $this->getDaysInMonth($month, $year);
    	$date = getdate(mktime(12, 0, 0, $month, 1, $year));
    	
    	$first = $date["wday"];
		$this->monthNames = $this->tMonths();
    	$monthName = $this->monthNames[$month - 1];
    	
    	$prev = $this->adjustDate($month - 1, $year);
    	$next = $this->adjustDate($month + 1, $year);
    	
		//$showYear = 1;
    	if ($showYear == 1)
    	{
    	   
			if($Ajax==1)
			{ 	$GLOBALS['tmpl']->assign('prevMonth', HOME_PATH . "/index.php?p=calendar&action=switch&koobi_monat=" . $prev[0] . "&koobi_jahr=" . $prev[1] . "&area=" . AREA);
				$GLOBALS['tmpl']->assign('nextMonth', HOME_PATH . "/index.php?p=calendar&action=switch&koobi_monat=" . $next[0] . "&koobi_jahr=" . $next[1] . "&area=" . AREA);
			} else {
				$GLOBALS['tmpl']->assign('prevMonth', $this->getCalendarLink($prev[0], $prev[1]));
    	    	$GLOBALS['tmpl']->assign('nextMonth', $this->getCalendarLink($next[0], $next[1]));
			}
    	}
    	else
    	{
    	    $prevMonth = "";
    	    $nextMonth = "";
    	}
    	
    	$GLOBALS['tmpl']->assign('header', $monthName . (($showYear > 0) ? " " . $year : ""));
		
		
    	// TAGE ARRAY
		$this->dayNames = $this->dayNames();
		
		$GLOBALS['tmpl']->assign('DayNamesArray', 
			array($this->dayNames[($this->startDay)%7],
				$this->dayNames[($this->startDay+1)%7],
				$this->dayNames[($this->startDay+2)%7],
				$this->dayNames[($this->startDay+3)%7],
				$this->dayNames[($this->startDay+4)%7],
				$this->dayNames[($this->startDay+5)%7],
				$this->dayNames[($this->startDay+6)%7])
				);
		
    	// Welcher Tag ist am Start? Wichtig, damit richtige Spalte gewählt wird
    	$d = $this->startDay + 1 - $first;
    	while ($d > 1)
    	    $d -= 7;

        // Hier wird geprüft, wann heute ist, damit der heutige Tag hervorgehoben werden kann
        $today = getdate(time());
    	
		$cal_data = array();
		while ($d <= $daysInMonth)
    	{
		    $cal_data_days = array(); 
    	    for ($i = 0; $i < 7; $i++)
    	    {
        	  
			   $link = "";
			   $birthdays = "";
			   // Gibt es Kalenderereignisse?
			    $show_type = (isset($_REQUEST['show']) &&  $_REQUEST['show'] == 'private') ? 'private' : 'public';
				$dbextra = (isset($_REQUEST['show']) &&  $_REQUEST['show'] == 'private') ? " AND Typ = 'private' AND Benutzer = '" . UID . ";' " : " AND Typ = 'public'";

                $db_start = " WHERE ((Start BETWEEN '" . mktime(0, 0, 0, $month, $d, $year) . "' AND '" . mktime(23, 59, 59, $month, $d, $year) . "')";
                $db_start .= " OR (( tdays != 0) AND ( tdays >= '" . mktime(0, 0, 0, $month, $d, $year) . "' ) AND ( start <= '" . mktime(23, 59, 59, $month, $d-1, $year) . "') ))";

                $CalQuery = "SELECT * FROM " . PREFIX . "_kalender $db_start $db_end $dbextra ORDER BY Start ASC";
                $CalSQL = $GLOBALS['db']->Query($CalQuery);
				$numSQL = $CalSQL->numrows();
				
				$items = array();
				
			    // Geburtstage
				$birthdays = CalendarBirthdays($d, $month, $year,0);
				
				while ($row = $CalSQL->fetchrow())
				{
             		if ($row->Titel != '') 
					{ 
						
						$descr = (!empty($row->Beschreibung)) ? $row->Beschreibung : "";
                    	$id = $row->Id;
                     	$tdays = $row->tdays;
						
						$link = HOME_PATH . "/index.php?p=calendar&amp;action=events&amp;show=$show_type&amp;koobi_monat=".date("m", $row->Start)."&amp;koobi_jahr=".date("Y", $row->Start)."&amp;koobi_tag=".date("d", $row->Start)."&amp;area=" . AREA;
						
						$row->link_event_only = HOME_PATH . "/index.php?p=calendar&amp;action=events&amp;show=$show_type&amp;koobi_monat=".date("m", $row->Start)."&amp;koobi_jahr=".date("Y", $row->Start)."&amp;koobi_tag=".date("d", $row->Start)."&amp;area=" . AREA . "#$row->Id";
						$row->day_event_link = $link;
						
						
						if(date("Y", $row->Start) != $year)
						{
							$row->is_not_inyear = 1;
						}
						
                        array_push($items, $row);
                    } 
					
                } 
				
				 
				// Geburtstage auslesen, wenn kein privater Kalender
				
				
				
				// Kleiner oder großer Kalender?
			    if($SmallBig == 'small')
				{
					$class = ($year == $today["year"] && $month == $today["mon"] && $d == $today["mday"]) ? "calendarToday" : "calendar";
    	        } else {
					$class = ($year == $today["year"] && $month == $today["mon"] && $d == $today["mday"]) ? "calendarTodayBig" : "calendarBig";
    	       	}
				  
				if ($d > 0 && $d <= $daysInMonth)
    	        {
    	            $CalDataInner->thelink = (($link == "") ? (($SmallBig=='big') ? "<strong>$d</strong>" : "$d") : ( ($SmallBig!='big' && ($year == $today["year"] && $month == $today["mon"] && $d == $today["mday"]) ) ? "<a class=\"calendarLinkSmall\" href=\"$link\"><strong>$d</strong></a>" : "<a class=\"calendarLink\" href=\"$link\"><strong>$d</strong></a>" ));
					$CalDataInner->tdclass = $class;
					$CalDataInner->packed_events_link = $link;
				} else {
    	            $minus_month = date("d", mktime(0,0,0,$month,$d,$year));
					$minus_month = eregi_replace("^0", "", $minus_month);
					$CalDataInner->thelink = "&nbsp;<span class=\"calendarInactiveDay\">$minus_month</span>";
					$CalDataInner->tdclass = "calendarBlanc";
					$CalDataInner->packed_events_link = $link;
					$CalDataInner->packed_events_niy = 1;
				 }
      	         
				
				
				$CalDataInner->countitems = count($items);
				$CalDataInner->Ereignisse = $items;
				$CalDataInner->Geburtstage = $birthdays;
				
				array_push($cal_data_days,$CalDataInner);   
				
				unset($birthdays);
				unset($link);
				unset($CalDataInner);
				unset($row);
        	    $d++;
    	    }
		
		
		
    	$row->StartWeek = mktime(0, 0, 0, $month, $d-7, $year);
		$row->EndWeek = mktime(23, 59, 59, $month, $d-1, $year);
		
		$row->CalDataInner = $cal_data_days;
    	array_push($cal_data, $row);
		}
    	
		#if(isset($_REQUEST['show']) && $_REQUEST['show'] != 'private')
		#{
			#$birthdays = $this->Birthdays($d, $month, $year, 1, 0, 3, 1);
		#	$birthdays = CalendarBirthdays($d, $month, $year);
		#	$GLOBALS['tmpl']->assign('birthdays', $birthdays);
		#}
				
		$GLOBALS['tmpl']->assign('linkmonth', date("m", mktime(0,0,1, $month, 1, $year)));
		$GLOBALS['tmpl']->assign('linkyear', date("Y", mktime(0,0,1, $month, 1, $year)));
		
		$GLOBALS['tmpl']->assign('cal_data', $cal_data);
		$s = $GLOBALS['tmpl']->fetch($template);
		
		return $s; 	
    }
    
	//
	// Wochenübersicht
	//
	function displayWeek($start,$end)
	{
		if(UGROUP==2 && $_REQUEST['show']=='private')
		{
			header("Location:index.php?p=calendar");
			exit;
		}
		
		$show_type = ($_REQUEST['show'] == 'private') ? 'private' : 'public';
		$dbextra = ($_REQUEST['show'] == 'private') ? " AND Typ = 'private' AND Benutzer = '" . UID . ";' " : " AND Typ = 'public'";
	
		$day_end = $end - 86400;
		
		$db_start = " WHERE Start >= '" . $start . "' AND  Start <='" . $end . "'";
	
		$CalQuery = "SELECT * FROM " . PREFIX . "_kalender $db_start $dbextra ORDER BY start ASC";
		$CalSQL = $GLOBALS['db']->Query($CalQuery);
		$items = array();
		while($row = $CalSQL->fetchrow())
		{
			
			array_push($items, $row);
		}
		return $items;
	}
	
	//============================================
	// Jahresübersicht
	//============================================
    function getYearHTML($year)
    {
    	$the_tpl = $GLOBALS['config']['source'] . '/calendar/calendar_small_years.tpl';
		
		$d = getdate(time()); 
		$year = ($_REQUEST['koobi_jahr'] != '') ? (int)$_REQUEST['koobi_jahr'] : $d['year'];
		
    	$prev = $this->getCalendarLink(0, $year - 1);
    	$next = $this->getCalendarLink(0, $year + 1);
        
		for($i=0; $i<12; $i++)
		{
			$GLOBALS['tmpl']->assign('cal_' . $i, $this->getMonthView($i + $this->startMonth, $year, $the_tpl,'2','small'));
		}
		
    }

    
    function adjustDate($month, $year)
    {
        $a = array();  
        $a[0] = $month;
        $a[1] = $year;
        
        while ($a[0] > 12)
        {
            $a[0] -= 12;
            $a[1]++;
        }
        
        while ($a[0] <= 0)
        {
            $a[0] += 12;
            $a[1]--;
        }
        
        return $a;
    }

	//============================================
	// Neueintrag
	//============================================
	function insertEvent()
	{
		if ($_REQUEST['newevent'] == 1 && UGROUP != 2)
		{
			if (@$_REQUEST['s_wday'] == 1)
			{
				$start = mktime(0, 0, 1, $_REQUEST['month'], $_REQUEST['day'], $_REQUEST['year']);
				$ende = mktime(23, 59, 59, $_REQUEST['month'], $_REQUEST['day'], $_REQUEST['year']);
			} 
			else 
			{
				$start = mktime($_REQUEST['s_std'], $_REQUEST['s_min'], 0, $_REQUEST['month'], $_REQUEST['day'], $_REQUEST['year']);
				$ende = mktime($_REQUEST['e_std'], $_REQUEST['e_min'], 0, $_REQUEST['month'], $_REQUEST['day'], $_REQUEST['year']);
			} 
			$datum = (int)$_REQUEST['day'] . '-' . (int)$_REQUEST['month'] . '-' . (int)$_REQUEST['year'];
			$tdays = ($_REQUEST['days'] > 1) ? mktime(23, 59, 59, $_REQUEST['month'], $_REQUEST['day'] + ($_REQUEST['days']-1), $_REQUEST['year']) : 0 ;
			$pb = $_REQUEST['show'];
			$pb = ($pb == 'public' && !permission('calendar_event_new')) ? 'private' : $pb;
		
			if ($_REQUEST['name'] != '')
			{
				$sql = $GLOBALS['db']->Query("INSERT INTO " . PREFIX . "_kalender (Id,Benutzer,Datum,Titel,Beschreibung,Start,Ende,Typ,wd,Gewicht,tdays) VALUES (
				'',
				'" . UID . "',
				'$datum',
				'" . strip_tags($_REQUEST['name']) . "',
				'" . addslashes($_REQUEST['text']) ."',
				'$start',
				'$ende',
				'$pb',
				'" . @abs((int)$_REQUEST['s_wday']) . "',
				'" . @abs((int)$_REQUEST['weight']) . "',
				'" . @abs((int)$tdays) . "'
				)");
			} 
		} 
	}
	
	
	//============================================
	// Ereignis(se) auslesen
	//============================================
	function showEvents()
	{
       $db_end = '';
		
		$month = $_REQUEST['koobi_monat'];
		$day = $_REQUEST['koobi_tag'];
		$year = $_REQUEST['koobi_jahr'];
		
		$dbextra = ($_REQUEST['show'] == 'private') ? " AND Typ = 'private' AND Benutzer = '" . UID . ";' " : " AND Typ = 'public'";
        $db_start = " WHERE ((Start between '" . mktime(0, 0, 0, $month, $day, $year ) . "' AND '" . mktime(23, 59, 59, $month, $day, $year) . "')";
        $db_start .= " OR (( tdays != 0) AND ( tdays >= '" . mktime(0, 0, 0, $month, $day, $year) . "' ) AND (Start <= '" . mktime(23, 59, 59, $month, $day-1, $year) . "') ))";

        $dbfirst = "SELECT * FROM " . PREFIX . "_kalender $db_start $db_end $dbextra AND id != '$_REQUEST[id]' ORDER BY start ASC";
        $sql = $GLOBALS['db']->Query($dbfirst);

        $events = array();
        while ($row= $sql->fetchrow())
		{
            $row->weight = $this->dispWeight($row->Gewicht);
			$row->descr = kcodes($row->Beschreibung);
			$row->descr = dosmilies($row->descr);
			$row->UserName = fetchusername($row->Benutzer);
			array_push($events, $row);
        } 
		
		$GLOBALS['tmpl']->assign('events', $events); 	
		$GLOBALS['tmpl']->assign('UID', UID); 	
	}
	
	//============================================
	// Ereignis bearbeiten
	//============================================
	function updateEvent()
	{
		$start = mktime($_REQUEST['s_std'], $_REQUEST['s_min'], 0, $_REQUEST['koobi_monat'], $_REQUEST['koobi_tag'], $_REQUEST['koobi_jahr']);
        $ende = mktime($_REQUEST['e_std'], $_REQUEST['e_min'], 0, $_REQUEST['koobi_monat'], $_REQUEST['koobi_tag'], $_REQUEST['koobi_jahr']);
        $wday = (@$_REQUEST['s_wday'] == 1) ? 1 : 0; 
            
		$disp_type = addslashes($_REQUEST['show']);
			
        $sql = "UPDATE " . PREFIX . "_kalender SET   
				Titel = '" . addslashes(strip_tags($_REQUEST['name'])) . "',
				Beschreibung = '" . addslashes($_REQUEST['text']) . "',
				Gewicht = '". addslashes($_REQUEST['weight']) ."',
				Start = '$start',
				Ende = '$ende',
				wd = '$wday',
				Erledigt = '" . @abs((int)$_REQUEST['done']) . "',
				Typ = '".$disp_type."' 
			WHERE 
				Id='" . addslashes($_REQUEST['id']) . "'";
		$GLOBALS['db']->Query($sql);
	}
	
	//============================================
	// Objekte auslesen
	//============================================
	function fetchData()
	{
		$sql = $GLOBALS['db']->Query("SELECT * FROM " . PREFIX . "_kalender WHERE Id='" . $_REQUEST['id'] . "'");
    	$row = @$sql->fetchrow();
		return $row;
	}
	
	//============================================
	// Ereignis löschen
	//============================================
	function deleteDataById()
	{
		$GLOBALS['db']->Query("DELETE FROM " . PREFIX . "_kalender WHERE Id='" . $_REQUEST['id'] . "'");
	}
	
	
	//============================================
	// Suche
	//============================================
	function searchEvent($own='')
	{
	   	$find = mysql_escape_string($_GET['qc']);
        $fq = "SELECT * FROM " . PREFIX . "_kalender WHERE (Titel LIKE '%$find%' OR Beschreibung LIKE '%$find%' AND Typ = 'public') OR (Titel LIKE '%$find%' OR Beschreibung LIKE '%$find%' AND Typ = 'private' AND Benutzer = '" . UID . "') ORDER BY Start DESC";
        $sql = $GLOBALS['db']->Query($fq);
        
		$results = array();
		if (strlen($find) >= 3)
		{
            while ($row = $sql->fetchrow())
			{
                $row->month = date("n", $row->Start);
                $row->year = date("Y", $row->Start);
                $row->day = date("j", $row->Start);
                array_push($results, $row);
            } 
        } 
        
		if (strlen($find) < 3)
		{
			$GLOBALS['tmpl']->assign('warning', $GLOBALS['config_vars']['Calendar_search_noinsert']);
		}
		$GLOBALS['tmpl']->assign('search', 1); 
		$GLOBALS['tmpl']->assign('results', $results);
	}
	
	
	function myEvents()
	{
	   	$find = mysql_escape_string(@$_GET['qc']);
		
		if (strlen($find) >= 3)
		{
			$search = "(Titel LIKE '%$find%' OR Beschreibung LIKE '%$find%' AND Typ = 'public' AND Benutzer = '" . UID . "') OR (Titel LIKE '%$find%' OR Beschreibung LIKE '%$find%' AND Typ = 'private' AND Benutzer = '" . UID . "')";
		} else {
			$search = "(Benutzer = '" . UID . "')";
		}
		
		
        $fq = "SELECT * FROM " . PREFIX . "_kalender WHERE {$search} ORDER BY Start DESC";
        $sql = $GLOBALS['db']->Query($fq);
        
		$results = array();
		
		while ($row = $sql->fetchrow())
		{
			$row->month = date("n", $row->Start);
			$row->year = date("Y", $row->Start);
			$row->day = date("j", $row->Start);
			array_push($results, $row);
		} 
      
		$GLOBALS['tmpl']->assign('search', 1); 
		$GLOBALS['tmpl']->assign('results', $results);
	}
	
// Ende der Klasse	
}
?>