<?
@session_start();
$base_path='../';
require_once('../connect/connect.php');
include_once('../lib/lib.php');
require('pdk.php');

$Action='element_two.php';

if ($_GET['id_element'] && $_SESSION['ses_s']['level']==2)
	{
	$_SESSION['ses_s']['id_element']=intval($_GET['id_element']);
	$cat=mysql_query("SELECT * FROM element WHERE id_element='".$_SESSION['ses_s']['id_element']."'");
	$r_cat=mysql_fetch_array($cat);
	$_SESSION['ses_s']['element']=$r_cat['element'];
	}

if(!$_SESSION['ses_s']['id_element']) exit("Не определен родительский элемент");

if ($_GET['id_el_two'] && $_GET['per'])
	{
	if ($_GET['per']==1) $per=1; else $per=-1;
	sort_num('element_two', 'id_element_two', 'num', 'element_two', intval($_GET['id_el_two']), $per, " WHERE id_element='".$_SESSION['ses_s']['id_element']."'");
	include('_svod_element_two.php');
	}

if ($_POST["del"] && $_POST["del_element"]) 
	{ 
	mysql_query ("DELETE FROM element_two WHERE id_element_two IN (".$_POST["del_element"].") ") or die(mysql_error());
	include('_svod_element_two.php');
	echo "<HTML><HEAD><meta http-equiv='Refresh' charset='utf-8' content='0; URL=refresh.php?i=".$Action."'></HEAD></HTML>";
	exit();
	}


if ($_POST['del'] && $_POST['id_element_two']) 
	{
	mysql_query ("DELETE FROM element_two WHERE id_element_two='".$_POST['id_element_two']."'") or die(mysql_error());
	include('_svod_element_two.php');
	echo "<HTML><HEAD><meta http-equiv='Refresh' charset='utf-8' content='0; URL=refresh.php?i=".$Action."'></HEAD></HTML>";
	exit();
	}

if ($_POST["edit"] && $_POST['id_element_two']) 
	{ 
	$element_two = htmlspecialchars(rtrim($_POST['element_two']));
	if(!$element_two) $err .='Укажите элемент списка<br>';

	if(!$err)
		{
		mysql_query("UPDATE element_two SET element_two='".$element_two."' WHERE id_element_two='".$_POST['id_element_two']."'");
		include('_svod_element_two.php');
		echo "<HTML><HEAD><meta http-equiv='Refresh' charset='utf-8' content='0; URL=refresh.php?i=".$Action."'></HEAD></HTML>";
		exit();
		}
	}

if ($_POST["add"]) 
	{ 
	$element_two = $_POST['element_two'];
	if(!$element_two) $err .='Укажите элемент списка<br>';
	$arr_element_twos=explode("\n", $element_two);

	if(!$err)
		{
		foreach($arr_element_twos as $arr_gr)
			{
			if(trim($arr_gr))
			mysql_query("INSERT INTO element_two (id_element, element_two) VALUES ('".$_SESSION['ses_s']['id_element']."', '".htmlspecialchars(rtrim($arr_gr))."')");
			}
		include('_svod_element_two.php');
		echo "<HTML><HEAD><meta http-equiv='Refresh' charset='utf-8' content='0; URL=refresh.php?i=".$Action."'></HEAD></HTML>";
		exit();
		}
	}

$titl="Список: ".$_SESSION['ses_s']['spisok']." (".$_SESSION['ses_s']['element'].")";
require ('top.php'); 

echo '<TABLE width=100%><TR><TD valign=top width=40%>';

if($err) echo '<p><font color=red>'.$err.'</font></p>';

if ($_REQUEST['edit'] && $_REQUEST['id_element_two'])
	{
	$rm = mysql_query ("SELECT * FROM element_two WHERE id_element_two='".$_REQUEST['id_element_two']."'");
	$rrm=mysql_fetch_array($rm);
	?>
	<form action="<? echo $Action; ?>" method="post" name="form_edit" >
		<table width="100%" cellspacing="0" cellpadding="0" >
			<tr>
				<td width="29%" >
 				<input name="id_element_two" type="hidden" value="<? echo $rrm['id_element_two']; ?>">
  				<input name="element_two" type="text" value="<? echo $rrm['element_two']; ?>" size="40">
				<td width="21%" >
  				<input class="submit" type="submit" name="edit" value="Изменить">
		</table>
	</form>
	<?
	}


