<?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 = "";
if (isset ( $ufq )) {
	$ufq = $ufq . "&";
} else
	$ufq = "";

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>

<?php
// ++++++++++++++
// 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>';
}
// +++++++++++++++
?>
<div class="jumbotron">
	<h2>
		Browse data <img id="help" alt="help"
			title="help - how does the search work"
			src="<?php echo Yii::$app->request->baseUrl?>/images/icons/help.png" />
	</h2>
</div>

<div class="browser">
Click on the grey butterfly to load the details for a specific record.
<?php
if (! \Yii::$app->user->isGuest) {
	echo "<br/><br/><br/><br/><br/><br/>";
}
?>
<table id='myTableBrowse' class='tablesorter' width='100%'>
		<thead>
			<tr>
				<th></th>
<?php
echo "           <th id='fullScientificName'><a href='?" . $ufq . "sort=" . $sortScientificName . "'>Scientificname</a></th>";
// <!-- <th>Family</th> -->

echo "           <th id='untiID'><a href='?" . $ufq . "sort=" . $sortUnitID . "'>UnitID</th>
                <th id='recordbasis'><a href='?" . $ufq . "sort=" . $sortRecordbasis . "'>Recordbasis</th>
                <th id='institutioncode'><a href='?" . $ufq . "sort=" . $sortInstitutioncode . "'>Institution</th>
                <th id='collectioncode'><a href='?" . $ufq . "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='" . Yii::$app->request->baseUrl . "/images/bulletfly.png' alt='see details'/></td>";
	if(is_array($elt->fullScientificName))
	{
	echo "<td style=\"word-wrap: break-word\"><a href=\"?" . $ufq . "fullScientificName=" . urlencode ( $elt->fullScientificName[0] ) . "\">" . $elt->fullScientificName[0] . "</a></td>"; }
	else {
	echo "<td style=\"word-wrap: break-word\"><a href=\"?" . $ufq . "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=\"?" . $ufq . "unitID=" . urlencode ( $elt->unitID ) . "\">" . $elt->unitID . "</a></td>";
	echo "<td style=\"word-wrap: break-word\"><a href=\"?" . $ufq . "recordbasis=" . urlencode ( $elt->recordbasis ) . "\">" . $elt->recordbasis . "</a></td>";
	echo "<td style=\"word-wrap: break-word\"><a href=\"?" . $ufq . "institutioncode=" . urlencode ( $elt->institutioncode ) . "\">" . $elt->institutioncode . "</a></td>";
	echo "<td style=\"word-wrap: break-word\"><a href=\"?" . $ufq . "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( {
        	"autoWidth": false,
                "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="<?php echo Yii::$app->request->baseUrl?>/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);
          });

        $("#help").on('click', function(e)  {
            window.open('http://wiki.ggbn.org/ggbn/GGBN_Data_Portal_Explanations','_blank');
                });
});
         
        </script>
			<style>
.dataTables_wrapper {
	position: absolute;
	clear: both;
}
</style>