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

<?php
    if ( (!isset($_SESSION["login"]))or(!isset($_SESSION["pass"])) ) {
	    print "<center><strong>you need to <a href=\"login.php\">login</a> as an authorised curator to proceed.</strong>";
	    print '</center></body></html>';
	   include ("frames_footer.php");
		return;
	}
?>


<?
$id=$_GET["id"];

//REFERENCE
$reference=(array) null;
$requete="SELECT * from \"REFERENCE\" where \"BIO_ID\"='$id'";
$stmt=pg_query($c,$requete);
$row=pg_fetch_array($stmt);
$reference[count($reference)]=$row["BIO_ID"]."@".$row["PUBMED_ID"]."@".$row["TITLE"]."@".$row["PAPER"]."@".$row["AUTHORS"]."@".$row["URL"]."@".$row["COMMENTS"];

//print_r($reference);

//ENTRY_ORIGIN
$entry_origin=(array) null;
$requete="SELECT * from \"ENTRY_ORIGIN\" where \"BIO_ID\"='$id'";
$stmt=pg_query($c,$requete);
$row=pg_fetch_array($stmt);
$entry_origin[count($entry_origin)]=$row["AUTHOR_ID"]."@".$row["BIO_ID"]."@".$row["ENTRY_DATE"]."@".$row["CURATOR_ID"]."@".$row["CURATION_DATE"]."@".$row["ANNOTATOR_ID"]."@".$row["ANNOTATION_DATE"];

//IN_SITU
$in_situ=(array) null;
$requete="SELECT * from \"IN_SITU\" where \"IN_SITU_ID\"='$id'";
$stmt=pg_query($c,$requete);
$row=pg_fetch_array($stmt);
$wild=$row["WILD_TYPE"];
$in_situ[count($in_situ)]=$row["IN_SITU_ID"]."@".$row["DEV_STAGE_ID"]."@".$row["SPECIES"]."@".$row["IM_COMMENTS"]."@".$row["EXP_COMMENTS"]."@".$row["TIME"]."@".$row["TEMPERATURE"]."@".$row["WILD_TYPE"]."@".$row["CHECKED"]."@".$row["PRIVATE"];

//TARGET
$target=(array) null;
$requete="SELECT * from \"TARGET\" where \"IN_SITU_ID\"='$id'";
$stmt=pg_query($c,$requete);
while ($row=pg_fetch_array($stmt))
{
$target[count($target)]=$row["TARGET_ID"]."@".$row["IN_SITU_ID"]."@".$row["REGION_ID"]."@".$row["PROBE_ID"]."@".$row["METHOD"]."@".$row["INTENSITY"]."@".$row["SUBSTRATE"]."@".$row["SUBCELL_POS"]."@".$row["COMMENTS"]."@".$row["NAME"]."@".$row["SURE"]."@".$row["PART_OF"];
}

//PICTURE
$picture=(array) null;
$requete="SELECT * FROM \"PICTURE\" where \"IN_SITU_ID\"='$id'";
$stmt=pg_query($c,$requete);
while ($row=pg_fetch_array($stmt))
{
$picture[count($picture)]=$row["PICTURE_ID"]."@".$row["IN_SITU_ID"]."@".$row["IMAGE"]."@".$row["THUMBNAIL"]."@".$row["ANNOTATED_IMAGE"]."@".$row["CONTROL"]."@".$row["PATH"];
}


//Cas ou c'est pas un wild type
if ($wild!='y')
{
//MUTANT
$mutant=(array) null;
$requete="SELECT * from \"MUTANT\" where \"IN_SITU_ID\"='$id'";
$stmt=pg_query($c,$requete);
while ($row=pg_fetch_array($stmt))
{
$mutant[count($mutant)]=$row["BIO_ID"]."@".$row["IN_SITU_ID"]."@".$row["FROM_ID"]."@".$row["TO_ID"]."@".$row["UP_DOWN"]."@".$row["NAME"]."@".$row["INHIBITOR_ID"]."@".$row["TYPE"]."@".$row["COMMENTS"]."@".$row["CTRL_ISH_ID"];
}

//EMBRYO_MANIP
$embryo_manip=(array) null;
$requete="SELECT * from \"EMBRYO_MANIP\" where \"IN_SITU_ID\"='$id'";
$stmt=pg_query($c,$requete);
while ($row=pg_fetch_array($stmt))
{
$embryo_manip[count($embryo_manip)]=$row["IN_SITU_ID"]."@".$row["STRUCTURE_ID"]."@".$row["FROM_ID"]."@".$row["COMMENTS"]."@".$row["CTRL_ISH_ID"];
}

}

/*
print_r($reference);
print "<hr>";
print_r($entry_origin);
print "<hr>";
print_r($in_situ);
print "<hr>";
print_r($target);
print "<hr>";
print_r($picture);
print "<hr>";
print_r($mutant);
print "<hr>";
print_t($embryo_manip);
print "<hr>";
*/


//Creation d'une nouvelle entree dans le BIO_ID
//pg_query($c,"begin");

