<?php
if(!file_exists("../conn.php")){
header("Location: index.php");
exit();
}
include "../conn.php";
include "../includenp/includefile.php";
include "installsql.php";
 $edit=false;
 $edit_id=0; 
 $displayorder='';
 $name='';
 $field_type=1;
 $table_name='';
 $tableparameter='';
 
$tablename="mastertable";
if(isset($_GET['editid']))
{
  $edit_id=$_GET['editid']; 
  $edit=true; 
  }
   if($edit){  
		$pdelre =array(); 
		$pdelr = "select 
		*
		from 
		".$tablename."		where  id ='$edit_id'";
		
		$pdelq = mysql_query($pdelr,$eclipse);
		$pdelre = mysql_fetch_array($pdelq);
		extract ($pdelre);
		
		$tableparameter=" WHERE id!='$id'";
		}
  
  
  if(isset($_POST['savegau'])){
  $mendatry_check_array=array('name'); // mention all field which is mendetary
 $unset_array=array('savegau','edit_id');
 
 if(isset($_POST['id'])){
   $edit_id=$_POST['id'];  
   }
    $tempdata=$post_data=$_POST;

 extract ($tempdata);
   
   
$rmsg = "";
$proceed ="";	

if(strlen($edit_id)>0)
$editcond='and id  !='. $edit_id;
else $editcond="";

	 
	foreach($mendatry_check_array as $key=>$value)
	{	
		 if(strlen($$value) < 1)
		 {
		 $proceed = $proceed + 1; 	
		 }	
	}
	
	
	 
	
	 
	 if($proceed>0)
	 {
$rmsg='Please ensure all mandatory fields (marked with *) are filled in';
}
else{
			
			
		 $query="SELECT name FROM ".$tablename." where name='$name' ".$editcond;	
					$result=mysql_query($query,$eclipse);
					$row=mysql_num_rows($result);
					if($row > 0)
					{
					$proceed = $proceed + 1; 	
					$rmsg = $rmsg . "<li>Name already Exist</li>";
					}  
	}
	 
	if($proceed < 1){
	
foreach($unset_array as $key=>$value)
unset($post_data[$value]);
			 foreach( $post_data as $k=>$v){
			 $sql_data_array[$k] = $v;
					 }
					 
					 
		
		if($edit_id>0){
		
		
			$updatecond="id ='$edit_id'";
			tep_db_perform($tablename, $sql_data_array,"update",$updatecond);		
			$rmsg = " Record updated";
			extract ($tempdata);
		}else{			
			
			tep_db_perform($tablename, $sql_data_array,"insert");		
			$rmsg = " Record inserted";
			
			
			foreach($tempdata as $key=>$value)
			{	
			$$key='';		
					}
				//extract ($tempdata);  
	
	 
		}
	
				
	
	
	}
	else
	{
	$haderr="yes";
	$rmsg = $rmsg;
	}
 

  
  } 
  
  if($edit) $bmsg="UPDATE"; else $bmsg="ADD";
  
  
  function del_master($id,$tablename='')
			{
			
			
			 $query="SELECT s.id FROM ".$SINGLE_FIELD_TABLE." s,			 
			 ".MASTERTABLENAME." m			 
			  WHERE 
			  	 m.id   ='$id' and
			  	 s.mastertableid    = m.id   and 
				 m.field_type=2	  ";	
					
					
					
					
					$result=mysql_query($query,$eclipse);
					$row=mysql_num_rows($result);
					if($row > 0)
					{
					while($data=mysql_fetch_array($result)){
					$single_field_id=$data['id'];
					$query="DELETE FROM ".DOUBLE_FIELS_TABLE." WHERE  single_field_id    ='$single_field_id'";
					mysql_query($query);
					}
					}  
			
			
			
			$query="DELETE FROM ".SINGLE_FIELD_TABLE." WHERE  mastertableid    ='$id'";
			mysql_query($query);
			
			$query="DELETE FROM ".MASTERTABLENAME." WHERE  id   ='$id'";
			mysql_query($query);
			
			}



