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

$Action = $_SERVER['PHP_SELF'];

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form")) 
{
$parent_id=0;

if($_POST['id_catalog'])
$parent_id=intval($_POST['id_catalog']);
	
$typ=intval($_POST['typ']);

$err="";

$catalog = htmlspecialchars(trim($_POST['catalog']));

if($catalog=="") $err .= "Не указано название<br>";
if($arr_typ_catalog && $typ)
	{
	$catalog_typ = $_POST['catalog_typ'];
	if(!$catalog_typ) $err .= "Не указан тип каталога<br>";
	}

if(!$err)
	{
    $r = mysql_query("SELECT max(num) as ma FROM catalog WHERE parent_id = '".$parent_id."'");
	$rr = mysql_fetch_array($r);
	$nums=$rr['ma'];
	$nums++;

	mysql_query("INSERT INTO catalog (catalog_typ, catalog,	parent_id, typ, num) 			
	VALUES ('".$catalog_typ."', '".$catalog."','".$parent_id."', '".$typ."', '".$nums."')"); 
	$id_catalog=mysql_insert_id();
	
	
	if (! (mkdir("../document/".$id_catalog)))
		{
		mysql_query("DELETE FROM catalog WHERE id_catalog='".$id_catalog."'");
		$err = "Ошибка при создании папки";
		} 
	else
		chmod("../document/".$id_catalog, 0755);	
	
	if(!$err)
		{
		include_once('_svod_catalog.php');

		mysql_query("UPDATE global SET cashe=NOW()");

		echo "<HTML><HEAD><meta http-equiv='Refresh' charset='utf-8' content='0; URL=refresh.php?i=str.php'></HEAD></HTML>";
		exit();
		}
	}
}

if(isset($_REQUEST['id_catalog'])) $id_catalog=$_REQUEST['id_catalog']; else $id_catalog="";
if(isset($_REQUEST['typ'])) $typ=$_REQUEST['typ']; else $typ=0;

if($typ) {$dob="каталог"; $dob2="Каталог";} else {$dob="папку"; $dob2="Папка";}

$titl="Добавить ".$dob;
require ('top.php'); 


 if($err) echo '<p><font color=red>'.$err.'</font></p>'; ?>
	<form name="form" method="POST" action="<? echo $Action; ?>">
      <table width="100%" border="0" cellspacing="2" cellpadding="1" >
        <tr>
          <td>
		  <?=$dob2?> (название)<br>
          <input size="50" name="catalog" value="<? echo $catalog; ?>">
	  <? if($typ) { ?>
        <tr>
          <td>
			<?
			echo 'Тип каталога<br>';
			echo '<select name="catalog_typ">';
			echo '<option value=""></option>';
		  	
			foreach($arr_typ_catalog as $index=>$arr_t)
				{
				if(file_exists("../plugin/".$index) && is_dir("../plugin/".$index))
					{
					?>
					<option value="<?=$index?>" <? if($index==$catalog_typ) echo 'selected'; ?> ><?=$arr_t['name']?></option>
					<?
					}
				}
			
			echo '</select>';
		} 
		?>
        <tr>
		  <td>
		  <input type="submit" class="submit" name="Submit" value="Добавить">
		 
		 
		 </table>
		      <input type="hidden" name="id_catalog" value="<? echo $id_catalog; ?>">
		      <input type="hidden" name="typ" value="<? echo $typ; ?>">
		      <input type="hidden" name="MM_insert" value="form">
    </form>
<?
require('bottom.php');
?>