<?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 table_name, id_tables from tables");
		OCIExecute($s, OCI_DEFAULT);
		$i=0;
		while (OCIFetch($s))
			{
				$tables[$i]=htmlspecialchars(ociresult($s,"TABLE_NAME"));
				$id_tables[$i]=htmlspecialchars(ociresult($s,"ID_TABLES"));
				$i++;
			}
	}
elseif(isset($_GET["table"]))
	{
		$table=htmlspecialchars($_GET["table"]);
		$s = OCIParse($c, "select field_name,id_fields from fields where id_table=".$table);
		OCIExecute($s, OCI_DEFAULT);
		$i=0;
		while (OCIFetch($s))
			{
				$fields[$i]=htmlspecialchars(ociresult($s,"FIELD_NAME"));
				$id_fields[$i]=htmlspecialchars(ociresult($s,"ID_FIELDS"));
				$i++;
			}
	}
elseif(isset($_GET["field"]))
	{
		$field=htmlspecialchars($_GET["field"]);
	}
elseif(isset($_GET["id"])){
		$id=htmlspecialchars($_GET["id"]);
		$s = OCIParse($c, "select slots.id_slots, slots.slot_name, tables.table_name, fields.field_name from slots join fields on fields.id_fields=slots.id_field join tables on tables.id_tables=fields.id_table where slots.id_slots=".$id);
		OCIExecute($s, OCI_DEFAULT);
		while (OCIFetch($s))
			{
				$slt_name=htmlspecialchars(ociresult($s,"SLOT_NAME"));
				$tbl_name=htmlspecialchars(ociresult($s,"TABLE_NAME"));
				$fld_name=htmlspecialchars(ociresult($s,"FIELD_NAME"));
			}
	}
elseif(isset($_POST["name"]))
	{
		$field_id=htmlspecialchars($_POST["field_id"]);
		$name=htmlspecialchars($_POST["name"]);
		$s = OCIParse($c, "insert into slots(slot_name,id_field) values('".$name."',".$field_id.")");
		OCIExecute($s, OCI_DEFAULT);
		echo "<meta http-equiv='refresh' content='0; url=slots.php'>"; 
	}
elseif(isset($_POST["edit_id"]))
	{
		$edit_id=htmlspecialchars($_POST["edit_id"]);
		$slt_name=htmlspecialchars($_POST["slt_name"]);
		$s = OCIParse($c, "update slots set slot_name='".$slt_name."' where id_slots=".$edit_id);
		OCIExecute($s, OCI_DEFAULT);
		echo "<meta http-equiv='refresh' content='0; url=slots.php'>"; 
	}
elseif(isset($_GET["tbl_fld"]))
	{
		$tbl_fld=htmlspecialchars($_GET["tbl_fld"]);
		$s = OCIParse($c, "select table_name, id_tables from tables");
		OCIExecute($s, OCI_DEFAULT);
		$i=0;
		while (OCIFetch($s))
			{
				$tables_double[$i]=htmlspecialchars(ociresult($s,"TABLE_NAME"));
				$id_tables_double[$i]=htmlspecialchars(ociresult($s,"ID_TABLES"));
				$i++;
			}
		$s = OCIParse($c, "select slots.id_slots, tables.id_tables, fields.id_fields from slots join fields on fields.id_fields=slots.id_field join tables on tables.id_tables=fields.id_table where id_slots=".$tbl_fld);
		OCIExecute($s, OCI_DEFAULT);
		while (OCIFetch($s))
			{
				$tbl=htmlspecialchars(ociresult($s,"ID_TABLES"));
				$fld=htmlspecialchars(ociresult($s,"ID_FIELDS"));
				
			}
	}
elseif(isset($_GET["tbl_new"]))
	{
		$tbl_new=htmlspecialchars($_GET["tbl_new"]);
		$fld_old=htmlspecialchars($_GET["fld_id"]);
		$id_slot_edit=htmlspecialchars($_GET["slt_id"]);
		$s = OCIParse($c, "select field_name,id_fields from fields where id_table=".$tbl_new);
		OCIExecute($s, OCI_DEFAULT);
		$i=0;
		while (OCIFetch($s))
			{
				$fields_double[$i]=htmlspecialchars(ociresult($s,"FIELD_NAME"));
				$id_fields_double[$i]=htmlspecialchars(ociresult($s,"ID_FIELDS"));
				$i++;
			}
	}
