<link href="ssi.css" rel="stylesheet" type="text/css">
<?php
define("MASTERTRCLASS","thbg");
define("NAMECLASS","bluebold");
define("NAMELINKCLASS","normalTxt");




$masterid=0;
$mode="view";
$CHILDNAME='';
$selfpageurl=$_SERVER['PHP_SELF'];

if(isset($_REQUEST['masterid']))
$masterid=$_REQUEST['masterid'];

if(isset($_REQUEST['mode']))
$mode=$_REQUEST['mode'];





if($masterid==0)
{

?>

<?php 				
			$sqln=" SELECT id FROM  ".MASTERTABLENAME ." ORDER BY displayorder";
			
			$resultn = mysql_query($sqln,$eclipse); 
			$numrows = $row = mysql_num_rows($resultn);
     			if($numrows > 0 )
				{
				?>				
				<TABLE class=table cellSpacing=3 cellPadding=3 width="80%" align=center border=0>
                      <TR valign="top" class="<?php echo MASTERTRCLASS;?>" >					
					  <TH align="left" class="<?php echo NAMECLASS;?>">Name</TH>
				  </TR>
			 <?php			 
			 $sql="SELECT * FROM  ".MASTERTABLENAME ." ORDER BY displayorder";			 
			 $result = mysql_query($sql,$eclipse); 
			 while ($row = mysql_fetch_array($result))
			 {								
			 extract($row); 
			 ?>
				<TR valign="top" class="trbg">
					<TD width="75%" align=left class="<?php echo NAMELINKCLASS;?>"><a href="<?php $selfpageurl;?>?masterid=<?php echo $id;?>&mode=view"><?php echo $name;?></a></TD>
				  </TR>
				  <?php
					}				
					?>  
</TABLE>				
				<?php				
					}				
} /// end of view master table name

