<?php
session_start();
session_cache_limiter("private");
if(!isset($_SESSION['user']))
	header("Location: login.php");
if( ($_SESSION['user'] == "rab") || ($_SESSION['user'] == "admin"))
	header("Location: index.php");
$login="<p>Вы зашли, как ".$_SESSION['user'].".<input type='button' onclick='location.href=\"login.php?logoff\";'/ value='Выход'>";
?>
<html>
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=windows-1251" />
<?php
$fh = fopen('login.cfg', "r") or die("Can't open file!");
$login = fread($fh, filesize('login.cfg'));
fclose($fh);
$fh = fopen('pass.cfg', "r") or die("Can't open file!");
$pass = fread($fh, filesize('pass.cfg'));
fclose($fh);
$fh = fopen('sid.cfg', "r") or die("Can't open file!");
$sid = fread($fh, filesize('sid.cfg'));
fclose($fh);
  
$c=OCILogon($login, $pass, $sid);
if(!$c)
			{
			echo "Невозможно подключится к базе: " . var_dump( OCIError() );
			die();
			}

if(isset($_GET["new"])){
	$new=htmlspecialchars($_GET["new"]);
	$s = OCIParse($c, "select * from frames");
	OCIExecute($s, OCI_DEFAULT);
	$i=0;
	while (OCIFetch($s))
			{
				$frames_name[$i]=htmlspecialchars(ociresult($s,"FRAME_NAME"));
				$frames_id[$i]=htmlspecialchars(ociresult($s,"ID_FRAMES"));
				$i++;
			}
}elseif(isset($_GET["fill"])){
	$fill=htmlspecialchars($_GET["fill"]);
	$s = OCIParse($c, "select frame_name from frames where id_frames=".$fill);
	OCIExecute($s, OCI_DEFAULT);
	OCIFetch($s);
	$frame_name=htmlspecialchars(ociresult($s,"FRAME_NAME"));
	$s = OCIParse($c, "select slots_to_frames.id_slots as id_slots, slots.slot_name as slot_name, slots.id_field as id_field from slots_to_frames join slots on slots.id_slots=slots_to_frames.id_slots where slots_to_frames.id_frames=".$fill);
	OCIExecute($s, OCI_DEFAULT);
	$i=0;
	while (OCIFetch($s))
			{
				$slots_id[$i]=htmlspecialchars(ociresult($s,"ID_SLOTS"));
				$slots_name[$i]=htmlspecialchars(ociresult($s,"SLOT_NAME"));
				$field_id[$i]=htmlspecialchars(ociresult($s,"ID_FIELD"));
				//echo $field_id[$i].";";
				$i++;
			}
	if(isset($field_id)){
	
	for($i=0;$i<count($field_id);$i++)
		{
			$s = OCIParse($c, "select tables.table_name as TABLE_NAME, fields.field_name as FIELD_NAME from fields join  tables on tables.id_tables=fields.id_table where id_fields=".$field_id[$i]);
			OCIExecute($s, OCI_DEFAULT);
			OCIFetch($s);
			$table_name=htmlspecialchars(ociresult($s,"TABLE_NAME"));
			$field_name=htmlspecialchars(ociresult($s,"FIELD_NAME"));
			//echo $table_name." ".$field_name;
			$s2 = OCIParse($c, "select distinct(".$field_name.") as VV from ".$table_name);
			//echo "select distinct(".$field_name.") as vv from ".$table_name;
			OCIExecute($s2, OCI_DEFAULT);
			$j=0;
			//$slots_values[$i]=array();
			while (OCIFetch($s2))
					{
						if(htmlspecialchars(ociresult($s2,"VV")) != "")
							{
							$slots_values[$i][$j]=htmlspecialchars(ociresult($s2,"VV"));
							//echo $slots_values[$i][$j].";".$i."-".$j;
							$j++;
							}
					}
					//print_r($slots_values);
		}
		}
}elseif(isset($_GET["delete"])){
	$delete=htmlspecialchars($_GET["delete"]);
	$s = OCIParse($c, "delete from frame_exmpl where id_exmpl=".$delete);
	OCIExecute($s, OCI_DEFAULT);
	echo "<meta http-equiv='refresh' content='0; url=frame_exmpl.php'>"; 
}elseif(isset($_POST["id_frame"])){
	$id_frame=htmlspecialchars($_POST["id_frame"]);
	$s = OCIParse($c, "select s_id_exmpl.nextval as id from dual");
			OCIExecute($s, OCI_DEFAULT);
			OCIFetch($s);
			$id_exmpl=htmlspecialchars(ociresult($s,"ID"));
	foreach($_POST as $key => $value)
		{
			//echo substr($key,0,4);
			
			if(substr($key,0,4) == "slt_"){
				$id_slt=substr($key,4);
				$s = OCIParse($c, "insert into Frame_exmpl(id_frames, id_slots, slt_value, id_exmpl) values(".$id_frame.", ".$id_slt.", '".$value."', ".$id_exmpl.")");
				OCIExecute($s, OCI_DEFAULT);
			}
		}
	echo "<meta http-equiv='refresh' content='0; url=frame_exmpl.php'>"; 
}else{
	//$query = "select frame_exmpl.id_exmpl as id, frame_exmpl.slt_value, frames.frame_name,slots.slot_name from frame_exmpl join frames on frames.id_frames=frame_exmpl.id_frames join slots on slots.id_slots=frame_exmpl.id_slots";
	$s = OCIParse($c, "select frame_exmpl.id_exmpl as id, frame_exmpl.id_fr_exmpl, frame_exmpl.slt_value, frames.frame_name,slots.slot_name from frame_exmpl join frames on frames.id_frames=frame_exmpl.id_frames join slots on slots.id_slots=frame_exmpl.id_slots order by id_exmpl");
	OCIExecute($s, OCI_DEFAULT);
	$i=0;
	$j=0;
	$prev_id=-1;
	while (OCIFetch($s))
			{
				$id=htmlspecialchars(ociresult($s,"ID"));
				if($prev_id != $id)
					{
					$i++;
					$main_id[$i]=$id;
					$prev_id=$main_id[$i];
					$j=0;
					}
				$main_frame[$i]=htmlspecialchars(ociresult($s,"FRAME_NAME"));
				$main_slot[$i][$j]=htmlspecialchars(ociresult($s,"SLOT_NAME"));
				$main_value[$i][$j]=htmlspecialchars(ociresult($s,"SLT_VALUE"));
				$j++;
		
			}
}	
			
			
OCICommit($c);
OCILogoff($c);
?>
	<script language="JavaScript">
		function num(count){
		/*
			for(var k=0;k<count;k++)
				{
				if(document.forms[0].elements[k].checked){
					return document.forms[0].elements[k].value;
					}
				}*/

				input = document.getElementsByTagName('input');

for(i=0;i<(input).length;i++){

   if(input[i].getAttribute('type')=='radio'){
	if(input[i].checked)
     return input[i].getAttribute('value');

   }

			}
			}
	</script>
	<link rel="stylesheet" href="/style/style.css" type="text/css" />
	</head>
	<body>
		<table border=1>
			<tr>
				<td width=300>
					<iframe src="menu.php" height=550 id="myIframe" scrolling=auto frameborder=0 >
						Ваш бразуер не поддерживает фрэймы, для работы АСУ воспользуйтесь другим ПО
					</iframe>
				</td>
	<td width=900 valign=top><table width="700"><td align='right'>Вы зашли как <?php echo $_SESSION['user'];?>, <input type='button' onclick='location.href="login.php?logoff";'/ value='Выход'></td></table><br/>
