$campo){
if ($key==0) $valori="'$idcns',";
elseif ($key==1) {$valori.= "null"; $oldidg=$campo;}
elseif ($key==6) $valori.= ",0";
else $valori.= ",'$campo'";
if ($key==2) $numgruppo= $campo;
if ($key==8) $isnew=1;
}
if (!$isnew and isset($valori)) $valori.=",null,null";
if(isset($valori)){
$sql="insert into ".$prefix."_ele_gruppo values($valori)";
$res_gruppo = $dbi->prepare("$sql");
$res_gruppo->execute();
$sql="select id_gruppo from ".$prefix."_ele_gruppo where num_gruppo='$numgruppo' and id_cons='$idcns'";
$resnew = $dbi->prepare("$sql");
$resnew->execute();
list ($newidg) = $resnew->fetch(PDO::FETCH_NUM);
unset($valori);
if($oldidg)
$_SESSION['gruppi']['idg_'.$oldidg]=$newidg;
# inslista($oldidg,$newidg);
}
}
}
function inslista()#$oldidg,$newidg
{
global $prefix, $dbi;
global $ar_lista,$ar_candi,$idcns;
foreach ($ar_lista as $rigalista){
if(!isset($rigalista[3])) continue;
$oldidl=0;
$okl=0;
$oldidg=$rigalista[3];
$newidg=$_SESSION['gruppi']['idg_'.$oldidg];
foreach($rigalista as $key=>$campo){
if ($key==0) $valori="'$idcns',";
elseif ($key==1) {$valori.= "null";$oldidl=$campo;}
elseif ($key==3) {$valori.= ",'$newidg'"; if ($campo!=$oldidg) $okl=1;}
elseif ($key==4) $valori.= ",0";
else $valori.= ",'$campo'";
if ($key==2) $numlista= $campo;
}
if(isset($valori)){
if ($okl) {$okl=0;continue;}
$sql="insert into ".$prefix."_ele_lista values($valori)";
$res_lista = $dbi->prepare("$sql");
$res_lista->execute();
$sql="select id_lista from ".$prefix."_ele_lista where num_lista='$numlista' and id_cons='$idcns'";
$reslnew = $dbi->prepare("$sql");
$reslnew->execute();
list ($newidl) = $reslnew->fetch(PDO::FETCH_NUM);
unset($valori);
if($oldidl)
$_SESSION['liste']['idl_'.$oldidl]=$newidl;
# inscandi($oldidl,$newidl);
}
}
}
function inscandi()#$oldidl,$newidl
{
global $prefix, $dbi;
global $ar_candi,$idcns;
foreach ($ar_candi as $rigacandi){
if(!isset($rigacandi[2])) continue;
$okc=0;
$oldidl=$rigacandi[2];
$newidl=$_SESSION['liste']['idl_'.$oldidl];
foreach($rigacandi as $key=>$campo){#echo "$key -- $campo
";
if (count($rigacandi)!=9) {unset($valori);continue;}
if ($key==0) $valori= "null,";
elseif ($key==1) $valori.="'$idcns',";
elseif ($key==2) {$valori.= "'$newidl'"; if ($campo!=$oldidl) $okc=1;}
else $valori.= ",'$campo'";
}
if(isset($valori)){
if ($okc) {$okc=0;continue;}
$sql="insert into ".$prefix."_ele_candidati values($valori)";
$res_lista = $dbi->prepare("$sql");
$res_lista->execute();
}
}
#foreach($_SESSION['liste'] as $key=>$val) echo "$key -- $val
";
}
$sql="SELECT t1.id_cons, t2.descrizione FROM ".$prefix."_ele_cons_comune as t1 left join ".$prefix."_ele_consultazione as t2 on t1.id_cons_gen=t2.id_cons_gen where t1.id_comune='$id_comune' and t2.id_cons_gen='$id_cons_gen'";
$res = $dbi->prepare("$sql");
$res->execute();
list($id_cons,$descrizione) = $res->fetch(PDO::FETCH_NUM);
if (!isset($_FILES['datafile']['tmp_name']) or !is_uploaded_file($_FILES['datafile']['tmp_name']))
{
ele();
if (isset($_GET['help'])) $help=intval($_GET['help']);
global $help,$language;
if (isset($help)) include("language/$language/ele_importa.html");
echo "
";
////////////////////////////
}else{
$idcns=$id_cons;
$sql="delete from ".$prefix."_ele_voti_ref where id_cons=$idcns";
$res_del = $dbi->prepare("$sql");
$res_del->execute();
$sql="delete from ".$prefix."_ele_voti_candidati where id_cons=$idcns";
$res_del = $dbi->prepare("$sql");
$res_del->execute();
$sql="delete from ".$prefix."_ele_voti_lista where id_cons=$idcns";
$res_del = $dbi->prepare("$sql");
$res_del->execute();
$sql="delete from ".$prefix."_ele_voti_gruppo where id_cons=$idcns";
$res_del = $dbi->prepare("$sql");
$res_del->execute();
$sql="update ".$prefix."_ele_sezioni set validi='0', contestati='0', validi_lista='0', nulli='0',bianchi='0',contestati_lista='0', voti_nulli_lista='0' where id_cons=$idcns";
$res_del = $dbi->prepare("$sql");
$res_del->execute();
$sql="delete from ".$prefix."_ele_voti_parziale where id_cons=$idcns";
$res_del = $dbi->prepare("$sql");
$res_del->execute();
$sql="delete from ".$prefix."_ele_candidati where id_cons=$idcns";
$res_del = $dbi->prepare("$sql");
$res_del->execute();
$sql="delete from ".$prefix."_ele_lista where id_cons=$idcns";
$res_del = $dbi->prepare("$sql");
$res_del->execute();
$sql="delete from ".$prefix."_ele_gruppo where id_cons=$idcns";
$res_del = $dbi->prepare("$sql");
$res_del->execute();
$datafile=$_FILES['datafile']['tmp_name'];
$arrFile = file($datafile);
$handle = fopen($datafile, "r");
$test=array();
$errore=0;
$fine=0;
$numgruppo=0;
$numlista=0;
// Set counters
$currentLine = 0;
$cntFile = count($arrFile);
$tabs=array($prefix."_ele_gruppo",$prefix."_ele_lista",$prefix."_ele_candidati",$prefix."_ele_circoscrizione");
$x=0;$k=0;
$scarto=0;
$primog=0;
$primol=0;
$conta=array();
$currentLine = 0;
$x=0;$k=0;
$y=0;
$ar_gruppo=array(array());
$ar_lista=array(array());
$ar_candi=array(array());
$z=0;
$tab=substr($arrFile[$currentLine],1,-2);
$conf=$tabs[$x];
if($k==0) {while (substr($arrFile[$currentLine],1,-2)!=$conf and $currentLine <= $cntFile) $currentLine++; $k++;}
$currentLine++;
while($currentLine <= $cntFile and $fine==0){
# $appo=substr($arrFile[$currentLine],1,-2);
if(isset($arrFile[$currentLine]))
$appo=substr($arrFile[$currentLine],1,-2);
else $appo='';
if (isset($tabs[($x+1)]) and $appo==$tabs[($x+1)]){ $x++;$conf=$tabs[$x];$currentLine++; continue;}
$test=explode(':',$appo); if(!is_array($test)) {die("errore di import
");}
foreach($test as $key=>$val)
if ($conf==$prefix."_ele_gruppo"){
$ar_gruppo[$z][$key]=addslashes(base64_decode($val));}
elseif ($conf==$prefix."_ele_lista"){
if($primog==0){
$gruppofil= array_filter($ar_gruppo);
$numgruppo=count($gruppofil);
insgruppo();
$primog=1;
unset($ar_gruppo);
}
$ar_lista[$z][$key]=addslashes(base64_decode($val));}
elseif ($conf==$prefix."_ele_candidati"){
if($primog==0){
$gruppofil= array_filter($ar_gruppo);
$numgruppo=count($gruppofil);
insgruppo();
$primog=1;
unset($ar_gruppo);
}
elseif($primol==0){
$listafil= array_filter($ar_lista);
$numlista=count($listafil);
inslista();
$primol=1;
unset($ar_lista);
}
$ar_candi[$z][$key]=addslashes(base64_decode($val));
}
elseif ($conf==$prefix."_ele_circoscrizione"){
if($primog==0){
$gruppofil= array_filter($ar_gruppo);
$numgruppo=count($gruppofil);
insgruppo();
$primog=1;
unset($ar_gruppo);
}
elseif($primol==0){
$listafil= array_filter($ar_lista);
$numlista=count($listafil);
inslista();
$primol=1;
unset($ar_lista);
}else{
inscandi();
unset($ar_candi);
$fine=1;
break;
}
}
$currentLine++;
$z++;
}
fclose($handle);
if ($numgruppo){
# insgruppo();
Header("Location: admin.php?op=gruppo&id_cons_gen=$id_cons_gen");
}
elseif ($numlista) {
inslista(0,0);
Header("Location: admin.php?op=lista&id_cons_gen=$id_cons_gen");
}
else Header("Location: admin.php?op=lista&id_cons_gen=$id_cons_gen");
}
echo"";
include("footer.php");
?>