Index: /trunk/admin/modules/Elezioni/salva_ref.php
===================================================================
--- /trunk/admin/modules/Elezioni/salva_ref.php	(revision 261)
+++ /trunk/admin/modules/Elezioni/salva_ref.php	(revision 261)
@@ -0,0 +1,155 @@
+<?php
+/************************************************************************/
+/* Eleonline - Raccolta e diffusione dei dati elettorali                */
+/************************************************************************/
+/* Modulo salva affluenze                                               */
+/* Amministrazione                                                      */
+/************************************************************************/
+session_name('sesadmin');
+  // gestione sessione
+$a = session_id();
+if(empty($a)) session_start();
+if(!$_SESSION['aid'])
+    die ("You can't access this file directly..."); 
+$aid=$_SESSION['aid'];
+include("../../config.php");
+	try{
+	$dbi = new PDO("mysql:host=$dbhost;charset=latin1", $dbuname, $dbpass, array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION)); 
+     	$sql = "use $dbname";
+    	$dbi->exec($sql);
+        }
+	catch(PDOException $e)
+	{
+	    echo $sql . "<br>" . $e->getMessage();
+	}             
+
+if (isset($_GET['genere'])) $genere=intval($_GET['genere']); else $genere='0';
+if (isset($_GET['id_cons_gen'])) $id_cons_gen=intval($_GET['id_cons_gen']); else $id_cons_gen='0';
+if (isset($_GET['id_cons'])) $id_cons=intval($_GET['id_cons']); else $id_cons='0';
+if (isset($_GET['op'])) $op=intval($_GET['op']); else $op='0';
+if (isset($_GET['id_circ'])) $id_circ=intval($_GET['id_circ']); else $id_circ='0';
+if (isset($_GET['id_sez'])) $id_sez=intval($_GET['id_sez']); else $id_sez='0';
+if (isset($_GET['id_sede'])) $id_sede=intval($_GET['id_sede']); else $id_sede='0';
+if (isset($_GET['do'])) $do=intval($_GET['do']); else $do='0';
+if (isset($_GET['validi'])) $validi=intval($_GET['validi']); else $validi='0';
+if (isset($_GET['nulli'])) $nulli=intval($_GET['nulli']); else $nulli='0';
+if (isset($_GET['bianchi'])) $bianchi=intval($_GET['bianchi']); else $bianchi='0';
+if (isset($_GET['contestati'])) $contestati=intval($_GET['contestati']); else $contestati='0';
+if (isset($_GET['votinulli'])) $votinulli=intval($_GET['votinulli']); else $votinulli='0';
+if (isset($_GET['id_comune'])) $id_comune=intval($_GET['id_comune']); else $id_comune='0';
+if (isset($_GET['ops'])) $ops=intval($_GET['ops']); else $ops='0';
+if (isset($_GET['delete'])) $delete=intval($_GET['delete']); else $delete='';
+if (isset($_GET['circo'])) get_magic_quotes_gpc() ? $circo=$_GET['circo']:$circo=addslashes($_GET['circo']); else $circo='';
+
+global $prefix,$id_parz,$fileout;
+$sql="select num_gruppo from ".$prefix."_ele_gruppo where id_cons='$id_cons' $circo ORDER BY num_gruppo  ";
+$result = $dbi->prepare("$sql");
+$result->execute();
+while(list($i)=$result->fetch(PDO::FETCH_BOTH)) {
+	$gruv="id_gruppo$i";$siv="si$i";$nov="no$i";$valv="val$i";$biav="bia$i";$conv="con$i";$nulv="nul$i";
+	if (isset($_GET[$gruv])) $id_gruppo[$i]=intval($_GET[$gruv]); else $id_gruppo[$i]='0';
+	if (isset($_GET[$siv])) $si[$i]=intval($_GET[$siv]); else $si[$i]='0';
+	if (isset($_GET[$nov])) $no[$i]=intval($_GET[$nov]); else $no[$i]='0';
+	if (isset($_GET[$valv])) $val[$i]=intval($_GET[$valv]); else $val[$i]='0';
+	if (isset($_GET[$biav])) $bia[$i]=intval($_GET[$biav]); else $bia[$i]='0';
+	if (isset($_GET[$conv])) $con[$i]=intval($_GET[$conv]); else $con[$i]='0';
+	if (isset($_GET[$nulv])) $nul[$i]=intval($_GET[$nulv]); else $nul[$i]='0';
+
+}
+
+if (!isset($fileout)) $fileout='';
+#if(($voti_u+$voti_d) and !$voti_t) $voti_t=$voti_u+$voti_d;
+if ($fileout) while (!$fp = fopen($fileout,"a"));
+
+##################
+	if($do==1) {
+		$sql="delete from ".$prefix."_ele_voti_ref where id_cons='$id_cons' and id_sez='$id_sez'";
+		$res = $dbi->prepare("$sql");
+		$res->execute();
+	}else{
+
+		$sql="select num_gruppo from ".$prefix."_ele_voti_ref where id_cons='$id_cons' and id_sez='$id_sez'";
+		$result = $dbi->prepare("$sql");
+		$result->execute();
+		if($result->rowCount()) {
+			while(list($i)=$result->fetch(PDO::FETCH_NUM)){
+				$sql="update  ".$prefix."_ele_voti_ref set si='".$si[$i]."',no='".$no[$i]."',validi='".$val[$i]."',bianchi='".$bia[$i]."',contestati='".$con[$i]."',nulli='".$nul[$i]."' where id_cons='$id_cons' and id_sez='$id_sez' and id_gruppo='".$id_gruppo[$i]."'";
+				$res = $dbi->prepare("$sql");
+				$res->execute();
+			}
+		}else{
+			$sql="select num_gruppo,id_gruppo from ".$prefix."_ele_gruppo where id_cons='$id_cons'";
+			$result = $dbi->prepare("$sql");
+			$result->execute();
+			while(list($i,$id_gruppo)=$result->fetch(PDO::FETCH_NUM)){
+				$sql="insert into  ".$prefix."_ele_voti_ref values('$id_cons','$id_gruppo','$id_sez','$i','".$si[$i]."','".$no[$i]."','".$val[$i]."','".$bia[$i]."','".$con[$i]."','".$nul[$i]."')";
+				$res = $dbi->prepare("$sql");
+				$res->execute();
+			}
+			
+		}
+}
+
+
+
+controllo_votir($id_cons,$id_sez,$genere,$id_gruppo);
+
+if ($fileout) fclose($fp);
+$BASE=substr($_SERVER['PHP_SELF'], 0, strrpos($_SERVER['REQUEST_URI'], "/")-16);
+Header("Location: ".$BASE."admin.php?op=voti&id_cons_gen=$id_cons_gen&id_circ=$id_circ&id_sede=$id_sede&id_sez=$id_sez&do=spoglio&ops=$ops");
+#################################
+
+
+
+
+	
+function controllo_votir($id_cons,$id_sez,$genere){
+	global $prefix,$dbi,$id_sede,$id_cons_gen;
+		##############################
+	$err=0; $err2=0;
+	$sql="SELECT id_gruppo FROM ".$prefix."_ele_gruppo where id_cons='$id_cons'";
+	$resref = $dbi->prepare("$sql");
+	$resref->execute();	
+	$numscru=$resref->rowCount(); $rifscru=0;
+	while(list($idrefgruppo)=$resref->fetch(PDO::FETCH_NUM)) {
+		$sql="SELECT si,no,validi,nulli,bianchi,contestati FROM ".$prefix."_ele_voti_ref where id_sez='$id_sez' and id_gruppo='$idrefgruppo'";
+		$res2 = $dbi->prepare("$sql");
+		$res2->execute();
+		$refscru=$res2->rowCount(); 
+		$sql="SELECT max(voti_complessivi) FROM ".$prefix."_ele_voti_parziale where id_sez='$id_sez' and id_gruppo='$idrefgruppo'";
+		$res3 = $dbi->prepare("$sql");
+		$res3->execute();
+		list($voti)=$res3->fetch(PDO::FETCH_NUM);
+		$rifscru++;
+		list($si,$no,$validi,$nulli,$bianchi,$contestati)=$res2->fetch(PDO::FETCH_NUM);
+		if(($si+$no==$validi) and ($validi+$nulli+$bianchi+$contestati==$voti)) 
+			continue;
+		else {$err=1; break;}	
+
+	
+
+#############
+		$tipo='referendum';
+		$sql="select id from ".$prefix."_ele_controlli where tipo='$tipo' and id_sez='$id_sez' and id='$idrefgruppo' ";
+		$res = $dbi->prepare("$sql");
+		$res->execute();
+		$righe=$res->rowCount();
+		if($righe){
+			if(!$err){ 
+				$sql="delete from ".$prefix."_ele_controlli where tipo='$tipo' and id='$id_sez' ";
+				$res = $dbi->prepare("$sql");
+				$res->execute();
+			}
+			$err=0;
+		}
+		if($err){
+			$sql="insert into ".$prefix."_ele_controlli value('$id_cons','$id_sez','$tipo','$id_sez')";
+			$res = $dbi->prepare("$sql");
+			$res->execute();		
+		}
+#############
+	}
+	include("ele_colora_sez.php");
+}
+
+?>
Index: /trunk/admin/modules/Elezioni/salva_voti.php
===================================================================
--- /trunk/admin/modules/Elezioni/salva_voti.php	(revision 261)
+++ /trunk/admin/modules/Elezioni/salva_voti.php	(revision 261)
@@ -0,0 +1,161 @@
+<?php
+/************************************************************************/
+/* Eleonline - Raccolta e diffusione dei dati elettorali                */
+/************************************************************************/
+/* Modulo salva affluenze                                               */
+/* Amministrazione                                                      */
+/************************************************************************/
+session_name('sesadmin');
+  // gestione sessione
+$a = session_id();
+if(empty($a)) session_start();
+if(!$_SESSION['aid'])
+    die ("You can't access this file directly..."); 
+$aid=$_SESSION['aid'];
+include("../../config.php");
+	try{
+	$dbi = new PDO("mysql:host=$dbhost;charset=latin1", $dbuname, $dbpass, array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION)); 
+     	$sql = "use $dbname";
+    	$dbi->exec($sql);
+        }
+	catch(PDOException $e)
+	{
+	    echo $sql . "<br>" . $e->getMessage();
+	}             
+
+if (isset($_GET['genere'])) get_magic_quotes_gpc() ? $genere=$_GET['genere']:$genere=addslashes($_GET['genere']); else $genere='';
+if (isset($_GET['id_cons_gen'])) get_magic_quotes_gpc() ? $id_cons_gen=$_GET['id_cons_gen']:$id_cons_gen=addslashes($_GET['id_cons_gen']); else $id_cons_gen='';
+if (isset($_GET['id_cons'])) get_magic_quotes_gpc() ? $id_cons=$_GET['id_cons']:$id_cons=addslashes($_GET['id_cons']); else $id_cons='';
+if (isset($_GET['op'])) get_magic_quotes_gpc() ? $op=$_GET['op']:$op=addslashes($_GET['op']); else $op='';
+if (isset($_GET['id_circ'])) get_magic_quotes_gpc() ? $id_circ=$_GET['id_circ']:$id_circ=addslashes($_GET['id_circ']); else $id_circ='';
+if (isset($_GET['id_sez'])) get_magic_quotes_gpc() ? $id_sez=$_GET['id_sez']:$id_sez=addslashes($_GET['id_sez']); else $id_sez='';
+if (isset($_GET['id_sede'])) get_magic_quotes_gpc() ? $id_sede=$_GET['id_sede']:$id_sede=addslashes($_GET['id_sede']); else $id_sede='';
+if (isset($_GET['validi'])) get_magic_quotes_gpc() ? $validi=$_GET['validi']:$validi=addslashes($_GET['validi']); else $validi='';
+if (isset($_GET['nulli'])) get_magic_quotes_gpc() ? $nulli=$_GET['nulli']:$nulli=intval($_GET['nulli']); else $nulli='';
+if (isset($_GET['bianchi'])) get_magic_quotes_gpc() ? $bianchi=$_GET['bianchi']:$bianchi=addslashes($_GET['bianchi']); else $bianchi='';
+if (isset($_GET['contestati'])) get_magic_quotes_gpc() ? $contestati=$_GET['contestati']:$contestati=addslashes($_GET['contestati']); else $contestati='';
+if (isset($_GET['votinulli'])) get_magic_quotes_gpc() ? $votinulli=$_GET['votinulli']:$votinulli=addslashes($_GET['votinulli']); else $votinulli='01-01-1900';
+if (isset($_GET['id_comune'])) get_magic_quotes_gpc() ? $id_comune=$_GET['id_comune']:$id_comune=addslashes($_GET['id_comune']); else $id_comune='';
+if (isset($_GET['ops'])) get_magic_quotes_gpc() ? $ops=$_GET['ops']:$ops=addslashes($_GET['ops']); else $ops='';
+if (isset($_GET['delete'])) get_magic_quotes_gpc() ? $delete=$_GET['delete']:$delete=addslashes($_GET['delete']); else $delete='';
+
+global $prefix,$id_parz,$genere,$fileout,$id_cons;
+if (!isset($fileout)) $fileout='';
+#if(($voti_u+$voti_d) and !$voti_t) $voti_t=$voti_u+$voti_d;
+if ($fileout) while (!$fp = fopen($fileout,"a"));
+
+$salvato=0;
+
+##################
+$sql="select solo_gruppo,disgiunto from ".$prefix."_ele_cons_comune where id_cons='$id_cons' ";
+$result = $dbi->prepare("$sql");
+$result->execute();
+list($flagsg,$disgiunto)=$result->fetch(PDO::FETCH_NUM);
+$sql="select * from ".$prefix."_ele_sezioni where id_cons='$id_cons' and id_sez='$id_sez' ";
+$result = $dbi->prepare("$sql");
+$result->execute();
+$ar=$result->fetch(PDO::FETCH_BOTH);
+$validilista=$validi-$ar['solo_gruppo'];
+$log_data= date("Y/m/d");
+$log_ora=getdate(time());	
+$orario=($log_ora['hours'].":".$log_ora['minutes'].":".$log_ora['seconds']);
+
+#if($ops==3 and ($genere==3 or $genere==4 or $genere==5) and ($votog or $genere==4)){
+	$sql="insert into ".$prefix."_ele_log values('$id_cons','$id_sez','$aid','$log_data','$orario','validi_lista:$ar[14] nulli:$ar[7] bianchi:$ar[8] ','validi_lista=$validilista, nulli=$nulli,bianchi=$bianchi','_ele_sezioni')";
+	$res = $dbi->prepare("$sql");
+	$res->execute();
+	$sql="update  ".$prefix."_ele_sezioni set validi='$validi', contestati='$contestati', nulli='$nulli',bianchi='$bianchi', voti_nulli='$votinulli' where id_cons='$id_cons' and id_sez='$id_sez' ";
+	$res = $dbi->prepare("$sql");
+	$res->execute();
+	if ($fileout) fwrite($fp,"$sql;\n");
+/*}else{
+ 	$sql="insert into ".$prefix."_ele_log values('$id_cons','$id_sez','$aid','','','validi:$ar[6] nulli:$ar[7] bianchi:$ar[8] contestati:$ar[9] voti_nulli:$ar[13]','validi=$validi, nulli=$nulli,bianchi=$bianchi,contestati=$contestati, voti_nulli=$votinulli','_ele_sezioni')";
+	$res = $dbi->prepare("$sql");
+	$res->execute();
+	$sql="update  ".$prefix."_ele_sezioni set validi='$validi', nulli='$nulli',bianchi='$bianchi',contestati='$contestati',voti_nulli='$votinulli' where id_cons='$id_cons' and id_sez='$id_sez' ";
+	$res = $dbi->prepare("$sql");
+	$res->execute();
+	if ($fileout) fwrite($fp,"$sql;\n"); */
+#}
+controllo_voti($id_cons,$id_sez);
+
+if ($fileout) fclose($fp);
+$BASE=substr($_SERVER['PHP_SELF'], 0, strrpos($_SERVER['REQUEST_URI'], "/")-16);
+Header("Location: ".$BASE."admin.php?op=voti&id_cons_gen=$id_cons_gen&id_circ=$id_circ&id_sede=$id_sede&id_sez=$id_sez&do=spoglio&ops=$ops");
+#################################
+
+
+
+
+	
+function controllo_voti($id_cons,$id_sez){
+	global $prefix,$dbi,$id_sede,$id_con_gen,$genere;
+		##############################
+	$sql="select * from ".$prefix."_ele_controllosez where id_cons='$id_cons' and id_sez='$id_sez'";
+	$resc = $dbi->prepare("$sql");
+	$resc->execute();
+	$conta=$resc->rowCount();
+	$err=0; $err2=0;
+	$sql="SELECT id_gruppo FROM ".$prefix."_ele_gruppo where id_cons='$id_cons'";
+	$resref = $dbi->prepare("$sql");
+	$resref->execute();	
+	if($genere==0){ 
+		$numscru=$resref->rowCount(); $rifscru=0;
+		while(list($idrefgruppo)=$resref->fetch(PDO::FETCH_NUM)) {
+			$sql="SELECT si,no,validi,nulli,bianchi,contestati FROM ".$prefix."_ele_voti_ref where id_sez='$id_sez' and id_gruppo='$idrefgruppo'";
+			$res2 = $dbi->prepare("$sql");
+			$res2->execute();
+			$refscru=$res2->rowCount(); 
+			$sql="SELECT max(voti_complessivi) FROM ".$prefix."_ele_voti_parziale where id_sez='$id_sez' and id_gruppo='$idrefgruppo'";
+			$res3 = $dbi->prepare("$sql");
+			$res3->execute();
+			list($voti)=$res3->fetch(PDO::FETCH_NUM);
+			$rifscru++;
+			list($si,$no,$validi,$nulli,$bianchi,$contestati)=$res2->fetch(PDO::FETCH_NUM);
+			if(($si+$no==$validi) and ($validi+$nulli+$bianchi+$contestati==$voti)) 
+				continue;
+			else {$err=1; break;}	
+		}
+	}else{
+		#per le altre consultazione
+		$sql="SELECT max(voti_complessivi) FROM ".$prefix."_ele_voti_parziale where id_sez='$id_sez'";
+		$res3 = $dbi->prepare("$sql");
+		$res3->execute();
+		list($voti)=$res3->fetch(PDO::FETCH_NUM);
+		$sql="SELECT validi FROM ".$prefix."_ele_sezioni where id_sez='$id_sez' and id_cons='$id_cons' ";
+		$res2 = $dbi->prepare("$sql");
+		$res2->execute();
+		list($validi) = $res2->fetch(PDO::FETCH_NUM);
+		if($validi) {
+			$status=0;
+			$query="SELECT validi,nulli,bianchi,contestati,voti_nulli,solo_gruppo,validi_lista,contestati_lista,voti_nulli_lista,solo_lista FROM ".$prefix."_ele_sezioni as t1 where t1.id_sez='$id_sez'";
+			$sql="$query";
+			$res4 = $dbi->prepare("$sql");
+			$res4->execute();
+			list($validi, $nulli, $bianchi,$contestati,$votinulli)=$res4->fetch(PDO::FETCH_NUM);
+			if (($validi+$nulli+$bianchi+$contestati+$votinulli)!=$voti)
+				{$err=1;}
+		} 
+	}
+	$tipo='votanti';
+	$sql="select id from ".$prefix."_ele_controlli where tipo='$tipo' and id_sez='$id_sez' ";
+	$res = $dbi->prepare("$sql");
+	$res->execute();
+	$righe=$res->rowCount();
+	if($righe){
+		if(!$err){ 
+			$sql="delete from ".$prefix."_ele_controlli where tipo='$tipo' and id='$id_sez' ";
+			$res = $dbi->prepare("$sql");
+			$res->execute();
+		}
+		$err=0;
+	}
+	if($err){
+		$sql="insert into ".$prefix."_ele_controlli value('$id_cons','$id_sez','$tipo','$id_sez')";
+		$res = $dbi->prepare("$sql");
+		$res->execute();		
+	}
+	include("ele_colora_sez.php");
+}
+
+?>
