<?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['sms']) && sizeof($_POST['sms']) > 0)
		{
			foreach($_POST['sms'] as $id => $x)
			{
				$user_id = $dba->GetOne('mobile_sms', $id, 'pridal');
				Dump('pridal user_id: '.$user_id);
				if($_POST['check'][$id] == 1)
				{
					$query = "UPDATE mobile_sms SET confirmed = 'a', cat = '".$_POST['category'][$id]."', text = '".addslashes(htmlspecialchars($_POST['text'][$id]))."' WHERE ID = '$id' limit 1";
					dump($query);
					$dba->ExecuteQuery($query);
					// pridanie kreditov
					fnPridajKred($user_id, 1);
					$predmet = 'Potvrdená SMS s ID '.$id;
					$sprava = 'Vami pridaná SMS s ID '.$id.' bola potvrdená a zobrazuje sa medzi ostatnými SMSkami v kategórii <b>'.strtoupper($categories[$_POST['category'][$id]]).'</b>. Získali ste za ňu 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, visible_sender) values ('$predmet', '$sprava', -1, ".$user_id.", NOW(), '0')";
					$dba->ExecuteQuery($query);
				}
				else 
				{
					$query = "DELETE FROM mobile_sms WHERE ID='$id' limit 1";
					dump($query);
					$dba->ExecuteQuery($query);
					$predmet = 'Nepotvrdená SMS s ID '.$id;
					$sprava = 'Vami pridaná SMS s ID '.$id.' nebola potvrdená a bola zmazaná. Prosím, pridávajte len zaujímavé SMS. Táto správa je automaticky generovaná, prosím neodpovedajte na ňu.';
					$query = "INSERT INTO mobile_posta (predmet, text, user_from, user_to, cas, visible_sender) values ('$predmet', '$sprava', -1, ".$user_id.", NOW(), '0')";
					$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.cat,
			  a.text,
			  b.login
	          FROM mobile_sms as a, mobile_users as b
			  WHERE 
			  a.pridal = b.ID AND
			  a.confirmed = 'n'
			  order by a.ID asc
			  limit 0, 10";
	$array = $dba->GetArray($query);
	dump($array);
	echo "<table width='90%'><form action='".$_SERVER['PHP_SELF']."?u=$u' method='POST'>";
	foreach($array as $sms)
	{
		echo "<tr>";
		echo "<td width='10%'>";
		echo "<input type='hidden' name='sms[".$sms['ID']."]' value='1'/>";
		echo "Potvrdit: <input type='checkbox' name='check[".$sms['ID']."]' value='1' />";
		echo "</td>";
		echo "<td width='80%'><textarea rows='3' cols='100' name='text[".$sms['ID']."]'>".stripslashes(html_entity_decode($sms['text']))."</textarea></td><td>";
		echo "Pridal ".$sms['login']."<br />";
		echo "Kategoria <select name='category[".$sms['ID']."]' class='field'><br />";
		foreach($sms_cats as $cat_id => $cat_name)
		{
			echo "<option value='$cat_id'";
			if($cat_id == $sms['cat'])
			{
				echo " selected";
			}
			echo ">$cat_name</option>";
		}
		echo "</select><br />";
		echo "Znakov: ".strlen($sms['text']);
		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>