<html xmlns:AI>
	<head>
	<meta charset="utf-8">
	<title></title> 
	<link href='dm.css' rel='stylesheet' type='text/css'>
		<?php 
			include "functions.php";
		?>
		<link href='dm.css' rel='stylesheet' type='text/css'>
	</head>
<html>
<body leftmargin=0 topmargin=0>
	<div align="center">
	<table width="100%" border="0" cellspacing="3" cellpadding="0">
		<?php
			include "header.php";
		?>
		<tr>
			<td align="center" valign="top" class="content">
			<?php
				DBConnect();
				$action = ( isset($_REQUEST['action'] ) ? $_REQUEST['action'] : "" );
				$planetid = ( isset($_REQUEST['planetid'] ) ? $_REQUEST['planetid'] : "" );
				$n = $_REQUEST['n'];
				
				$query = "SELECT ID,Name,Name_en,Datei,Upload,Views FROM planeten where ID=$n";
				$sth = $db->prepare( $query );
				$sth->execute();
				$line = $sth->fetch();
				$planetid = $line['ID'];
				
				if( $action == "addtag" ) {
					$newtag = ( isset($_REQUEST['newtag'] ) ? $_REQUEST['newtag'] : "" );
					$newtagen = ( isset($_REQUEST['newtagen'] ) ? $_REQUEST['newtagen'] : "" );
					$query = "INSERT INTO tag (tag,tag_en) VALUES ('$newtag','$newtagen')";
					$sth = $db->prepare( $query );
					$sth->execute();
  				$tagid =  $db->lastInsertId();
					$query = "INSERT INTO planet_tag (planet,tag) VALUES ('$planetid','$tagid')";
  				$result = $db->query($query);
				}
				
				if( $action == "edittags" ) {
					$query = "delete from planet_tag where planet=".$n;
  				$result = $db->query($query);

					$tags = $_POST['tags'];
					foreach ($tags as $tag=>$value) {
						$query = "INSERT INTO planet_tag (planet,tag) VALUES ('$n','$value')";
  					$result = $db->query($query);
	        }				
	      }
				
			?>
			<?php
				print "<img src='../planeten/small/".$line['Datei']."'>";
				print "<form action='index.php' method='post' id='mainform'>"; 
			?>
			<input type="hidden" name="action" value="edit">
			<input type="hidden" name="n" value="<?php print $n; ?>">
			<table>
				<tr>
					<td bgcolor="#EEEEEE" align="center">Name</td>
					<td colspan=2 bgcolor="#EEEEEE" align="left"><input type="text" name="Name" value="<?php print $line["Name"]; ?>" size=80></td>
				</tr> 
				<tr>
					<td bgcolor="#EEEEEE" align="center">Name englisch</td>
					<td colspan=2 bgcolor="#EEEEEE" align="left"><input type="text" name="Name_en" value="<?php print $line["Name_en"]; ?>" size=80></td>
				</tr> 
				<tr>
					<td bgcolor="#EEEEEE" align="center">Datei</td>
					<td colspan=2 bgcolor="#EEEEEE" align="left"><input type="text" name="Datei" value="<?php print $line["Datei"]; ?>" size=50></td>
				</tr> 
				<tr>
					<td bgcolor="#EEEEEE" align="center">Upload</td>
					<td colspan=2 bgcolor="#EEEEEE" align="left"><input type="text" name="Upload" value="<?php print $line["Upload"]; ?>" size=50></td>
				</tr> 
				<tr>
					<td bgcolor="#EEEEEE" align="center">Views</td>
					<td colspan=2 bgcolor="#EEEEEE" align="left"><input type="text" name="Views" value="<?php print $line["Views"]; ?>" size=50></td>
				</tr> 
				<tr>
					<td bgcolor="#EEEEEE" align="center">&nbsp;</td>
					<td colspan=2 bgcolor="#EEEEEE" align="left"><input type="submit" value="Speichern"></td>
				</tr> 
				<tr>
					<td bgcolor="#EEEEEE" align="center">&nbsp;</td>
					<td colspan=2 bgcolor="#EEEEEE" align="left"><a href="index.php">Abbrechen und zur&uuml;ck zur Liste</a></td>
				</tr> 
			</table>
			</form>
			<table>
				<tr>
					<td bgcolor="#EEEEEE" align="center">Tags</td>
					<td colspan=2 bgcolor="#EEEEEE" align="left">
						<form action='edit.php' method='post' id='mainform'>
						<input type="hidden" name="action" value="edittags">
						<input type="hidden" name="n" value="<?php print "$n"; ?>">
						<?php
							$query = "select t.tag_en,t.tag,pt.tag as tagID,pt.planet as planetID from tag t, planeten p, planet_tag pt where t.ID=pt.tag and p.ID=pt.planet and p.ID=$n";
							$tagIDS = array();
							$result = $db->query( $query );
							foreach( $result as $line ) {
		  					print "<input type='checkbox' name='tags[]' value='".$line['tagID']."' checked>".htmlspecialchars($line['tag'])."/".$line['tag_en']."<br>";
		  					$tagIDS[] = $line['tagID'];
		  				}
		  				
							$query1 = "SELECT count(*) as count FROM tag t order by tag";
	  					$result = $db->query( $query1 );
							$line1 = $result->fetchColumn();
				
		  				$query = "SELECT t.tag as tag,t.tag_en as tag_en, t.ID as tagID FROM tag t order by tag";
							$result = $db->query( $query );
							print "<table><tr>\n";
							$counter = 1;
							foreach( $result as $line ) {
		  					$found = array_search( $line['tagID'] , $tagIDS );
		  					if( $found === FALSE ) {
		  						print "<td width='33%'><input type='checkbox' name='tags[]' value='".$line['tagID']."' >".htmlspecialchars($line['tag']."/".$line['tag_en'])."<br></td>";
		  					}
		  					if( $counter % 3 == 0 ) {
									print "</tr><tr>\n";
		  					}
		  					$counter++;
		  					
		  				}
							print "</tr></table>\n";
						?>
						<input type="submit" value="Speichern">
						</form>
					</td>
				</tr> 
				<tr>
					<td bgcolor="#EEEEEE" align="center">Add Tag</td>
					<td colspan=2 bgcolor="#EEEEEE" align="left">
						<form action='edit.php' method='post' id='mainform'>
						<input type="hidden" name="planetid" value="<?php print "$planetid"; ?>">
						<input type="hidden" name="n" value="<?php print "$n"; ?>">
						<input type="hidden" name="action" value="addtag">
						Deutsch <input type="text" name="newtag" value="" size=50><br>
						Englisch <input type="text" name="newtagen" value="" size=50><br>
						<input type="submit" value="Speichern">
					</form>
					</td>
				</tr> 
			</table>
			</td>
		</tr>
	</table>
	</div>

</body>
</html>