//BIO_ENTITY
$requete="INSERT INTO \"BIO_ENTITY\" (\"BIO_ID\",\"CURATED\") values (nextval('\"S_BIO_ID\"'),'y')";
//print $requete."<hr>";
pg_query($c,$requete) or die (pg_last_error($c));

//REFERENCE
$col="";
$val="";
$tmp=explode("@",$reference[0]);
//$ref_bio_id=$tmp[0];
$ref_pubmed_id=$tmp[1];
$ref_title=$tmp[2];
$ref_paper=$tmp[3];
$ref_authors=$tmp[4];
$ref_url=$tmp[5];
$ref_comments=$tmp[6];
$req=false;
$col.="\"BIO_ID\"";$val.="currval('\"S_BIO_ID\"'),";
if (strlen($ref_pubmed_id)!=0) {$col.="\"PUBMED_ID\"";$val.="'$ref_pubmed_id'";$req=true;}
if (strlen($ref_title)!=0) {$col.="\"TITLE\"";$val.="'$ref_title'";$req=true;}
if (strlen($ref_paper)!=0) {$col.="\"PAPER\"";$val.="'$ref_paper'";$req=true;}
if (strlen($ref_authors)!=0) {$col.="\"AUTHORS\"";$val.="'$ref_authors'";$req=true;}
if (strlen($ref_url)!=0) {$col.="\"URL\"";$val.="'$ref_url'";$req=true;}
if (strlen($ref_comments)!=0) {$col.="\"COMMENTS\"";$val.="'$ref_comments'";$req=true;}

if ($req)
{
$requete="INSERT INTO \"REFERENCE\" ($col) values ($val);";
$requete=str_replace("\"\"","\",\"",$requete);
$requete=str_replace("''","','",$requete);
$requete=str_replace(",)",")",$requete);
//print $requete."<hr>";
pg_query($c,$requete);
//print $requete;
}

//ENTRY_ORIGIN
$col="";
$val="";
$tmp=explode("@",$entry_origin[0]);
$eo_author_id=$tmp[0];
//$eo_bio_id=$tmp[1];
$eo_entry_date=$tmp[2];
$eo_curator_id=$tmp[3];
$eo_curation_date=$tmp[4];
$eo_annotator_id=$tmp[5];
$eo_annotation_date=$tmp[6];
if (strlen($eo_author_id)!=0) {$col.="\"AUTHOR_ID\"";$val.="'$eo_author_id',";}
$col.="\"BIO_ID\"";$val.="currval('\"S_BIO_ID\"'),";
if (strlen($eo_entry_date)!=0) {$col.="\"ENTRY_DATE\"";$val.="'$eo_entry_date'";}
if (strlen($eo_curator_id)!=0) {$col.="\"CURATOR_ID\"";$val.="'$eo_curator_id'";}
if (strlen($eo_curation_date)!=0) {$col.="\"CURATION_DATE\"";$val.="'$eo_curation_date'";}
if (strlen($eo_annotator_id)!=0) {$col.="\"ANNOTATOR_ID\"";$val.="'$eo_annotator_id'";}
if (strlen($eo_annotation_date)!=0) {$col.="\"ANNOTATION_DATE\"";$val.="'$eo_annotation_date'";}

$requete="INSERT INTO \"ENTRY_ORIGIN\" ($col) values ($val);";
$requete=str_replace("\"\"","\",\"",$requete);
$requete=str_replace("''","','",$requete);
//print $requete."<hr>";
pg_query($c,$requete);
//print $requete;

//IN_SITU
$col="";
$val="";
$tmp=explode("@",$in_situ[0]);
//$is_in_situ_id=$tmp[0];
$is_dev_stage_id=$tmp[1];
$is_species=$tmp[2];
$is_im_comments=str_replace("'","\'",$tmp[3]);
$is_exp_comments=$tmp[4];
$is_time=$tmp[5];
$is_temperature=$tmp[6];
$is_wild_type='y';
$is_checked=$tmp[8];
$is_private=$tmp[9];


$col.="\"IN_SITU_ID\"";$val.="currval('\"S_BIO_ID\"'),";
if (strlen($is_dev_stage_id)!=0) {$col.="\"DEV_STAGE_ID\"";$val.="'$is_dev_stage_id'";}
if (strlen($is_species)!=0) {$col.="\"SPECIES\"";$val.="'$is_species'";}
if (strlen($is_im_comments)!=0) {$col.="\"IM_COMMENTS\"";$val.="'$is_im_comments'";}
if (strlen($is_exp_comments)!=0) {$col.="\"EXP_COMMENTS\"";$val.="'$is_exp_comments'";}
if (strlen($is_time)!=0) {$col.="\"TIME\"";$val.="'$is_time'";}
if (strlen($is_temperature)!=0) {$col.="\"TEMPERATURE\"";$val.="'$is_temperature'";}
if (strlen($is_wild_type)!=0) {$col.="\"WILD_TYPE\"";$val.="'$is_wild_type'";}
if (strlen($is_checked)!=0) {$col.="\"CHECKED\"";$val.="'$is_checked'";}
if (strlen($is_private)!=0) {$col.="\"PRIVATE\"";$val.="'$is_private'";}


