<?php
/*
* list.php
* http://tasuke.dna.affrc.go.jp/
*
* Author: Ryutaro Itoh
*
* Copyright (c) 2013 National Institute of Agrobiological Sciences   
*
* This software is released under the MIT License.
*
* http://opensource.org/licenses/mit-license.php
*/

require '../conf/config.php';

//////////////////////////////////  Connecting with DB
if( !$conn = new mysqli($host, $user, $pswd, $db)) {
    error_log("Connection failed.\n");
    exit;
}

(isset($_POST['prm_ext_acc'])) ? $prm_ext_acc=$_POST['prm_ext_acc'] : returnError("Error");

////////////////////////////////// prepare 
$ids = array();
$dbs = array();
$grp = array();
$key_regist = array();
$str;
$uniq_array;
$x=0;
$y=0;

if ( file_exists( "../conf/order.conf" )) {
    $flag=0;
    $line = file_get_contents("../conf/order.conf");
    $array = explode("\n",$line);
    $uniq_array = array_unique($array);
    $x = count($uniq_array);
    $query = "SELECT accession_id FROM sys_accession where accession_id= ? ";
    $stmt = $conn->stmt_init();
    $stmt->prepare($query);
    for($i=0;$i<$x;$i++){
        $id_test = $uniq_array[$i];
        $stmt->bind_param("s", $id_test); 
        $stmt->execute();
        $stmt->store_result();
        $stmt->bind_result($row0);
        while ($stmt->fetch()) {
            $chk[]=$row0;
            $key_regist[$row0]=1;
            $flag=1;
        }
    }
    if($flag == 1){
        $y=count($chk);
    }else{
        $y=0;
    }
    $stmt->free_result();
    $stmt->close();
}

if(!empty($prm_ext_acc)){
    $flag = 0;
    $chk = array();
    $ext_array = explode(",",$prm_ext_acc);
    $ext_uniq_array = array_unique($ext_array);
    $x = count($ext_uniq_array);
    for($i=0;$i<$x;$i++){
        if(empty($key_regist)){
            array_push($chk,$ext_uniq_array[$i]);
            $flag=1;
        }else{
            if(array_key_exists($ext_uniq_array[$i], $key_regist)){
                array_push($chk,$ext_uniq_array[$i]);
                $flag=1;
            }
        }
    }
    if($flag == 1){
        $y=count($chk);
    }else{
        $y=0;
    }
    $uniq_array = $ext_uniq_array;
}

if ($x>0 and $y>0 ){
    $query = "SELECT accession_id, accession_db, accession_name, variety, sub_variety, origin, origin_2, type FROM sys_accession where accession_id= ? ";
    $stmt = $conn->stmt_init();
    $stmt->prepare($query);
    for($i=0;$i<$x;$i++){
        $ac_id = $uniq_array[$i];
        $stmt->bind_param("s", $ac_id); 
        $stmt->execute();
        $stmt->store_result();
        $stmt->bind_result($row0,$row1,$row2,$row3,$row4,$row5,$row6,$row7);
        while ($stmt->fetch()) {
            $ids[]=$row0;
            $dbs[]=$row1;
            $names[]=$row2;
            $variety[]=$row3;
            $sub_variety[]=$row4;
            $origins[]=$row5;
            $origins_2[]=$row6;
            $type[]=$row7;
        }
    }
    $stmt->free_result();
    $stmt->close();
    $str="[";
    for ($i=0; $i<count($ids); $i++) {
        $str=$str."{\"id\":\"".$ids[$i]."\",\"db\":\"".$dbs[$i]."\",\"name\":\"".$names[$i].
                  "\",\"variety\":\"".$variety[$i]."\",\"grp\":\"".$sub_variety[$i].
                  "\",\"origin\":\"".$origins[$i]."\",\"origin_2\":\"".$origins_2[$i]."\",\"type\":\"".$type[$i]."\"}"; 
        if ( $i<count($ids)-1 ) {
            $str=$str.",";
        }
    }
    $str=$str."]|[";
}else{
    $query = "SELECT accession_id, accession_db, accession_name, variety, sub_variety, origin, origin_2, type FROM sys_accession order by cast(substring(accession_db, 2) as unsigned) asc";
    $stmt = $conn->stmt_init();
    $stmt->prepare($query);
    $stmt->execute();
    $stmt->store_result();
    $stmt->bind_result($row0,$row1,$row2,$row3,$row4,$row5,$row6,$row7);
    while ($stmt->fetch()) {
        $ids[]=$row0;
        $dbs[]=$row1;
        $names[]=$row2;
        $variety[]=$row3;
        $sub_variety[]=$row4;
        $origins[]=$row5;
        $origins_2[]=$row6;
        $type[]=$row7;
    }
    $stmt->free_result();
    $stmt->close();
    $str="[";
    for ($i=0; $i<count($ids); $i++) {
        $str=$str."{\"id\":\"".$ids[$i]."\",\"db\":\"".$dbs[$i]."\",\"name\":\"".$names[$i].
                  "\",\"variety\":\"".$variety[$i]."\",\"grp\":\"".$sub_variety[$i].
                  "\",\"origin\":\"".$origins[$i]."\",\"origin_2\":\"".$origins_2[$i]."\",\"type\":\"".$type[$i]."\"}"; 
        if ( $i<count($ids)-1 ) {
            $str=$str.",";
        }
    }
    $str=$str."]|[";
}

