<?php

require '../system/start.php';
require '../system/functions.php';
only_level(3);
$str['mesto'] = 'Обменник';
$str['block'] = $set['site'].' / Панель управления / Обменник';
include '../style/'.$style.'/head.php';
switch($act)
{
	default:
	echo '<div class="on"><a href="?act=sels">Управление разделами</a></div>
    <div class="on2"><a href="?act=usels">Управление подразделами</a></div>
    <div class="where"><a href="index.php">Админ-панель</a></div>';
	break;
    case 'sels':
    echo '<div class="mess"><a href="?act=add_sel">Добавить раздел</a></div>';
    $query = mysql_query("select * from `files_sel`");
    if(mysql_num_rows($query) == 0)
    {
        echo '<div class="forum1">Разделы еще не созданы!</div>';
    }
    else
    {
        while ($row = mysql_fetch_assoc($query))
        {
   	        $i++;
	       if($i%2)
	       {
		  		echo '<div class="forum1">';
	       }
            else
	       {
		      	echo '<div class="forum2">';
	       }
			echo $row['title'].'<br />
			<a href="?act=edit_sel&amp;id='.$row['id'].'">[ред]</a> <a href="?act=delete_sel&amp;id='.$row['id'].'">[удл]</a></div>';
        }
    }
    echo '<div class="where"><a href="index.php">Админ-панель</a></div>';
    break;
	case 'add_sel':
	if(isset($_POST['title']) && !empty($_POST['title']))
	{
		mysql_query ("INSERT INTO `files_sel` (title) VALUES ('".protect($_POST['title'])."')");
		echo '<div class="where">Раздел успешно создан!</div>';
	}
	echo '<div class="on"><form method="post" action="?act=add_sel">
	<div>
	Название:<br />
	<input type="text" name="title" maxlength="32" value="" /><br />
	<input type="submit" value="Добавить" />
	</div></form></div><div class="where"><a href="index.php">Админ-панель</a></div>';
	break;
	case 'edit_sel':
	$query = mysql_fetch_assoc(mysql_query("select * from `files_sel` where `id` = ".int($_GET['id']).""));
	if(empty($query['title']))
	{
		echo '<div class="on">Раздел не найден!</div>';
		include '../style/'.$style.'/foot.php';
		exit;
	}
	if(isset($_POST['title']) && !empty($_POST['title']))
	{
		$title = protect($_POST['title']);
		mysql_query ("UPDATE `files_sel` SET `title` = '".$title."' WHERE `id` = ".$query['id']."");
        echo '<div class="on">Раздел успешно обновлен!</div>
		<div class="where"><a href="index.php">Админ-панель</a></div>';
		include '../style/'.$style.'/foot.php';
		exit;
	}
	echo '<div class="on"><form method="post" action="?act=edit_sel&amp;id='.$query['id'].'">
	<div>
	Название:<br />
	<input type="text" name="title" maxlength="32" value="'.$query['title'].'" /><br />
    <input type="submit" value="Обновить" />
	</div></form></div><div class="where"><a href="index.php">Админ-панель</a></div>';
	break;
	case 'delete_sel':
	$query = mysql_fetch_assoc(mysql_query("select * from `files_sel` where `id` = ".int($_GET['id']).""));
	if(empty($query['title']))
	{
		echo '<div class="on">Раздел не найден!</div>';
		include '../style/'.$style.'/foot.php';
		exit;
	}
	mysql_query ("DELETE FROM `files_sel` WHERE `id` = ".$query['id']."");
	echo '<div class="on">Раздел успешно удален!</div>
	<div class="where"><a href="index.php">Админ-панель</a></div>';
	break;
    case 'usels':
    echo '<div class="mess"><a href="?act=add_usel">Добавить подраздел</a></div>';
    $query = mysql_query("select * from `files_usel` order by `id` limit ".$page.",".$set['str']."");
    $total = mysql_num_rows(mysql_query("select * from `files_usel`"));
    if(mysql_num_rows($query) == 0)
    {
        echo '<div class="forum1">Подразделы еще не созданы!</div>';
    }
    else
    {
        while ($row = mysql_fetch_assoc($query))
        {
            $i++;
	       if($i%2)
	       {
		      echo '<div class="forum1">';
	       }
        else
	       {
		      echo '<div class="forum2">';
	       }
            echo $row['title'].'<br />
            <a href="?act=edit_usel&amp;id='.$row['id'].'">[ред]</a> <a href="?act=delete_usel&amp;id='.$row['id'].'">[удл]</a></div>';
	   }
       if($total > $set['str'])
        {
	       pages($page, $total, 'files.php?act=usels&amp;', $set['str']);
        }
    }
    echo '<div class="where"><a href="index.php">Админ-панель</a></div>';
    break;
    case 'add_usel':
    $query = mysql_query("select * from `files_sel` order by `id`");
    if(mysql_num_rows($query) == 0)
    {
       echo '<div class="forum1">Разеделы еще не созданы!</div>';
    }
    else
    {
	    if(isset($_POST['title']) && !empty($_POST['title']))	{
			$format = '';
			// вычисление разрешений
			for($i = 1; $i <= 28; $i++) {
				
				if ($_POST['format' . $i])
					$format .= $_POST['format' . $i] . ','; 
			}
		
			mysql_query("INSERT INTO `files_usel` SET `id_sel` = '" . int($_POST['id_sel']) . "',
													  `title` = '" . protect($_POST['title']) . "',
													  `format` = '" . $format ."'");
		  echo '<div class="where">Подраздел успешно создан!</div>' . $format;
	   }
	   echo '<div class="on"><form method="post" action="?act=add_usel">
	   <div>
	   Название:<br />
	   <input type="text" name="title" maxlength="32" value="" /><br />
       Раздел:<br />
       <select size="1" name="id_sel">';
       while($row = mysql_fetch_assoc($query))
       {
            echo '<option value="'.$row['id'].'">'.$row['title'].'</option>';
       }
        echo '</select><br/>
		Расширения:<br />
		<input name="format1" type="checkbox" value="gif" checked /> - gif<br />
		<input name="format2" type="checkbox" value="jpg" checked /> - jpg<br />
		<input name="format3" type="checkbox" value="jpeg" checked /> - jpeg<br />
		<input name="format4" type="checkbox" value="png" checked /> - png<br />
		<input name="format5" type="checkbox" value="mp3" checked /> - mp3<br />
		<input name="format6" type="checkbox" value="3gp" checked /> - 3gp<br />
		<input name="format7" type="checkbox" value="3gpp" checked /> - 3gpp<br />
		<input name="format8" type="checkbox" value="jar" checked /> - jar<br />
		<input name="format9" type="checkbox" value="jad" checked /> - jad<br />
		<input name="format10" type="checkbox" value="zip" checked /> - zip<br />
		<input name="format11" type="checkbox" value="rar" checked /> - rar<br />
		<input name="format12" type="checkbox" value="thm" checked /> - thm<br />
		<input name="format13" type="checkbox" value="nth" checked /> - nth<br />
		<input name="format14" type="checkbox" value="sis" checked /> - sis<br />
		<input name="format15" type="checkbox" value="sys" checked /> - sys<br />
		<input name="format16" type="checkbox" value="m4a" checked /> - m4a<br />
		<input name="format17" type="checkbox" value="mmf" checked /> - mmf<br />
		<input name="format18" type="checkbox" value="mid" checked /> - mid<br />
		<input name="format19" type="checkbox" value="midi" checked /> - midi<br />
		<input name="format20" type="checkbox" value="amr" checked /> - amr<br />
		<input name="format21" type="checkbox" value="wav" checked /> - wav<br />
		<input name="format22" type="checkbox" value="aac" checked /> - aac<br />
		<input name="format23" type="checkbox" value="swf" checked /> - swf<br />
		<input name="format24" type="checkbox" value="mtf" checked /> - mtf<br />
		<input name="format25" type="checkbox" value="txt" checked /> - txt<br />
		<input name="format26" type="checkbox" value="avi" checked /> - avi<br />
		<input name="format27" type="checkbox" value="mp4" checked /> - mp4<br />
		<input name="format28" type="checkbox" value="sisx" checked /> - sisx<br />
	   <input type="submit" value="Добавить" />
	   </div></form></div><div class="where"><a href="index.php">Админ-панель</a></div>';
    }
    break;
    case 'edit_usel':
    $q = mysql_query("select * from `files_sel` order by `id`");
	$query = mysql_fetch_assoc(mysql_query("select * from `files_usel` where `id` = ".int($_GET['id']).""));
	if(empty($query['title']))
	{
		echo '<div class="on">Подраздел не найден!</div>';
		include '../style/'.$style.'/foot.php';
		exit;
	}
	if(isset($_POST['title']) && !empty($_POST['title']))
	{
		
		// расширения
		$format = '';
		for($i = 1; $i <= 28; $i++) {
				
				if ($_POST['format' . $i])
					$format .= $_POST['format' . $i] . ','; 
		}
		
		if ($format != '')
			$zap = ",`format` = '" . $format . "'";
		
		$title = protect($_POST['title']);
		
		mysql_query("UPDATE `files_usel` SET `id_sel` = '" . int($_POST['id_sel']) . "',
											 `title` = '" . $title . "'" . $zap . "
											  WHERE `id` = '" . $query['id'] . "'");
		
        echo '<div class="on">Подраздел успешно обновлен!</div>
		<div class="where"><a href="index.php">Админ-панель</a></div>';
		include '../style/'.$style.'/foot.php';
		exit;
	}
	echo '<div class="on"><form method="post" action="?act=edit_usel&amp;id='.$query['id'].'">
	<div>
	Название:<br />
	   <input type="text" name="title" maxlength="32" value="'.$query['title'].'" /><br />
       Раздел:<br />
       <select size="1" name="id_sel">';
       while($row = mysql_fetch_assoc($q))
       {
            echo '<option value="'.$row['id'].'">'.$row['title'].'</option>';
       }
        echo '</select><br/>
   	   Описание: (если надо)<br />
	   <input type="text" name="des" maxlength="32" value="" /><br />
	   Расширения:(если не менять оставить пустым)<br />
		<input name="format1" type="checkbox" value="gif" /> - gif<br />
		<input name="format2" type="checkbox" value="jpg" /> - jpg<br />
		<input name="format3" type="checkbox" value="jpeg" /> - jpeg<br />
		<input name="format4" type="checkbox" value="png" /> - png<br />
		<input name="format5" type="checkbox" value="mp3" /> - mp3<br />
		<input name="format6" type="checkbox" value="3gp" /> - 3gp<br />
		<input name="format7" type="checkbox" value="3gpp" /> - 3gpp<br />
		<input name="format8" type="checkbox" value="jar" /> - jar<br />
		<input name="format9" type="checkbox" value="jad" /> - jad<br />
		<input name="format10" type="checkbox" value="zip" /> - zip<br />
		<input name="format11" type="checkbox" value="rar" /> - rar<br />
		<input name="format12" type="checkbox" value="thm" /> - thm<br />
		<input name="format13" type="checkbox" value="nth" /> - nth<br />
		<input name="format14" type="checkbox" value="sis" /> - sis<br />
		<input name="format15" type="checkbox" value="sys" /> - sys<br />
		<input name="format16" type="checkbox" value="m4a" /> - m4a<br />
		<input name="format17" type="checkbox" value="mmf" /> - mmf<br />
		<input name="format18" type="checkbox" value="mid" /> - mid<br />
		<input name="format19" type="checkbox" value="midi" /> - midi<br />
		<input name="format20" type="checkbox" value="amr" /> - amr<br />
		<input name="format21" type="checkbox" value="wav" /> - wav<br />
		<input name="format22" type="checkbox" value="aac" /> - aac<br />
		<input name="format23" type="checkbox" value="swf" /> - swf<br />
		<input name="format24" type="checkbox" value="mtf" /> - mtf<br />
		<input name="format25" type="checkbox" value="txt" /> - txt<br />
		<input name="format26" type="checkbox" value="avi" /> - avi<br />
		<input name="format27" type="checkbox" value="mp4" /> - mp4<br />
		<input name="format28" type="checkbox" value="sisx" /> - sisx<br />
        <input type="submit" value="Обновить" />
	</div></form></div><div class="where"><a href="index.php">Админ-панель</a></div>';
	break;
    case 'delete_usel':
	$query = mysql_fetch_assoc(mysql_query("select * from `files_usel` where `id` = ".int($_GET['id']).""));
	if(empty($query['title']))
	{
		echo '<div class="on">Подраздел не найден!</div>';
		include '../style/'.$style.'/foot.php';
		exit;
	}
	mysql_query ("DELETE FROM `files_usel` WHERE `id` = ".$query['id']."");
	echo '<div class="on">Подраздел успешно удален!</div>
	<div class="where"><a href="index.php">Админ-панель</a></div>';
	break;
}
include '../style/'.$style.'/foot.php';
?>