<?php
	if(isset($frames_id))
		{
			echo "<form action='frame_exmpl.php' method='get'>";
			echo "Фрейм: <select name='fill'>";
			echo "<option value=-1></option>";
			for($i=0;$i<count($frames_id);$i++)
				{
					echo "<option value=".$frames_id[$i].">".$frames_name[$i]."</option>";
				}
			echo "</select>";
			echo "<input type='submit' value=' Далее '>";
			echo "</form>";
		}
	elseif(isset($fill))
		{
			//print_r($slots_values);
			if(isset($slots_name)){
			echo "<form action='frame_exmpl.php' method='POST'>";
			echo "Фрейм-экземпляр <b>".$frame_name."</b>";
			echo "<table border=1><input type='hidden' name='id_frame' value=".$fill." />";
			for($i=0;$i<count($slots_name);$i++)
				{
					echo "<tr><td>".$slots_name[$i]." </td><td><select name=slt_".$slots_id[$i].">";
					echo "<option value=-1></option>";
					for($j=0;$j<count($slots_values[$i]);$j++)
						{
							echo "<option value='".$slots_values[$i][$j]."'>".$slots_values[$i][$j]."</option>";
						}
					echo "</select></td></tr>";
				}
			echo "</table>";
			if($_SESSION['user']=='expert')
				echo "<input type='submit' value='Создать' /><input type='button' onclick='location.href=\"frame_exmpl.php?new\"' value='Назад' />";
			echo "</form>";
			}else{
				echo "Фрейм <b>".$frame_name."</b> не содержит слотов!";
				echo "<input type='button' onclick='location.href=\"frame_exmpl.php?new\"' value='Назад' />";
			}
		}
	else
		{
			echo "<table border=1>";
			echo "<tr><td></td><th>ID</th><th>Фрейм</th><th>Слоты</th><th>Значения</th></tr>";
			if(isset($main_id)){
			for($i=1;$i<=count($main_id);$i++)
				{	
					echo "<tr><td><input type=radio name='id' value=".$main_id[$i]."><br/></td><td>".$main_id[$i]."</td><td>".$main_frame[$i]."</td>";
						if(isset($main_slot[$i])){
						echo "<td><table border=1>";
						for($j=0;$j<count($main_slot[$i]);$j++)
							{
							echo "<tr><td>".$main_slot[$i][$j]."</td></tr>";
							}
						echo "</table></td>";
						}
						if(isset($main_value[$i])){
						echo "<td><table border=1>";
						for($j=0;$j<count($main_value[$i]);$j++)
							{
							echo "<tr><td>".$main_value[$i][$j]."</td></tr>";
							}
						echo "</table></td>";
						}
						echo "</tr>";
				}
			}
			echo "</table>";
			if($_SESSION['user']=='expert'){
			echo "<input type='button' value='   Заполнить   ' onclick='location.href=\"frame_exmpl.php?new\"'>";
			echo "<input type='button' value='   Удалить   ' onclick='location.href=\"frame_exmpl.php?delete=\"+num(0)'>";
			}
			
		}
?>
				</td>
			</tr>
		</table>
	</body>
</html>