<?php session_start();  //Developed by ITechScripts
$catname=$_SESSION['catname'];
$cat_id=$_SESSION['cat_id'];



require 'include/configurations.php'; // ITECHBIDS Configuration File
require 'include/header.php';
	
?>
<html>
<head>
<title>:::Auction:::Admin Area</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="stylesheet" href="../cssfiles/style.css"
</head>
<body>
<br>
<?

	if(isset($_POST['btnAdd']))
	 {
		$field=$_POST['txtField'];
		$dtype=$_POST['cboDtype'];
		$ftype=$_POST['cboFtype'];
		$length=$_POST['txtLength'];
		$tablename=$catname;
		$create_query="create table $tablename(tableid int(11) auto_increment primary key,productid int(11) null default '0',";
		for ($i=0;$i<count($field);$i++)
	    {
				  			$create_query.="$field[$i] $dtype[$i] ($length[$i]) null,";
		}
   $create_query=rtrim($create_query,", "); 		
   $create_query.=" )";
//   echo $create_query;
   $create_result=mysql_query($create_query);
   
	/* $ins_query="insert into site_categories(category_name,category_id,category_head_id) values('$catname',$region,0)";
	$ins_result=mysql_query($ins_query);
	$cat_head=mysql_insert_id($conn); */
	if($create_result)
	{
		$cat_ins_sql="insert into site_categories(category_name,category_head_id) values('$catname',0)";
		$cat_ins_res=mysql_query($cat_ins_sql);
		if($cat_ins_res)
		{
//			$err_mess="Category $category Sucessfully Added";
    $cat_id=mysql_insert_id(); 
	$filepath="themes/"."$catname".".tpl";
	$f=fopen("../".$filepath,"w");
    /*fwrite($f,"<table border=0 align=left width=100% cellpadding=5 cellspacing=2 class=box>\n");
    fwrite($f,"<form name='frm".$catname."' method=post action='<?php ship_itechd.php?>'>\n");  */
	for ($i=0;$i < count($field);$i++)
   {
    if($ftype[$i]=="textarea")
	{
     /*  fwrite($f,"tr><td><b><font size=2> $field[$i]</font></b></td></tr><tr><td><input type=$ftype[$i] name=$field[$i]></td></tr>\n");*/
    $val='$_SESSION['.$field[$i].']';
	fwrite($f,"<tr><td><b><font size=2>$field[$i]</font></b></td></tr><tr><td><textarea name=$field[$i] cols=15 rows=5><?=$val?></textarea></td></tr>\n");
	}
	else
	{
	$val='$_SESSION['.$field[$i].']';
    fwrite($f,"<tr><td><b><font size=2>$field[$i]</font></b></td></tr><tr><td><input type=$ftype[$i] name=$field[$i] value=<?= $val ?> ></td></tr>\n");
	}
	
   }
	
	/* fwrite($f,"<tr><td><input type=submit value=Continue></td></tr>");   
	fwrite($f,"</form>");    */
	//fwrite($f,"</table>");  
	$ins_query="insert into downcats(category_id,tablename,file_path) values($cat_id,'$tablename','$filepath')";
	$ins_result=mysql_query($ins_query);
	if($ins_result)
	$msg="CustomField Created Successfuly!";
	else
	$msg="Sorry!Problems in Customfield Creation!";
	} // if($cat_ins_res)
	else
	$msg="Sorry!Problems in Customfield Creation!";
	}
	}
	?>