////////////////////////////////////////////////////////// List of tracks
$queryT = "SELECT DISTINCT track FROM sys_track order by seq";
$stmtT = $conn->stmt_init();
$stmtT->prepare($queryT);
$stmtT->execute();
$stmtT->store_result();
$stmtT->bind_result($rowT0);
$tracks = array();

while ($stmtT->fetch()) {
    $tracks[]=$rowT0;
}
$stmtT->free_result();
$stmtT->close();
$queryC = "SELECT count(seq) FROM sys_track WHERE parent=0 AND track= ? ";
$stmtC = $conn->stmt_init();
$stmtC->prepare($queryC);
$queryTY = "SELECT type FROM sys_track WHERE track= ? ";
$stmtTY = $conn->stmt_init();
$stmtTY->prepare($queryTY);
for ($i=0; $i<count($tracks); $i++) {
    $objectType = "";
    $viewType = "general";
    $stmtC->bind_param("s", $tracks[$i]); 
    $stmtC->execute();
    $stmtC->store_result();
    $stmtC->bind_result($rowC);
    $stmtTY->bind_param("s", $tracks[$i]); 
    $stmtTY->execute();
    $stmtTY->store_result();
    $stmtTY->bind_result($rowTY);
    while ($stmtTY->fetch()) {
        $objectType = $rowTY;
    }
    while ($stmtC->fetch()) {
        $color = "#000000";
        if ( $rowC > 0 ) {
          $color = "#000080";
        }
        if ($objectType == "DE") {
            $color = "#FF0080";
            $viewType = "DE";
        }
        $str=$str."{\"track\":\"".$tracks[$i]."\",\"p\":\"1\",\"h\":\"4\",\"c\":\"".$color."\",\"s\":\"".$i."\",\"t\":\"".$viewType."\"}";
        if ( $i<count($tracks)-1 ) {
            $str=$str.",";
        }
    }
}
$stmtC->free_result();
$stmtC->close();
$stmtTY->free_result();
$stmtTY->close();
$str=$str."]|[";

////////////////////////////////////////////////////////// List of Cond
$str=$str."{\"id\":\"".$defaultCond."\"},";
if($result = $conn->query("select condition_id from sys_condition order by table_id asc")) {
    while ($rowCD0 = $result->fetch_row()) {
        $str=$str."{\"id\":\"".$rowCD0[0]."\"},";
    }
    $result->close();
}
$str = substr($str, 0, -1);
$str=$str."]";

echo $str;

////////////////////////////////////////////////////////// Function
function returnError($str){
    echo $str;
}

?>
