<?php
/**
* Getting data from original specimen data provider
* <br/><br/>
* @author Gabriele Droege, DNA Bank Network <contact@dnabank-network.org>
* @version 2.0
* @package Input
* @copyright Copyright © 2011 DNA Bank Network http://www.dnabank-network.org<br>The contents of this file are subject to the Mozilla Public License Version 1.1
* @filesource
* @license http://www.mozilla.org/MPL/ MPL
*/

    /********************************************************************************************
    * increasing time for trying to connect to provider                                         *
    *********************************************************************************************/
    set_time_limit(1000);


    $processor = new XSLTProcessor; 
    $xsl = new DOMDocument;

    /********************************************************************************************
    * loading xstl stylesheet, depending on required schema                                     *
    *********************************************************************************************/
if($ID_Schema == 1) {
 $xsl->load('../input/XSLT/en_ABCD1.2_Summary.xslt');
 }
if($ID_Schema == 2 OR $ID_Schema == 7) {
 $xsl->load('../input/XSLT/en_ABCD2.0_Summary.xslt');
}
if($ID_Schema == 3 OR $ID_Schema == 6) {
 $xsl->load('../input/XSLT/en_DarwinCore_UnitDetail.xslt');
}
if($ID_Schema == 4) {
 $xsl->load('../input/XSLT/en_ABCD2.05_Summary.xslt');
}
if($Protocol != "BIOCASE" and $FK_Provider == "")
{ 
if($SourceUnitID != "") { $SpecimenNumber = $SourceUnitID; } else { $SpecimenNumber = $SpecimenNumber; }
if($SourceInstitutionCode != "") { $InstCode = $SourceInstitutionCode; } else { $InstCode = $InstCode; }
if($SourceCode != "") { $CollCode = $SourceCode; } else {$SourceCode = $SourceCode; }

  include("../input/gbif_occurrences.php");
}

    /********************************************************************************************
    * parsing the provider url and checking availability                                        *
    *********************************************************************************************/
$inf = parse_url($Provider);

 $host = $inf['host'];


 $path = $inf['path'];
 if (isset($inf['query'])) $path .= '?'.$inf['query'];

 if (isset($inf['port'])) $port = $inf['port'];
 else $port = 80;
