<?php
/**
* Search, check whether provider data and cached data are the same or if not recommend updating cached data
* @author Gabriele Droege, DNA Bank Network <contact@dnabank-network.org>
* @version 2.0
* @package Search
* @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
*/

$sqlcheckTripleID = "SELECT ID_Cache FROM cachecollection WHERE UnitID_Specimen = '$sqlUnitID' AND CollectionCode_Specimen = '$sqlSourceName' AND InstitutionCode_Specimen = '$sqlSourceInstitutionCode'";
//echo $sqlcheckTripleID;
$resultcheckTripleID = mysqli_query($GLOBALS["___mysqli_ston"], $sqlcheckTripleID);

if(!mysqli_affected_rows($GLOBALS["___mysqli_ston"]))
{ echo "<span class='error'>Specimen record identifiers differ from cached version!</span> Go to <a href='http://wiki.bgbm.org/dnabankwiki/index.php/Search/Edit#Detailed_record_view' target='_blanc'>Wiki</a> for further assistance.";  

$sqlTripleID = "SELECT UnitID_Specimen, CollectionCode_Specimen, InstitutionCode_Specimen, GUID_Specimen FROM cachecollection WHERE ID_Cache = '$ID_Cache'";
$resultTripleID = mysqli_query($GLOBALS["___mysqli_ston"], $sqlTripleID);

while($row = mysqli_fetch_object($resultTripleID))
{  
echo "</td></tr>
<th>Parameter</th><th>Cache Data</th><th>Current Provider Data</th>";
echo "<tr><td>UnitID</td><td>";
if ($row->UnitID_Specimen != $sqlUnitID)  { echo "<span class='error'>".$row->UnitID_Specimen."</span></td><td><span class='error'>".$sqlUnitID."</span>"; } 
else { echo $row->UnitID_Specimen."</td><td>".$sqlUnitID; } echo "</td></tr>";
echo "<tr><td>Collection Code</td><td>";
if ($row->CollectionCode_Specimen != $sqlSoureName)  { echo "<span class='error'>".$row->CollectionCode_Specimen."</span></td><td><span class='error'>".$sqlSourceName."</span>"; } 
else { echo $row->CollectionCode_Specimen."</td><td>".$sqlSourceName; } echo "</td></tr>";
echo "<tr><td>Institution Code</td><td>";
if ($row->InstitutionCode_Specimen != $sqlSoureInstitutionCode)  { echo "<span class='error'>".$row->InstitutionCode_Specimen."</span></td><td><span class='error'>".$sqlSourceInstitutionCode."</span>"; } 
else { echo $row->InstitutionCode_Specimen."</td><td>".$sqlSourceInstitutionCode; } echo "</td></tr>";
echo "<tr><td>Unit GUID</td><td>";
if ($row->GUID_Specimen != $sqlUnitGUID)  { echo "<span class='error'>".$row->GUID_Specimen."</span></td><td><span class='error'>".$sqlUnitGUID."</span>"; } 
else { echo $row->GUID_Specimen."</td><td>".$sqlUnitGUID; } echo "</td></tr><tr><td>";
 } 
} // if(!mysql_affected_rows($resultcheckTripleID))
 
