<?php


$patid = $_GET['patid'];
$patname_mod = $_GET['patname_mod'];
$size = $_GET['size'];
$browser = $_GET['browser'];
$scwidth = $_GET['scwidth'];
$scheight = $_GET['scheight'];
$userid = $_GET['userid'];
$permission = $_GET['permission'];
$study_uid = $_GET['study_uid'];
$date = $_GET['date'];
$zipfile = $_GET['zipfile'];

?>

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

<META HTTP-EQUIV="refresh" content="1; 
  URL=./tmp/<?php echo $study_uid ?>/dcm/pack/<?php echo $zipfile ?>">


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

<?php

/*

<META HTTP-EQUIV="refresh" content="1; 
  URL=./tmp/<?php echo $study_uid ?>/dcm/pack/<?php echo $zipfile ?>">

*/

//
//	2007.10.04
//	Made by tadashi tsuchimoto @ MITT
//	make_jpeg_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);






	
	

  $patid=chop($patid);
  $patname_mod=chop($patname_mod);
  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\">$patname_mod</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/" . $study_uid . "/" . "dcm" . "/" . "pack" . "/" . $zipfile ; 



if(!file_exists($zip_file_path)){



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

//tmpディレクトリ作成・取得
$patid=chop($patid);
$date=chop($date);

@$target_dir=touch_dir($study_uid,"dcm","pack");
$target_path=($target_dir . "/" );    



//query result set
$i = 0;

$sql = sql_generator("get_all_file_from_studyuid");
$rtn_image = $db_handle->prepare($sql);
$rtn_image->bindParam(1, $study_uid);
$rtn_image->execute();

while ($image_row = $rtn_image->fetch(PDO::FETCH_ASSOC)) {

	$path = trim($image_row['path']) ;
	$path = $file_root_path . $path;
	//print("$path<br>");
	
	
	$file_name=basename($path);
	
	$serial = $i;
	if($i<10){
	$serial = "000" . $serial;
	}elseif($i<100){
	$serial = "00" . $serial;
	}elseif($i<1000){
	$serial = "0" . $serial;
	}
	
	$target_file_path=$target_path .   $serial . ".dcm"; 


	$jpg_file_name =  $serial . ".dcm.jpg";
	//$file_name_array[$i]=$jpg_file_name;
	$file_name_array[$i] = $serial . ".dcm";


	//Symbolic link original file to target files. 
	if(is_link($target_file_path)){
		if($path != linkinfo($target_file_path)){
		//system("rm -f $target_file_path");
		unlink($target_file_path);
		symlink($path,$target_file_path);
		}
	}
	else{
		symlink($path,$target_file_path);
	}

$i = $i +1;
}

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



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

chdir($target_path);

exec("$zip_prog -r $zip_file   $file_arg");



//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</a>
");





}
else{

print("
<CENTER><image src=pgctn.gif><a href=$zip_file_path>Click Here to Download Packed DICOM Files</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=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=\"return 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>



</CENTER>





</body>
</html>
