<?php
	
	session_start();
	require_once('../conf/definitions.php');
	require_once('../conf/objs.php');
	require_once('../conf/functions.php');
	
	$u=$_GET['u'];
	
	if(!$_SESSION['mobile_admin_logged_in'])
	{
		header("location: index.php");
	}
	
	$dba = new db;
	
	if(isset($_POST['submit']))
	{
		if(is_array($_POST['image']) && sizeof($_POST['image']) > 0)
		{
			foreach($_POST['image'] as $id => $x)
			{
				$user_id = $dba->GetOne('mobile_pozadia', $id, 'pridal');
				Dump('pridal user_id: '.$user_id);
				if($_POST['check'][$id] == 1)
				{
					$query = "UPDATE mobile_pozadia SET confirmed = 'a', cat = '".$_POST['category'][$id]."' WHERE ID = $id limit 1";
					dump($query);
					$dba->ExecuteQuery($query);
					// pridanie kreditov
					fnPridajKred($user_id, 1);
					$predmet = 'Potvrdené pozadie s ID '.$id;
					$sprava = 'Vami pridané pozadie s ID '.$id.' bolo potvrdené a zobrazuje sa medzi ostatnými pozadiami v kategórii <b>'.strtoupper($categories[$_POST['category'][$id]]).'</b>. Získali ste zaň 1 4mobile kredit. Táto správa je automaticky generovaná, prosím neodpovedajte na ňu.';
					$query = "INSERT INTO mobile_posta (predmet, text, user_from, user_to, cas) values ('$predmet', '$sprava', -1, ".$user_id.", NOW())";
					$dba->ExecuteQuery($query);
				}
				else 
				{
					$file = $dba->GetOne('mobile_pozadia', $id, 'file');
					if(unlink('../pozadia2/'.$file))
					{
						dump('Pozadie '.$id.' zmazane');
					}
					else 
					{
						dump('Pozadie '.$id.' NEZMAZANE');
					}
					$query = "DELETE FROM mobile_pozadia WHERE ID=$id limit 1";
					dump($query);
					$dba->ExecuteQuery($query);
					$predmet = 'Nepotvrdené pozadie s ID '.$id;
					$sprava = 'Vami pridané pozadie s ID '.$id.' nebolo potvrdené a bolo zmazané. Prosím, pridávajte len kvalitné pozadia. Táto správa je automaticky generovaná, prosím neodpovedajte na ňu.';
					$query = "INSERT INTO mobile_posta (predmet, text, user_from, user_to, cas) values ('$predmet', '$sprava', -1, ".$user_id.", NOW())";
					$dba->ExecuteQuery($query);
				}
			}
		}
	}
	
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head><title>4mobile - backoffice</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-2">
<link rel="stylesheet" href="style.css">
<script language="JavaScript" src="script.js"></script>
</head>
<body bgcolor="#000000" align=center>
	
<?php
	include('menu.php');
	$query = "SELECT 
				  a.ID,
				  a.file,
				  a.cat,
				  b.login
	          FROM 
	              mobile_pozadia as a, mobile_users as b
			  WHERE 
				  a.pridal = b.ID AND
				  a.confirmed = 'n'
			  ORDER BY
			       a.ID ASC
			  limit 0, 100";
	$array = $dba->GetArray($query);
	dump($array);
	echo "<table><form action='".$_SERVER['PHP_SELF']."?u=$u' method='POST'>";
	foreach($array as $image)
	{
		echo "<tr>";
		echo "<td>";
		echo "<input type='hidden' name='image[".$image['ID']."]' value='1'/>";
		echo "Potvrdit: <input type='checkbox' id='check_".$image['ID']."' name='check[".$image['ID']."]' value='1' />";
		echo "</td>";
		echo "<td>";
		echo "<label for='check_".$image['ID']."'><img src='../pozadia2/".$image['file']."' alt='' align='left'/></label> Pridal ".$image['login']."<br />";
		echo "Kategoria <select name='category[".$image['ID']."]' class='confirm_field'><br />";
		list($width, $height, $type, $attr) = getimagesize("../pozadia2/".$image['file']);
		foreach($categories as $cat_id => $cat_name)
		{
			echo "<option value='$cat_id'";
			if($cat_id == $image['cat'])
			{
				echo " selected";
			}
			echo ">$cat_name</option>";
		}
		echo "</select><br />";
		echo "Rozlisenie: $width x $height px";
		echo "Velkost: ".round(filesize("../pozadia2/".$image['file'])/1024, 2)." kB";
		echo "</td>";
		echo "</tr>";
	}
	echo "<tr><td colspan='2' align='center'><input type='submit' name='submit' value='Potvrd' class='button'/></td></tr></form></table>";

?>

</body>
</html>