<?php 
// ======================================================================== //
//   KOOBI - CMS                                                            //
//   SCRiPTMAFiA 2005 - THE DiRTY HANDS ON YOUR SCRiPTS                     //
//                                                                          //
//   DREAM4 - AM MICHELSGRUND 7 - 69469 WEINHEIM - GERMANY                  //    
//   TEL. +49(0) 6201 - 980442 / FAX. +49(0) 6201 - 64809                   //
// ======================================================================== //
if (!checkactive('calendar')) include(dirname(__FILE__) . "/lib/notactive.php");

// --------------------------------------------------------------------------------------------------------------
// tage und monate in array
$days = @explode(",", $lang['calendar_dayarray']);
$months = @explode(",", $lang['calendar_montharray']);
$months_short = @explode(",", $lang['calendar_montharrayshort']);
$weight = @explode(",", $lang['calendar_weight']);

// --------------------------------------------------------------------------------------------------------------
// ein tag in sekunden
define("DAY", (86400));

// --------------------------------------------------------------------------------------------------------------
// termin loeschen
if ($_REQUEST['delevent'] == 1) {
    $sql = new dbc("SELECT uid FROM " . PREFIX . "_calendar WHERE id='" . escs($_REQUEST['id']) . "'");
    $row = $sql->fetchrow();
    if ($row->uid != UID) {
        $EOUT = msg("nopermission", "f_err_permited", str_replace("__URL__", "index.php?p=calendar&amp;show=$_REQUEST[disp]&amp;area=$area", $lang['redirect']), "index.php?p=calendar&amp;show=$_REQUEST[disp]&amp;area=$area");
        $error = 1;
        $NOOUT = 1;
    } else {
        $sql = new dbc("DELETE FROM " . PREFIX . "_calendar WHERE id='" . escs($_REQUEST['id']) . "'");
        $EOUT = msg("event_delete", "event_delete_t", str_replace("__URL__", "index.php?p=calendar&amp;show=$_REQUEST[disp]&amp;area=$area", $lang['redirect']), "index.php?p=calendar&amp;show=$_REQUEST[disp]&amp;area=$area");
        $NOOUT = 1;
    } 
} 

// --------------------------------------------------------------------------------------------------------------
// termin bearbeiten
if ($_REQUEST['editevent'] == 1) {
    $sql = new dbc("SELECT uid FROM " . PREFIX . "_calendar WHERE id='" . escs($_REQUEST['id']) . "'");
    $row = $sql->fetchrow();
    if (($row->uid != UID) && (UGROUP != 1)) {
        $EOUT = msg("nopermission", "f_err_permited", str_replace("__URL__", "index.php?p=calendar&amp;show=$_REQUEST[disp]&amp;area=$area", $lang['redirect']), "index.php?p=calendar&amp;show=$_REQUEST[disp]&amp;area=$area");
        $error = 1;
        $NOOUT = 1;
    } else {
        // wenn titel nicht leer ist...
        if ($_REQUEST['name'] != '') {
            // zeit
            $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']);
            $wday = ($_REQUEST['s_wday'] == 1) ? 1 : 0; 
            // beschreibung
            $text = ($_REQUEST['autobr'] == 1) ? nl2br($_REQUEST['descr']): $_REQUEST['descr'];
            $sql = "
						UPDATE " . PREFIX . "_calendar set 
						name = '" . strip_tags($_REQUEST['name']) . "',
						descr = '" . strip_tags($text, "<br><br /><small><pre><b><strong><em><i><span><div>") . "',
						weight = '$_REQUEST[weight]',
						start = '$start',
						ende = '$ende',
						wd = '$wday',
						done = '" . (int)$_REQUEST['done'] . "'
						WHERE id='" . escs($_REQUEST['id']) . "'
						";

            $query = new dbc($sql);
        } 
        $EOUT = msg("event_edit", "event_edit_t", str_replace("__URL__", "index.php?p=calendar&amp;year=$_REQUEST[year]&amp;month=$_REQUEST[month]&amp;day=$_REQUEST[day]&amp;id=$_REQUEST[id]&amp;display=$_REQUEST[display]&amp;area=$_REQUEST[area]", $lang['redirect']), "index.php?p=calendar&amp;year=$_REQUEST[year]&amp;month=$_REQUEST[month]&amp;day=$_REQUEST[day]&amp;id=$_REQUEST[id]&amp;display=$_REQUEST[display]&amp;area=$_REQUEST[area]");
        $NOOUT = 1;
    } 
} 


