<html>
<head>
<title>kontiki</title>

<?PHP
	include("db.php");

	function showNoSchedule()
	{
		echo "We have no schedule for this day!";
	}

	// add an argument for wich month to show
	function showCalendar()
	{
		$firstDayInMonth = date('w', time() - (date('j') -1) * 24*60*60);
		if($firstDayInMonth == 0)
			$firstDayInMonth += 7;
		--$firstDayInMonth;
		echo "First day in this month is: $firstDayInMonth<br>\n";
		$currentDate = date('d');
		$currentMonth = date('m');
		$currentYear = date('Y');
		$today = date('j');
		$prevMonthsDays = date('t', time() - date('j') * 24*60*60);

		echo "<table border=1 cellpadding=0 cellspacing=0>\n<tr>\n";

		for($n = $prevMonthsDays - ($firstDayInMonth + 7) +1; $n <= $prevMonthsDays; $n++)
		{
			echo "<td><a href=\"$_SERVER[PHP_SELF]?viewDate=\">$n</a></td>\n";
			if(date('w', time() - ($prevMonthsDays - $n + $currentDate) * 24 * 60 * 60) == 0)
				echo "</tr>\n<tr>\n";
		}

		echo "</table>\n";

	}

	function showInputMode()
	{
		PHP?>
<form method="post" enctype="multipart/form-data" action="<?PHP echo $_SERVER[PHP_SELF]; PHP?>">
<table border=0 celppadding=0 cellspacing=0>
	<tr>
		<td>År:</td>
		<td><select name="year"><option value="2007">2007</option><option value="2008">2008</option><option value="2009">2009</option></select></td>
	</tr>
        <tr>
                <td>Månad:</td>
                <td><select name="month"><?PHP for($n = 1;$n <= 12;$n++) echo "<option value=\"$n\">$n</option>"; PHP?></select></td>
        </tr>
        <tr>
                <td>Datum:</td>
                <td><select name="date"><?PHP for($n = 1;$n <= 31;$n++) echo "<option value=\"$n\">$n</option>"; PHP?></select></td>
        </tr>
	<tr>
		<td>Rubrik:</td>
		<td><input type="text" name="subject"></td>
	</tr>
	<tr>
		<td>Ingress:</td>
		<td><textarea name="ingress"></textarea></td>
	</tr>
	<tr>
		<td>Text:</td>
		<td><textarea name="textbody"></textarea></td>
	</tr>
	<tr>
		<td>Länk 1:</td>
		<td><input type="text" name="lnk1"></td>
	</tr>
	<tr>
		<td>Länk 2:</td>
		<td><input type="text" name="lnk2"></td>
	</tr>
	<tr>
		<td>Bild 1:</td>
		<td><input type="file" name="img1"></td>
	</tr>
	<tr>
		<td>Bild 2:</td>
		<td><input type="file" name="img2"></td>
	</tr>
	<tr>
		<td>Bild 3:</td>
		<td><input type="file" name="img3"></td>
	</tr>
	<tr>
		<td colspan=2><input type="submit" value="Spara"></td>
	</tr>
</table>
</form>
		<?PHP
	}

	function storeNewEvent()
	{
		$images = "";
		if($_FILES[img1] != "")
		{
			$imageDestination = "img/calendar/" . $_FILES[img1][name];
			move_uploaded_file($_FILES[img1][tmp_name], $imageDestination);
			$images = $images . $_FILES[img1][name] . ";";
		}
		else
			$images = $images . ";";
		if($_FILES[img2] != "")
		{
			$imageDestination = "img/calendar/" . $_FILES[img2][name];
			move_uploaded_file($_FILES[img2][tmp_name], $imageDestination);
			$images = $images . $_FILES[img2][name] . ";";
		}
		else
			$images = $images . ";";
		if($_FILES[img3] != "")
		{
			$imageDestination = "img/calendar/" . $_FILES[img3][name];
			move_uploaded_file($_FILES[img3][tmp_name], $imageDestination);
			$images = $images . $_FILES[img3][name] . ";";
		}
		else
			$images = $images . ";";
		$links = $_POST[lnk1] . ";" . $_POST[lnk2];
		$date = $_POST[year] . "-" . str_pad($_POST[month], 2, "0", STR_PAD_LEFT) . "-" . str_pad($_POST[date], 2, "0", STR_PAD_LEFT);
		$query = "insert into calendar values('','$date','$_POST[subject]','$_POST[ingress]','$_POST[textbody]','$links','$images')";
		if(mysql_query($query))
			echo "query ok";
		else
			echo "query not ok";
	}
PHP?>

</head>

<body>

<?PHP
	showCalendar();

	if($_POST[subject] != "")
	{
		storeNewEvent();
	}

	if($_GET[viewDate] != "")
	{
		$date = calcDate($_GET[viewDate]);
		echo "trying to view date $date<br>";
		$query = "select * from calendar where date='$date'";
		$result = mysql_query($query);
		$line = @mysql_fetch_array($result);
		if($line != "")
		{
			echo "$line[rubrik]<br>$line[ingress]<br>$line[date]<br>$line[kropp]<br>";

			$links = explode(';', $line[links]);
			foreach($links as $link)
			{
				if($link != '')
					echo "<a href=\"$link\">$link</a><br>\n";
			}

			$images = explode(';', $line[images]);
			foreach($images as $image)
			{
				if($image != '')
					echo "<img src=\"img/calendar/$image\"><br>\n";
			}
		}
		else
			showNoSchedule();
	}
	else
	{
		showInputMode();
	}
PHP?>

</body>
</html>
