<?php
include_once ("../core/config.php");
$UTENTE = login();
$DIRITTI = diritti('Utente');
view_top();

if (isset($_POST['utente'])) {
	$utente = $_POST['utente'];

	$old = $new = array();
	foreach ($_POST as $id => $valore) {
		if (preg_match ("/^new-(\d+)/", $id, $ris)) $new[$ris[1]] = $valore;
		if (preg_match ("/^old-(\d+)/", $id, $ris)) $old[$ris[1]] = $valore;
	}

	foreach ($new as $id => $valore) {
		if ($new[$id] == $old[$id]) continue;

		if ($valore >1) 
			$stato = 1;
		else
			$stato = $valore;

		$query = "	SELECT
					condes_regole.id_condes_macchine,
					condes_regole.id_proxy_acl_list,
					condes_macchine.id_proxy_pool
				FROM
					condes_regole
				JOIN
					condes_macchine
					ON
						condes_regole.id_condes_macchine = condes_macchine.id
				WHERE
					condes_regole.id = $id
		";
		$res = mysql_query( $query, $DB_ID );
		$dato = mysql_fetch_array ( $res );

		$query = "	UPDATE
					condes_regole
				SET
					stato = $valore,
					modifica = NOW()
				WHERE
					id_condes_macchine = ".$dato['id_condes_macchine']."
					AND
					id_proxy_acl_list = ".$dato['id_proxy_acl_list'];
		$res = mysql_query( $query, $DB_ID );

		$query = "	INSERT INTO
					proxy_acl_ip
				SET
					idacllist = ".$dato['id_proxy_acl_list'].",
					idpool = ".$dato['id_proxy_pool'].",
					stato = $stato,
					data = NOW()
				ON DUPLICATE KEY UPDATE
					stato = $stato,
					data = NOW()
		";
		$res = mysql_query( $query, $DB_ID );

		$query = "	INSERT INTO
					condes_logs
				SET
					id_utenti = ".$UTENTE['id'].",
					id_condes_regole = $id,
					stato = $valore,
					data = NOW()
		";
		$res = mysql_query( $query, $DB_ID );
	}
}

$tabella = new html (0,"90%",array(15,2,20,2,15,2,5,0));
?>
<form name="" method="post">
<input type="hidden" name="utente" value="<?php print $UTENTE['id'] ?>">

	<tr>
		<td><input type="submit" value="Applica"></td>
		<td colspan="7" class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
	</tr>
<?php 
$tabella->riga ();

$query = "	SELECT 
			condes_regole.id,
			condes_regole.stato,
			proxy_pool.nome,
			proxy_pool.ip,
			proxy_acl_list.nome AS acl,
			condes_regole.permanente
		FROM 
			condes_regole
		JOIN
			utenti
			ON
				condes_regole.id_utenti = utenti.id
				AND
				utenti.id = ".$UTENTE['id']."
		JOIN
			condes_macchine
			ON
				condes_regole.id_condes_macchine = condes_macchine.id
		JOIN
			proxy_pool
			ON
				condes_macchine.id_proxy_pool = proxy_pool.id
		JOIN
			proxy_acl_list
			ON
				condes_regole.id_proxy_acl_list = proxy_acl_list.id
		WHERE
			condes_regole.attivo = 1
		ORDER BY
			-proxy_pool.nome DESC,
			proxy_pool.ip
	";

$res = mysql_query( $query, $DB_ID );
while ($dato = mysql_fetch_array ( $res )) {
?>
	<tr>
		<td rowspan=2 class="textl">
			<input type="hidden" name="old-<?php print $dato['id'] ?>" value="<?php print $dato['stato'] ?>">
			<select name="new-<?php print $dato['id'] ?>">
				<option value="0" <?php if ($dato['stato'] == 0 ) print "selected" ?>>Disattivo</option>
				<option value="1" <?php if ($dato['stato'] == 1 ) print "selected" ?>>Attivo</option>
<?php if ($dato['permanente']) { ?>		
				<option value="2" <?php if ($dato['stato'] == 2 ) print "selected" ?>>Permanente</option> 
<?php } ?>
			</select>
		</td>
		<td rowspan=2 class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="descrizione">Macchina</td>
		<td rowspan=2 class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="descrizione">ACL</td>
		<td rowspan=2 class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
		<td class="radio">Perm</td>
		<td rowspan=2 class="spazioh"><img src="<?php print $CONF['base_url'] ?>/img/spazio.gif"></td>
	</tr>
	<tr>
		<td class="textl"><?php print $dato['ip']; if ($dato['nome'] != "") print "(".$dato['nome'].")" ?></td>
		<td class="textl"><?php print $dato['acl'] ?></td>
		<td class="textc"><?php if ($dato['permanente']) print "Si"; else print "No"; ?></td>
	</tr>
<?php
$tabella->riga ();
} ?>
</form>
<?php
$tabella->close ();
view_footer(); 
?>
