<!doctype html public "-//w3c//dtd html 3.2//ja">
<html>
<head>
<title>refered image(order sheet) update to server</title>
<meta http-equiv=content-type content="image/jpeg; charset=x-euc-jp">


</head>
<body BACKGROUND="./img/yellow.jpg" onLoad="setTimeout('window.close()',500)" >

<?php
// 
//
//	2002.1.1
//	DICOM to WWW Gateway :PHP script
//	Made by Norya Yokohama % Medical Division @ WERC
//  2004.9.19
//  added by tadashi tsuchimoto @ MITT belong to yokohamazyuku
//  insert/delete refered images menu
//


$report_on = $_POST['report_on'];
$ruid = $_POST['ruid'];
$studyUID = $_POST['studyUID'];


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

  include "sql.inc";


if(is_uploaded_file($_FILES["userfile"]["tmp_name"])){


	if($db_type == "pgsql"){
	
	$db_handle->beginTransaction();
	$oid = $db_handle->pgsqlLOBCreate();
	$stream = $db_handle->pgsqlLOBOpen($oid, 'w');
	$local = fopen($_FILES["userfile"]["tmp_name"], 'rb');
	stream_copy_to_stream($local, $stream);
	$local = null;
	$stream = null;
	$sql = "update reportlevel set refimage6 = ?  where ruid = ? ";
	$rtn_referimage_do6 = $db_handle->prepare($sql);
	$rtn_referimage_do6->bindParam(1, $oid);
	$rtn_referimage_do6->bindParam(2, $ruid);
	$rtn_referimage_do6->execute();
	$db_handle->commit();
	
	}else{
	
	$fp = fopen($_FILES["userfile"]["tmp_name"], 'rb');
	
	$sql = "update reportlevel set refimage6 = ?  where ruid = ? ";
	$rtn_referimage_do6 = $db_handle->prepare($sql);
	$rtn_referimage_do6->bindParam(1, $fp, PDO::PARAM_LOB);
	$rtn_referimage_do6->bindParam(2, $ruid);
	$rtn_referimage_do6->execute();
	
	}

}


if(@$ward){

$sql = "update reportlevel set ward= ? where ruid= ?";
$rtn_referimage_do6_ward = $db_handle->prepare($sql);
$rtn_referimage_do6_ward->bindParam(1, $ward);
$rtn_referimage_do6_ward->bindParam(2, $ruid);
$rtn_referimage_do6_ward->execute();
  
  }


$sql = "select 
  reportlevel.refimage2 as refimage2,
  reportlevel.refimage3 as refimage3,
  reportlevel.refimage4 as refimage4,
  reportlevel.refimage5 as refimage5,
  reportlevel.refimage6 as refimage6,
  reportlevel.refimage as refimage from reportlevel 
  where reportlevel.ruid = ?";
$rtn_referimage_do6_select = $db_handle->prepare($sql);
$rtn_referimage_do6_select->bindParam(1, $ruid);
$rtn_referimage_do6_select->execute();

  while ($rtn_referimage_do6_select_row = $rtn_referimage_do6_select->fetch(PDO::FETCH_ASSOC)) {
$refimage = $rtn_referimage_do6_select_row['refimage'] ;
$refimage2 = $rtn_referimage_do6_select_row['refimage2'] ;
$refimage3 = $rtn_referimage_do6_select_row['refimage3'] ;
$refimage4 = $rtn_referimage_do6_select_row['refimage4'] ;
$refimage5 = $rtn_referimage_do6_select_row['refimage5'] ;
$refimage6 = $rtn_referimage_do6_select_row['refimage6'] ;
}


print("<p>");

if($refimage){
	if($refimage2){
		if($refimage3){
			if($refimage4){
				if($refimage5){
					$ref_num = 6;
					      }
					$ref_num = 5;
				      }	
				$ref_num = 4;
				}
			$ref_num = 3;
			}
		$ref_num = 2;
	      }

if(!@$ref_num){
  $ref_num = 1;
}


$studyUID = trim($studyUID);

//print("$report_on $studyUID");

if($report_on){

$sql = "update study set 
report= :report
where study.study_iuid= :studyUID ";
$rtn_referimage_do6_report_on = $db_handle->prepare($sql);
//$rtn_insert_with_ruid->execute(array($report_on,$studyUID));
$rtn_referimage_do6_report_on->bindParam(':report', $report_on);
$rtn_referimage_do6_report_on->bindParam(':studyUID', $studyUID);
$rtn_referimage_do6_report_on->execute();

//if report_on end
}else{

$sql = "update study set 
report= null 
where study.study_iuid= :studyUID ";
$rtn_referimage_do6_report_on = $db_handle->prepare($sql);
//$rtn_insert_with_ruid->execute(array($report_on,$studyUID));
//$rtn_referimage_do6_report_on->bindParam(1, 'null');
$rtn_referimage_do6_report_on->bindParam(':studyUID', $studyUID);
$rtn_referimage_do6_report_on->execute();


//if report_on end
}

/*
if($refimage6){
echo "file exist";
print("<img src=\"show_orderimage.php?ruid=$ruid\" Width=500 Height=500>");
}
*/






?>


</body>
</html>
