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

$id_blok=$_REQUEST['id_blok'];
$id_catalog=$_REQUEST['id_catalog'];

if(isset($_POST['MM_insert']) && $_POST['MM_insert']=="form1")
	{
	$zag = trim($_POST['zag']);
	$opis = trim($_POST['opis']);
	$alt = trim($_POST['alt']);

	$r1=mysql_query("SELECT num FROM subblok WHERE id_blok='".$id_blok."' AND id_catalog='".$id_catalog."' ORDER BY num DESC LIMIT 1");
	$rr1=mysql_fetch_array($r1);
	if($rr1['num']) $new_num=$rr1['num']+1; else $new_num=1;

	$base="image/";

	if (isset($_FILES['fil']['tmp_name']) && (!empty ($_FILES['fil']['tmp_name'])))
		{
		$ext = type_img($_FILES['fil']['type']);
		if ($ext != ".jpg" && $ext != ".png" && $ext != ".gif") 
		$err .= "Допускаются только форматы JPEG, PNG, GIF<br>";
		}

	if(!$err)
		{
		mysql_query("INSERT INTO subblok (id_blok, id_catalog, zag, opis, alt, num) 
		VALUES ('".$id_blok."', '".$id_catalog."', '".$zag."', '".$opis."', '".$alt."', '".$new_num."')");
		$ins=mysql_insert_id();

		if (!empty ($_FILES['fil']['tmp_name']))
			{
			$adres_small = $base.$ins."ssblok".$ext."";
			if($_POST['r_foto_mini']<50) $_POST['r_foto_mini']=50;
			if (!smalling ($_FILES['fil']['tmp_name'], "../".$adres_small, $_POST['r_foto_mini'], $_POST['r_mini'], $ext)) 
			$adres_small = "";
 			mysql_query("UPDATE subblok SET small='".$adres_small."' WHERE id_subblok='".$ins."'");                    
			}

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

$titl="Добавление записи";
require ('top.php'); 

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

?>
	
	<form enctype="multipart/form-data" name="form1" method="POST" action="add_blok.php">
      <table width="738" border="0" cellspacing="0" cellpadding="0" >
        <tr>
          <td >Заголовок</td>
          <td>
		  <textarea name="zag" cols="40" rows="1" ></textarea>
		  </td>
          </tr>
        <tr>
          <td >Текст</td>
          <td><textarea name="opis" cols="50" rows="15"></textarea></td>
          </tr>
		  
		<tr>
          <td >Alt</td>
          <td><textarea name="alt" cols="40" rows="1"></textarea></td>
          </tr>
		  
		  
    <tr>
    <td valign="top">Размер фото в пикселях</td>
    <td><input type="text" name="r_foto_mini" size="30" value="<?  echo $rru['r_foto_mini']; ?>">
      <br><input name="r_mini" type="radio" value="1" <? if($rru['r_mini']==1) echo "checked"; ?>> вписанный в квадрат
      <br><input name="r_mini" type="radio" value="2" <? if($rru['r_mini']==2) echo "checked"; ?>> по горизонтали
      <br><input name="r_mini" type="radio" value="3" <? if($rru['r_mini']==3) echo "checked"; ?>> по вертикали
      <br><input name="r_mini" type="radio" value="4" <? if($rru['r_mini']==4) echo "checked"; ?>> обрезанный до квадрата 
	</td>
  </tr>
       <tr>
          <td >Загрузка фото</td>
          <td><input type="file" name="fil" size= "63" ></td>
        </tr>
        <tr>
          <td >&nbsp;</td>
          <td><input class="submit" type="submit" name="Submit" value="Добавить"></td>
          </tr>
      </table>
      <input type="hidden" name="MM_insert" value="form1">
      <input type="hidden" name="id_blok" value="<? echo $id_blok; ?>">
      <input type="hidden" name="id_catalog" value="<? echo $id_catalog; ?>">
    </form>
<?
require('bottom.php');
?>