if($_POST['paket_del'] && $_POST['ch'])
	{
	$del_element = implode(",",$_POST['ch']);
	echo '<table width=300 bordercolor=red border=3 cellSpacing=0 cellPadding=5><tr><td>
	<p>Вы действительно хотите удалить элементы под номерами <b>'.$del_element.'</b> ?</p>';
	?>
	<form action="<? echo $Action; ?>" method="post" name="form_del" >
 	<input name="del_element" type="hidden" value="<? echo $del_element; ?>">
 	<input class="submit" type="submit" name="del" value="ДА">
 	<input class="submit" type="submit" name="not" value="НЕТ">
	</form>
	<?
	echo '</td></tr></table>';
	}


if (!$_REQUEST['id_element_two'] && !$_POST['ch']) 
	{ 
	?>
	<form name="form_add" method="POST" action="<? echo $Action; ?>">
		<table width="100%" cellspacing="0" cellpadding="0" >
			<tr>
				<td>
				<textarea name="element_two" cols="40" rows="8" ></textarea> 
				<br>
				<input class="submit" type="submit" name="add" value="Добавить элемент списка 2-го уровня">
				<p><font color=green>Примечание:<br>Вы можете добавлять целым списком. Единственное условие - каждый элемент списка должет начинаться с новой строки</font></p>
		</table>
	</form>
	<?
	} 

echo '</TD><TD valign=top>';

$r = mysql_query ("SELECT * FROM element WHERE id_spisok='".$_SESSION['ses_s']['id_spisok']."' ORDER BY id_element") or die(mysql_error());


?>
<select name="java-navigator" onchange="top.location.href = this.options[this.selectedIndex].value;" size="1">
<?
while ($rr = mysql_fetch_assoc($r)) 
	{ 
	?>
	<option style="color: #009933" value="?id_element=<?=$rr['id_element']?>" <? if($rr['id_element']==$_SESSION['ses_s']['id_element']) echo 'selected'; ?>><?=$rr['element']?></option>
	<?
	}
	?>
</select>
<?
echo '<p><a href="element.php">К 1-му уровню списка: '.$_SESSION['ses_s']['spisok'].'</a></p>';

$r1 = mysql_query ("SELECT * FROM element_two WHERE id_element='".$_SESSION['ses_s']['id_element']."' ORDER BY num, element_two ASC") or die(mysql_error());
$nr1 = mysql_num_rows ($r1);
if ($nr1 <1) echo "<p>Список пуст</p>";

if ($nr1 >0)
	{
	echo '<form action="'.$Action.'" method=post >';
	echo '<TABLE cellpadding="1" cellspacing="1" >';
	$i=0;
	while ($rr1 = mysql_fetch_assoc($r1))
		{ 
		$i++;
		echo '<tr>';
		echo '<td width="30">';
		echo '<input type="checkbox" name=ch[] value="'.$rr1['id_element_two'].'" ';
		if($_POST['ch'] && in_array($rr1['id_element_two'],$_POST['ch'])) echo 'checked';
		echo ' >';
		echo '<td  width="30">';
 		echo '<a href = "'.$Action.'?edit=1&id_element_two='.$rr1['id_element_two'].'"><img hspace="2" src="../icon/edit.png" alt="Изменить название" border="0" ></a>';
		
		echo '<td  width="30">';
		if($i>1)
		echo ' <a title="Переместить на одну позицию вверх" href = "?id_el_two='.$rr1['id_element_two'].'&per=1"><img border=0 src="../icon/up.gif"></a>';
		
		echo '<td  width="30">';
		if($i<$nr1)
		echo ' <a title="Переместить на одну позицию вниз" href = "?id_el_two='.$rr1['id_element_two'].'&per=2"><img border=0 src="../icon/down.gif"></a>';
		
		echo '<td>';
		echo $rr1['element_two'];  
		}
	echo '</TABLE>';
	echo '<br>
	<input type="submit" class="submit" name="paket_del" value="Отмеченные удалить">';
	echo '</form>';
	}

echo '</TD></TR></TABLE>';
require ('bottom.php'); 
?>