<!-- Copyright (C) 2009 DNA Bank Network
http://www.dnabank-network.org

The contents of this file are subject to the Mozilla Public License Version 1.1
See LICENSE.TXT at the top of this package for the full license terms. -->

<?php
    include ("head.php");
    include ("auth.php");
?>
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>DNA Bank Network - Publications</title>
<link rel="stylesheet" type="text/css" href="format.css">
<SCRIPT LANGUAGE="JavaScript1.1" src="Functions.js"></SCRIPT>
<NOSCRIPT>Bitte aktivieren Sie Javascript!</NOSCRIPT>

</head>
<body>
<form name="formular" method="POST" action="Publications.php"> 
<table id="tablemain" border="0">
	<tr>
  <td id="tdLogo"><img border="0" src="images/Logo-DNA-Bank-Network-ocker.jpg" width="123" height="78"></td>
  		<td>
		<h1>DNA Bank Network - Publications</h1></td>
    <td id="tdLogo"><img border="0" src="images/Logo.jpg"></td>
	</tr>
    <tr>
  <td><?php
    echo "Logged in as: ";
    $log = $_SESSION["log"];
    echo $log;


    ?></td><td colspan="2"><a href="logout.php">Logout</a>.</td>
  </tr>
<tr>
<td valign="top" align="center" valign="middle" colspan="3">
<table id="table" width="98%" border="0" style="margin:30 0 0 0">
<tr><td align="center" colspan="2"><a class="black" href="Help-Publications2.php" target="_blank">>>> Help <<<</a><br><br></td></tr>
<tr>
<td align="center" colspan="2">

<table border="0">
<tr><td colspan="3">Um eine Publikation einer DNA-Probe zuzuordnen, geben Sie bitte einen Teil des Paper-Titels ein sowie die gewünschten DNA-Nummern oder Gen Bank Accession Numbers. Sie können in einem ersten Schritt auch nur prüfen lassen, ob die Publikation schon in der Datenbank ist.</td></tr>
    	<tr>
			        <td>
			        Search for Title:</td><td> <input type="text" name="formSearchTitle" id="InputVeryLong2" value="<?php echo $_POST['formSearchTitle']; ?>"/></td><td id="tdbutton">or show all Papers: <input type="checkbox" id="checkbox" name="formShowAll" <?php if(isset($formShowAll)) { echo " checked"; }?>>&nbsp;&nbsp;<input type="submit" id="button" name="formSubmitCheck" value="Search">&nbsp;&nbsp; <a href="new_publication.php" target="_blank">add new publication</a>