<? if($msg)
{
?>
<table align="center" width="80%" cellpadding="5" cellspacing="2" >
<form name="frmCategory" method="post" action="custom_category.php">
<tr><td colspan="5" align="center"><font size="2" color="#FF0000"><b> <?= $msg; ?> </b> </font> </td></tr>
<tr align="center"><td><input type="submit" class="button"   value=Back ></td></tr>
</form>
</table>
<?
$msg="";
require 'include/footer.php';
exit();
}

		if(isset($_POST['btn_Create'])) 
		{
		$fieldnumber=$_POST['txtFieldno'];
		if($fieldnumber>15)
		 {
			$err_flag=1;
		}
		else
		{
		
?>
<table align="center" width="80%" cellpadding="5" cellspacing="2" class="ft">

<form name="frmCategory" method="post" action="definetable.php">
<tr >
<td><b>S.No</b></td>
<td><b>Fieldname</b></td>
<td><b>Data Type</b></td>
<td><b>Length(Range)</b></td>
<td><b>Field Type</b></td>
</tr>
<tr><td colspan="5" align="right"  bgcolor="#eeeee7"> <a href="#" id="dislink" onClick="window.open('custom_help.php','pop_up','toolbar=no, top=0,location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=400, height=400')"><font color=red>Help </font></a></td></tr>
<?
	for($i=1;$i<=$fieldnumber;$i++) 
	{
?>
<tr bgcolor="#eeeee7">
<td><?=$i?>.</td>
<td><input type="text" class="bluetext"name="txtField[]" ></td>
<td><select name="cboDtype[]">
<option value="">Select</option>
<option value="VARCHAR">VARCHAR</option>
<option value="TINYINT">TINYINT</option>
<!--<option value="TEXT">TEXT</option>
<option value="LONGTEXT">LONGTEXT</option>
 <option value="DATE">DATE</option>
<option value="DATETIME">DATETIME</option> -->
<option value="INT">INT</option>
<!-- <option value="DECIMAL">DECIMAL</option> -->
</select></td>
<td><input type="text" class="bluetext"name="txtLength[]" ></td>
<td>
<select name="cboFtype[]" class="cbo">
<option value="">Select</option>
<option value="text">Textbox</option>
<option value="textarea">Text Area</option>
<!-- <option value="file">File Field</option> -->
</select>
</td>
</tr>
<?
	}
?>
<tr bgcolor="#eeeee7"><td colspan="5" align="center"><font color="red">Note:&nbsp;All Fields are Required.</font></td></tr>
<tr bgcolor="#eeeee7"><td colspan="5" align="center"><input type="submit" class="button"   name="btnAdd" value=" Add " ></td></tr>
</form>
</table>
<br><br>
<?
 require 'include/footer.php';
?>
<?
		}
		if($type=="add") {
?>
<table align="center" width="80%" cellpadding="5" cellspacing="2" class="ft">
<?	
	if($err_flag==1) {
?>
<tr bgcolor="#EFEFE7">
<td class="ct" height="25"  colspan="2" align="center">You are Allowed to Create a Maximum of only 15 Fields</td>
</tr>
<tr bgcolor="#EFEFE7">
<td class="ct" height="25"  colspan="2" align="center"><a href="#" onClick="history.go(-1)" id="tablink">Back</a></td>
</tr>
<?
	}
?>
</table>
<?
		}
		if($type=="edit") {
	$modifysql="select * from ";
?>

<?
		}
		exit();
		}
?>
<table align="center" width="80%" cellpadding="5" cellspacing="2" class="ft">
<form name="frmCategory" method="post" action="<? $_SERVER['PHP_SELF']?>">
<tr >
<td class="ct" height="25" colspan="2"><b>	Define Structure for the Category <?=$catname;?></td> 
</tr>
<tr bgcolor="#EFEFE7">
      <td class="ct" height="25"  colspan="2"><b>If you like to add other fields for this 
        particular category.You can Define a Maximum of 15 user defined Fields.Please Don't Give Space between the Field Name.
      </td>
</tr>
<tr bgcolor="#EFEFE7">
<td class="ct" height="25"><b>Enter the Number of Fields you like to Create</td>
<td><input type="text" class="bluetext"name="txtFieldno" ></td>
</tr>
<tr bgcolor="#EFEFE7">
<td colspan="2" align="center"><input type="submit" class="button"   name="btn_Create"  value=" Create "></td>
</tr>
</form>
</table>
<br><br>
<?
 require 'include/footer.php';
?>
</body>