<?php
include_once ("config.php");
$UTENTE = login();
isadmin();
view_top();

if (isset ($_GET['var'])) {
	$var = $_GET['var'];
	$query = "SELECT * FROM conf WHERE variabile='$var'";
	$res = mysql_query( $query, $DB_ID );
	$dato = mysql_fetch_array ( $res );
?>

<form action="modconf.php" method="post">
<input type="hidden" name="variabile" value="<?php print $var ?>">

<?php $tabella = new html (0, "90%", array(2,15,2,50,0)); ?>
<tr>
	<td><img src="img/spazio.gif" width="50" height="1" alt=""></td>
	<td class="valoretxt" width="150">Variabile</td>
	<td><img src="img/spazio.gif" width="20" height="1" alt=""></td>
	<td class="valore" width="500"><?php print $dato['variabile'] ?></td>
	<td><img src="img/spazio.gif" width="180" height="1" alt=""></td>
</tr>
<?php $tabella->riga(); ?>
<tr>
	<td><img src="img/spazio.gif" width="50" height="1" alt=""></td>
	<td class="valoretxt" width="150">Commento</td>
	<td><img src="img/spazio.gif" width="20" height="1" alt=""></td>
	<td class="valore" width="500"><?php print $dato['Commento'] ?></td>
	<td><img src="img/spazio.gif" width="180" height="1" alt=""></td>
</tr>
<?php $tabella->riga(); ?>
<tr>
	<td><img src="img/spazio.gif" width="50" height="1" alt=""></td>
	<td class="valoretxt" width="150">Valore</td>
	<td><img src="img/spazio.gif" width="20" height="1" alt=""></td>
	<td class="valore" width="500">
<?php
	switch ($dato['variabile']) {
		case "template_base":
			$query = "SELECT * FROM template WHERE attivo = 1";
			$restempl = mysql_query( $query, $DB_ID ); 
			print "\t<select name='valore'>\n";
			while ($datotempl = mysql_fetch_array ( $restempl )) {
				if ($datotempl['id'] == $dato['valore']) 
					$check = "selected";
				else
					$check = "";
				
				print "\t\t<option name='valore' value='".$datotempl['id']."' $check>".$datotempl['nome']."</option>\n";
			}
			print "\t</select>\n";
			break;
		default:
			print "<input type=\"text\" name=\"valore\" size=\"80\" maxlength=\"256\" value=\"".$dato['valore']."\">";
	}
?>
	</td>
	<td><img src="img/spazio.gif" width="180" height="1" alt=""></td>
</tr>
<?php $tabella->riga(); ?>
<tr><td colspan="5"><img src="img/spazio.gif" width="1" height="5" alt=""></td></tr>
<tr><td align="center" colspan="5">
	<input type="submit" value="Modifica">
	&nbsp;&nbsp;&nbsp;
	<input type="button" value="Annulla" onclick="location.href = 'conf.php';"></button>
</td></tr>
<?php 
$tabella->riga(); 
$tabella->close(); 
?>
</form>

<?php } else if (isset ($_POST['variabile'])) {
	$variabile = $_POST['variabile'];
	$valore = $_POST['valore'];
	switch ($variabile) {
		case "template_base":
			$query = "SELECT * FROM template WHERE id = $valore";
			$res = mysql_query( $query, $DB_ID );
			$dato = mysql_fetch_array ( $res );
			$val = $dato['max_perm'];
			$query = "UPDATE conf SET valore = $val WHERE variabile = 'permanenti'";
			mysql_query( $query, $DB_ID );
			$val = $dato['max_temp'];
			$query = "UPDATE conf SET valore = $val WHERE variabile = 'temporanei'";
			mysql_query( $query, $DB_ID );
			$val = $dato['max_tempo'];
			$query = "UPDATE conf SET valore = $val WHERE variabile = 'tempo_temporanei'";
			mysql_query( $query, $DB_ID );
			$val = $dato['totale'];
			$query = "UPDATE conf SET valore = $val WHERE variabile = 'totale'";
			mysql_query( $query, $DB_ID );
		default:
			$query = "UPDATE conf SET valore = '$valore' WHERE variabile = '$variabile'";
			if (mysql_query( $query, $DB_ID )) 
				$testo = "Modifica della variabile $variabile effettuata con successo";
			else
				$testo = "Modifica della variabile $variabile non avvenuta: ".mysql_error();
	}?>
<?php 
$tabella = new html (1, "90%", array(0)); ?>
	<tr><td><img src="img/spazio.gif" width="1" height="10" alt=""><td></tr>
	<tr><td><?php print $testo ?></td></tr>
	<tr><td><img src="img/spazio.gif" width="1" height="5" alt=""></td></tr>
	<tr><td><input type="button" value="Ok" onclick="location.href = 'conf.php';"></button></td></tr>
	<tr><td><img src="img/spazio.gif" width="1" height="10" alt=""></td></tr>
<?php 
$tabella->close();
	} else { 
$tabella = new html (1, "90%", array(0)); ?>
	<tr><td><img src="img/spazio.gif" width="1" height="10" alt=""><td></tr>
	<tr><td>Operazione non permessa</td></tr>
	<tr><td><input type="button" value="Ok" onclick="location.href = 'main.php';"></button></td></tr>
	<tr><td><img src="img/spazio.gif" width="1" height="10" alt=""><td></tr>
<?php
$tabella->close();
}
view_footer(); 
?>
