<?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
//=================================================================
if(!defined("BASE_DIR")) exit;
if(get_active('calendar') && file_exists(BASE_DIR . '/class/Class.Calendar.php'))
{
	include_once(BASE_DIR . '/class/Class.Calendar.php');

	$_REQUEST['koobi_jahr'] = (isset($_REQUEST['koobi_jahr']) && is_numeric($_REQUEST['koobi_jahr']) && strlen($_REQUEST['koobi_jahr'])==4 && $_REQUEST['koobi_jahr'] <= 2050) ? abs((int)$_REQUEST['koobi_jahr']) : date("Y");
	$_REQUEST['koobi_tag'] = (isset($_REQUEST['koobi_tag']) && is_numeric($_REQUEST['koobi_tag']) && strlen($_REQUEST['koobi_tag'])<=2 && $_REQUEST['koobi_tag'] <= 31) ? abs((int)$_REQUEST['koobi_tag']) : date("d");
	$_REQUEST['koobi_monat'] = (isset($_REQUEST['koobi_monat']) && is_numeric($_REQUEST['koobi_monat']) && strlen($_REQUEST['koobi_monat'])<=2 && $_REQUEST['koobi_monat'] <= 12) ? abs((int)$_REQUEST['koobi_monat']) : date("m");
	
	switch(@$_REQUEST['show'])
	{
		case '':
		case 'public':
			$_REQUEST['show'] = 'public';
		break;
		
		case 'private':
			$_REQUEST['show'] = 'private';
		break;
	}
	
	$cal = new KoobiCalendar;
	
	$d = getdate(time()); 
	$year = ($_REQUEST['koobi_jahr'] != '') ? (int)$_REQUEST['koobi_jahr'] : $d['year'];
	$month = ($_REQUEST['koobi_monat'] != '') ? (int)$_REQUEST['koobi_monat'] : $d['mon'];
	
	$current_year = date("Y");
	$current_month = date("m");
		
	switch(@$_REQUEST['action'])
	{
		//============================================
		// Kalenderuebersicht
		//============================================
		case '' :
			$sname = $GLOBALS['config_vars']['Calendar'];
			$header = "<a href=\"index.php?p=calendar&amp;koobi_monat=$current_month&amp;koobi_jahr=$current_year&amp;area=$area\">" . $GLOBALS['config_vars']['Calendar'] . "</a>";
			
			//============================================
			// Kalender klein (vorheriger und naechster Monat)
			//============================================
			$m_p = date("m", mktime(0,0,1, $month-1 ,1, $year));
			$y_p = date("Y", mktime(0,0,1, $month-1 ,1, $year));
		
			$m_n = date("m", mktime(0,0,1, $month+1 ,1, $year));
			$y_n = date("Y", mktime(0,0,1, $month+1 ,1, $year));
			
			//============================================
			// Kalender aktueller Monat (Grossansicht)
			//============================================
			$GLOBALS['tmpl']->assign('mp' , $m_p);
			$GLOBALS['tmpl']->assign('yp' , $y_p);
			$GLOBALS['tmpl']->assign('mn' , $m_n);
			$GLOBALS['tmpl']->assign('yn' , $y_n);
			
			$GLOBALS['tmpl']->assign('text_prev_m', $cal->getMonthNameSimple($m_p) . ' ' . $y_p);
			$GLOBALS['tmpl']->assign('text_next_m', $cal->getMonthNameSimple($m_n) . ' ' . $y_n);
			$GLOBALS['tmpl']->assign('Year', $year);
			$GLOBALS['tmpl']->assign('showtype', (@$_REQUEST['show']=='private') ? 'private' : 'public');
			$GLOBALS['tmpl']->assign('calendar', $cal->getMonthView($month, $year, $GLOBALS['config']['source'] . '/calendar/calendar_big.tpl', '1', 'big'));
			$GLOBALS['tmpl']->assign('calendar_prev', $cal->getMonthView($m_p, $y_p,$GLOBALS['config']['source'] . '/calendar/calendar_small_prev_next.tpl',1,'small'));
			$GLOBALS['tmpl']->assign('calendar_next', $cal->getMonthView($m_n, $y_n,$GLOBALS['config']['source'] . '/calendar/calendar_small_prev_next.tpl',1,'small'));
			$GLOBALS['tmpl']->assign('month' , $cal->displayMonth());
			$GLOBALS['tmpl']->assign('currentmonth' , $month);
			$GLOBALS['tmpl']->assign('startYear', $cal->startYear());
			$GLOBALS['tmpl']->assign('privatePublic', $cal->privatePublic());
			
			
			//============================================
			// Kalender ausgeben
			//============================================
			$GLOBALS['tmpl']->assign('headernav', $header);
			$GLOBALS['tmpl']->assign('pagetitle_html', clean_page_title($header));
			$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/calendar/calendar.tpl')); 	
		break;
	
		//============================================
		// Wochenuebersicht
		//============================================
		case 'week' :
			if(!is_numeric($_REQUEST['weekstart']) || !is_numeric($_REQUEST['weekend']))
			{
				exit;
			}
			$f_t = (abs((int)$_REQUEST['weekstart']) && abs((int)$_REQUEST['weekend'])) ? date("d.m.Y", $_REQUEST['weekstart']) . ' - ' . date("d.m.Y", $_REQUEST['weekend']) : '';
			
			
			$start = (int)$_REQUEST['weekstart'];
			$end   = (int)$_REQUEST['weekend'];
			
			$smonth = date("m", $_REQUEST['weekstart']);
			$sday = date("d", $_REQUEST['weekstart']);
			$syear = date("Y", $_REQUEST['weekstart']);
		
			$smonth_end = date("m", $_REQUEST['weekend']);
			$sday_end = date("d", $_REQUEST['weekend']);
			$syear_end = date("Y", $_REQUEST['weekend']);
			
			$dates_array[0] = mktime(0,0,0,$smonth,$sday,$syear);
			
			for($i=1;$i<7;$i++)
			{
				$dates_array[$i] = mktime(0,0,0,$smonth,$sday+$i,$syear);
			}
			
			$sname = $GLOBALS['config_vars']['Calendar'] . $GLOBALS['config_vars']['PageSep'] . $GLOBALS['config_vars']['Calendar_weekview'] . $GLOBALS['config_vars']['PageSep'] . $f_t;
			$header = "<a  href=\"index.php?p=calendar&amp;koobi_monat=$current_month&amp;koobi_jahr=$current_year&amp;area=$area\">" . $GLOBALS['config_vars']['Calendar'] . "</a>" . $GLOBALS['config_vars']['PageSep'] . $GLOBALS['config_vars']['Calendar_weekview'];
			
			
			$GLOBALS['tmpl']->register_function('CalendarBirthdays', 'CalendarBirthdays');
			$GLOBALS['tmpl']->assign('week_pref_start', mktime(0, 0, 0, $smonth, $sday-7, $syear));
			$GLOBALS['tmpl']->assign('week_pref_end', mktime(0, 0, 0, $smonth, $sday, $syear));
			$GLOBALS['tmpl']->assign('week_next_start', mktime(0, 0, 0, $smonth, $sday+7, $syear));
			$GLOBALS['tmpl']->assign('week_next_end', mktime(0, 0, 0, $smonth, $sday+14, $syear));
			$GLOBALS['tmpl']->assign('second_month', explode('-', date("m-Y", $_REQUEST['weekend'])));
			$GLOBALS['tmpl']->assign('days_inmonth', $cal->getDayCount());
			$GLOBALS['tmpl']->assign('month_array', $cal->getMonthNames());
			$GLOBALS['tmpl']->assign('dates_array', $dates_array);
			$GLOBALS['tmpl']->assign('items', $cal->displayWeek($start,$end));
			$GLOBALS['tmpl']->assign('days', $cal->getDayNamesWeek());
			$GLOBALS['tmpl']->assign('showtype', ($_REQUEST['show']=='private') ? 'private' : 'public');
			$GLOBALS['tmpl']->assign('privatePublic', $cal->privatePublic());
			$GLOBALS['tmpl']->assign('month' , $cal->displayMonth());
			$GLOBALS['tmpl']->assign('currentmonth' , $month);
			$GLOBALS['tmpl']->assign('Year', $year);
			$GLOBALS['tmpl']->assign('startYear', $cal->startYear());
			$GLOBALS['tmpl']->assign('calendar_now', $cal->getMonthView($smonth, $syear, $GLOBALS['config']['source'] . '/calendar/calendar_small_prev_next.tpl',1,'small'));
			$GLOBALS['tmpl']->assign('headernav', $header);
			$GLOBALS['tmpl']->assign('pagetitle_html', clean_page_title($sname));
			
			if($smonth != $smonth_end)
			{
				$GLOBALS['tmpl']->assign('calendar_next', $cal->getMonthView($smonth_end, $syear_end, $GLOBALS['config']['source'] . '/calendar/calendar_small_prev_next.tpl',1,'small'));
			}
			$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/calendar/calendar_week.tpl')); 
		break;
	
		//============================================
		// Jahresuebersicht
		//============================================
		case 'displayyear':
			$sname = $GLOBALS['config_vars']['Calendar'] . $GLOBALS['config_vars']['PageSep'] . $GLOBALS['config_vars']['Calendar_yearView'].  $year;
			$header = "<a href=\"index.php?p=calendar&amp;koobi_monat=$current_month&amp;koobi_jahr=$current_year&amp;area=$area\">" . $GLOBALS['config_vars']['Calendar'] . "</a>" . $GLOBALS['config_vars']['PageSep'] . $GLOBALS['config_vars']['Calendar_yearView'] . ' ' .  $year;
			
			$GLOBALS['tmpl']->assign('headernav', $header);
			$GLOBALS['tmpl']->assign('pagetitle_html', clean_page_title($sname));
			
			$GLOBALS['tmpl']->assign('showtype', ($_REQUEST['show']=='private') ? 'private' : 'public');
			$GLOBALS['tmpl']->assign('startYear', $cal->startYear());
			$GLOBALS['tmpl']->assign('jahre', $cal->getCurrentYearView());
			$GLOBALS['tmpl']->assign('year_prev', $year-1);
			$GLOBALS['tmpl']->assign('year_next', $year+1);
			$GLOBALS['tmpl']->assign('Year', $year);
			$GLOBALS['tmpl']->assign('years', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/calendar/calendar_year_small.tpl'));
			$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/calendar/calendar_year.tpl')); 
		break;
	
		//============================================
		// Form
		//============================================
		case 'newevent' :
			if ((permission('calendar_event_new') || permission('calendar_event') || UGROUP==1) && (UGROUP!=2))
			{
				$sname = $GLOBALS['config_vars']['Calendar']  . $GLOBALS['config_vars']['PageSep'] . $GLOBALS['config_vars']['Calendar_newEvent'];
				$header = "<a href=\"index.php?p=calendar&amp;area=$area\">" . $GLOBALS['config_vars']['Calendar'] . "</a>" . $GLOBALS['config_vars']['PageSep'] . $GLOBALS['config_vars']['Calendar_newEvent'];
			
				$GLOBALS['tmpl']->assign('headernav', $header);
				$GLOBALS['tmpl']->assign('pagetitle_html', clean_page_title($sname));
				$GLOBALS['tmpl']->assign('startYear', $cal->startYear());
				$GLOBALS['tmpl']->assign('month' , $cal->displayMonth());
				$GLOBALS['tmpl']->assign('currentmonth' , $month);
				$GLOBALS['tmpl']->assign('weight', @explode(",", $GLOBALS['config_vars']['Calendar_Weight']));
				$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/calendar/calendar_event_form.tpl')); 
			} else {
				noperm();
			}
		break;
		
		//============================================
		// Eintragen
		//============================================
		case 'insertevent' :
			if(!permission('calendar_event') || UGROUP == 2)
			{
				noperm();
			} else {
				$cal->insertEvent();
				koobi_message('Calendar_newEvent', 'Calendar_editEventNewM', HOME_PATH . '/index.php?p=calendar&show=' . $_REQUEST['show'] . '&area=' . $_REQUEST['area']);
			}
		break;
		
		//============================================
		// Ereignisse eines bestimmten Tages
		//============================================
		case 'events' :
			if (UGROUP == 2 && $_REQUEST['show'] == 'private')
			{
			   noperm();
			}
			
			$even_date = ' ' . abs((int)$_REQUEST['koobi_tag']) . '.' . abs((int)$_REQUEST['koobi_monat']) . '.' . abs((int)$_REQUEST['koobi_jahr']);
			$sname = '<a href="index.php?p=calendar&amp;area=' . $area . '">' . $GLOBALS['config_vars']['Calendar'] . '</a>'  . $GLOBALS['config_vars']['PageSep'] . $GLOBALS['config_vars']['Calendar_EventsOn'] . $even_date . ' ('.$cal->dispType() . ')';
		
			$cal->showEvents();
			
			$GLOBALS['tmpl']->assign('headernav', $sname);
			$GLOBALS['tmpl']->assign('pagetitle_html', clean_page_title($sname));
			$GLOBALS['tmpl']->assign('weight', @explode(",", $lang['calendar_weight']));
			$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/calendar/calendar_display_events.tpl')); 
		break;
		
		
		//============================================
		// Ereignis löschen
		//============================================
		case 'delevent' :
			$row = $cal->fetchData();
			$redir = "index.php?p=calendar&amp;action=events&amp;show=$_REQUEST[show]&amp;koobi_monat=$_REQUEST[koobi_monat]&amp;koobi_jahr=$_REQUEST[koobi_jahr]&amp;koobi_tag=$_REQUEST[koobi_tag]&amp;area=$_REQUEST[area]";
			if (@$row->Benutzer != UID && !permission('edit_all_events'))
			{
				noperm();
			} 
			else 
			{
				$cal->deleteDataById();
				koobi_message('Calendar_eventDel', 'Calendar_eventDelM', HOME_PATH . '/index.php?p=calendar&show=' . $_REQUEST['show'] . '&area=' . $_REQUEST['area']);
			} 
		break;
	
		//============================================
		// Ereignis bearbeiten
		//============================================
		case 'editevent' :
			$row = $cal->fetchData();
			$redir = "index.php?p=calendar&amp;action=events&amp;show=$_REQUEST[show]&amp;koobi_monat=$_REQUEST[koobi_monat]&amp;koobi_jahr=$_REQUEST[koobi_jahr]&amp;koobi_tag=$_REQUEST[koobi_tag]&amp;area=$_REQUEST[area]#" . addslashes($_REQUEST['id']);
			if ((!permission('calendar_event')) || ($row->Benutzer != UID && !permission('edit_all_events')))
			{
				noperm();
			} 
			else 
			{
				switch(@$_REQUEST['subaction'])
				{
				case '' :
					$even_date = ' ' . (int)$_REQUEST['koobi_tag'] . '.' . (int)$_REQUEST['koobi_monat'] . '.' . (int)$_REQUEST['koobi_jahr'];
					$header = "<a href=\"index.php?p=calendar&amp;area=$area\">" . $GLOBALS['config_vars']['Calendar'] . "</a>" . $GLOBALS['config_vars']['PageSep'] . "<a  href=\"index.php?p=calendar&amp;action=events&amp;show=$_REQUEST[show]&amp;koobi_monat=$_REQUEST[koobi_monat]&amp;koobi_jahr=$_REQUEST[koobi_jahr]&amp;koobi_tag=$_REQUEST[koobi_tag]&amp;area=$_REQUEST[area]#" . addslashes($_REQUEST['id']) ."\">" . $GLOBALS['config_vars']['Calendar_EventsOn'] . $even_date . "</a>" . $GLOBALS['config_vars']['PageSep'] . ' ' . $GLOBALS['config_vars']['Calendar_eventEdit'];
					
					$GLOBALS['tmpl']->assign('headernav', $header);
					$GLOBALS['tmpl']->assign('pagetitle_html', clean_page_title($header));
					$GLOBALS['tmpl']->assign('row', $row);
					$GLOBALS['tmpl']->assign('startYear', $cal->startYear());
					$GLOBALS['tmpl']->assign('month' , $cal->displayMonth());
					$GLOBALS['tmpl']->assign('weight', @explode(",", $GLOBALS['config_vars']['Calendar_Weight']));
					$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/calendar/calendar_event_form.tpl')); 
				break;
				
				case 'save' :
					$cal->updateEvent();
					koobi_message('Calendar_editEvent', 'Calendar_editEventM', HOME_PATH . '/' . $redir);
				break;
				}
			} 
		break;
	
		//============================================
		// Geburtstage
		//============================================
		case 'birthdays' :
			$sname = "<a href=\"index.php?p=calendar&amp;area=$area\">" . $GLOBALS['config_vars']['Calendar'] . "</a>" . $GLOBALS['config_vars']['PageSep'] . $GLOBALS['config_vars']['Calendar_Birthdays'];
			$birthdays = CalendarBirthdays($_REQUEST['koobi_tag'], $_REQUEST['koobi_monat'], $_REQUEST['koobi_jahr'], 1);
			
			$GLOBALS['tmpl']->assign('headernav', $sname);
			$GLOBALS['tmpl']->assign('pagetitle_html', clean_page_title($sname));
			$GLOBALS['tmpl']->assign('birthdays', $birthdays);
			$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/calendar/calendar_birthdays.tpl')); 
		break;
		
		//============================================
		// Suche
		//============================================
		case 'search' : 
			$sname = $GLOBALS['config_vars']['Calendar']  . $GLOBALS['config_vars']['PageSep'] . $GLOBALS['config_vars']['Search'] ;
			$cal->searchEvent();
			$header = "<a href=\"index.php?p=calendar&amp;area=$area\">" . $GLOBALS['config_vars']['Calendar'] . "</a>" . $GLOBALS['config_vars']['PageSep'] . $GLOBALS['config_vars']['Calendar_search'];
			$GLOBALS['tmpl']->assign('headernav', $header);
			$GLOBALS['tmpl']->assign('pagetitle_html', clean_page_title($sname));
			$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/calendar/calendar_searchresults.tpl')); 
		break;
		
		case 'myevents':
			if(UGROUP==2)
			{
				noperm();
			} else {
				$sname = $GLOBALS['config_vars']['Calendar']  . $GLOBALS['config_vars']['PageSep'] . $GLOBALS['config_vars']['Calendar_MyEvents'] ;
				$cal->myEvents();
				$header = "<a href=\"index.php?p=calendar&amp;area=$area\">" . $GLOBALS['config_vars']['Calendar'] . "</a>" . $GLOBALS['config_vars']['PageSep'] . $GLOBALS['config_vars']['Calendar_MyEvents'];
				$GLOBALS['tmpl']->assign('headernav', $header);
				$GLOBALS['tmpl']->assign('pagetitle_html', clean_page_title($sname));
				$GLOBALS['tmpl']->assign('content', $GLOBALS['tmpl']->fetch($GLOBALS['config']['source'] . '/calendar/calendar_myevents.tpl')); 
			}
		break;
		
		case 'switch':
			
			$d = getdate(time()); 
			$year = (isset($_REQUEST['koobi_jahr']) && $_REQUEST['koobi_jahr'] != '') ? (int)$_REQUEST['koobi_jahr'] : $d['year'];
			$month = (isset($_REQUEST['koobi_monat']) && $_REQUEST['koobi_monat'] != '') ? (int)$_REQUEST['koobi_monat'] : $d['mon'];
			$erg = $Calendar->getMonthView($month, $year, $GLOBALS['config']['source'] . '/calendar/calendar_small_raw.tpl','1','small',1);
			define('AJAX_OUTPUT',1);
			$_REQUEST['noout']=1;
			echo $erg;
			//$GLOBALS['tmpl']->assign('SmallCalendar', $Calendar->getMonthView($month, $year, $GLOBALS['config']['source'] . '/calendar/calendar_small.tpl','1','small'));
		break;
		
	}
} else {
	notActive();
}
?>