// --------------------------------------------------------------------------------------------------------------
// neuer termin
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'];
    $text = ($_REQUEST['autobr'] == 1) ? nl2br($_REQUEST['descr']): $_REQUEST['descr'];
    $tdays = ($_REQUEST['days'] > 1) ? mktime(23, 59, 59, $_REQUEST['month'], $_REQUEST['day'] + ($_REQUEST['days']-1), $_REQUEST['year']) : 0 ;
    $pb = (UGROUP == 1 && $_REQUEST['show'] == 'public') ? 'public' : 'private';

    if ($_REQUEST['name'] != '') {
        $sql = new dbc("INSERT INTO " . PREFIX . "_calendar (
	id,
	uid,
	datum,
	name,
	descr,
	start,
	ende,
	type,
	wd,
	weight,
	tdays
) VALUES (
	'',
	'" . UID . "',
	'$datum',
	'" . strip_tags($_REQUEST['name']) . "',
	'" . strip_tags($text, "<br><br /><small><pre><b><strong><em><i><span><div>") . "',
	'$start',
	'$ende',
	'$pb',
	'" . (int)$_REQUEST['s_wday'] . "',
	'" . (int)$_REQUEST['weight'] . "',
	'" . (int)$tdays . "'
	)"
            );
    } 
    // weiterleiten
    $EOUT = msg("event_created", "event_created_t", str_replace("__URL__", "index.php?p=calendar&show=$_REQUEST[show]&area=$_REQUEST[area]", $lang['redirect']), "index.php?p=calendar&show=$_REQUEST[show]&area=$_REQUEST[area]");
    $NOOUT = 1;
} 
// --------------------------------------------------------------------------------------------------------------
// kalenderübersicht
// wurde form angeschick? wenn nicht, nehme aktuelles datum
$month = ($_GET['month'] != '') ? (int)$_GET['month'] : date ("m");
$year = ($_GET['year'] != '') ? (int)$_GET['year'] : date("Y");
$tmpl->assign('ya', $year);
// ist datum initialisiert?
if (!@checkdate($month, 1, $year)) {
    $today_array = getdate();
    $month = $today_array['mon'];
    $year = $today_array['year'];
} 

$start = mktime (0, 0, 0, $month, 1, $year, 0);
$first_array = getdate($start);

$tmpl->assign('weight', $weight);
// damit können wir kurze monatsnamen verwenden
function mname($m)
{
    global $months_short;
    $real = $months_short[$m-1];
    return $real;
} 

