<?php
function calendar()
{
	global $UTIL_DIR, $ZEND_DIR, $DOKUWIKI_CALENDAR, $GLOBALS,
		$current_user, $DATA_DIR;

	include_once($UTIL_DIR . "/events.php");
	include_once($UTIL_DIR . "/notify.php");
	if($ZEND_DIR != "") include_once($UTIL_DIR . "/googlecalendar.php");

	$str = "";

	$events = new Events($DATA_DIR . "/calendar.xml");

	foreach($events->events as $e) {
		if($e->concert == "true") $str .= $e->show_simple();
	}

	if(!isset($GLOBALS['date']))
		$date = time() - (date("N", time()) - 1) * 24 * 60 * 60;
	else
		$date = $GLOBALS['date'] - (date("N", $GLOBALS['date'])-1) * 24 * 60 * 60; 

	if($GLOBALS['action'] =="addentry") {
		$title = stripslashes($GLOBALS['title']);
		$description = stripslashes($GLOBALS['description']);
		if($GLOBALS['concert'] == "on") $concert = "true";
		else $concert = "false";

		$time = strtotime($GLOBALS['txtdate'] . " " .
											$GLOBALS['txttimefrom'] . ":00");
		if($time !== FALSE) {
			$duration = strtotime($GLOBALS['txtdate'] . " " .
														$GLOBALS['txttimeto'] . ":00") - $time;
			$eid = time();
			$event = new Event($eid, $title, $concert, $time, $duration, $description,
												 $current_user->uid);
			$events->add($event);
			$events->write();
			
			if($ZEND_DIR != "") googleCalendarEvent($event);
			
			if($DOKUWIKI_CALENDAR != "") {
				$file = $DOKUWIKI_CALENDAR;
				$fp = fopen($file, "w");
				
				fprintf($fp, "=====DIEs calendar=====\n");
				fprintf($fp, "Do not edit this file - it is automatically generated " .
								"by the ExecutionRoom CMS.\n\n");
				fprintf($fp, "====Events====\n");
				foreach($events->events as $event) {
					fprintf($fp, "  * %s - %s %s: %s\n",
									date("j.n.Y G:i", $event->starttime),
									date("j.n.Y G:i", $event->starttime + $event->duration),
									$event->title, $event->description);
				}
				fclose($fp);
			}
			
			notify("calendar", "New calendar entry:\n" . $GLOBALS['title'] . "\n" .
						 date("r", $time) . "\n" . $GLOBALS['description'] . "\n" .
						 $FORUM_URL . "/?mode=calendar&date=" . $time);
			
			$str .= "		<meta http-equiv=\"refresh\" content=\"0; " .
				"URL=?mode=calendar&amp;date=" . $time . "\"/>\n";
			
		} else {
			$str .= "<div class=\"error\">ERROR: Date is not in the correct " .
				"format! It was NOT posted. Please try again.</div>\n";
		}
	}

	if($GLOBALS['action'] =="delete") {
			$events->delete($GLOBALS['eid']);
			$events->write();
	}

	$str .= "<p style=\"text-align: center;\">\n";
	$str .= "<a href=\"?mode=calendar&amp;date=" .
		sprintf("%d", $date - 7 * 24 * 60 * 60) . "\">[&lt;]</a>\n";
	$str .= "&nbsp;&nbsp;\n";
	$str .= "<a href=\"?mode=calendar&amp;date=" .
		sprintf("%d", $date - 31 * 24 * 60 * 60) . "\">[&lt;&lt;]</a>\n"; 
	$str .= "&nbsp;&nbsp;\n";
	$str .= date("F Y", $date) . "\n";
	$str .= "&nbsp;&nbsp;\n";
	$str .= "<a href=\"?mode=calendar&amp;date=" .
		sprintf("%d", $date + 31 * 24 * 60 * 60) . "\">[&gt;&gt;]</a> \n";
	$str .= "&nbsp;&nbsp;\n";
	$str .= "<a href=\"?mode=calendar&amp;date=" .
		sprintf("%d", $date + 7 * 24 * 60 * 60) . "\">[&gt;]</a><br/>\n";
	$str .= "<a href=\"?mode=calendar\">[Today]</a><br/>\n";
	$str .= "</p>\n";

	if($client_is_mobile_device) {
		for($day = 0; $day < 7; $day++) {
			$str .= "  <div class=\"mobilecalentry\">\n";
			$str .= "    <div class=\"mobilecalheader\">\n";
			$t = $date + $day * 24 * 60 * 60;
			$str .= "      " . date("l j/n", $t) . "\n";
			$str .= "<a style=\"text-decoration: none;\" " .
				"href=\"?mode=calendar&amp;date=". 
				sprintf("%d", $date) . "&amp;adddate=". $t .
				"&amp;action=add\">[+]</a>\n";
			$str .= "    </div>\n";

			$t = strtotime(date("F j Y", $date + $day * 24 * 60 * 60));
			$str .=  "    <div class=\"mobilecalcontent\"";
			if($t ==  strtotime(date("F j Y", time())))
				$str .= " style=\"background: #113;\"";
			elseif($day > 4) $str .=  " style=\"background: #311;\"";
			$str .=  ">\n";

			$str .= $events->show($t, $t + 24 * 60 * 60);

			$str .=  "    </div>\n";
			$str .=  "  </div>\n";

		}
	} else {

		$str .= "<table class=\"week\">\n";
		$str .= "  <tr class=\"day\">\n";

		for($day = 0; $day < 7; $day++) {
			$str .=  "    <td class=\"header\">\n";
			$t = $date + $day * 24 * 60 * 60;
			$str .=  "      " . date("l j/n", $t) . "\n";
			$str .=  "    </td>\n";
		}

		$str .= " </tr>\n";
		$str .= "  <tr class=\"day\">\n";

		for($day = 0; $day < 7; $day++) {
			$t = strtotime(date("F j Y", $date + $day * 24 * 60 * 60));

			$str .=  "    <td class=\"content\"";
			if($t ==  strtotime(date("F j Y", time())))
				$str .=  " style=\"background: #113;\"";
			elseif($day > 4) $str .=  " style=\"background: #311;\"";
			$str .=  ">\n";

			$str .=  "     <a class=\"button\" href=\"?mode=calendar&amp;date=" .
				sprintf("%d", $date) . "&amp;adddate=". $t .
				"&amp;action=add\">Add</a>\n";

			$str .= $events->show($t, $t + 24 * 60 * 60);
			$str .=  "    </td>\n";
		}

		$str .= " </tr>\n";
		$str .= "</table>\n";
	}

	if($GLOBALS['adddate'] != "") {
		$str .= "<form method=\"post\" action=\"?mode=calendar&amp;action=" .
			"addentry\">\n";
		$str .= "Title: <input name=\"title\" value=\"\"><br/>\n";
		$str .= "Concert: <input type=\"checkbox\" name=\"concert\"/><br/>\n";
		$str .= "Desription: <textarea name=\"description\"></textarea><br/>\n";
		$str .= "Date: <input name=\"txtdate\" value=\"".
			date("F j Y", $GLOBALS['adddate']) . "\"><br/>\n";
		$str .= "From-Time: <input name=\"txttimefrom\" value=\"19:00\"><br/>\n";
		$str .= "To-Time: <input name=\"txttimeto\" value=\"22:00\"><br/>\n";
		$str .= "<br/>\n";
		$str .= "<button type=\"submit\">Add</button>\n";
		$str .= "</form>\n";
	}

	return $str;
}
?>