$requete="INSERT INTO \"IN_SITU\" ($col) values ($val);";
$requete=str_replace("\"\"","\",\"",$requete);
$requete=str_replace("''","','",$requete);
//print $requete."<hr>";
pg_query($c,$requete);
//print $requete;

//TARGET
for ($i=0;$i<count($target);$i++)
{
$col="";
$val="";
$tmp=explode("@",$target[$i]);
$t_target_id=$tmp[0];
//$t_in_situ_id=$tmp[1];
$t_region_id=$tmp[2];
$t_probe_id=$tmp[3];
$t_method=$tmp[4];
$t_intensity=$tmp[5];
$t_substrate=$tmp[6];
$t_subcell_pos=$tmp[7];
$t_comments=$tmp[8];
$t_name=$tmp[9];
$t_sure=$tmp[10];
$t_part_of=$tmp[11];


if (strlen($t_target_id)!=0) {$col.="\"TARGET_ID\"";$val.="'$t_target_id',";}
$col.="\"IN_SITU_ID\"";$val.="currval('\"S_BIO_ID\"'),";
if (strlen($t_region_id)!=0) {$col.="\"REGION_ID\"";$val.="'$t_region_id'";}
if (strlen($t_probe_id)!=0) {$col.="\"PROBE_ID\"";$val.="'$t_probe_id'";}
if (strlen($t_method)!=0) {$col.="\"METHOD\"";$val.="'$t_method'";}
if (strlen($t_intensity)!=0) {$col.="\"INTENSITY\"";$val.="'$t_intensity'";}
if (strlen($t_substrate)!=0) {$col.="\"SUBSTRATE\"";$val.="'$t_substrate'";}
if (strlen($t_subcell_pos)!=0) {$col.="\"SUBCELL_POS\"";$val.="'$t_subcell_pos'";}
if (strlen($t_comments)!=0) {$col.="\"COMMENTS\"";$val.="'$t_comments'";}
if (strlen($t_name)!=0) {$col.="\"NAME\"";$val.="'$t_name'";}
if (strlen($t_sure)!=0) {$col.="\"SURE\"";$val.="'$t_sure'";}
if (strlen($t_part_of)!=0) {$col.="\"PART_OF\"";$val.="'$t_part_of'";}


$requete="INSERT INTO \"TARGET\" ($col) values ($val);";
$requete=str_replace("\"\"","\",\"",$requete);
$requete=str_replace("''","','",$requete);
//print $requete."<hr>";
pg_query($c,$requete);
//print $requete;
}

//PICTURE
for ($i=0;$i<count($picture);$i++)
{
$col="";
$val="";
$tmp=explode("@",$picture[$i]);
//$p_picture_id=$tmp[0];
//$p_in_situ_id=$tmp[1];
$p_image=$tmp[2];
$p_thumbnail=$tmp[3];
$p_annotated_image=$tmp[4];
$p_control=$tmp[5];
$p_path=$tmp[6];

$col.="\"PICTURE_ID\"";$val.="nextval('\"S_PICTURE_ID\"'),";
$col.="\"IN_SITU_ID\"";$val.="currval('\"S_BIO_ID\"'),";

if (strlen($p_image)!=0) {$col.="\"IMAGE\"";$val.="'$p_image'";}
if (strlen($p_thumbnail)!=0) {$col.="\"THUMBNAIL\"";$val.="'$p_thumbnail'";}
if (strlen($p_annotated_image)!=0) {$col.="\"ANNOTATED_IMAGE\"";$val.="'$p_annotated_image'";}
if (strlen($p_control)!=0) {$col.="\"CONTROL\"";$val.="'$p_control'";}
if (strlen($p_path)!=0) {$col.="\"PATH\"";$val.="'$p_path'";}


$requete="INSERT INTO \"PICTURE\" ($col) values ($val);";
$requete=str_replace("\"\"","\",\"",$requete);
$requete=str_replace("''","','",$requete);
//print $requete."<hr>";
pg_query($c,$requete);
//print $requete;
}

//On recup l'id de la nouvelle ish
$requete="SELECT currval('\"S_BIO_ID\"') as \"ID\"";
$stmt=pg_query($c,$requete);
$row=pg_fetch_array($stmt);
$iddup=$row["ID"];


pg_query($c,"commit");


// 	include("update-interactions.php");
// 	update_evidence($c, $iddup);


print "<br><br><center><strong>Duplication done</strong></center>";

print "<br><br><center><a target=\"_blank\" href=\"insitu.php?id=$iddup\">Go to the new ISH : $iddup</a>&nbsp;&nbsp;&nbsp;&nbsp<a target=\"_blank\" href=\"insitu.php?id=$id\">Go back to the \"original\" ish</a></center>";




include ("frames_footer.php");
?>