else {   // if(mysql_affected_rows($resultcheckTripleID)) = Check for Triple ID was fine

if($sqlTypeStatus == '') { $sqlTypeStatus2 = 'IS NULL'; } else {$sqlTypeStatus2 = "= '".$sqlTypeStatus."'"; }

$sqlcheckCollectingEvent = "SELECT * FROM cachecollection WHERE (CountryName = '$sqlCountryName' OR CountryISO2 = '$sqlISO2') AND Collectors = '$sqlPersonName' 
AND (CollectionDate = '$sqlCollectionDate' OR CollectionDateMin = '$sqlCollectionDateMin' OR CollectionDateMax = '$sqlCollectionDateMax') AND CollectorsNo = '$sqlCollectorsNo' 
AND Locality = '$sqlLocality' AND Altitude = '$sqlAltitude' AND Longitude = '$sqlLongitude' AND Latitude = '$sqlLatitude' AND TypeStatus = '$sqlTypeStatus2' AND ID_Cache = '$ID_Cache'";

$resultcheckCollectingEvent = mysqli_query($GLOBALS["___mysqli_ston"], $sqlcheckCollectingEvent);

if(mysqli_affected_rows($GLOBALS["___mysqli_ston"]))
{ echo "<span class='error'>Specimen record collecting event data or type status differ from cached version! Do you want to update the cached specimen record?</span>";  
		if(mysqli_num_rows($test) > 0)  // information coming from query.php line 2261
	{  echo "<br/><span class='error'>This specimen record is related to ".mysqli_num_rows($test)." other DNA samples.</span> (see below for list of samples)"; }
	
$sqlCollectingEvent = "SELECT * FROM cachecollection WHERE ID_Cache = '$ID_Cache'";
$resultCollectingEvent = mysqli_query($GLOBALS["___mysqli_ston"], $sqlCollectingEvent);

while($row = mysqli_fetch_object($resultCollectingEvent))
{
echo "</td></tr>
<th>Parameter</th><th>Cache Data</th><th>Current Provider Data</th>";
echo "<tr><td>Country Name</td><td>";
if ($row->CountryName != $sqlCountryName)  { echo "<span class='error'>".$row->CountryName."</span></td><td><span class='error'>".$sqlCountryName."</span>"; } 
else { echo $row->CountryName."</td><td>".$sqlCountryName; } echo "</td></tr>";
echo "<tr><td>Country ISO Code</td><td>";
if ($row->CountryISO2 != $sqlISO2)  { echo "<span class='error'>".$row->CountryISO2."</span></td><td><span class='error'>".$sqlISO2."</span>"; } 
else { echo $row->CountryISO2."</td><td>".$sqlISO2; } echo "</td></tr>";
echo "<tr><td>Collectors</td><td>";
if ($row->Collectors != $sqlPersonName)  { echo "<span class='error'>".$row->Collectors."</span></td><td><span class='error'>".$sqlPersonName."</span>"; } 
else { echo $row->Collectors."</td><td>".$sqlPersonName; } echo "</td></tr>";
echo "<tr><td>Collector's Number</td><td>";
if ($row->CollectorsNo != $sqlCollectorsNo)  { echo "<span class='error'>".$row->CollectorsNo."</span></td><td><span class='error'>".$sqlCollectorsNo."</span>"; } 
else { echo $row->CollectorsNo."</td><td>".$sqlCollectorsNo; } echo "</td></tr>";
echo "<tr><td>Collection Date</td><td>";
if ($row->CollectionDate != $sqlCollectionDate)  { echo "<span class='error'>".$row->CollectionDate."</span></td><td><span class='error'>".$sqlCollectionDate."</span>"; } 
else { echo $row->CollectionDate."</td><td>".$sqlCollectionDate; } echo "</td></tr>";
echo "<tr><td>Collection Date Min</td><td>";
if ($row->CollectionDateMin != $sqlCollectionDateMin)  { echo "<span class='error'>".$row->CollectionDateMin."</span></td><td><span class='error'>".$sqlCollectionDateMin."</span>"; } 
else { echo $row->CollectionDateMin."</td><td>".$sqlCollectionDateMin; } echo "</td></tr>";
echo "<tr><td>Collection Date Max</td><td>";
if ($row->CollectionDateMax != $sqlCollectionDateMax)  { echo "<span class='error'>".$row->CollectionDateMax."</span></td><td><span class='error'>".$sqlCollectionDateMax."</span>"; } 
else { echo $row->CollectionDateMax."</td><td>".$sqlCollectionDateMax; } echo "</td></tr>";
echo "<tr><td>Locality</td><td>";
if ($row->Locality != $sqlLocality)  { echo "<span class='error'>".$row->Locality."</span></td><td><span class='error'>".$sqlLocality."</span>"; } 
else { echo $row->Locality."</td><td>".$sqlLocality; } echo "</td></tr>";
echo "<tr><td>Altitude</td><td>";
if ($row->Altitude != $sqlAltitude)  { echo "<span class='error'>".$row->Altitude."</span></td><td><span class='error'>".$sqlAltitude."</span>"; } 
else { echo $row->Altitude."</td><td>".$sqlAltitude; } echo "</td></tr>";
echo "<tr><td>Longitude | Latitude</td><td>";
if (($row->Longitude != $sqlLongitude) or ($row->Latitude != $sqlLatitude))  { echo "<span class='error'>".$row->Longitude." | ".$row->Latitude."</span></td><td>
<span class='error'>".$sqlLongitude." | ".$sqlLatitude."</span>"; } 
else { echo $row->Longitude." | ".$row->Latitude."</td><td>".$sqlLongitude." | ".$sqlLatitude; } echo "</td></tr>";
echo "<tr><td>Type Status</td><td>";
if ($row->TypeStatus != $sqlTypeStatus)  { echo "<span class='error'>".$row->TypeStatus."</span></td><td><span class='error'>".$sqlTypeStatus."</span>"; } 
else { echo $row->TypeStatus."</td><td>".$sqlTypeStatus; } echo "</td></tr><tr><td>";
 } 

} // if(!mysql_affected_rows($resultcheckCollectingEvent))

else { // if(mysql_affected_rows($resultcheckCollectingEvent)) = Check for Collecting Data and Type Status was fine

if($sqlNameAuthorYearString != "") {
foreach ($sqlNameAuthorYearString as $index => $NameAuthorYear)
{  $PreferredFlag = $sqlPreferredIdentificationFlag[$index];
   $Genus = $sqlGenus[$index];
   $Subgenus = $sqlSubgenus[$index];
   $FirstEpithet = $sqlFirstEpithet[$index];
   $SecondEpithet = $sqlSecondEpithet[$index];
   
   if($PreferredFlag == '' and $PreferredFlag != '0') { $PreferredFlag = '1'; }
   
   if($sqlPreferredIdentificationFlag[$index] == '1' or $sqlPreferredIdentificationFlag[$index] == 'true') {
   
   $sqlCheckIdentification = "SELECT * FROM cachetaxonidentified WHERE Genus = '$Genus' AND Subgenus = '$Subgenus' AND FirstEpithet = '$FirstEpithet' AND 
   SecondEpithet = '$SecondEpithet' AND NameAuthorYear = '$NameAuthorYear' AND (PreferredFlag = '1' OR PreferredFlag = 'true') AND FK_Cache = '$ID_Cache'";
   $resultCheckIdentification = mysqli_query($GLOBALS["___mysqli_ston"], $sqlCheckIdentification);
   
	if(!mysqli_affected_rows($GLOBALS["___mysqli_ston"]))
	{ echo "<span class='error'>Preferred identification data differ from cached version! Do you want to update the cached specimen record?</span> 
			<input type='submit' name='formSubmitNewRecord' value='Update Record' title='Update cached specimen record with original provider data.'>";  
			if(mysqli_num_rows($test) > 0)  // information coming from query.php line 2261
	{  echo "<br/><span class='error'>This specimen record is related to ".mysqli_num_rows($test)." other DNA samples.</span>  (see below for list of samples)"; }
  
	$sqlIdentification = "SELECT * FROM cachetaxonidentified WHERE FK_Cache = '$ID_Cache' AND (PreferredFlag = '1' OR PreferredFlag = 'true')";
	$resultIdentification = mysqli_query($GLOBALS["___mysqli_ston"], $sqlIdentification);

	while($row = mysqli_fetch_object($resultIdentification))
	{
	echo "</td></tr>";
	if ($PreferredFlag = '1' or $PreferredFlag = 'true') {
	echo "<th>Parameter</th><th>Cache Data</th><th>Current Provider Data</th>";
	echo "<tr><td>Name Author Year</td><td>";
	if($row->NameAuthorYear != $NameAuthorYear) { echo "<span class='error'>".$row->NameAuthorYear."</span></td><td><span class='error'>".$NameAuthorYear."</span>"; } 
	else {echo $row->NameAuthorYear."</td><td>".$NameAuthorYear; } echo "</td></tr>";
	echo "<tr><td>Genus</td><td>";
	if($row->Genus != $Genus) { echo "<span class='error'>".$row->Genus."</span></td><td><span class='error'>".$Genus."</span>"; } 
	else { echo $row->Genus."</td><td>".$Genus; } echo "</td></tr>";
	echo "<tr><td>Subgenus</td><td>";
	if($row->Subgenus != $Genus) { echo "<span class='error'>".$row->Subgenus."</span></td><td><span class='error'>".$Subgenus."</span>"; } 
	else { echo $row->Subgenus."</td><td>".$Subgenus; } echo "</td></tr>";
	echo "<tr><td>First Epithet</td><td>";
	if($row->FirstEpithet != $FirstEpithet) { echo "<span class='error'>".$row->FirstEpithet."</span></td><td><span class='error'>".$FirstEpithet."</span>"; } 
	else { echo $row->FirstEpithet."</td><td>".$FirstEpithet; } echo "</td></tr>";
	echo "<tr><td>Second Epithet</td><td>";
	if($row->SecondEpithet != $SecondEpithet) { echo "<span class='error'>".$row->SecondEpithet."</span></td><td><span class='error'>".$SecondEpithet."</span>"; } 
	else { echo $row->SecondEpithet."</td><td>".$SecondEpithet; } echo "</td></tr>";
	echo "<tr><td>Preferred?</td><td>";
	if($row->PreferredFlag != $PreferredFlag) { echo "<span class='error'>".$row->PreferredFlag."</span></td><td><span class='error'>".$PreferredFlag."</span>"; } 
	else { echo $row->PreferredFlag."</td><td>".$PreferredFlag; } echo "</td></tr><tr><td>";
		} // if ($PreferredFlag = '1' or $PreferredFlag = 'true')
		} // while($row = mysql_fetch_object($resultIdentification))
		
	} //if(!mysql_affected_rows($resultCheckIdentification))

else { //if(mysql_affected_rows($resultCheckIdentification))

if(isset($sqlFileURI)) {
foreach($sqlFileURI as $FileURI)
{ $sqlCheckMultimedia = "SELECT * FROM cachemultimedia WHERE Multimedia = '$FileURI' AND FK_Cache = '$ID_Cache'";
  $resultCheckMultimedia = mysqli_query($GLOBALS["___mysqli_ston"], $sqlCheckMultimedia);
} }// foreach($sqlFileURI as $FileURI)
  if(!mysqli_affected_rows($GLOBALS["___mysqli_ston"]))
 { echo "<span class='error'>Multimedia urls differ from cached version! Do you want to update the cached specimen record?</span> 
			<input type='submit' name='formSubmitNewRecord' value='Update Record' title='Update cached specimen record with original provider data.'>";  
			if(mysqli_num_rows($test) > 0)  // information coming from query.php line 2261
			{  echo "<br/><span class='error'>This specimen record is related to ".mysqli_num_rows($test)." other DNA samples.</span>  (see below for list of samples)"; }
			
	$sqlMultimedia = "SELECT * FROM cachemultimedia WHERE FK_Cache = '$ID_Cache'";
	$resultMultimedia = mysqli_query($GLOBALS["___mysqli_ston"], $sqlMultimedia);
	echo "</td></tr>";
	echo "<th>Parameter</th><th>Cache Data</th>";
	while($row = mysqli_fetch_object($resultMultimedia))
	{ 
	echo "<tr><td>File URI</td>";
	echo "<td><span class='error'><a href='".$row->Multimedia."'>".$row->Multimedia."</a></span></td></tr><tr><td>";
		}
  } // if(!mysql_affected_rows())
  
  else {
  
  echo "<span class='fine'>Checked successfully: Original provider data are in accordance with cached data, updating not required.</span>";
  }




 }	//if(mysql_affected_rows($resultCheckIdentification)) = Check for Identification Data was fine
 
 } // if($sqlPreferredIdentificationFlag[$index] == '1' or $sqlPreferredIdentificationFlag[$index] == 'true')
 
   } // foreach ($sqlNameAuthorYearString as $NameAuthorYears => $index)
} //if($sqlNameAuthorYearString != "")
else {

   $sqlCheckIdentification = "SELECT * FROM cachetaxonidentified WHERE Genus = '$Genus1' AND Subgenus = '$Subgenus1' AND FirstEpithet = '$FirstEpithet1' AND 
   SecondEpithet = '$SecondEpithet1' AND NameAuthorYear = '$NameAuthorYear1' AND (PreferredFlag = '1' OR PreferredFlag = 'true') AND FK_Cache = '$ID_Cache'";
   //echo $sqlCheckIdentification;
   $resultCheckIdentification = mysqli_query($GLOBALS["___mysqli_ston"], $sqlCheckIdentification);
   
	if(mysqli_affected_rows($GLOBALS["___mysqli_ston"]))
	{ echo "<span class='error'>Preferred identification data differ from cached version! Do you want to update the cached specimen record?</span> 
			<input type='submit' name='formSubmitNewRecord' value='Update Record' title='Update cached specimen record with original provider data.'>";  
			if(mysqli_num_rows($test) > 0)  // information coming from query.php line 2261
	{  echo "<br/><span class='error'>This specimen record is related to ".mysqli_num_rows($test)." other DNA samples.</span>  (see below for list of samples)"; }
  
	$sqlIdentification = "SELECT * FROM cachetaxonidentified WHERE FK_Cache = '$ID_Cache' AND (PreferredFlag = '1' OR PreferredFlag = 'true')";
	$resultIdentification = mysqli_query($GLOBALS["___mysqli_ston"], $sqlIdentification);

	while($row = mysqli_fetch_object($resultIdentification))
	{
	echo "</td></tr>";
	if ($PreferredFlag = '1' or $PreferredFlag = 'true') {
	echo "<th>Parameter</th><th>Cache Data</th><th>Current Provider Data</th>";
	echo "<tr><td>Name Author Year</td><td>";
	if($row->NameAuthorYear != $NameAuthorYear1) { echo "<span class='error'>".$row->NameAuthorYear."</span></td><td><span class='error'>".$NameAuthorYear1."</span>"; } 
	else {echo $row->NameAuthorYear."</td><td>".$NameAuthorYear1; } echo "</td></tr>";
	echo "<tr><td>Genus</td><td>";
	if($row->Genus != $Genus1) { echo "<span class='error'>".$row->Genus."</span></td><td><span class='error'>".$Genus1."</span>"; } 
	else { echo $row->Genus."</td><td>".$Genus1; } echo "</td></tr>";
	echo "<tr><td>Subgenus</td><td>";
	if($row->Subgenus != $Genus1) { echo "<span class='error'>".$row->Subgenus."</span></td><td><span class='error'>".$Subgenus1."</span>"; } 
	else { echo $row->Subgenus."</td><td>".$Subgenus1; } echo "</td></tr>";
	echo "<tr><td>First Epithet</td><td>";
	if($row->FirstEpithet != $FirstEpithet1) { echo "<span class='error'>".$row->FirstEpithet."</span></td><td><span class='error'>".$FirstEpithet1."</span>"; } 
	else { echo $row->FirstEpithet."</td><td>".$FirstEpithet1; } echo "</td></tr>";
	echo "<tr><td>Second Epithet</td><td>";
	if($row->SecondEpithet != $SecondEpithet1) { echo "<span class='error'>".$row->SecondEpithet."</span></td><td><span class='error'>".$SecondEpithet1."</span>"; } 
	else { echo $row->SecondEpithet."</td><td>".$SecondEpithet1; } echo "</td></tr>";
	echo "<tr><td>Preferred?</td><td>";
	if($row->PreferredFlag != $PreferredFlag1) { echo "<span class='error'>".$row->PreferredFlag."</span></td><td><span class='error'>".$PreferredFlag1."</span>"; } 
	else { echo $row->PreferredFlag."</td><td>".$PreferredFlag1; } echo "</td></tr><tr><td>";
		} // if ($PreferredFlag = '1' or $PreferredFlag = 'true')
		} // while($row = mysql_fetch_object($resultIdentification))
		
}	//if(mysql_affected_rows($resultCheckIdentification)) = Check for Identification Data was fine		

else { //if(mysql_affected_rows($resultCheckIdentification))
 echo "<span class='fine'>Checked successfully: GBIF provider data are in accordance with cached data, updating not required.</span>";
  }
}


}  // if(mysql_affected_rows($resultcheckCollectingEvent)) = Check for Collecting Data was fine


} // if(mysql_affected_rows($resultcheckTripleID)) = Check for Triple ID was fine

?>