elseif(isset($_GET["delete"]))
	{
		$delete_id=htmlspecialchars($_GET["delete"]);
		$s = OCIParse($c, "delete from slots_to_frames where id_slots=".$delete_id);
		OCIExecute($s, OCI_DEFAULT);
		$s = OCIParse($c, "delete from frame_exmpl where id_slots=".$delete_id);
		OCIExecute($s, OCI_DEFAULT);
		$s = OCIParse($c, "delete from slots where id_slots=".$delete_id);
		OCIExecute($s, OCI_DEFAULT);
		echo "<meta http-equiv='refresh' content='0; url=slots.php'>"; 
	}
elseif(isset($_POST["new_slot"]))
	{
		$new_slot=htmlspecialchars($_POST["new_slot"]);
		$fld_new=htmlspecialchars($_POST["fld_new"]);
		//echo $new_slot;
		$s = OCIParse($c, "update slots set id_field=".$fld_new." where id_slots=".$new_slot);
		OCIExecute($s, OCI_DEFAULT);
		echo "<meta http-equiv='refresh' content='0; url=slots.php?id=".$new_slot."'>"; 
	}
else{
		$s = OCIParse($c, "select slots.id_slots, slots.slot_name, tables.table_name, fields.field_name from slots join fields on fields.id_fields=slots.id_field join tables on tables.id_tables=fields.id_table");
		OCIExecute($s, OCI_DEFAULT);
		$i=0;
		while (OCIFetch($s))
			{
				$slots_name[$i]=htmlspecialchars(ociresult($s,"SLOT_NAME"));
				$id_slots[$i]=htmlspecialchars(ociresult($s,"ID_SLOTS"));
				$table_name[$i]=htmlspecialchars(ociresult($s,"TABLE_NAME"));
				$field_name[$i]=htmlspecialchars(ociresult($s,"FIELD_NAME"));
				$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;
					}
				}
			}
	</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($tables))
		{
		echo "\n\n<form method='get' action='slots.php'>";
		echo "\n<table border=1>";
		echo "\n<tr>";
		echo "\n<td>Таблица </td>";
		echo "\n<td><select name='table'>";
		for($i=0;$i<count($tables);$i++)
			{
			echo "<option value=".$id_tables[$i].">".$tables[$i]."</option>";
			}
		echo "</select></td>";
		echo "\n</tr>";
		echo "\n</table>";
		echo "\n<input type='submit' value='Далее'>";
		echo "<input type='button' value='Назад' onclick='location.href=\"slots.php\"'>";
		echo "\n</form>";
		}
	elseif(isset($fields))
		{
		echo "\n\n<form method='get' action='slots.php'>";
		echo "\n<table border=1>";
		echo "\n<tr>";
		echo "\n<td>Поле </td>";
		echo "\n<td><select name='field'>";
		for($i=0;$i<count($fields);$i++)
			{
			echo "<option value=".$id_fields[$i].">".$fields[$i]."</option>";
			}
		echo "</select></td>";
		echo "\n</tr>";
		echo "\n</table>";
		echo "\n<input type='submit' value='Далее'>";
		echo "<input type='button' value='Назад' onclick='location.href=\"slots.php?new\"'>";
		echo "\n</form>";
		}
	elseif(isset($field))
		{
		echo "\n\n<form method='post' action='slots.php'>";
		echo "\n<table border=1>";
		echo "\n<tr>";
		echo "\n<td>Название слота </td>";
		echo "\n<td><input name='name' type='text' value='' size='40'/>";
		echo "<input type='hidden' name='field_id' value=".$field." /></td>";
		echo "\n</tr>";
		echo "\n</table>";
		echo "\n<input type='submit' value='Далее'>";
		echo "<input type='button' value='Отмена' onclick='location.href=\"slots.php\"'>";
		echo "\n</form>";
		}
	elseif(isset($name))
		{
			//echo $name."--".$field_id;
		}
	elseif(isset($id))
		{
			echo "\n\n<form method='post' action='slots.php'>";
			echo "\n<table border=1>";
			echo "\n<tr><td>Имя слота:</td>";
			echo "\n<td><input type='hidden' name='edit_id' value=".$id." /><input type='text' name='slt_name' size=40 value='".$slt_name."'/></td></tr>";
			echo "\n<tr><td>Таблица->Поле:</td>";
			if($_SESSION['user']=='expert')
				echo "\n<td><a href='slots.php?tbl_fld=".$id."'>".$tbl_name."->".$fld_name."</a></td></tr>";
			else
				echo "\n<td>".$tbl_name."->".$fld_name."</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($fld))
		{
		echo "\n\n<form method='get' action='slots.php'>";
		echo "\n<table border=1>";
		echo "\n<tr>";
		echo "\n<td>Таблица </td>";
		echo "\n<td><select name='tbl_new'>";
		$slt="";
		for($i=0;$i<count($tables_double);$i++)
			{
			if($id_tables_double[$i]==$tbl)
				{
				$slt="selected";
				}
			else
				{
				$slt="";
				}
			echo "<option ".$slt." value=".$id_tables_double[$i].">".$tables_double[$i]."</option>";
			}
		echo "</select><input type='hidden' name='fld_id' value=".$fld." /><input type='hidden' name='slt_id' value=".$tbl_fld." /></td>";
		echo "\n</tr>";
		echo "\n</table>";
		echo "\n<input type='submit' value='Далее'>";
		echo "<input type='button' value='Назад' onclick='location.href=\"slots.php?id=".$tbl_fld."\"'>";
		echo "\n</form>";
		}
	elseif(isset($fld_old))
		{
			echo "\n\n<form method='post' action='slots.php'>";
			echo "\n<table border=1>";
			echo "\n<tr>";
			echo "\n<td>Поле </td>";
			echo "\n<td><input type='hidden' name='new_slot' value=".$id_slot_edit." /><select name='fld_new'>";
			$slt="";
			for($i=0;$i<count($fields_double);$i++)
				{
				if($fld_old==$id_fields_double[$i])
					{
						$slt="selected";
					}
				else
					{
						$slt="";
					}
				echo "<option ".$slt." value=".$id_fields_double[$i].">".$fields_double[$i]."</option>";
				}
			echo "</select></td>";
			echo "\n</tr>";
			echo "\n</table>";
			if($_SESSION['user']=='expert')
				echo "\n<input type='submit' value='Сохранить'>";
			echo "<input type='button' value='Назад' onclick='location.href=\"slots.php?tbl_fld=".$id_slot_edit."\"'>";
			echo "<input type='button' value='Отмена' onclick='location.href=\"slots.php?id=".$id_slot_edit."\"'>";
			echo "\n</form>";
		}
	elseif(isset($slots_name)){
		echo "\n\n<form method='get' action='slots.php'>";
		echo "\n<table border=1>";
		echo "\n<tr><th> </th><th>Название слота</th><th>Таблица</th><th>Поле</th></tr>";
		$rn=0;
		for($i=0;$i<count($slots_name);$i++)
			{
			if($rn>3)
				$rn=0;
			if($rn==0)
				$color="#fbec5d";
			if($rn==1)
				$color="#fcdd76";
			if($rn==2)
				$color="#ffdb58";
			if($rn==3)
				$color="#f0dc82";
			$rn++;
			echo "\n<tr  bgcolor='".$color."'>"; 
			echo "<td><input type=radio name='id' value=".$id_slots[$i]."><br/></td>\n";
			echo "<td><a href='slots.php?id=".$id_slots[$i]."'>".$slots_name[$i]."</a></td>";
			echo "\n<td>".$table_name[$i]."</td>";
			echo "\n<td>".$field_name[$i]."</td></tr>";
			}
		echo "\n</table>";
		if($_SESSION['user']=='expert'){
		echo "<input type='button' value='   Создать   ' onclick='location.href=\"slots.php?new\"'>";
		
		echo "\n<input type='button' value='  Удалить  ' onclick='location.href=\"slots.php?delete=\"+num($i);'>";
		}
		echo "\n<input type='button' value='  Изменить  ' onclick='location.href=\"slots.php?id=\"+num($i);'>";
		echo "\n</form>";
		}
	else
		{
			if($_SESSION['user']=='expert')
				echo "<input type='button' value='   Создать   ' onclick='location.href=\"slots.php?new\"'>";
		}
	
	
?>
				</td>
			</tr>
		</table>
	</body>
</html>