<?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"]))
	{
		$s = OCIParse($c, "select slot_name, id_slots from slots");
		OCIExecute($s, OCI_DEFAULT);
		$i=0;
		while (OCIFetch($s))
			{
				$adding_slots[$i]=htmlspecialchars(ociresult($s,"SLOT_NAME"));
				$id_slots_adding[$i]=htmlspecialchars(ociresult($s,"ID_SLOTS"));
				$i++;
			}
		$s = OCIParse($c, "select slots.slot_name, slots_to_frames.id_slots from slots_to_frames join slots on slots.id_slots=slots_to_frames.id_slots where slots_to_frames.id_frames=0");
		OCIExecute($s, OCI_DEFAULT);
		$i=0;
		while (OCIFetch($s))
			{
				$added_slots[$i]=htmlspecialchars(ociresult($s,"SLOT_NAME"));
				$id_slots_added[$i]=htmlspecialchars(ociresult($s,"ID_SLOTS"));
				$i++;
			}
		$s = OCIParse($c, "select frames.frame_name, frames.id_frames from frames");
		OCIExecute($s, OCI_DEFAULT);
		$i=0;
		while (OCIFetch($s))
			{
				$par_frames[$i]=htmlspecialchars(ociresult($s,"FRAME_NAME"));
				$par_id_frames[$i]=htmlspecialchars(ociresult($s,"ID_FRAMES"));
				$i++;
			}
}elseif(isset($_GET["adding_slots"]))
			{
				if($_GET["adding_slots"]!=-1)
					{
						echo "add";
						$s = OCIParse($c, "insert into slots_to_frames(id_slots,id_frames) values(".$_GET["adding_slots"].",0)");
						OCIExecute($s, OCI_DEFAULT);
					}
				echo "<meta http-equiv='refresh' content='0; url=frames.php?new'>"; 
}elseif(isset($_GET["delete_slots"]))
			{
				if($_GET["delete_slots"]!=-1)
					{
						//echo "delete from slots_to_frames where id_slots=".$_GET["delete_slots"]." and id_frames=0";
						$s = OCIParse($c, "delete from slots_to_frames where id_slots=".$_GET["delete_slots"]." and id_frames=0");
						OCIExecute($s, OCI_DEFAULT);
					}
				echo "<meta http-equiv='refresh' content='0; url=frames.php?new'>"; 
}elseif(isset($_GET["id"]))
	{
		$id=htmlspecialchars($_GET["id"]);
		$s = OCIParse($c, "select child.id_frames as ID_FRAMES, child.frame_name as FRAME_NAME, (case when par.frame_name is NULL then '-' else par.frame_name end) as PAR_FRAMES from frames child left join frames par on par.id_frames=child.id_parient where child.id_frames=".$id);
		OCIExecute($s, OCI_DEFAULT);
		while (OCIFetch($s))
			{
				$edit_fr_name=htmlspecialchars(ociresult($s,"FRAME_NAME"));
				$edit_pr=htmlspecialchars(ociresult($s,"PAR_FRAMES"));
			}
		
		$s = OCIParse($c, "select frames.frame_name, frames.id_frames from frames where id_frames != ".$id);
		OCIExecute($s, OCI_DEFAULT);
		$i=0;
		while (OCIFetch($s))
			{
				$edit_par_frames[$i]=htmlspecialchars(ociresult($s,"FRAME_NAME"));
				$edit_par_id_frames[$i]=htmlspecialchars(ociresult($s,"ID_FRAMES"));
				$i++;
			}
		$s = OCIParse($c, "select slots_to_frames.id_slots, slots.slot_name from slots_to_frames join slots on slots.id_slots=slots_to_frames.id_slots where id_frames=".$id);
		OCIExecute($s, OCI_DEFAULT);
		$i=0;
		while (OCIFetch($s))
			{
				$edit_slots_name[$i]=htmlspecialchars(ociresult($s,"SLOT_NAME"));
				$edit_slots_id[$i]=htmlspecialchars(ociresult($s,"ID_SLOTS"));
				$i++;
			}
}elseif(isset($_GET["slots_edit"]))
	{
		$slots_edit=htmlspecialchars($_GET["slots_edit"]);
		$s = OCIParse($c, "select slots_to_frames.id_slots, slots.slot_name from slots_to_frames join slots on slots.id_slots=slots_to_frames.id_slots where id_frames=".$slots_edit);
		OCIExecute($s, OCI_DEFAULT);
		$i=0;
		while (OCIFetch($s))
			{
				$slots_edit_name[$i]=htmlspecialchars(ociresult($s,"SLOT_NAME"));
				$slots_edit_id[$i]=htmlspecialchars(ociresult($s,"ID_SLOTS"));
				$i++;
			}
		$s = OCIParse($c, "select id_slots, slot_name from slots");
		OCIExecute($s, OCI_DEFAULT);
		$i=0;
		while (OCIFetch($s))
			{
				$availble_slots_name[$i]=htmlspecialchars(ociresult($s,"SLOT_NAME"));
				$availble_slots_id[$i]=htmlspecialchars(ociresult($s,"ID_SLOTS"));
				$i++;
			}
		
		
}elseif(isset($_POST["frame_name"]))
	{
		$frame_name=htmlspecialchars($_POST["frame_name"]);
		$par_frames_id=htmlspecialchars($_POST["par_frames_id"]);
		$s = OCIParse($c, "insert into frames(frame_name,id_parient) values('".$frame_name."',".$par_frames_id.")");
		OCIExecute($s, OCI_DEFAULT);
		$s = OCIParse($c, "select max(id_frames) as ID_FRAMES from frames");
		OCIExecute($s, OCI_DEFAULT);
		OCIFetch($s);
		$id_frame=htmlspecialchars(ociresult($s,"ID_FRAMES"));
		$s = OCIParse($c, "update slots_to_frames set id_frames=".$id_frame." where id_frames = 0");
		OCIExecute($s, OCI_DEFAULT);
		echo "<meta http-equiv='refresh' content='0; url=frames.php'>"; 
}elseif(isset($_GET["delete"])){
		$delete_id=htmlspecialchars($_GET["delete"]);
		$s = OCIParse($c, "delete from slots_to_frames where id_frames=".$delete_id);
		OCIExecute($s, OCI_DEFAULT);
		$s = OCIParse($c, "delete from frames where id_frames=".$delete_id);
		OCIExecute($s, OCI_DEFAULT);
		echo "<meta http-equiv='refresh' content='0; url=frames.php'>"; 
}elseif(isset($_GET["new_slots"])){
		$new_slots=htmlspecialchars($_GET["new_slots"]);
		$restore_id=htmlspecialchars($_GET["restore_id"]);
		//echo "insert into slots_to_frames(id_slots,id_frames) values(".$new_slots.",".$restore_id.")";
		$s = OCIParse($c, "insert into slots_to_frames(id_slots,id_frames) values(".$new_slots.",".$restore_id.")");
		OCIExecute($s, OCI_DEFAULT);
		echo "<meta http-equiv='refresh' content='0; url=frames.php?slots_edit=".$restore_id."'>";
	
}elseif(isset($_POST["id_edit"])){
		$id_edit=htmlspecialchars($_POST["id_edit"]);
		$parent_edit=htmlspecialchars($_POST["edit_par"]);
		$name_edit=htmlspecialchars($_POST["name_edit"]);
		$s = OCIParse($c, "update frames set frame_name='".$name_edit."', id_parient=".$parent_edit." where id_frames=".$id_edit);
		OCIExecute($s, OCI_DEFAULT);
		//echo "update frames set frame_name='".$name_edit."', id_parient=".$parent_edit." where id_frames=".$id_edit;
		echo "<meta http-equiv='refresh' content='0; url=frames.php'>"; 

}elseif(isset($_GET["frm"])){
	$frm=htmlspecialchars($_GET["frm"]);
	$remove_slot=htmlspecialchars($_GET["remove_slot"]);
	$s = OCIParse($c, "delete from slots_to_frames where id_frames=".$frm." and id_slots=".$remove_slot);
	OCIExecute($s, OCI_DEFAULT);
	echo "<meta http-equiv='refresh' content='0; url=frames.php?slots_edit=".$frm."'>";
	
}else{
	$s = OCIParse($c, "select child.id_frames as ID_FRAMES, child.frame_name as FRAME_NAME, (case when par.frame_name is NULL then '-' else par.frame_name end) as PAR_FRAMES from frames child left join frames par on par.id_frames=child.id_parient");
		OCIExecute($s, OCI_DEFAULT);
		$i=0;
		while (OCIFetch($s))
			{
				$show_frames[$i]=htmlspecialchars(ociresult($s,"FRAME_NAME"));
				$show_id_frames[$i]=htmlspecialchars(ociresult($s,"ID_FRAMES"));
				$show_parient[$i]=htmlspecialchars(ociresult($s,"PAR_FRAMES"));
				$i++;
			}
}
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($adding_slots))
		{
			echo "\n\n<form method='post' action='frames.php'>";
			echo "\n<table border=1>";
			echo "\n<tr>";
			echo "\n<td>Имя фрейма </td>";
			echo "\n<td><input type='text' value='' name='frame_name' /></td></tr>";
			echo "<tr><td>Слоты</td><td>";
			if(isset($added_slots))
				{
					echo "<table border=1>";
					for($j=0;$j<count($added_slots);$j++)
						{
						echo "<tr><td><input type=radio name='id' value=".$id_slots_added[$j]."></td><td>".$added_slots[$j]."</td></tr>";
						}
					echo "</table>";
					if($_SESSION['user']=='expert')
						echo "<input type='button' value='Удалить' onclick='location.href=\"frames.php?delete_slots=\"+(num(0))'>";
				}
			echo "\n<br/><select name='slots_adding' id='slots_adding'>";
			echo "<option value='-1'></option>";
			for($i=0;$i<count($adding_slots);$i++)
				{
				echo "<option value=".$id_slots_adding[$i].">".$adding_slots[$i]."</option>";
				}
			echo "</select>";
			if($_SESSION['user']=='expert')
				echo "<input type='button' value='Добавить' onclick='location.href=\"frames.php?adding_slots=\"+document.getElementById(\"slots_adding\").options[document.getElementById(\"slots_adding\").selectedIndex].value'>";

			echo "</td></tr></tr><td>Фрейм-родитель</td><td>";
			echo "\n<br/><select name='par_frames_id' id='par_frames_id'>";
			echo "<option value='0'></option>";
			for($i=0;$i<count($par_frames);$i++)
				{
				echo "<option value=".$par_id_frames[$i].">".$par_frames[$i]."</option>";
				}
			echo "</select>";
			
			echo "\n</td></tr>";
			echo "\n</table>";
			if($_SESSION['user']=='expert')
				echo "\n<input type='submit' value='Сохранить'>";
			echo "<input type='button' value='Отмена' onclick='location.href=\"slots.php\"'>";
			echo "\n</form>";
		}
	elseif(isset($id)){
		echo "<form action='frames.php' method='post'>";
		echo "<input type='hidden' name='id_edit' value=".$id." />";
		echo "<table border=1><tr><td>Имя фрейма</td><td><input type='text' name='name_edit' value='".$edit_fr_name."'/></td></tr>";
		echo "<tr><td>Слоты</td><td>";
		if(isset($edit_slots_id))
			{
				echo "<table border=1><tr><th>ID</th><th>Имя слота</th></tr>";
				for($i=0;$i<count($edit_slots_id);$i++)
					{
						echo "<tr><td>".$edit_slots_id[$i]."</td><td>".$edit_slots_name[$i]."</td></tr>";
					}
				echo "</table>";
				}
				if($_SESSION['user']=='expert')
					echo "<input type='button' value='Редактировать' onclick='location.href=\"frames.php?slots_edit=".$id."\"'>";
			
		echo "</td></tr>";
		echo "<tr><td>Родитель</td><td>";
		echo "<select name='edit_par'>";
		echo "<option value=0></option>";
		for($i=0;$i<count($edit_par_frames);$i++)
			{
				if(($edit_par_frames[$i]==$edit_pr)&&($edit_pr != "-"))
					{
					echo "<option selected value=".$edit_par_id_frames[$i].">".$edit_par_frames[$i]."</option>";
					}
				else
					{
					echo "<option value=".$edit_par_id_frames[$i].">".$edit_par_frames[$i]."</option>";
					}
			}
		
		echo "</table>";
		if($_SESSION['user']=='expert')
			echo "<input type='submit' value='Сохранить'/>";
		echo "<input type='button' value='   Отмена   ' onclick='location.href=\"frames.php\"'>";
		echo "</form>";
	}elseif(isset($slots_edit)){
		echo "<input type='button' value='   К фрейму   ' onclick='location.href=\"frames.php?id=".$slots_edit."\"'>";
		echo "<br/>";
		echo "<form action='frames.php' method='get'>";
		echo "<input type='hidden' name='restore_id' value=".$slots_edit." />";
		echo "<select name='new_slots'>";
		echo "<option value=0></option>";
		for($i=0;$i<count($availble_slots_name);$i++)
			{
					echo "<option value=".$availble_slots_id[$i].">".$availble_slots_name[$i]."</option>";
			}
		if($_SESSION['user']=='expert')
			echo "<input type='submit' value='Добавить'/></form>";
		if(isset($slots_edit_id))
			{
			echo "<table border=1><tr><th></th><th>ID</th><th>Имя слота</th></tr>";
					for($i=0;$i<count($slots_edit_id);$i++)
						{
							echo "<tr><td><input type=radio name='id' value=".$slots_edit_id[$i]."><br/></td><td>".$slots_edit_id[$i]."</td><td>".$slots_edit_name[$i]."</td></tr>";
						}
					echo "</table>";
				if($_SESSION['user']=='expert')
					echo "<input type='button' value='Удалить' onclick='location.href=\"frames.php?frm=".$slots_edit."&remove_slot=\"+num(0)'>";	
			}
	}
	else{
		echo "<table border=1><tr><th></th><th>ID</th><th>Название фрейма</th><th>Родитель фрейма</th></tr>";
			if(isset($show_frames))
			{
			for($i=0;$i<count($show_frames);$i++)
			{
				echo "<tr><td><input type=radio name='id' value=".$show_id_frames[$i]."><br/></td><td>".$show_id_frames[$i]."</td><td>".$show_frames[$i]."</td><td>".$show_parient[$i]."</td></tr>";
			}
		}
		echo "</table>";
		if($_SESSION['user']=='expert'){	
			echo "<input type='button' value='   Создать   ' onclick='location.href=\"frames.php?new\"'>";
			
			echo "<input type='button' value='   Удалить   ' onclick='location.href=\"frames.php?delete=\"+num(0)'>";
		}
		echo "<input type='button' value='   Изменить   ' onclick='location.href=\"frames.php?id=\"+num(0)'>";
		}
?>
				</td>
			</tr>
		</table>
	</body>
</html>