</td></tr>
    <tr>
    <td>and Gen Bank Numbers:</td>
    <td><input type="text" id="InputVeryLong2" name="formGenbankNumber" value="<?php echo $_POST['formGenbankNumber']; ?>"></td><td>Please separate numbers with semicolon (e.g. AJ746400;AJ746394)</td>
    </tr>
    <tr>
    <td>or DNA Extraction Numbers:</td>
    <td><input type="text" id="InputVeryLong2" name="formDnaBankNumber" value="<?php echo $_POST['formDnaBankNumber']; ?>"></td>
    <td>Please separate numbers with semicolon (e.g. 236;289;324)</td>
    </tr>
    
    <tr><td colspan="3">
    
    <?php
    
    if($formSubmitSavePub)
    {
    if($_POST['x']=="")
    { unset($x); 
    echo "<span class='error'>Please select a publication!</span>"; }

       
    if($_POST['y']=="")
    { unset($y); 
    echo "<span class='error'>Please select a fragment!</span>"; }
    
    else {    
    if(isset($_POST['x']))
    {$mykey = key($_POST['formPublications']);

     if($mykey = $_POST['x'])
        { 
        $IDPublication = $_POST['formPublications'][$mykey];
     } }  //if($_POST['x']!="")

    
    foreach($_POST['y'] as $index2 => $check)
    {
      if(isset($check))
      {
        $IDAmplification = $_POST['IDAmplification'][$check];
        $IDDNA = $_POST['IDDNA'][$check];
        $FragmentTest = $_POST['formFragment'][$check];
        
        $resultPub= "SELECT tablepublications.ID_Publications, " .
              "tablepublications.Editors, " .
              "tablepublications.Year, " .
              "tablepublications.Title, " .
              "tablejournals.Journal_View, " .
              "tablepublications.Volume, " .
              "tablepublications.Pages, " .
              "tablepublications.Book, " .
              "tablepublications.Publisher, " .
              "tablepublications.Link " .
              "FROM tablepublications " .
              "LEFT JOIN tablejournals ON tablepublications.ID_Journal = tablejournals.ID_Journal " .
              "WHERE tablepublications.ID_Publications = '$IDPublication'";

    $sqlPub = mysql_query($resultPub);
    
    
    
    if(mysql_num_rows($sqlPub))
    {
    $gesamt = mysql_num_rows($sqlPub); 
    for ($i=1; $i<=$gesamt; $i++) {
    $row=mysql_fetch_array($sqlPub);
    $publications[] = $row['ID_Publications'];
    foreach ($publications as $Test)
    {
$resultaut = "SELECT tablepublications_authors.ID_Publications, " .
           "tableauthors.Author, " .
           "tablepublications_authors.Order " .
           "FROM  tablepublications_authors, tableauthors " .
           "WHERE tablepublications_authors.ID_Author = tableauthors.ID_Author " .
           "  AND tablepublications_authors.ID_Publications = '$Test' " .
           "ORDER BY tablepublications_authors.Order";
        
    $sqlaut = mysql_query($resultaut);           
while ($roww = mysql_fetch_object($sqlaut))
    {
    
      if(isset($Author[$roww->ID_Publications]))
      {$Author[$roww->ID_Publications] .= ", ";}
      else
      {$Author[$roww->ID_Publications] .= ""; }

      $tmp = $roww->Author;
      $Author[$roww->ID_Publications] .= $tmp; 
      $View = $Author[$roww->ID_Publications]; 
      }}    
    
    
$Paper = $View;
if($row['Editors']) { $Paper .= " ".$row['Editors']; }
$Paper .= " (".$row['Year']."): ".$row['Title'];
if($row['Journal_View']) { $Paper .= " ".$row['Journal_View']; }
if(!$row['Journal_View']) { $Paper .= " IN: ".$row['Book']." ".$row['Publisher']; }
if($row['Volume']) { $Paper .= " ".$row['Volume'].": "; }
if($row['Pages']) { $Paper .= " ".$row['Pages']."."; }

//if($row['Link']) { $Paper .= " ".build_href1($row['Link'],$row['Link']); }
    
     }  }  
    $resultTest = "SELECT ID_Pub_Amp, ID_Publications, ID_Amplification FROM tablepublications_amplifications " .
                  "WHERE ID_Publications = '$IDPublication' " .
                  "AND ID_Amplification = '$IDAmplification'";
           
    $sqlTest = mysql_query($resultTest);
    if(mysql_num_rows($sqlTest))
    { echo "<span class='error'>Die Verbindung zwischen dieser Publikation und dieser Amplifikation wurde bereits gespeichert!</span>";}
    else {
            
        $result1 = "INSERT INTO tablepublications_amplifications (ID_Publications, ID_Amplification, ID_DNA, Paper_Cache, Fragment, Created_Who) VALUES ('$IDPublication','$IDAmplification','$IDDNA','$Paper','$FragmentTest','$log')";
        $sql1 = mysql_query($result1);
        
#######################################Error 1###############################################################

if(!$sql1)
{ $msg = $result1."\n";
  $msg .= "####Error 1####";
  trigger_error($msg, E_USER_ERROR);
  include ("Errorreport.php");   }
  
#############################################################################################################          
        
       } 
      }
   
    }
    

    
    if($sql1)
    { echo "<span class='fine'>Data have been saved!</span>"; }
    
    }
    
    
    
    }
    
    
    
    
    
    
    
    
    
    if($formSubmitCheck)
    {
    if(empty($formSearchTitle) and !isset($formShowAll))
    { echo "<span class='error'>Please enter a Title!</span><br>";}
    if(!empty($formSearchTitle) and isset($formShowAll))
    { echo "<span class='error'>Please neither enter a Title or select the checkbox!</span><br>";}
    
    else {
    $result1= "SELECT tablepublications.ID_Publications, " .
              "tablepublications.Editors, " .
              "tablepublications.Year, " .
              "tablepublications.Title, " .
              "tablejournals.Journal_View, " .
              "tablepublications.Volume, " .
              "tablepublications.Pages, " .
              "tablepublications.Book, " .
              "tablepublications.Publisher, " .
              "tablepublications.Link " .
              "FROM tablepublications " .
              "LEFT JOIN tablejournals ON tablepublications.ID_Journal = tablejournals.ID_Journal " .
              "WHERE tablepublications.Title LIKE '%$formSearchTitle%'";

    $sql1 = mysql_query($result1);
if(!mysql_num_rows($sql1))
    {echo "<span class='error'>No hits found for: ".$_POST['formSearchTitle']."</span><br>";}
    else
    {
    echo "<table width='100%'>";
    echo "<tr><td colspan='2'><hr/></td></tr>";
    echo "<tr><td colspan='2'><h3>Please select a publication</h3></td></tr>";

    $gesamt = mysql_num_rows($sql1); 
    for ($i=1; $i<=$gesamt; $i++) {
    $row=mysql_fetch_array($sql1);
    $publications[] = $row['ID_Publications'];
    foreach ($publications as $Test)
    {
$result2 = "SELECT tablepublications_authors.ID_Publications, " .
           "tableauthors.Author, " .
           "tablepublications_authors.Order " .
           "FROM  tablepublications_authors, tableauthors " .
           "WHERE tablepublications_authors.ID_Author = tableauthors.ID_Author " .
           "  AND tablepublications_authors.ID_Publications = '$Test' " .
           "ORDER BY tablepublications_authors.Order";

$sql2 = mysql_query($result2);           
while ($roww = mysql_fetch_object($sql2))
    {
    
      if(isset($Author[$roww->ID_Publications]))
      {$Author[$roww->ID_Publications] .= ", ";}
      else
      {$Author[$roww->ID_Publications] .= ""; }

      $tmp = $roww->Author;
      $Author[$roww->ID_Publications] .= $tmp; 
      $View = $Author[$roww->ID_Publications]; 
      }}    
    
    
echo "<tr><td><input type='radio' name='x' value='".$i."'><input type='hidden' name='formPublications[".$i."]' value='".$row['ID_Publications']."'></td><td>".$View,
$row['Editors'] ? " ".$row['Editors'] : "",
" (".$row['Year']."): ".$row['Title'],
$row['Journal_View'] ? " ".$row['Journal_View'] : " IN: ".$row['Book']." ".$row['Publisher'],
$row['Volume'] ? " ".$row['Volume'].": " : "",
$row['Pages'] ? " ".$row['Pages']."." : "",
$row['Link'] ? " ".build_href1($row['Link'],$row['Link']) : "";
echo "</td></tr>";
echo "<tr><td colspan='2'><hr /></td></tr>";
    
     }
     echo "</table>";
    }}
    
    if(empty($formDnaBankNumber) and empty($formGenbankNumber))
    { echo "<span class='error'>Please enter a Gen Bank Number or DNA Extraction Number!</span>";}
    
    else {
    
            $DnaArray = explode(";", $formDnaBankNumber);
            while($i = each($DnaArray))
            {
            $Dna = trim($i[1]);
            $string .= 'tablednabanknumbers.Dna_Bank_Number = "'.$Dna.'" OR '; }
            
            $l=strlen ($string);
            $max = $l-4;
            $str = "(".substr($string, 0, $max).")";
            
            $GenBankArray = explode(";", $formGenbankNumber);
            while($i = each($GenBankArray))
            {
            $Gen = trim($i[1]);
            $stringGen .= 'tableamplifications.GenBankNumber = "'.$Gen.'" OR '; }
            
            $l=strlen ($stringGen);
            $maxGen = $l-4;
            $str1 = "(".substr($stringGen, 0, $maxGen).")";
            
                
$result3 = "SELECT tableamplifications.ID_Amplification, tablednabanknumbers.Dna_Bank_Number, " .
           "tableamplifications.GenBankNumber, " .
           "tableamplifications.Amplification_Success, " .
           "tableamplifications.Amplification_Date, " .
           "tableamplifications.Link, " .
           "tableamplifications.ID_DNA, " .
           "tablefragment.Fragment " .
           "FROM tableamplifications
            LEFT JOIN tablednabanknumbers ON tableamplifications.ID_DNA = tablednabanknumbers.ID_DNA
            LEFT JOIN tablefragment ON tableamplifications.ID_Fragment = tablefragment.ID_Fragment
            WHERE ";
           if($formDnaBankNumber != "") {$result3 .= $str; }
           if($formDnaBankNumber != "" and $formGenbankNumber != "")
           {$result3 .= " OR "; } 
           if($formGenbankNumber != "") {$result3 .= $str1; }
           $result3 .= " ORDER BY tablednabanknumbers.Dna_Bank_Number";
$sql3 = mysql_query($result3);    


if(!mysql_num_rows($sql3))
{echo "<b>No Amplifications found</b>";}
else { 
echo "<table width='70%'><tr><td colspan='4'><h3>Please select fragments</h3></td></tr><th>Select</th><th>DNA Extraction No</th><th>GenBank No</th><th>Fragment</th><th>Amplification successful</th><th>Amplification Date</th>";

$gesamt = mysql_num_rows($sql3); 
for ($i=1; $i<=$gesamt; $i++) {
$row=mysql_fetch_array($sql3);

echo "<tr><td align='center'><input type='checkbox' name='y[".$i."]' value='".$i."'><input type='hidden' name='IDAmplification[".$i."]' value='".$row['ID_Amplification']."'><input type='hidden' name='IDDNA[".$i."]' value='".$row['ID_DNA']."'>";
echo "<td align='center'>".$row['Dna_Bank_Number']."</td><td align='center'>".build_href1($row['Link'],$row['GenBankNumber'])."</td><td align='center'>".$row['Fragment']."<input type='hidden' name='formFragment[".$i."]' value='".$row['Fragment']."'></td><td  align='center'>".$row['Amplification_Success']."</td><td align='center'>".DatumsWandler($row['Amplification_Date'])."</td></tr>";
}
echo "<tr><td colspan='4'><input type='submit' name='formSubmitSavePub' id='button' value='Save'></td></tr>";
echo "</table>";

}
    
    
    }
    
    
    
    }
    
    
    ?>
    </td></tr>
    
    
    
    
    
<tr><td colspan="3"><hr /></td></tr>

<tr><td colspan="3"><input type="button" value="Return to Menu" name="formSubmitMenu" id="button" style="float:right" onClick="self.location.href = 'Index-Publications.php';"></td></tr>

</table>
</td>
</tr>
</table>
</form>
</body>
</html>