$urlstr = "";


if (isset($_POST['delete']) && isset($_POST['chk']))
{
	$del_all=$_POST['chk'];
	
	$j= 0;
	for($i=0;$i<count($del_all);$i++)
	{
		if(is_numeric($del_all[$i]))
		{
			$dellid = $del_all[$i];
			del_master($dellid,$primary_table_name);
					
			
			$j = $j +1;
		}
	}
	$rmsg = $j ." records deleted";
}
  
  

?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Control Pannel</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link href="ssi.css" rel="stylesheet" type="text/css">
<script language="javascript" src="functions.js"></script>
<script language="javascript">
<!--
function countChecks1() {
	var abc = confirm("Do you really want to delete this record?");
	if(abc) {
	
	} else {
		return false;
	}
}
function pageHandler(form){
var URL = document.pagescounter.site.options[document.pagescounter.site.selectedIndex].value;
window.location.href=URL;
}
//-->
</script>
</head>
<body>
<table width="98%" border="0" cellspacing="0" cellpadding="2" align="center">
  <tr>
    <td width="1%"></td>
	<td width="98%"></td>
	<td width="1%"></td>
  </tr>
  <tr>
   <td width="1%"></td>
    <td width="98%"><table width="100%"  border="0" cellpadding="0" cellspacing="0" class="centertable">
      <tr>
        <td><table width="90%"  border="0" align="center" cellpadding="0" cellspacing="0">
          <tr>
            <td height="50">&nbsp;</td>
          </tr>
          <tr>
            <td class="hhead">
		 
			Manage Master Table </td>
          </tr>
          <tr>
            <td>
              <table width="100%"  border="0" align="center" cellpadding="0" cellspacing="0">
          <tr>
            <td><?php 
		  if(isset($rmsg))
		  	{
			$errmsg = $rmsg;
			?>
			<table width="100%"  border="0" cellpadding="0"  class="errborder">
  <tr>
    <td>
<table width="100%"  border="0" cellpadding="2" cellspacing="0">
  <tr>
    <td width="9%" align="center">&nbsp;</td>
    <td width="91%" class="errFont"><?php echo $errmsg; ?></td>
  </tr>
</table>
</td>
  </tr>
</table>
<?php 
			
		   	}
			?>
            </td>
          </tr>
          <tr>
            <td>
              <table width="100%"  border="0" cellpadding="2" cellspacing="0">
			  <tr class="bluebold">
			    <td colspan="5">				
				
				 <form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>" name="save" >
				
				 <input type="hidden" name="edit_id" value="<?php  echo $edit_id;?>">
				 <table width="100%"  border="0" cellpadding="2" cellspacing="0">
			  <tr class="bluebold">
			    <td width="12%">Display Order </td>
			    <td width="27%" align="center">Name*</td>
			    <td width="20%" align="center">Field Type </td>
			    <td colspan="2">Select Primary Table When Field Type is 2 </td>
			    </tr>
			  <tr class="bluebold">
			    <td><input name="displayorder" type="text" value="<?php echo $displayorder; ?>" size="4" /></td>
			    <td><span class="normalTxt">
			      <input name="name" type="text" value="<?php echo $name; ?>" size="30" />
			    </span></td>
			    <td align="center"><span class="normalTxt">
				<select name="field_type" >
				<option value="1" <?php if($field_type==1) echo "selected"; ?>>1</option>
				<option value="2" <?php if($field_type==2) echo "selected"; ?>>2</option>				
				</select>
			
			    </span></td>
			    <td width="24%" align="center"><?php echo draw_dynamic_pull_down_menue('table_name',$tablename,'id','name',$tableparameter,  $table_name,  '','select ','n');?> </td>
			    <td width="17%">&nbsp;</td>
			    </tr>
			  <tr class="bluebold">
			    <td>&nbsp;</td>
			    <td>&nbsp;</td>
			    <td>&nbsp;</td>
			    <td colspan="2"><input name="savegau" type="submit" class="hbutton" id="savegau3" value="<?php echo $bmsg;?>"></td>
			    </tr>
			  <tr class="bluebold">
			    <td>&nbsp;</td>
			    <td>&nbsp;</td>
			    <td>&nbsp;</td>
			    <td colspan="2">&nbsp;</td>
			    </tr>
			  <tr class="bluebold">
			    <td>&nbsp;</td>
			    <td>&nbsp;</td>
			    <td>&nbsp;</td>
			    <td colspan="2"><a href="master.php">New </a></td>
			    </tr>
				 </table>
				</form>
				
				</td>
			    </tr>

                  <tr>
                    <td colspan="5">
			<?php 				
			$sqln=" SELECT id FROM  ".$tablename ." ORDER BY displayorder";		
			$resultn = mysql_query($sqln,$eclipse); 
			$numrows = $row = mysql_num_rows($resultn);
     			if($numrows > 0 )
				{
				?>
				<form action="<?php echo $_SERVER['PHP_SELF'];?>" method="post"  name="form1" ID="form1">
				<TABLE class=table cellSpacing=3 cellPadding=3 width="100%" align=center border=0>
                      <TR valign="top" class="thbg">					
					  <TH align="center" class="bluebold">Select</TH>					 
                      <TH align="center" class="bluebold">Edit</TH>
                      <TH align="center" class="bluebold">ID</TH>
                      <TH align="center" class="bluebold">Display Order </TH>					   
					  <TH align="center" class="bluebold">Name</TH>
					  <TH width="11%" align=center class="bluebold">Field Type </TH>
                       <TH width="31%" align="center" class="bluebold">Drop Down Table Name </TH>
					                          </TR>
			 <?php			 
			 $sql="SELECT * FROM  ".$tablename ." ORDER BY displayorder";			 
			 $result = mysql_query($sql,$eclipse); 
			 while ($row = mysql_fetch_array($result))
			 {								
			 extract($row); 
			 ?>
				<TR valign="top" class="trbg">
				
					<TD width="12%" align=center><input name="chk[]" type="checkbox" id="chk[]" value="<?php  echo $id;?>" size="4"></TD>
					<TD width="12%" align=center><a href="master.php?editid=<?php echo $id; ?>">Edit</a></TD>
					<TD width="7%" align=center><?php  echo $id;?></TD>
					<TD  align=center><?php echo $displayorder;?>								      </TD>
					
					<TD width="29%" align=center class="normalTxt"><?php echo $name;?></TD>
					<TD align="center" class="normalTxt"><?php echo $field_type; ?></TD>
					<TD align="center" class="normalTxt"><?php echo get_single_field($tablename,'name','id',$table_name); ?></TD>
					</TR>	

		
<?php
}				
?>
  
<TR align="left">
                      <td colspan="7">
					 
					
					<input name="delete" type="submit"  onClick="return countChecks1 ()" class="hbutton" id="delete" value="DELETE SELECTED"></td>
			</TR>	
			
                </TABLE>
				</form>
				<?php				
					}
				else
				{
				?>
<TABLE class=table cellSpacing=1 cellPadding=0 
                                width="100%" align=center border=0>
                 
                    <TR>
                      <TD align=center class="bluebold">No Records to view </td>
                    </TR>
                 
                </TABLE>
				<?php
				}
				?></td>
                  </tr>
           
            </table></td>
          </tr>
        </table></td>
          </tr>
        </table></td>
      </tr>
    </table></td>
    <td width="1%"></td>
  </tr>
  <tr>
    <td width="1%"></td>
	<td></td>
	<td width="1%"></td>
  </tr>
</table>
</body>
</html>