<?php


// parameter

$patid = $_GET['patid'];
$size = $_GET['size'];
$browser = $_GET['browser'];
$scwidth = $_GET['scwidth'];
$scheight = $_GET['scheight'];
$userid = $_GET['userid'];
$permission = $_GET['permission'];

$studyUID = $_GET['studyUID'];



?>

<!doctype html public "-//w3c//dtd html 3.2//ja">
<html>
<head>

<title>make dicom packed file</title>
<meta http-equiv=content-type content="text/html; charset=x-euc-jp">
</head>
<body BACKGROUND="./yellow.jpg">

<?php

//
//	2004.6.14
//	DICOM to WWW Gateway :PHP script
//	Made by Norya Yokohama % Medical Division @ WERC 
//	make_dicom_pack.php
//


  include "init.inc";
  include "param_setting.ini";
  include "sql_generator.ini";
  include "file_mod.inc";
  $include_path=".";
  require("$include_path/dbconnect.ini");


//DB query

$db_handle = db_contact_start($db_type,$target_host,$target_db);

//patient query

$sql = sql_generator("show_study_seriesUID");
$rtn_seriesUID = $db_handle->prepare($sql);

print("$studyUID $sql");

	//get series with studyUID
	$rtn_seriesUID->execute(trim($studyUID));
	
	$study_count = $rtn_seriesUID->rowCount();
	print("$study_count");
	
	while ($seriesUID_row = $rtn_seriesUID->fetch(PDO::FETCH_ASSOC)) {
	
	
	$seriesUID = $seriesUID_row['seriesuid'] ;
	$sernum = $seriesUID_row['sernum'] ;
	$series_mod = $seriesUID_row['mod'] ;
	$char = "0";


	$name = $patname;




if(!$top){
$top=50;
$left=550;
}

  $trimed_mod = trim($mod);
//  $trimed_serdes = trim($serdes);
//  $trimed_studes = trim($studes);
//  $ruid = "$date"."$trimed_mod"."$stutim";
$ruid = $studyUID ;






if($winnum){
	$winnum = $winnum + 1;
}
else{
	$winnum = 1;
}

$win = "win"."$winnum";
//print("$win<br>");

if(!$winarray){
	$winarray = array();
	$winarray[] = $win;	
}
else{
	$winarray[] = $win;
}





$rtn_sumnail->execute(array($seriesUID));
	while ($sumnail_row = $rtn_sumnail->fetch(PDO::FETCH_ASSOC)) {
	$path = $sumnail_row['path'] ;
	}
	
$path = $file_root_path . $path;







	

// touch dir prepare procedure
$patid=trim($patid);
$date=trim($date);
$date=str_replace(" ", "",$date);
$studyUID=trim($studyUID);
$seriesUID=trim($seriesUID);	
if(!$wl){
@$target_dir=touch_dir($patid,$date,$studyUID,$seriesUID,"default");
}else{
$wlww_con = $wl . $ww;
@$target_dir=touch_dir($patid,$date,$studyUID,$seriesUID,$wlww_con);
}
$target_path=($target_dir . "/" );
$file_name=basename($path);//print("path: $path\n");
$target_file_path=$target_path . $file_name . ".dcm";




//Symbolic link original file to target files.

$jpg_file_name = $target_file_path . "-sum_w.jpg";
if(!file_exists($jpg_file_name)){

	if(is_link($target_file_path)){
	if($path != linkinfo($target_file_path)){
	system("rm -f $target_file_path");
	symlink($path,$target_file_path);
	}
	}
	else{
	symlink($path,$target_file_path);
	}

	@$wl_from_dicomtag = get_dicom_value("0028,1050",$path);
	@$ww_from_dicomtag = get_dicom_value("0028,1051",$path);
	@$patposition_from_dicomtag = get_dicom_value("0018,5100",$path);



	$wl_from_dicomtag = trim(floor($wl_from_dicomtag));
	$ww_from_dicomtag = trim(floor($ww_from_dicomtag));
	$patposition_from_dicomtag = trim(floor($patposition_from_dicomtag));
	
	if($wl_from_dicomtag){
	$wl = $wl_from_dicomtag;
	$ww = $ww_from_dicomtag;
	}

if($wl){
$jpg_file_name=dcmtojpg_sum1_w($target_file_path,$wl,$ww);
}else{
$jpg_file_name=dcmtojpg_sum1($target_file_path);
}
}

print("<img src=$jpg_file_name width=64>");
//end sumnail images 

array_push($study_sumnail_array, $jpg_file_name);

print("$studes" );

print("<A HREF=\"javascript:void(0);\" onclick=\"list.push(window.open('show_image.php?requestType=WADO&studyUID=$studyUID&seriesUID=$seriesUID&wl=$wl&ww=$ww','$seriesUID','toolbar=no,location=no,top=$top,left=$left,directories=no,status=no,menu=no,scrollbars=yes,resizable=yes,width=$ordinary_width,height=$ordinary_height'));list_status=list_status + 1;\">Series Open</a>");

array_push($win_open_list,"list.push(window.open('show_image.php?requestType=WADO&studyUID=$studyUID&seriesUID=$seriesUID&wl=$wl&ww=$ww','$seriesUID','toolbar=no,location=no,top=$top,left=$left,directories=no,status=no,menu=no,scrollbars=yes,resizable=yes,width=$ordinary_width,height=$ordinary_height'));list_status=list_status + 1;");


array_push($win_control_list,"$seriesUID");

print("<script language=\"JavaScript\">");
print("study_series_list['$studyUID'].push('$seriesUID');");
print("</script>");

print("$wl". "/" ."$ww". ":" ."$comment");
print("num;" . "_" . "$sernum" . ":");




}


  print("<center><H1><B>Packed DICOM</B></H1></center>\n");
  print("<table>");
  print("<TR><TH>Patient ID:</TH><tH  ALIGN=RIGHT bgcolor=\"#ccffff\" >$patid</TH></TR>");
  print("<TR><TH>Patient Name:</TH><tH ALIGN=LEFT bgcolor=\"#ccd555\">$patname4</TH></TR>");
  $fdate=formatDate(chop($date));
  print("<TR><TH>Exam. Date:</TH><tH ALIGN=LEFT bgcolor=\"#cccfff\">$fdate</TH>");
  print("</table>");