switch ($_REQUEST['display']) {
    case '' : 
        // seitentitel
        $sname = simpletitle($lang['calendar'] . SEPARATOR . $months[$month-1] . ' ' . $year);
        $header = $lang['calendar'] . SEPARATOR . $months[$month-1] . ' ' . $year;
        $tmpl->assign('days', $days);
        $cal_html .= $tmpl->fetch('calendar/cal_days.tpl'); 
        // --------------------------------------------------------------------------------------------------------------
        // kalender zusammensetzen
        foreach ($days as $day)
        for($zaehler = 1; $zaehler < (6 * 7); $zaehler++) {
            $tagArr = getdate($start);
            $items = array();

            if ((($zaehler) % 7) == 1) {
                if ($tagArr[mon] != $month) break;
                $cal_html .= "</tr><tr>\n";
            } 

            if ($zaehler < $first_array[wday] || $tagArr[mon] != $month) {
                $isnoday = 1;
                $tmpl->assign('isnoday', $isnoday);
                $cal_html .= $tmpl->fetch('calendar/cal_td_blanc.tpl');
            } else {
                $isnoday = 2;
                $tmpl->assign('isnoday', $isnoday);
                $tmpl->assign('wday', $tagArr[wday]);

                $dbextra = ($_REQUEST['show'] == 'private') ? " AND type = 'private' AND uid = '" . UID . ";' " : " AND type = 'public'";

                $db_start = " WHERE ((start between 
							'" . mktime(0, 0, 0, $tagArr['mon'], $tagArr['mday'], $tagArr['year']) . "' && 
							'" . mktime(23, 59, 59, $tagArr['mon'], $tagArr['mday'], $tagArr['year']) . "'
							)";
                $db_start .= " OR (( 
						tdays != 0) && ( tdays >= '" . mktime(0, 0, 0, $tagArr['mon'], $tagArr['mday'], $tagArr['year']) . "' ) && 
						( start <= '" . mktime(23, 59, 59, $tagArr['mon'], $tagArr['mday']-1, $tagArr['year']) . "') 
					  ))";

                $dbfirst = "SELECT * FROM " . PREFIX . "_calendar $db_start $db_end $dbextra ORDER BY start ASC";
                $sql = new dbc($dbfirst);

                while ($row = $sql->fetchrow()) {
                    if ($row->name != '') {
                        $special = $row->name;
                        $descr = $row->descr;
                        $id = $row->id;
                        $tdays = $row->tdays;
                        array_push($items, $row);
                    } 
                } 

                $isg = ($row->g == 1) ? 1 : 0;
                $isthisday = ($tagArr[mday] . "-" . $tagArr[mon] . "-" . $tagArr[year] == date("j-n-Y")) ? 1 : 0;
                $tmpl->assign('items', $items);
                $tmpl->assign('isge', $isg);
                $tmpl->assign('thisday', $isthisday);
                $tmpl->assign('monat_kurz', mname($tagArr[mon]));
                $tmpl->assign('specialday', $special);
                $tmpl->assign('datum', $tagArr[mday]);
                $tmpl->assign('special', $special);
                $tmpl->assign('descr', $descr);
                $tmpl->assign('id', $id);
                $tmpl->assign('month', $month);
                if ($_REQUEST['show'] == 'public') $tmpl->assign('birthd', today_birthdays($tagArr[mday], $tagArr[mon], $tagArr[year], 1));
                $items = "";
                $cal_html .= $tmpl->fetch('calendar/cal_td.tpl');

                $special = '';
                $isnoday = '';
                $descr = '';
                $isthisday = '';
                $dbfirst = '';

                $start += DAY;
            } 
        } 
        $cal_html .= $tmpl->fetch('calendar/table_end.tpl');

        $tmpl->assign('cal_html', $cal_html);
        $tmpl->assign('start_cal', date("Y")-6);
        $tmpl->assign('end_cal', date("Y") + 5);
        $tmpl->assign('months', $months);
        $tmpl->assign('select', $tmpl->fetch('calendar/select.tpl'));
        break; 
    // --------------------------------------------------------------------------------------------------------------
    // suche
    case 'search' :
        $find = mysql_escape_string(kspecialchars($_GET['qc']));
        $fq = "SELECT * FROM " . PREFIX . "_calendar WHERE 
			(name like '%$find%' OR descr like '%$find%' AND type = 'public')
			OR
			(name like '%$find%' OR descr like '%$find%' AND type = 'private' AND uid = '" . UID . "')
			ORDER BY start DESC
			";
        $sql = new dbc($fq);
        $results = array();

        if (strlen($find) >= 2) {
            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);
            } 
        } 
        $tmpl->assign('search', 1);
        if (strlen($find) < 2) $tmpl->assign('warning', $lang['calendar_search_noinsert']);
        $sname = simpletitle($lang['calendar'] . SEPARATOR . $lang['search']);
        $header = "<a href=\"index.php?p=calendar&amp;area=$area\">$lang[calendar]</a>" . SEPARATOR . $lang['search'] ;
        $tmpl->assign('results', $results);
        $tmpl->assign('results', $tmpl->fetch('calendar/results.tpl'));
        break; 
    // --------------------------------------------------------------------------------------------------------------
    // geburtstage
    case 'birthdays' :
        $sname = simpletitle($lang['birtdays'] . SEPARATOR . (int)$_REQUEST['day'] . "." . $months[$month-1] . ' ' . $year);
        $header = "<a href=\"index.php?p=calendar&amp;show=public&amp;year=$_REQUEST[year]&amp;month=$_REQUEST[month]&amp;area=$area\">$lang[calendar]</a>" . SEPARATOR . $lang['birtdays'] . " " . (int)$_REQUEST['day'] . "." . $months[$month-1] . ' ' . $year;
        $tmpl->assign('the_birthday', today_birthdays($_REQUEST['day'], $_REQUEST['month'], $_REQUEST['year'], 1, 1));
        break; 
    // --------------------------------------------------------------------------------------------------------------
    // privater termine
    case 'private' :

        $sql = new dbc("SELECT * FROM " . PREFIX . "_calendar WHERE id='" . escs($_REQUEST['id']) . "'");
        $row = $sql->fetchrow();
        if ($row->uid != UID) {
            $EOUT = stderror("nopermission", $lang['nopermission_t'], str_replace("__URL__", "index.php", $lang['redirect']), "index.php", 1);
            $error = 1;
            $NOOUT = 1;
        } else {
            $sname = simpletitle($lang['calendar'] . SEPARATOR . $lang['events'] . " " . (int)$_REQUEST['day'] . "." . $months[$month-1] . ' ' . $year);
            $header = "<a href=\"index.php?p=calendar&amp;display=&amp;year=$_REQUEST[year]&amp;month=$_REQUEST[month]&amp;show=$_REQUEST[display]&amp;area=$area\">$lang[calendar]</a>" . SEPARATOR . $lang['events'] . " " . (int)$_REQUEST['day'] . "." . $months[$month-1] . ' ' . $year;

            $smin_temp = date("i", $row->start);
            $smin = ($smin_temp < 10) ? substr($smin_temp, 1) : date("i", $row->start);

            $emin_temp = date("i", $row->ende);
            $emin = ($emin_temp < 10) ? substr($emin_temp, 1) : date("i", $row->ende); 
            // --------------------------------------------------------------------------------------------------------------
            // weitere termine des datum
            $dbextra = ($_REQUEST['display'] == 'private') ? " AND type = 'private' AND uid = '" . UID . ";' " : " AND type = 'public'";
            $db_start = " WHERE ((start between '" . mktime(0, 0, 0, $_REQUEST['month'], $_REQUEST['day'], $_REQUEST['year']) . "' && '" . mktime(23, 59, 59, $_REQUEST['month'], $_REQUEST['day'], $_REQUEST['year']) . "')";
            $db_start .= " OR (( tdays != 0) && ( tdays >= '" . mktime(0, 0, 0, $_REQUEST['month'], $_REQUEST['day'], $_REQUEST['year']) . "' ) && ( start <= '" . mktime(23, 59, 59, $_REQUEST['month'], $_REQUEST['day']-1, $_REQUEST['year']) . "') ))";

            $dbfirst = "SELECT * FROM " . PREFIX . "_calendar $db_start $db_end $dbextra AND id != '$_REQUEST[id]' ORDER BY start ASC";
            $sql = new dbc($dbfirst);

            $items = array();
            while ($row_extra = $sql->fetchrow()) {
                array_push($items, $row_extra);
            } 

            $tmpl->assign('s_std', date("G", $row->start));
            $tmpl->assign('e_std', date("G", $row->ende));
            $tmpl->assign('s_min', $smin);
            $tmpl->assign('e_min', $emin);
            $tmpl->assign('disp', 1);
            $tmpl->assign('row', $row);
            $tmpl->assign('items', $items);
            $tmpl->assign('eventdisplay', $tmpl->fetch('calendar/eventdisplay.tpl'));
        } 
        break; 
    // --------------------------------------------------------------------------------------------------------------
    // öffentliche termine
    case 'public' :
        $sql = new dbc("SELECT * FROM " . PREFIX . "_calendar WHERE id='" . escs($_REQUEST['id']) . "'");
        $row = $sql->fetchrow();
        $sname = simpletitle($lang['calendar'] . SEPARATOR . $lang['events'] . " " . (int)$_REQUEST['day'] . "." . $months[$month-1] . ' ' . $year);
        $header = "<a href=\"index.php?p=calendar&amp;display=&amp;year=$_REQUEST[year]&amp;month=$_REQUEST[month]&amp;show=$_REQUEST[display]&amp;area=$area\">$lang[calendar]</a>" . SEPARATOR . $lang['events'] . " " . (int)$_REQUEST['day'] . "." . $months[$month-1] . ' ' . $year;

        $smin_temp = date("i", $row->start);
        $smin = ($smin_temp < 10) ? substr($smin_temp, 1) : date("i", $row->start);

        $emin_temp = date("i", $row->ende);
        $emin = ($emin_temp < 10) ? substr($emin_temp, 1) : date("i", $row->ende); 
        // --------------------------------------------------------------------------------------------------------------
        // weitere termine des datum
        $dbextra = ($_REQUEST['display'] == 'private') ? " AND type = 'private' AND uid = '" . UID . ";' " : " AND type = 'public'";
        $db_start = " WHERE ((start between '" . mktime(0, 0, 0, $_REQUEST['month'], $_REQUEST['day'], $_REQUEST['year']) . "' && '" . mktime(23, 59, 59, $_REQUEST['month'], $_REQUEST['day'], $_REQUEST['year']) . "')";
        $db_start .= " OR (( tdays != 0) && ( tdays >= '" . mktime(0, 0, 0, $_REQUEST['month'], $_REQUEST['day'], $_REQUEST['year']) . "' ) && ( start <= '" . mktime(23, 59, 59, $_REQUEST['month'], $_REQUEST['day']-1, $_REQUEST['year']) . "') ))";

        $dbfirst = "SELECT * FROM " . PREFIX . "_calendar $db_start $db_end $dbextra AND id != '$_REQUEST[id]' ORDER BY start ASC";
        $sql = new dbc($dbfirst);

        $items = array();
        while ($row_extra = $sql->fetchrow()) {
            array_push($items, $row_extra);
        } 

        $tmpl->assign('s_std', date("G", $row->start));
        $tmpl->assign('e_std', date("G", $row->ende));
        $tmpl->assign('s_min', $smin);
        $tmpl->assign('e_min', $emin);
        $tmpl->assign('disp', 1);
        $tmpl->assign('row', $row);
        $tmpl->assign('items', $items);
        $tmpl->assign('eventdisplay', $tmpl->fetch('calendar/eventdisplay.tpl'));
        break;
} 

$tmpl->assign('eventform', $tmpl->fetch('calendar/eventform.tpl'));
$tmpl->assign("content", parsetrue("container/" . container("calendar_in"), $header , $tmpl->fetch("calendar/calendar.tpl")));

?>