<?php
/*
* list.php
* http://tasuke.dna.affrc.go.jp/
*
* Author: Ryutaro Itoh
*
* Copyright (c) 2013 National Institute of Agrobiological Sciences
*
*/

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

  if( !$conn = new mysqli($host, $user, $pswd, $db)) {
    error_log("Connection failed.\n");
    exit;
  }
  
  (isset($_POST['prm_phenotype'])) ? $prm_phenotype=$_POST['prm_phenotype'] : returnError("Error");
  (isset($_POST['prm_effgrps'])) ? $prm_effgrps = $_POST['prm_effgrps'] : returnError("Error");
  (isset($_POST['start']) && is_numeric($_POST['start'])) ? $prm_start_entry_num = $_POST['start'] : returnError("Error");
  (isset($_POST['length']) && is_numeric($_POST['length'])) ? $prm_length = $_POST['length'] : returnError("Error");
  (isset($_POST['draw']) && is_numeric($_POST['draw'])) ? $prm_draw = $_POST['draw'] : returnError("Error");

  $stmt = $conn->stmt_init();
  
  $where_effgrp = "";
  if(count($prm_effgrps) > 0){
  	$where_effgrp = array(" AND eff_grp IN (?");
  	$where_effgrp = array_merge($where_effgrp, array_fill(0, count($prm_effgrps) - 1, ", ?"));
  	array_push($where_effgrp, ")");
  	$where_effgrp = implode($where_effgrp);
  }
  $query = "SELECT count(*) FROM sys_phenotype_gene_eff a, sys_phenotype b WHERE b.phenotype = ? AND a.phenotype_id = b.phenotype_id" . $where_effgrp;
  $stmt->prepare($query);
  $bind_param_array = array("s" . str_repeat("i", count($prm_effgrps)));
  $bind_param_array_2 = array_merge(array($prm_phenotype), $prm_effgrps);
  for($i = 0; $i < count($bind_param_array_2); $i++){
  	$bind_param_array[] = &$bind_param_array_2[$i];
  }
  call_user_func_array(array($stmt, 'bind_param'), $bind_param_array); 
  $stmt->execute();
  $stmt->store_result();
  $records_total = 0;
  $stmt->bind_result($records_total);
  $stmt->fetch();
  $query = "SELECT accession_name, chr, pos, prob, gene_id, name, note, eff_name, eff_grp FROM sys_phenotype_gene_eff a, sys_phenotype b WHERE b.phenotype = ? AND a.phenotype_id = b.phenotype_id" . $where_effgrp . " ORDER BY eff_grp DESC, prob DESC LIMIT ?, ?";
  $stmt->prepare($query);
  $bind_param_array = array("s" . str_repeat("i", count($prm_effgrps)) . "ii");
  $bind_param_array_2 = array_merge(array($prm_phenotype), $prm_effgrps, array($prm_start_entry_num, $prm_length));
  for($i = 0; $i < count($bind_param_array_2); $i++){
  	$bind_param_array[] = &$bind_param_array_2[$i];
  }
  call_user_func_array(array($stmt, 'bind_param'), $bind_param_array);  
  $stmt->execute();
  $stmt->store_result();
  $stmt->bind_result($row0, $row1, $row2, $row3, $row4, $row5, $row6, $row7, $row8);
  $data_array = array();
  while ($stmt->fetch()) {
    //array_push($data_array, array($row0, $row1, $row2, $row3, $row4, $row5, $row6, $row7, $row8));
    array_push($data_array, array($row0, "<span onclick=\"_drawPlot($row1);\">$row1</span>", $row2, $row3, $row4, $row5, $row6, $row7, $row8));
  }
  
  echo json_encode(
  		array(
  				"draw" => (int)$prm_draw,
  				"recordsTotal" => $records_total,
  				"recordsFiltered" => $records_total,
  				"data" => $data_array,
  		)
  );
?>