?>

<br><br>

<?

print("<form action=\"show_study.php\" target=\"_self\">");
print("<input type=hidden name=date value=$date>");
print("<input type=hidden name=patid value=$patid>");
print("<input type=hidden name=name value=$name>");
print("<input type=hidden name=stutim value=$stutim>");
print("<input type=hidden name=size value=$size>");
print("<input type=hidden name=browser value=$browser>");
print("<input type=hidden name=scwidth value=$scwidth>");
print("<input type=hidden name=scheight value=$scheight>");
print("<input type=hidden name=userid value=$userid>");
print("<input type=hidden name=permission value=$permission>");
print("<input type=\"submit\" value=\"Back to study list\">");
print("</form>");
?>

<form action="main_menu.php" method="post">
<input type=hidden name=size value="<? echo$size; ?>">
<input type=hidden name=browser value="<? echo$browser; ?>">
<input type=hidden name=scwidth value="<? echo$scwidth; ?>">
<input type=hidden name=scheight value="<? echo$scheight; ?>">
<input type=hidden name=userid value="<? echo $userid; ?>">
<input type=hidden name=permission value="<? echo $permission; ?>">
<input type="submit" value="Back to main menu">


<br>

<table>
<tr>
<td>
<form>
<input type="button" value=" Back " onClick="history.back()">
<input type=hidden name=size value="<? echo$size; ?>">
<input type=hidden name=browser value="<? echo$browser; ?>">
<input type=hidden name=scwidth value="<? echo$scwidth; ?>">
<input type=hidden name=scheight value="<? echo$scheight; ?>">
<input type=hidden name=userid value="<? echo $userid; ?>">
<input type=hidden name=permission value="<? echo $permission; ?>">
</form>
</td>
<td>

<!--
<form>
<input type="button" value=" Forward " onClick="history.forward()">
<input type=hidden name=size value="<? echo$size; ?>">
<input type=hidden name=browser value="<? echo$browser; ?>">
<input type=hidden name=scwidth value="<? echo$scwidth; ?>">
<input type=hidden name=scheight value="<? echo$scheight; ?>">
<input type=hidden name=userid value="<? echo $userid; ?>">
<input type=hidden name=permission value="<? echo $permission; ?>">
</form>

-->

</td>
</tr>
</table>

</CENTER>





</body>
</html>
