<?php



$date = $_GET['date'];
$patid = $_GET['patid'];
$stutim = $_GET['stutim'];
$mod = $_GET['mod'];
$zipfile = $_GET['zipfile'];

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

?>

<!doctype html public "-//w3c//dtd html 3.2//ja">
<html>
<head>
  <META HTTP-EQUIV="refresh" content="1; 
  URL=../tmp/<?php echo $patid ?>/<?php echo $date ?>/<?php echo $stutim ?>/<?php echo $zipfile ?>">
<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_path=".";
  require("$include_path/dbconnect.ini");
  include "$include_path/init.inc";
  include "$include_path/file_mod.inc";

  $patid=chop($patid);
  $name=chop($name);
  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>");



$zip_file_path = "../tmp/" . $patid . "/" . $date . "/" . $stutim . "/" . $zipfile ; 


if(!file_exists($zip_file_path)){




class myDbConnect extends DbConnect {
      var $dbname = "DicomImage";
}



  $con = new myDbConnect;


//make SQL
 $join_sql = "instancetable.path AS path,
  serieslevel.mod AS mod ,
  serieslevel.serdes as serdes  
FROM studylevel,serieslevel,imagelevel,instancetable 
WHERE instancetable.imageuid = imagelevel.sopinsuid AND serieslevel.serinsuid = imagelevel.serparent AND studylevel.stuinsuid
= serieslevel.stuparent AND patientlevel.patid = studylevel.patparent ";

$sql="SELECT " . $join_sql . " AND patientlevel.patid='$patid' AND
studylevel.studat='$date' AND studylevel.stutim='$stutim' 
and serieslevel.mod = '$mod'  ORDER
BY imagelevel.sopinsuid";



  @$rtn = pg_exec($sql);
 if (!$rtn) {
    print("$pathの検索ができませんでした。");
    exit;
  }




  print("<hr><BR>\n");

//tmpディレクトリ作成・取得
$patid=chop($patid);
$date=chop($date);
$stutim=chop($stutim);
@$target_dir=touch_dir($patid,$date,$stutim);
$target_path=($target_dir . "/" );    

$file_num=pg_numrows($rtn);


for ($i=0;$i < $file_num; $i++) {
//query result set
    $path = chop(pg_result($rtn,$i,"path"));
    $mod  = chop(pg_result($rtn,$i,"mod"));
	$serdes  = chop(pg_result($rtn,$i,"serdes"));

//file path set
    $file_name=basename($path);
    $tok = strtok($file_name,".");
    $file_name = "";
    while($tok){
	$file_name = $file_name . $tok;
    $tok = strtok(".");
    }
    $file_name = substr($file_name,2);

    $target_path=($target_dir . "/" );	
	$file_name = $serdes . $file_name . ".dcm";
	$file_name_array[$i]=$file_name;

	     
    $target_file_path=$target_path .  $file_name ;//print

    //convert DICOM to Part 10 file
    $dcmto10 = "/usr/local/dicom/bin/dcm_ctnto10";
    exec("$dcmto10 -L $path $target_file_path");

}

//extract file arg.
for($i=0;$i<$file_num;$i++){
	$file_arg = $file_arg . " " . $file_name_array[$i];
}
//print $file_arg; 

//create autorun.inf file 
$prog = "UniViewer.exe";
$dcm_dic = "dcmdict.txt";
$usage = "usage.pdf";
$prog_path = "dicomviewer/univiewer/";


$bat_file=($target_path . "start.bat");
if(file_exists($bat_file))
unlink($bat_file);
touch($bat_file);
$fp=fopen($bat_file,"w+");
fwrite($fp,"$prog $file_arg");
fclose($fp);

$autorun_file=($target_path . "autorun.inf");
if(file_exists($autorun_file))
unlink($autorun_file);
touch($autorun_file);
$fq=fopen($autorun_file,"w+");
fwrite($fq,"[autorun]");
fwrite($fq,"\n");
fwrite($fq,"open= start.bat");
fwrite($fq,"\n");
fwrite($fq,"icon= ");
fwrite($fq,"\n");
fclose($fq);

//copy viewer & dic file
$dcm_dic_path = $prog_path . $dcm_dic;
$dcm_dic_target_path = $target_path . $dcm_dic;
//copy($dcm_dic_path,$dcm_dic_target_path);
$prog_file_path = $prog_path . $prog;
$prog_file_target_path = $target_path . $prog;
//copy($usage);
$usage_file_path = $prog_path . $usage;
$usage_file_target_path = $target_path . $usage;
//copy($prog_file_path,$prog_file_target_path);
exec("cp $dcm_dic_path $dcm_dic_target_path");
exec("cp $prog_file_path $prog_file_target_path");
exec("cp $usage_file_path $usage_file_target_path");

//make DICOMDIR file
chdir($target_path);
$dcmmkdir = "/usr/local/dicom/bin/dcmmkdir";
exec("$dcmmkdir $file_arg");

//make ZIP file
$zip_prog = exec("which zip");
$zip_file = $patid . "_"  . $patname4 ."_" . $date ."_" . $mod . "_" . $stutim .  ".zip";






exec("$zip_prog -r $zip_file $prog $dcm_dic start.bat autorun.inf DICOMDIR $usage $file_arg");


//exec("$zip_prog -r $zip_file $prog $dcm_dic DICOMDIR $file_arg");

$zip_file = $target_path . $zip_file;
print("
<CENTER><image src=pgctn.gif><a href=$zip_file>Click Here to Download Packed DICOM Files, DICOMDIR & Autorun.inf</a>
");

//finalize
unlink("autorun.inf");
//unlink("DICOMDIR");
unlink($prog);
unlink($dcm_dic);

for($i=0;$i<$file_num;$i++){
unlink(basename($file_name_array[$i]));
}


  pg_freeresult($rtn);
//  $con->dbClose;


}
else{

print("
<CENTER><image src=pgctn.gif><a href=$zip_file_path>Click Here to Download Packed DICOM Files, DICOMDIR & Autorun.inf</a>
");

}


?>

<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=\"検査一覧に戻る\">");
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="メインメニューに戻る">


<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>
