<?php
include ("connection.php");
include ("frames_header.php");
?>

<?php

if (isset($_POST["test"]))
    $test=$_POST["test"];
else 
    $test="n";

if (isset($_POST["species"]))
    $species=$_POST["species"];
else 
{
    $species="";
    echo "<strong>ERROR: please select a species.</strong> <br>";
    include("frames_footer.php");
    return;
}


if (isset($_POST["filepath"]))
{
    $path=$_POST["filepath"];
    $path = str_replace('"','',$path);
    
}
else 
{
    $path="";
    echo "<strong>ERROR: please select a Unigene library information file</strong> <br>";
    include("frames_footer.php");
    return;
}

if ($path!="")
{
    //echo "$path<br> ";
    $fdread=fopen($path, 'r');
    if(!($fdread))
    {
        echo" <H1>ERROR : Can't open the file :$path</H1> <BR>";
        include("frames_footer.php");
        return;
    }
    else
    {
        
        echo" <strong>The file $path is open </strong><BR>";
        $buffer = fgets($fdread, 4096);
        $cpt=1;
        $title="";
        $tissue="";
        $dev_stage="";
        $vector="";
        while(!feof($fdread))
        {
            
            if (ereg("^ID=([0-9]+)\n$", $buffer, $tabid))
            {
                $id=$tabid[1];
                echo "$cpt/ ";
                echo "<br>id=$id<br>";
                
                $cpt++;
                
                $title="";
                $tissue="";
                $dev_stage="";
                $vector="";
            }
            
            
            if (ereg("^TITLE=(.+)\n$", $buffer, $tabtitle))
            {
                $title=$tabtitle[1];
                echo "title=$title<br>";
            }
            
            if (ereg("^TISSUE=(.+)\n$", $buffer, $tabtissue))
            {
                $tissue=$tabtissue[1];
                echo "tissue=$tissue<br>";
            }
            
            if (ereg("^DEVELOPMENTAL_STAGE=(.+)\n$", $buffer, $tabdevstage))
            {
                $dev_stage=$tabdevstage[1];
                echo "Developmental stage=$dev_stage<br>";
            }
            
            if (ereg("^VECTOR=(.+)\n$", $buffer, $tabvector))
            {
                $vector=$tabvector[1];
                echo "vector=$vector<br>";
            }
            
            if ((ereg("^\n$", $buffer))||(ereg("^\ \n$", $buffer))) /*CHANGEMENT ICI CAR PB AVEC L'ESPACE => ereg("^\ \n$")*/
            {
                
                
                echo "id = $id // title=$title // tissue=$tissue // dev_stage=$dev_stage // vector=$vector<br>";
                $requete="SELECT \"LIBRARY_ID\" FROM \"LIBRARY\" where \"LIBRARY_ID\"='$id' and \"SPECIES\"= '$species'";
                $stmt = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
                if(!$row=pg_fetch_array($stmt))
                { 
                    $requete = "INSERT INTO \"LIBRARY\" (\"LIBRARY_ID\", \"TITLE\", \"TISSUE\", \"DEV_STAGE\", \"VECTOR\", \"SPECIES\") values ($id, '$title', '$tissue', '$dev_stage', '$vector', '$species')";
                    if($test=="n")
                    {
			pg_query($c,"begin");
                        $stmt1 = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
			pg_query($c,"commit");
                        //ocifreestatement($stmt1);
                    }/*
                    else
                    {
                        echo "<H3>fake : $requete </H3>";
                    }*/
                    
                }
                else
                { 
                    $requete="update \"LIBRARY\" set \"TITLE\"='$title', \"TISSUE\"='$tissue', \"DEV_STAGE\"='$dev_stage', \"VECTOR\"='$vector', \"SPECIES\"='$species' where \"LIBRARY_ID\"='$id' ";
                    if($test=="n")
                    {
			pg_query($c,"begin");
                        $stmt2 = pg_query($c,$requete) or die("<strong>Error</strong> ".pg_last_error($c));
                        pg_query($c,"commit");
                       // ocifreestatement($stmt2);
                    }/*
                    else
                    {
                        echo "<H3>fake : $requete</H3>";
                    }*/
                }
            }
	    
            $buffer = fgets($fdread, 4096);
        }
    }
}
?>


<?php
    include ("frames_footer.php");

?>