<?php


$jpg_file_name = $_POST['jpg_file_name'];
$jpg_file_name_tmp = $_POST['jpg_file_name_tmp'];
$ruid = $_POST['ruid'];




?>
<!doctype html public "-//w3c//dtd html 3.2//ja">
<html>
<head>
<title>refered image 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
//


  include "init.inc";
  $include_path=".";
  require("$include_path/dbconnect_old.ini");

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


  /* patid ¤ò URL encode */
  $con = new myDbConnect;

//insert report
$check = "select ruid from reportlevel where ruid = '$ruid'";
$check_do = pg_exec($check);
  $flag = pg_num_rows($check_do);
if(!$flag){
	$insert = "insert into reportlevel (ruid) values ('$ruid')";
	$insert_do = pg_exec($insert);
	print("insert new report<br>");
	}

$result = pg_query("BEGIN");


    $oid = pg_lo_create ();
//    echo ("$oid");
    $handle = pg_lo_open ($oid, "w");
//    echo ("$handle");
//    echo ("$userfile");
//    $fd = fopen($userfile,"r");
//    $img = fread($fd,filesize($userfile));

    $fd = fopen($jpg_file_name_tmp,"r");
    $img = fread($fd,filesize($jpg_file_name_tmp));

    pg_lowrite ($handle, $img);


  $prev_sql = " select refimage as refimage,
  reportlevel.refimage2 as refimage2,
  reportlevel.refimage3 as refimage3,
  reportlevel.refimage4 as refimage4,
  reportlevel.refimage5 as refimage5,
  reportlevel.refimage6 as refimage6
   from reportlevel where
  ruid = '$ruid'";



  $rtn = pg_exec($prev_sql);
 if (!$rtn) {
    print("no data");
    exit;
  }

    $i = 0;

    @$refimage = pg_result($rtn,$i,"refimage");
    @$refimage2 = pg_result($rtn,$i,"refimage2");
    @$refimage3 = pg_result($rtn,$i,"refimage3");
    @$refimage4 = pg_result($rtn,$i,"refimage4");
    @$refimage5 = pg_result($rtn,$i,"refimage5");
    @$refimage6 = pg_result($rtn,$i,"refimage6");


//    i18n_http_output("pass");

if($refimage){
	if($refimage2){
		if($refimage3){
			if($refimage4){
				if($refimage5){
					print("already many refered images.refimage No 5 is replaced<br>");
					print("if you want to replace other image, proceed manual.<br>");
					  $sql="update reportlevel set refimage5='$oid' where ruid='$ruid'";
					}
					else{
					$sql="update reportlevel set refimage5='$oid' where ruid='$ruid'";
					}
				}
				else{
				$sql="update reportlevel set refimage4='$oid' where ruid='$ruid'";
				}
			}	
			else{
			$sql="update reportlevel set refimage3='$oid' where ruid='$ruid'";
			}
		}	
		else{	
		$sql="update reportlevel set refimage2='$oid' where ruid='$ruid'";	
		}	
	}	
	else{
	$sql="update reportlevel set refimage='$oid' where ruid='$ruid'";		
	}


//  $sql="update reportlevel set refimage='$oid' where ruid='$ruid'";


$result = pg_query($sql);
$result = pg_query("commit");
$result = pg_query("END");



  $sql2 = "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 ='$ruid'";


  $rtn2 = pg_exec($sql2);

  $flag = pg_num_rows($rtn2);

//print("$flag<br>");

  if ($flag==1){

    $i = 0;

    $refimage = pg_result($rtn2,$i,"refimage");
    $refimage2 = pg_result($rtn2,$i,"refimage2");
    $refimage3 = pg_result($rtn2,$i,"refimage3");
    $refimage4 = pg_result($rtn2,$i,"refimage4");
    $refimage5 = pg_result($rtn2,$i,"refimage5");
    $refimage6 = pg_result($rtn2,$i,"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;
}

if($refimage){
print("Key images");
print("<br>");
print("<img src=\"show_refimage.php?ruid=$ruid&refimage=$refimage\" Width=310 Height=310>");
}

if($refimage2){
print("<img src=\"show_refimage.php?ruid=$ruid&refimage=$refimage2\" Width=310 Height=310>");
}

if($refimage3){
print("<br>");
print("<img src=\"show_refimage.php?ruid=$ruid&refimage=$refimage3\" Width=310 Height=310>");
}

if($refimage4){
print("<img src=\"show_refimage.php?ruid=$ruid&refimage=$refimage4\" Width=310 Height=310>");
}


if($refimage5){
print("<br>");
print("<img src=\"show_refimage.php?ruid=$ruid&refimage=$refimage5\" Width=310 Height=310>");
}





//  $con->dbClose;



?>


</body>
</html>