if($Provider)
{
error_reporting(E_ALL ^ E_WARNING ^ E_NOTICE);
$fp = @fsockopen($host, $port, $errno, $error, 5);
if(!$fp or !$host) { echo "<span class='error'>Connection to provider failed! ";
if($_GET['ID_DNA'] != "") { $ProviderCheck == true; echo "Show data from cache.</span>"; }
else { echo "Please contact your administrator.</span>";  } }
if($fp and $host) {

if(!fopen($Provider,"r")) { echo "<span class='error'>Provider url incorrect! ";
if($_GET['ID_DNA'] != "") { $ProviderCheck == true; echo "Show data from cache.</span>"; }
else { echo "Please contact your administrator.</span>";  } }
if(fopen($Provider,"r")) {

    /********************************************************************************************
    * $searchform != "true" means system currently uses the input form and not the edit form    *
    * $variable5 see line 145                                                                   *
    *********************************************************************************************/

if($searchform != "true" and $variable5 == "" and $_GET['sqlType'] != "Detail") {

if($ID_Schema == 3) 
{ 
    /************************************************************************************************
    * $ID_Schema == 3 means source is a DiGIR provider (see also tableschema                        *
    * DiGIR makes use of DarwinCore data schema which has two elements for a UnitID/CatalogueNumber *
    * CatalogueNumber and CatalogueNumberText, both or only one of them can be filled with content  *
    * if CatalogueNumberText != '' -> use this one; if not use the other                            *
    *************************************************************************************************/

$file1 = $Provider.$QueryPart1.$Digir_Resource1.$QueryPart1_1.$Digir_Source1."%3C%2Fdestination%3E%0A%09%09%3Ctype%3Esearch%3C%2Ftype%3E%0A%09%09%3C%2Fheader%3E%0A%09%09%3Csearch%3E%0A%09%09%3Cfilter%3E+%3Cequals%3E+%3Cdarwin%3ACatalogNumberText%3E".$SpecimenNumber."%3C%2Fdarwin%3ACatalogNumberText%3E+%3C%2Fequals%3E+%3C%2Ffilter%3E%0A%09%09%3Crecords++start%3D%220%22+limit%3D%2210%22%3E%0A%09%09%3Cstructure+schemaLocation%3D%22http%3A%2F%2Fsearch.biocase.org%2Feurope%2Fstatic%2FdarwinObis.xsd%22%2F%3E%0A%09%09%3C%2Frecords%3E%0A%09%09%3Ccount%3Efalse%3C%2Fcount%3E%0A%09%09%3C%2Fsearch%3E%0A%09%09%3C%2Frequest%3E"; 

$file2 = $Provider.$QueryPart1.$Digir_Resource1.$QueryPart1_1.$Digir_Source1."%3C%2Fdestination%3E%0A%09%09%3Ctype%3Esearch%3C%2Ftype%3E%0A%09%09%3C%2Fheader%3E%0A%09%09%3Csearch%3E%0A%09%09%3Cfilter%3E+%3Cequals%3E+%3Cdarwin%3ACatalogNumber%3E".$SpecimenNumber."%3C%2Fdarwin%3ACatalogNumber%3E+%3C%2Fequals%3E+%3C%2Ffilter%3E%0A%09%09%3Crecords++start%3D%220%22+limit%3D%2210%22%3E%0A%09%09%3Cstructure+schemaLocation%3D%22http%3A%2F%2Fsearch.biocase.org%2Feurope%2Fstatic%2FdarwinObis.xsd%22%2F%3E%0A%09%09%3C%2Frecords%3E%0A%09%09%3Ccount%3Efalse%3C%2Fcount%3E%0A%09%09%3C%2Fsearch%3E%0A%09%09%3C%2Frequest%3E";

     
     $xml = new DOMDocument;
     $xml->load($file1);
     $CatNoText = $xml->getElementsByTagName('CatalogNumberText');

     if($CatNoText->item(0)->nodeValue == $SpecimenNumber) {  
      $processor->setParameter('', 'formSpecimenNumber', $formSpecimenNumber);
      $processor->importStyleSheet($xsl);
      $xml1 = new DOMDocument('1.0', 'UTF-8');
      $xml1->load($file1);
      $html = $processor->transformToXML($xml1);
      echo $html; 
 }
     if($CatNoText->item(0)->nodeValue != $SpecimenNumber) { 
     $processor->setParameter('', 'formSpecimenNumber', $formSpecimenNumber);
     $processor->importStyleSheet($xsl);
     $xml2 = new DOMDocument('1.0', 'UTF-8');
     $xml2->load($file2);
     $html = $processor->transformToXML($xml2);
     echo $html;  
     
  }
 }

if($ID_Schema != 3) {
//$file = $Provider."&query=".urlencode($QueryPart1.$formSpecimenNumber.$QueryPart2);
if($SpecimenNumber == "" and $SourceUnitID == "") $SpecimenNumber = "*";
if($SpecimenNumber != "") $SpecimenNumber = $SpecimenNumber;
if($SourceUnitID != "") $SpecimenNumber = $SourceUnitID;
if($SourceInstitutionCode != "") $InstCode = $SourceInstitutionCode;
if($SourceCode != "") $CollCode = $SourceCode;


$filter = '<?xml version="1.0" encoding="UTF-8"?>
<request xmlns="http://www.biocase.org/schemas/protocol/1.3">
<header><type>search</type></header>
<search><requestFormat>http://www.tdwg.org/schemas/abcd/2.06</requestFormat>
<responseFormat start="0" limit="10">http://www.tdwg.org/schemas/abcd/2.06</responseFormat>
<filter>';
if($InstCode != "") { $filter .= "<and>"; }
if($CollCode != "") { $filter .= "<and>"; }
if($Taxon != "") { $filter .= "<and>"; }
if($CollNo != "") { $filter .= "<and>"; }
if($CollYear != "") { $filter .= "<and>"; }

$file = $Provider."&query=".urlencode($filter.'<like path="/DataSets/DataSet/Units/Unit/UnitID">');
$file .= $SpecimenNumber;
$file .= urlencode('</like>');
if($InstCode != "") {
$file .= urlencode('<like path="/DataSets/DataSet/Units/Unit/SourceInstitutionID">'); 
$file .= $InstCode;
$file .= urlencode('</like></and>'); }
if($CollCode != "") { 
$file .= urlencode('<like path="/DataSets/DataSet/Units/Unit/SourceID">'); 
$file .= $CollCode;
$file .= urlencode('</like></and>'); }
if($Taxon != "") {
$file .= urlencode('<like path="/DataSets/DataSet/Units/Unit/Identifications/Identification/Result/TaxonIdentified/ScientificName/FullScientificNameString">');
$file .= $Taxon;
$file .= urlencode('</like></and>'); }
if($CollNo != "") {
$file .= urlencode('<like path="/DataSets/DataSet/Units/Unit/CollectorsFieldNumber">');
$file .= $CollNo;
$file .= urlencode('</like></and>'); }
if($CollYear != "") {
$file .= urlencode('<like path="/DataSets/DataSet/Units/Unit/Gathering/DateTime/DateText">');
$file .= $CollYear;
$file .= urlencode('</like></and>'); }
$file .= urlencode('</filter><count>false</count></search></request>');


//$file = $Provider.$QueryPart1.$Digir_Resource1.$QueryPart1_1.$Digir_Source1.$QueryPart1_2.$SpecimenNumber.$QueryPart2;
//echo $file;
$processor->setParameter('', 'searchform', '');
$processor->setParameter('', 'formSpecimenNumber', $formSpecimenNumber);
$processor->importStyleSheet($xsl);
$xml3 = new DOMDocument('1.0', 'UTF-8');
$xml3->load($file);
$html = $processor->transformToXML($xml3);
echo $html;
  }

} //if($searchform = "true"  and $variable5 == "")     
 
     
 if($searchform != "true" and ($variable5 != "" or $_GET['sqlType'] == "Detail")) {

    /**********************************************************************************************
    * sometimes the user received more than one specimen record (by using *), than he/she has     *
    * to select one; to be sure that during query process the right one is called some further    *
    * help form elements are filled with content and are checked here                             *
    * this parameters are filled during the xslt transformation  (see scripts in folder XSLT)     *
    **********************************************************************************************/

if($ID_Schema == 3 and $CatalogNoText == "True")
{
$file = $Provider.$QueryPart1b.$Digir_Resource1.$QueryPart1_1.$Digir_Source1.$QueryPart1_2b."CatalogNumberText%3E".$SourceUnitID."%3C%2Fdarwin%3ACatalogNumberText".$QueryPart3.$SourceInstitutionCode.$QueryPart4.$SourceCode.$QueryPart5;

} 
if($ID_Schema == 3 and $CatalogNoText != "True")
{
$file = $Provider.$QueryPart1b.$Digir_Resource1.$QueryPart1_1.$Digir_Source1.$QueryPart1_2b."Cata
logNumber%3E".$SourceUnitID."%3C%2Fdarwin%3ACatalogNumber".$QueryPart3.$SourceInstitutionCode.$QueryPart4.$SourceCode.$QueryPart5;
} 

else
{

$file = $Provider."&query=".urlencode('<?xml version="1.0" encoding="UTF-8"?>
<request xmlns="http://www.biocase.org/schemas/protocol/1.3">
<header><type>search</type></header>
<search><requestFormat>http://www.tdwg.org/schemas/abcd/2.06</requestFormat>
<responseFormat start="0" limit="10">http://www.tdwg.org/schemas/abcd/2.06</responseFormat>
<filter><and><and><like path="/DataSets/DataSet/Units/Unit/UnitID">');
$file .= $SourceUnitID;
$file .= urlencode('</like><like path="/DataSets/DataSet/Units/Unit/SourceInstitutionID">');
$file .= $SourceInstitutionCode;
$file .= urlencode('</like></and><like path="/DataSets/DataSet/Units/Unit/SourceID">');
$file .= $SourceCode;
$file .= urlencode('</like></and></filter><count>false</count></search></request>');
 }
//echo $file;
$processor->importStyleSheet($xsl);
$processor->setParameter('', 'searchform', '');
$xml = new DOMDocument('1.0', 'UTF-8');
$xml->load($file);
$html = $processor->transformToXML($xml);
echo $html;  


 } // if($searchform != "true" and $variable5 != "") { 



    /************************************************************************************************
    * $searchform == "true" means system currently uses the edit/search form and not the input form *
    ************************************************************************************************/
    
    
if($searchform == "true")
{
if($ID_Schema == 3 and $CatalogNoText == "True")
{
$file = $Provider.$QueryPart1b.$Digir_Resource1.$QueryPart1_1.$Digir_Source1.$QueryPart1_2b."CatalogNumberText%3E".$SourceUnitID."%3C%2Fdarwin%3ACatalogNumberText".$QueryPart3.$SourceInstitutionCode.$QueryPart4.$SourceCode.$QueryPart5;

} 
if($ID_Schema == 3 and $CatalogNoText != "True")
{
$file = $Provider.$QueryPart1b.$Digir_Resource1.$QueryPart1_1.$Digir_Source1.$QueryPart1_2b."Cata
logNumber%3E".$SourceUnitID."%3C%2Fdarwin%3ACatalogNumber".$QueryPart3.$SourceInstitutionCode.$QueryPart4.$SourceCode.$QueryPart5;

} 

if($ID_Schema == 2)
{
$file = $Provider."&query=".urlencode('<?xml version="1.0" encoding="UTF-8"?>
<request xmlns="http://www.biocase.org/schemas/protocol/1.3">
<header><type>search</type></header>
<search><requestFormat>http://www.tdwg.org/schemas/abcd/2.06</requestFormat>
<responseFormat start="0" limit="10">http://www.tdwg.org/schemas/abcd/2.06</responseFormat>
<filter><and><and><like path="/DataSets/DataSet/Units/Unit/UnitID">');
$file .= $SourceUnitID;
$file .= urlencode('</like><like path="/DataSets/DataSet/Units/Unit/SourceInstitutionID">');
$file .= $SourceInstitutionCode;
$file .= urlencode('</like></and><like path="/DataSets/DataSet/Units/Unit/SourceID">');
$file .= $SourceCode;
$file .= urlencode('</like></and></filter><count>false</count></search></request>'); 

//echo $file;
$processor->importStyleSheet($xsl);
$processor->setParameter('', 'searchform', 'searchform');
$xml3 = new DOMDocument('1.0', 'UTF-8');
$xml3->load($file);
$dom = new DOMDocument();
$dom->Load($file);
include ("Save_ABCD2.06.php");
$html = $processor->transformToXML($xml3);
echo $html;     

}
    
  } //if($searchform == "true")
  
  
  } //if(fopen($Provider,"r"))
      
  }
 }
 
 
 
 if($FK_Provider == "")
{ include("../input/Save_GBIF.php"); }

?>