<?php
/**
 * this view generates fields for browsing all records
 * <br/><br/>
 * @author Gabriele Droege, Patricia Kelbert (BGBM, GGBN/DNA Bank Network) <contact@dnabank-network.org>
 * @version yii 2.0.3
 * @package View
 * @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
 */
use yii\helpers\Html;
use yii\widgets\LinkPager;
use app\controllers\SearchController;

use yii\helpers\Url;
use yii\web\View;
use yii\widgets\Pjax;

$this->title = 'GGBN Portal';

if(isset($fq)){
	$fq=$fq."&";
}
else
	$fq="";

echo '<link href="/ggbn_new/css/jquery.dataTables.css" rel="stylesheet">';

echo '<div class="left-column">';
foreach ($facets as $fn=>$f ){
	echo $fn."<br/>";
	echo $f."<br/>";
}
echo "<br/>Showing records ".$recordsFrom." to ".$recordsTo." (total ".$recordsTotal.")<br/>";
echo "Jump to page <input type='text' id='jumper' value='".($page+1)."'/><span id='jump'>GO!</span>";
?>
</div>
<div class="jumbotron"><h2>Browse data</h2></div>
echo'</div>';

//++++++++++++++
//Added: 27.07.15 by Anne Hartebrodt: Save Search button and Subscribe button for logged-in Users
if(!\Yii::$app->user->isGuest){
echo '<div class="right-column">';  
//Note: Pjax timeout was set to 10000 because of localhost's slow response: Might be reduced when migration to actual webserver.
 Pjax::begin([ 'enablePushState'=>false]); 
 	Yii::$app->session['search']=Yii::$app->request->url;
 	Yii::$app->session['records']=$pages->totalCount;
	?>
 	<div class="upper">
 	<p>Subscribe to this search and get notified by email if new records are added to the database</p>
 	<?= Html::a("Subscribe", ['account/subscribe'], ['class' => 'btn btn-primary','data-pjax'=>'w0','timeout'=>'100000']) ?>
 	<p></p>
 	</div>
 	<div class="lower">
 	<p>Save our search parameters without subscription</p>
    <?= Html::a("Save search", ['account/save'], ['class' => 'btn btn-primary','data-pjax'=>'w0','timeout'=>'100000']) ?>
    </div>
    <?php Pjax::end(); ?>
<?php 
echo '</div>';
}
else{
echo	'<div class="right-column">Log in to save your search parameters!</div>';
}
//+++++++++++++++

echo '<div class="jumbotron"><h2>Browse data</h2></div> <div class="browser">';

<div class="browser">
Click on the grey butterfly to load the details for a specific record.

<table id='myTableBrowse'  class='tablesorter' width='100%'>
<thead>
<tr>
		<th></th>
<?php 
echo"		<th id='fullScientificName'><a href='?".$fq."sort=".$sortScientificName."'>Scientificname</a></th>";
//<!-- <th>Family</th> --> 	

echo"		<th id='untiID'><a href='?".$fq."sort=".$sortUnitID."'>UnitID</th>
		<th id='recordbasis'><a href='?".$fq."sort=".$sortRecordbasis."'>Recordbasis</th>
		<th id='institutioncode'><a href='?".$fq."sort=".$sortInstitutioncode."'>Institution</th>
		<th id='collectioncode'><a href='?".$fq."sort=".$sortCollectioncode."'>Collection</th>";
?>
</tr></thead>



<tbody>
<?php 
foreach ( $doc as $elt ) {
	echo "<tr>";
	$tid=$lowestDoc[$elt->tripleidstoreid];
	$title="Get the details for this unit";
	if($tid[0]!=$elt->unitID){
		$title="Get the details for the corresponding DNA or tissue if available";
	}
	$url=Url::to ( array_merge ( [ 'search/record'], ["unitID"=>$tid[0],"collectioncode"=>$tid[1],"institutioncode"=>$tid[2]] ) );
		echo "<td><a title='".$title."' href='" . $url . "'><img src='/ggbn_new/images/bulletfly.png' alt='see details'/></td>";
		echo "<td style=\"word-wrap: break-word\"><a href=\"?".$fq."fullScientificName=".urlencode($elt->fullScientificName)."\">".$elt->fullScientificName."</a></td>";
// 		echo "<td><a href='?".$fq."familyName=".urlencode($elt->familyName)."'>".$elt->familyName."</a></td>";
		echo "<td style=\"word-wrap: break-word\"><a href=\"?".$fq."unitID=".urlencode($elt->unitID)."\">".$elt->unitID."</a></td>";
		echo "<td style=\"word-wrap: break-word\"><a href=\"?".$fq."recordbasis=".urlencode($elt->recordbasis)."\">".$elt->recordbasis."</a></td>";
		echo "<td style=\"word-wrap: break-word\"><a href=\"?".$fq."institutioncode=".urlencode($elt->institutioncode)."\">".$elt->institutioncode."</a></td>";
		echo "<td style=\"word-wrap: break-word\"><a href=\"?".$fq."collectioncode=".urlencode($elt->collectioncode)."\">".$elt->collectioncode."</a></td>";
	echo "</tr>";
}
echo"</tbody></table>";

echo "<br/>";

echo "</div>";

echo "<div class='centerDiv'>";
echo LinkPager::widget([
		'pagination' => $pages,
]);
echo"</div>";

?>
<script>
$(document).ready(function() {
	$('#myTableBrowse').DataTable( {
		"oLanguage": {"sSearch": "Filter records:"},
		 paging:false,
		 bSort:false,
		 bInfo : false,
		 "columns": [
		             { "width": "12" },
		            null,null,null,null,null
		           ]
	} );
	$('.browser').css('height',$('#myTableBrowse').height()+100);

	$('.sel').change(function() {
	    $(location).attr('href',$(this).val());
	  });

	$('#jump').click(function() {
	    var pagetogo=$('#jumper').val();
	    try{
		    var sorting=$(location).attr('search').split("sort=")[1].split("&")[0];
	    }catch(e){
		    var sorting=null;
		    }
	    var url="/ggbn_new/search/browse?page="+pagetogo+"&per-page=150";
	    if(sorting!=null)
		    url+="&sort="+sorting;
	    if (parseInt(Number(pagetogo)) == pagetogo && !isNaN(parseInt(pagetogo, 10)))
	    	$(location).attr('href',url);
	  });
});
	 
	</script>
	<style>
	.dataTables_wrapper {
  position: absolute;
  clear: both;
  }</style>