<?php
	#
	# 	DECLARE PAGE VARIABLES
	#

 	$my_pagetitle = " - Kalendarium";
	$my_PageTitle = " - Kalendarium";
	$my_frontpage = FALSE;
	$my_topp = "grillspett.jpg";
	$my_header = "kalender.gif";


	# 	BUILD HEADER

	include("../kontiki-header.php");

	# 	SET MAIL PREFS

	define("ADRESS", "info@kontiki.nu");
	define("SUBJECT", "ny epostadress fran hemsidan");

	#	CONNECT TO DATABASE

	$host = "mysql07.cliche.se";
	$user = "kontiki.nu";
	$pass = "nx3fwm";
	$db = "kontiki_nu";
	mysql_connect($host, $user, $pass) OR die ("Could not connect to the server.");
	mysql_select_db($db) OR die("Could not connect to the database.");

	#	FUNCTIONS

	function showNoSchedule()
	{
		echo "We have no schedule for this day!";
	}

        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 . ";";
                if($_FILES[banner] != "")
                {
                        $imageDestination = "img/banners/" . $_FILES[banner][name];
                        move_uploaded_file($_FILES[banner][tmp_name], $imageDestination);
                        $banner = $_FILES[banner][name];
                }

                $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','$banner')";
                mysql_query($query);
        }

        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>Banner:</td><td><input type="file" name="banner"></td></tr>
	<tr><td colspan=2><input type="submit" value="Spara"></td></tr>
</table>
</form>
                <?PHP
        }

	if(strlen($_GET[viewDate]) == 10)
	{
		$date = $_GET[viewDate];
		$query = "select * from calendar where date='$date'";
		$result = mysql_query($query);
		if(mysql_num_rows($result) > 0)
			while($line = @mysql_fetch_array($result))
			{
				echo "<b>$line[date] - $line[rubrik]</b><br>$line[ingress]<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";
				}
				echo "<br><center><hr width=50%></center><br>\n";
			}
	}
	elseif($_GET[admin] == true)
	{
		showInputMode();
	}
	elseif($_POST[textbody] != '')
	{
		storeNewEvent();
	}
	else
		showNoSchedule();

	include ("../kontiki-footer.php");
	# 	--------- 	rensa upp bättre?

?>