if($masterid>0)
{
$sql="SELECT name as mastertablename, field_type , table_name as secondfieldid FROM  ".MASTERTABLENAME ." WHERE id='$masterid'";			 
				
			  $result = mysql_query($sql,$eclipse); 
			 while ($row = mysql_fetch_array($result))
			 {								
			 extract($row); 
			 }
$primary_table_name=SINGLE_FIELD_TABLE;
if($secondfieldid!='')
{
$mastertablename1=get_field_name("mastertable","name","id ",$secondfieldid);
$CHILDNAME=$mastertablename;
$PARENTNAME=$mastertablename1;
}
//$PARENTNAME=$mastertablename;
else
{
$PARENTNAME=$mastertablename;
$CHILDNAME=$mastertablename;
}
if($mode=="view" || $mode=="delete")
{	

$perpage=15;

$serchstr='';
$searchsql='';
$TABLE_NAME=$primary_table_name;
$FIELD_SELECT=" 
*
 ";
$SINGLE_FIELD_SELECT=" id  ";
$ORDER_BY=" ORDER BY display_order   ";
$WHERE_CLOUSE=" WHERE  mastertableid='$masterid' ";

$urlstr='';

		function del_master($id,$tablename)
			{
			$query="DELETE FROM ".$tablename." WHERE  id   ='$id'";
			mysql_query($query);			
			$query="DELETE FROM ".DOUBLE_FIELS_TABLE." WHERE  single_field_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";
}
//en


 			  
?>
<table width="80%"  border="0" align="center" cellpadding="0" cellspacing="0">
          <tr>
            <td height="50" class="normalBoldblackTxt">Manage <?php echo $mastertablename;?></td>
          </tr>
          <tr>
            <td height="20" align="left" class="bluelogTxt"><a href="<?php $selfpageurl;?>?masterid=<?php echo $masterid;?>&mode=new">New</a> </td>
          </tr>
          <tr>
            <td><?php 
		  if(isset($rmsg))
		  	{
			echo display_error_msg($rmsg);
		   	}
			?>
            </td>
          </tr>
          <tr>
            <td>
              <table width="100%"  border="0" cellpadding="2" cellspacing="0">
              
                  <tr>
                    <td width="100%">
			<?php 
			/*$sqln = "SELECT id 
			FROM products";*/

			
			$sqln=$SQL_QUERY=" SELECT ".$SINGLE_FIELD_SELECT." FROM ".$TABLE_NAME.$WHERE_CLOUSE;
			$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">
				 <input type="hidden" name="delete" value="<?php  echo $mode;?>">
				 <input type="hidden" name="masterid" value="<?php  echo $masterid;?>">
				<TABLE class=table cellSpacing=3 cellPadding=3 width="100%" align=center border=0>
                      <TR valign="top" class="thbg">					
					  <TH width="14%" align="center" class="bluebold">Select</TH>					 
                      <TH width="16%" align="center" class="bluebold">Edit</TH>					
					  <TH align="center" class="bluebold"><?php echo $CHILDNAME;?></TH>					 
					  <TH width="30%" align=center class="bluebold"><span class="normalTxt">Sort Order</span></TH>
                       </TR>
			 <?php				
			if(!isset($_GET['page'])){$_GET['page']=1;} 
			if (!isset($recordstart)) {$recordstart=1;}
			
			$recordstart = ($_GET['page']-1)*$perpage; 
			
		 	 if(isset($_GET['page']))
			 {
		     $itk = ($_GET['page']* $perpage) - $perpage + 1;
		 	 }
			 	
			 /*$sql = "SELECT
			 * from products";*/
			 
			  $sql=$SQL_QUERY=" SELECT ".$FIELD_SELECT." FROM ".$TABLE_NAME.$WHERE_CLOUSE.$ORDER_BY."LIMIT $recordstart,$perpage";
			 $result = mysql_query($sql,$eclipse); 
			 while ($row = mysql_fetch_array($result))
			 {								
			 extract($row); 
			 ?>
				<TR valign="top" class="trbg">
				
				  <TD width="10%" align=center><input name="chk[]" type="checkbox" id="chk[]" value="<?php  echo $id;?>" size="4"></TD>
					<TD width="9%" align=center><a href="<?php $selfpageurl;?>?masterid=<?php echo $masterid;?>&mode=edit&editid=<?php echo $id; ?><?php echo $urlstr;?>"><img src="panelimages/b_edit.png" width="16" height="16" border="0"></a>					    </TD>
					
					<TD width="41%" align=center class="normalTxt"><?php echo $name;?></TD>
					
					<TD align="center" class="normalTxt"><?php echo $display_order;?></TD>
					
				  </TR>	

		
<?php
}				
?>
 
<TR align="left">
                      <td align="center"><a href="JavaScript:checkAll(document.form1)" class="normalLink">
            Check All</a>	</td>
					 <td align="center">   <a href="JavaScript:uncheckAll(document.form1)" class="normalLink">Unckeck All</a>   </td>
					 <td>
							           
			               <input name="delete" type="submit"  onClick="return docheck ()" class="hbutton" id="delete" value="DELETE SELECTED"></td>
			<td>&nbsp;</td>
			</TR>	
			
                </TABLE>
				</form>
				

<?php 
/*$query = "SELECT
			 * from products";*/
$query=$sqln;
$result = mysql_query($query); 
$totalreturned = mysql_num_rows($result); 
$pagecount = ceil($totalreturned/$perpage);     
$recordstart = ($_GET['page']-1)*$perpage; 
?>
<TABLE class=table cellSpacing=1 cellPadding=0 
                                width="100%" align=center border=0>
                 
                    <TR>
                      <TD align=center class="bluebold">
					<?php
					$urlstr="&mode=view&masterid=".$masterid;
					echo page_records($_GET['page'],$pagecount,$recordstart,$perpage,$urlstr); 
?></td>
                    </TR>
                 
                </TABLE>

				<?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>

<?php } //if($mode=="view" || $mode="delete") ?>
<?php if($mode=="new" || $mode=="edit"){	
$edit_id=0;
$name='';
$display_order=0;
$edit=false;
$second_fieldid=0;

if(isset($_GET['editid'])){
  $edit_id=$_GET['editid']; 
  $edit=true;
  
  }

if($edit){  
		$pdelre =array(); 
		$pdelr = "select 
		*
		from 
		".$primary_table_name."
		where  id ='$edit_id'";
		$pdelq = mysql_query($pdelr,$eclipse);
		$pdelre = mysql_fetch_array($pdelq);
		extract ($pdelre);	
 			 }
 


		if (isset($_POST['savegau']))
			{
	if(isset($_POST['edit_id'])){
   	$edit_id=$_POST['edit_id']; 
   }
   
  
    $tempdata=$post_data=$_POST;

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

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

	 
	 if(strlen($name) < 1)
		 {
		 $proceed = $proceed + 1; 	
		 }	
		 if(strlen($second_fieldid) < 1)
		 {
		 $proceed = $proceed + 1; 	
		 }	
		
	if($field_type==2){
	 if(strlen($second_fieldid) < 1)
		 {
		 $proceed = $proceed + 1; 	
		 }	
	
	}
	 
	
	 
	 if($proceed>0){
$rmsg='Please ensure all mandatory fields (marked with *) are filled in';
}
else{
			
			
		 $query="SELECT name FROM ".$primary_table_name." where name ='$name' and mastertableid='masterid' ".$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){
	

		$sql_data_array['name']=$post_data['name'];	
		$sql_data_array['display_order']=$post_data['display_order'];	
		$sql_data_array['mastertableid']=$masterid;	
		 
		 if(isset($post_data['second_fieldid']))
			$sql_data_array_second['second_fieldid']=$post_data['second_fieldid'];	
							 
		
		if($edit_id>0)
		{	
		
			$updatecond="id ='$edit_id'";
			$singlefieldid=$edit_id;
			tep_db_perform($primary_table_name, $sql_data_array,"update",$updatecond);	
			
			 if(isset($post_data['second_fieldid']))
			 {
			$updatecond="single_field_id='$singlefieldid'";
			tep_db_perform(DOUBLE_FIELS_TABLE, $sql_data_array_second,"update",$updatecond);	
			}
			
			$rmsg = " Record updated";
			
		}else{			
			
			tep_db_perform($primary_table_name, $sql_data_array,"insert");		
			$singlefieldid=mysql_insert_id();
			
			 if(isset($post_data['second_fieldid'])){
			$sql_data_array_second['single_field_id']=$singlefieldid;	
			tep_db_perform(DOUBLE_FIELS_TABLE, $sql_data_array_second,"insert");
			}
			
			
			$rmsg = " Record inserted";			
			
			
			
			$name='';
			$display_order=0;
			$second_fieldid=0;			
				//extract ($tempdata); 
		}
	
				
	
	
	}
	else
	{
	$haderr="yes";
	$rmsg = $rmsg;
	}
}
			


?>

<form method="POST" action="<?php echo $_SERVER['PHP_SELF'];?>" name="frmLocation"  ID="SaveForm" >
                 <input type="hidden" name="edit_id" value="<?php  echo $edit_id;?>">
				 <input type="hidden" name="mode" value="<?php  echo $mode;?>">
				 <input type="hidden" name="masterid" value="<?php  echo $masterid;?>">
				 
				
<table width="80%"  border="0" align="center" cellpadding="2" cellspacing="4">
                
                  <tr>
                    <td class="normalBoldblackTxt" ><?php if($edit) echo "EDIT"; else echo "NEW"; echo $PARENTNAME;?>  </td>
                  </tr>
                  <tr>
                    <td  ><?php 
		  if(isset($rmsg))
		  	{
			echo display_error_msg($rmsg);				
		   	}
			?></td>
                  </tr>
                  <tr height="5">
                    <td ></td>
                  </tr>
                  <tr>
                    <td class="bluebold"><table width="100%" align="center" cellspacing="0">
					<?php if($field_type==2){
					
					$tableparameter=" WHERE mastertableid='$secondfieldid'";
					$second_fieldid=get_single_field(DOUBLE_FIELS_TABLE,'second_fieldid','single_field_id',$edit_id);
					?>
			<tr>
				<td width="22%"><?php echo $PARENTNAME; ?>*
			  </td>
														  
				<td width="78%"><?php echo draw_dynamic_pull_down_menue('second_fieldid',SINGLE_FIELD_TABLE,'id','name',$tableparameter,  $second_fieldid,  '','select ','n');?>
				</td>
				</tr>
				<?php } ?>
			<tr>
			  <td class="bluebold">Name*:</td>
			  <td><input type="text" name="name" value="<?php echo $name; ?>" /></td>
			  </tr>
			<tr>
			  <td class="bluebold">Display Order </td>
			  <td><input type="text" name="display_order" value="<?php echo $display_order; ?>" /></td>
			  </tr>
			<tr>
				<td>
					<label></label></td>
				<td>&nbsp;
				</td>
				</tr>
			<tr>
			  <td></td>
			  <td align="left"><span class="bigblackTxt">
			    <input name="savegau" type="submit" class="hbutton" id="savegau3" value="<?php if($edit_id>0) echo "UPDATE"; else echo "ADD";?>">
        		<?php $previous=$selfpageurl."?mode=view&masterid=".$masterid;?>
				<input name="Button" type="button" class="hbutton" value="BACK" onclick="location.href='<?php echo $previous ;?>'" ></a>
</span></td>
			  </tr>
		</table>
</td>
                  </tr>
  </table>
  </form>









<?php } //if($mode=="new" || $mode="edit") ?>

<?php }  // end when masterid>0 ?>