<?php
include "scrinit.php";
include $menu;
require "dbconnect.php";
$dt=$_SESSION['dt'];
?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>

<h1>ΕΙΣΑΓΩΓΗ ΣΥΝΔΙΔΑΣΚΑΛΙΩΝ</h1>
<form method="post" enctype="multipart/form-data">
    Επιλέξτε το αρχείο CSV με τις επιλογές:
    <input type="file" name="fileToUpload" id="fileToUpload">
	<br>
	<p> Να διαγράψετε την πρώτη κενή στήλη και σειρά. Το υπόλοιπο άρχειο να παραμείνει στην αρχική του μορφή: </p>
	<br>
	<p>Κωδικοποίηση χαρακτήρων αρχείου (character encoding): <select name="charset">
       <option value="Windows-1253" selected>Windows-1253</option>
       <option value="ISO-8859-7">ISO-8859-7</option>
	   <option value="UTF-8">UTF-8</option>
    </select>
	<br>
    <p>Χαρακτήρας οριοθέτησης πεδίων (Delimiter): <select name="delim">
       <option value=";" selected><b>;</b></option>
       <option value=","><b>,</b></option>
    </select>
	<br>
	<p><input type="submit" value="ΑΝΕΒΑΣΜΑ ΑΡΧΕΙΟΥ" name="submit">
</form>
<?php
if (isset($_POST['submit'])) 
{
    if (is_uploaded_file($_FILES['fileToUpload']['tmp_name'])) 
	{
        echo "<h1>" . "File ". $_FILES['fileToUpload']['name'] ." uploaded successfully." . "</h1>";

	$fd=fopen($_FILES['fileToUpload']['tmp_name'],'r');
	$charset = $_POST['charset'];
	$file=$_FILES['fileToUpload'];
	$delim = $_POST['delim'];
        echo "<h2>Charset:" . $charset . "</h2>";
        echo "<h2>Delimiter:" . $delim . "</h2>";
        echo "<h2>Displaying contents:</h2>";
        echo "<table>\n";
		mysqli_query($con,'TRUNCATE TABLE syndidaskalies;');
		mysqli_query($con,'TRUNCATE TABLE syndcodes;');
		mysqli_query($con,'TRUNCATE TABLE syndkath;');
$i=0;
$mathima="";
$teach="";
$sid=0;
while(! feof($fd))
  {
	$list=fgetcsv($fd,0,$delim);
	for ($j=0;$j<count($list);$j++) {
		$list[$j] = $list[$j] == "" ? 'NULL' : iconv($charset,'UTF-8', $list[$j]);
	}
	$row=join('</td><td>',$list);
	$f1 = $list[0];
	$f2 = $list[1];
	$f3 = $list[2];
	$f4 = $list[3];
	if ($f1<>$f2 AND $f1 <> $f3)
	{	
	if(!in_array($f1, array("ΣΥΝΔΙΔΑΣΚΑΛΙΑ","ΟΝΟΜΑ")))		
	{
		if ($f1 == "ΤΜΗΜΑ" )
		{
			$mathima=$f2;
			mysqli_query($con,"INSERT INTO syndcodes  (synd)	VALUES ('$f2') " ); 
			$lastid = $con->insert_id;
			$t=$f3;
			$p=trim($f4);echo $p."-".$t;
			if (!empty($p) and !empty($t))
			{
			mysqli_query($con,"INSERT INTO syndkath  (synd,kathigitis,pmp)	VALUES ('$f2','$t','$p') " );
			}
			else
			{
			if (!empty($p))
			{
			   $pp=explode(",",$p);
			   $pp1=trim($pp[0]);$pp2=trim($pp[1]);
			   if (!empty($pp1))
			   {
				$sq1=mysqli_query($con,"select Name from teachers where PMP='$pp1'" );
				$row1=mysqli_fetch_array($sq1);
				$tn1=$row1['Name'];
				mysqli_query($con,"INSERT INTO syndkath  (synd,kathigitis,pmp)	VALUES ('$f2','$tn1','$pp1') " );
			   }
			   if (!empty($pp2))
			   {
				$sq1=mysqli_query($con,"select Name from teachers where PMP='$pp2'" );
				$row1=mysqli_fetch_array($sq1);
				$tn1=$row1['Name'];
				mysqli_query($con,"INSERT INTO syndkath  (synd,kathigitis,pmp)	VALUES ('$f2','$tn1','$pp2') " );
			   }
			   if (empty($pp1))
			   {
					$tt=explode(",",$t);
				   $tt1=trim($pp[0]);
				   $sq1=mysqli_query($con,"select PMP from teachers where Name='$tt1'" );
				   $row1=mysqli_fetch_array($sq1);
				   $pp1=$row1['PMP'];
				   mysqli_query($con,"INSERT INTO syndkath  (synd,kathigitis,pmp)	VALUES ('$f2','$tt1','$pp1') " );
			   }
				if (empty($pp2))
			   {
				   $tt=explode(",",$t);
				   $tt2=trim($pp[1]);
				   $sq1=mysqli_query($con,"select PMP from teachers where Name='$tt2'" );
				   $row1=mysqli_fetch_array($sq1);
				   $pp2=$row1['PMP'];
				   mysqli_query($con,"INSERT INTO syndkath  (synd,kathigitis,pmp)	VALUES ('$f2','$tt2','$pp2') " );
			   }
			}
			else
			{
			   $tt=explode(",",$t);
			   $tt1=trim($pp[0]);$tt2=trim($pp[1]);
			   $sq1=mysqli_query($con,"select PMP from teachers where Name='$tt1'" );
			   $row1=mysqli_fetch_array($sq1);
			   $pp1=$row1['PMP'];
			   mysqli_query($con,"INSERT INTO syndkath  (synd,kathigitis,pmp)	VALUES ('$f2','$tt1','$pp1') " );
			   if (!empty($tt2))
			   {
				$sq1=mysqli_query($con,"select PMP from teachers where Name='$tt2'" );
			    $row1=mysqli_fetch_array($sq1);
			    $pp2=$row1['PMP'];
			    mysqli_query($con,"INSERT INTO syndkath  (synd,kathigitis,pmp)	VALUES ('$f2','$tt2','$pp2') " );   
			   }
			}
		
		}
		}
		else
		{
			
			$sql="INSERT INTO syndidaskalies  (syndid,mitroo)	VALUES ('$lastid','$f2')" ;
			$sql   = str_replace("'NULL'", 'NULL', $sql);
			echo "<tr><td>" . $row . "</td>";
			if($i>=0) 
			{
			   echo "<td>" . $sql . "</td>";
			   if(mysqli_query($con,$sql)===TRUE) 
			   {
				  echo "<td>OK</td>";
			   } 
			   else 
			   {
				  echo "<td>" . mysqli_error($con) . "</td>";
			   }
			}
		}

	}
	}
	echo "</tr>\n";
	
	$i++;
	
  }
        echo "</table>\n";

fclose($fd);

    } 
	else 
	{
        echo "ΔΕΝ ΕΓΙΝΕ Η ΦΟΡΤΩΣΗ ΤΟΥ ΑΡΧΕΙΟΥ.";
    }
}
?>
<br /><br />

<a  href=<?php echo $menu; ?>  style="color:blue "  > <button>  Επιστροφή στο Μενού  </a>
</body>
</html>
