<?php include_once("class/class.MySQL.php");
$db	=new MySQL();

 ////////////////////////main_menu/////////////	

if($_GET['action']=='main_m')
{
$cntid=$_GET['rid'];
$menuids=$_POST['m_menuid'.$cntid];
$users=$_GET['usr_id'];

$resultdsg_cnt = mysql_query("SELECT * FROM tbl_user_rights where m_menu_id='$menuids' and s_submenu_id=0 and  user_id='$users'");
$rowsd_cnts = mysql_num_rows($resultdsg_cnt);

if($rowsd_cnts == 0)
{


   $cntid=$_GET['rid'];


    $data['user_id'] = $_GET['usr_id'];
	$data['m_menu_id'] = $_POST['m_menuid'.$cntid];
	/*$data['s_submenu_id'] = $_POST['chkadd'.$cntid];*/
	$data['add_right'] = $_POST['chkadd'.$cntid];
	$data['edit_right'] = $_POST['chkedit'.$cntid];
	$data['view_right'] = $_POST['chkeview'.$cntid];
	$data['delete_right'] = $_POST['chkdel'.$cntid];
	


	$db->Insert($data,'tbl_user_rights');
	
	
}
else
{
$cntid=$_GET['rid'];


    $user_id = $_GET['usr_id'];
	$m_menu_id = $_POST['m_menuid'.$cntid];
	/*$data['s_submenu_id'] = $_POST['chkadd'.$cntid];*/
	$add_right = $_POST['chkadd'.$cntid];
	$edit_right = $_POST['chkedit'.$cntid];
	$view_right = $_POST['chkeview'.$cntid];
	$delete_right = $_POST['chkdel'.$cntid];
	
/*
	$empwhere['m_menu_id']		= $_POST['m_menuid'.$cntid];
	$db->Update('tbl_user_rights',$data,$empwhere);*/

	
mysql_query("UPDATE tbl_user_rights SET user_id = '$user_id', m_menu_id = '$m_menu_id',add_right='$add_right',edit_right='$edit_right',view_right='$view_right',delete_right='$delete_right'   WHERE m_menu_id = '$m_menu_id' and user_id='$user_id' and s_submenu_id=0");
	

}	



}   ////////////////////////main_menu-end/////////////	

////////////////////////sub_menu-start/////////////	

if($_GET['action']=='sub_m')
{
  
$cntid=$_GET['rid'];
$m_menu=$_GET['m_mid'];
$menuids=$_POST['m_menuid'.$m_menu];
$users=$_GET['usr_id'];
$sub_m_id = $_POST['s_menuid'.$cntid];


$resultdsg_cnt = mysql_query("SELECT * FROM tbl_user_rights where m_menu_id='$menuids' and s_submenu_id='$sub_m_id' and  user_id='$users'");
$rowsd_cnts = mysql_num_rows($resultdsg_cnt);

if($rowsd_cnts == 0)
{


   $cntid=$_GET['rid'];
   $m_menu=$_GET['m_mid'];


    $data['user_id'] = $_GET['usr_id'];
	$data['m_menu_id'] = $_POST['m_menuid'.$m_menu];
	$data['s_submenu_id'] = $_POST['s_menuid'.$cntid];
	$data['add_right'] = $_POST['s_chkadd'.$cntid];
	$data['edit_right'] = $_POST['s_chkedit'.$cntid];
	$data['view_right'] = $_POST['s_chkeview'.$cntid];
	$data['delete_right'] = $_POST['s_chkdel'.$cntid];
	


	$db->Insert($data,'tbl_user_rights');
}
else
{
     $cntid=$_GET['rid'];
     $m_menu=$_GET['m_mid'];

    $user_id = $_GET['usr_id'];
	$m_menu_id = $_POST['m_menuid'.$m_menu];
	$s_submenu_id = $_POST['s_menuid'.$cntid];
	$add_right = $_POST['s_chkadd'.$cntid];
	$edit_right = $_POST['s_chkedit'.$cntid];
	$view_right = $_POST['s_chkeview'.$cntid];
	$delete_right = $_POST['s_chkdel'.$cntid];
	
/*
	$empwhere['m_menu_id']		= $_POST['m_menuid'.$cntid];
	$db->Update('tbl_user_rights',$data,$empwhere);*/

	
mysql_query("UPDATE tbl_user_rights SET user_id = '$user_id', m_menu_id = '$m_menu_id',s_submenu_id='$s_submenu_id',add_right='$add_right',edit_right='$edit_right',
view_right='$view_right',delete_right='$delete_right'   WHERE m_menu_id = '$m_menu_id' and user_id='$user_id' and s_submenu_id='$s_submenu_id'");
	

}	




}
////////////////////////sub_menu-end/////////////	


////////////////////////main-sub_menu-start/////////////	

if($_GET['action']=='m_s_menus')
{

$s_sid=$_GET['s_id'];
$m_mid=$_GET['m_mid'];
$users=$_GET['usr_id'];
                 /////////////code for main menu////////////////////
    for($j=1; $j<=$m_mid; $j++)
	{     $menuids=$_POST['m_menuid'.$j];
	      $resultdsg_cnt = mysql_query("SELECT * FROM tbl_user_rights where m_menu_id='$menuids' and s_submenu_id=0 and  user_id='$users'");
          $rowsd_cnts = mysql_num_rows($resultdsg_cnt);
		  if($rowsd_cnts == 0)
          {
		       $data['user_id'] = $_GET['usr_id'];
	           $data['m_menu_id'] = $_POST['m_menuid'.$j];
	           /*$data['s_submenu_id'] = $_POST['chkadd'.$cntid];*/
	           $data['add_right'] = $_POST['chkadd'.$j];
	           $data['edit_right'] = $_POST['chkedit'.$j];
	           $data['view_right'] = $_POST['chkeview'.$j];
	           $data['delete_right'] = $_POST['chkdel'.$j];
	           $db->Insert($data,'tbl_user_rights');
		  } 
		  else
		  {
		      $user_id = $_GET['usr_id'];
	          $m_menu_id = $_POST['m_menuid'.$j];
	          $add_right = $_POST['chkadd'.$j];
	          $edit_right = $_POST['chkedit'.$j];
	          $view_right = $_POST['chkeview'.$j];
	          $delete_right = $_POST['chkdel'.$j];
	


mysql_query("UPDATE tbl_user_rights SET user_id = '$user_id', m_menu_id = '$m_menu_id',add_right='$add_right',edit_right='$edit_right',view_right='$view_right',delete_right='$delete_right'   WHERE m_menu_id = '$m_menu_id' and user_id='$user_id' and s_submenu_id=0");
		  } 
		  
		  
		  
		  
/*		  	    for($q=1; $q<=$s_sid; $q++)
	{     $menuids=$_POST['m_menuid'.$q];
	      $sub_m_id = $_POST['s_menuid'.$q];
          $sub_resultdsg_cnt = mysql_query("SELECT * FROM tbl_user_rights where m_menu_id='$menuids' and s_submenu_id='$sub_m_id' and  user_id='$users'");
          $sub_rowsd_cnts = mysql_num_rows($sub_resultdsg_cnt);
		  if($sub_rowsd_cnts == 0)
          {
             $datas['user_id'] = $_GET['usr_id'];
	         $datas['m_menu_id'] = $_POST['m_menuid'.$j];
	         $datas['s_submenu_id'] = $_POST['s_menuid'.$q];
	         $datas['add_right'] = $_POST['s_chkadd'.$q];
	         $datas['edit_right'] = $_POST['s_chkedit'.$q];
	         $datas['view_right'] = $_POST['s_chkeview'.$q];
	         $datas['delete_right'] = $_POST['s_chkdel'.$q];
	         $db->Insert($datas,'tbl_user_rights');
		  } 
		  else
		  {
		     $user_id = $_GET['usr_id'];
	         $m_menu_id = $_POST['m_menuid'.$j];
	         $s_submenu_id = $_POST['s_menuid'.$q];
	         $add_right = $_POST['s_chkadd'.$q];
	         $edit_right = $_POST['s_chkedit'.$q];
	         $view_right = $_POST['s_chkeview'.$q];
	         $delete_right = $_POST['s_chkdel'.$q];
			 
			 
			 
			 mysql_query("UPDATE tbl_user_rights SET user_id = '$user_id', m_menu_id = '$m_menu_id',s_submenu_id='$s_submenu_id',add_right='$add_right',edit_right='$edit_righ',
             view_right='$view_right',delete_right='$delete_right'   WHERE m_menu_id = '$m_menu_id' and user_id='$user_id' and s_submenu_id='$s_submenu_id'");
		  }
		   
	}*/	 
		  
		  
	}
	


	

}

////////////////////////main-menu-end/////////////	

////////////////////////sub-menu-update start/////////////	

if($_GET['action']=='s_menus')
{

$s_sid=$_GET['s_id'];
$m_mid=$_GET['m_mid'];
$users=$_GET['usr_id'];
$m_sub=$_GET['m_sub'];
$sub_cnts=$s_sid-$m_sub;
 $sub_cnts=$sub_cnts+1;

     for($q=$sub_cnts; $q<=$s_sid; $q++)
	{     $menuids=$_POST['m_menuid'.$m_mid];
	      $sub_m_id = $_POST['s_menuid'.$q];
          $sub_resultdsg_cnt = mysql_query("SELECT * FROM tbl_user_rights where m_menu_id='$menuids' and s_submenu_id='$sub_m_id' and  user_id='$users'");
          $sub_rowsd_cnts = mysql_num_rows($sub_resultdsg_cnt);
		  if($sub_rowsd_cnts == 0)
          {
             $datas['user_id'] = $_GET['usr_id'];
	         $datas['m_menu_id'] = $_POST['m_menuid'.$m_mid];
	         $datas['s_submenu_id'] = $_POST['s_menuid'.$q];
	         $datas['add_right'] = $_POST['s_chkadd'.$q];
	         $datas['edit_right'] = $_POST['s_chkedit'.$q];
	         $datas['view_right'] = $_POST['s_chkeview'.$q];
	         $datas['delete_right'] = $_POST['s_chkdel'.$q];
			 
			
			 
	         $db->Insert($datas,'tbl_user_rights');
		  } 
		  else
		  {
		     $user_id = $_GET['usr_id'];
	         $m_menu_id = $_POST['m_menuid'.$m_mid];
	         $s_submenu_id = $_POST['s_menuid'.$q];
	         $add_right = $_POST['s_chkadd'.$q];
	         $edit_right = $_POST['s_chkedit'.$q];
	         $view_right = $_POST['s_chkeview'.$q];
	         $delete_right = $_POST['s_chkdel'.$q];
			 
/*	echo "UPDATE tbl_user_rights SET user_id = '$user_id', m_menu_id = '$m_menu_id',s_submenu_id='$s_submenu_id',add_right='$add_right',edit_right='$edit_right',
             view_right='$view_right',delete_right='$delete_right'   WHERE m_menu_id = '$m_menu_id' and user_id='$user_id' and s_submenu_id='$s_submenu_id'";	*/	 
			 
			 mysql_query("UPDATE tbl_user_rights SET user_id = '$user_id', m_menu_id = '$m_menu_id',s_submenu_id='$s_submenu_id',add_right='$add_right',edit_right='$edit_right',
             view_right='$view_right',delete_right='$delete_right'   WHERE m_menu_id = '$m_menu_id' and user_id='$user_id' and s_submenu_id='$s_submenu_id'");
		  }
		   
	}
}


////////////////////////sub-menu-update end/////////////	


////////////////////////sub-of-sub-menu-update start/////////////	

if($_GET['action']=='sub_sub')
{


  
 $cntid=$_GET['rid'];
 $m_menu=$_GET['m_mid'];
 $menuids=$_POST['m_menuid'.$m_menu];
 $users=$_GET['usr_id'];
 $sub_m_id = $_POST['s_menuid_s'.$cntid];


$resultdsg_cnt = mysql_query("SELECT * FROM tbl_user_rights where m_menu_id='$menuids' and s_submenu_id='$sub_m_id' and  user_id='$users'");
$rowsd_cnts = mysql_num_rows($resultdsg_cnt);

if($rowsd_cnts == 0)
{


   $cntid=$_GET['rid'];
   $m_menu=$_GET['m_mid'];


    $data['user_id'] = $_GET['usr_id'];
	$data['m_menu_id'] = $_POST['m_menuid'.$m_menu];
	$data['s_submenu_id'] = $_POST['s_menuid_s'.$cntid];
	$data['add_right'] = $_POST['s_chkadd_s'.$cntid];
	$data['edit_right'] = $_POST['s_chkedit_s'.$cntid];
	$data['view_right'] = $_POST['s_chkeview_s'.$cntid];
	$data['delete_right'] = $_POST['s_chkdel_s'.$cntid];
	


	$db->Insert($data,'tbl_user_rights');
}
else
{
     $cntid=$_GET['rid'];
     $m_menu=$_GET['m_mid'];

    $user_id = $_GET['usr_id'];
	$m_menu_id = $_POST['m_menuid'.$m_menu];
	$s_submenu_id = $_POST['s_menuid_s'.$cntid];
	$add_right = $_POST['s_chkadd_s'.$cntid];
	$edit_right = $_POST['s_chkedit_s'.$cntid];
	$view_right = $_POST['s_chkeview_s'.$cntid];
	$delete_right = $_POST['s_chkdel_s'.$cntid];
	
/*
	$empwhere['m_menu_id']		= $_POST['m_menuid'.$cntid];
	$db->Update('tbl_user_rights',$data,$empwhere);*/

	
mysql_query("UPDATE tbl_user_rights SET user_id = '$user_id', m_menu_id = '$m_menu_id',s_submenu_id='$s_submenu_id',add_right='$add_right',edit_right='$edit_right',
view_right='$view_right',delete_right='$delete_right'   WHERE m_menu_id = '$m_menu_id' and user_id='$user_id' and s_submenu_id='$s_submenu_id'");
	

}	


}





////////////////////////sub-of-sub-menu-update end/////////////	

if($_GET['action']=='s_menus_sub')
{

$s_sid=$_GET['s_id'];
$m_mid=$_GET['m_mid'];
$users=$_GET['usr_id'];
$m_sub=$_GET['m_sub'];
$sub_cnts=$s_sid-$m_sub;
$sub_cnts=$sub_cnts+1;

     for($q=$sub_cnts; $q<=$s_sid; $q++)
	{     $menuids=$_POST['m_menuid'.$m_mid];
	      $sub_m_id = $_POST['s_menuid_s'.$q];
          $sub_resultdsg_cnt = mysql_query("SELECT * FROM tbl_user_rights where m_menu_id='$menuids' and s_submenu_id='$sub_m_id' and  user_id='$users'");
          $sub_rowsd_cnts = mysql_num_rows($sub_resultdsg_cnt);
		  if($sub_rowsd_cnts == 0)
          {
             $datas['user_id'] = $_GET['usr_id'];
	         $datas['m_menu_id'] = $_POST['m_menuid'.$m_mid];
	         $datas['s_submenu_id'] = $_POST['s_menuid_s'.$q];
	         $datas['add_right'] = $_POST['s_chkadd_s'.$q];
	         $datas['edit_right'] = $_POST['s_chkedit_s'.$q];
	         $datas['view_right'] = $_POST['s_chkeview_s'.$q];
	         $datas['delete_right'] = $_POST['s_chkdel_s'.$q];
			 
			
			 
	         $db->Insert($datas,'tbl_user_rights');
		  } 
		  else
		  {
		     $user_id = $_GET['usr_id'];
	         $m_menu_id = $_POST['m_menuid'.$m_mid];
	         $s_submenu_id = $_POST['s_menuid_s'.$q];
	         $add_right = $_POST['s_chkadd_s'.$q];
	         $edit_right = $_POST['s_chkedit_s'.$q];
	         $view_right = $_POST['s_chkeview_s'.$q];
	         $delete_right = $_POST['s_chkdel_s'.$q];
			 
/*	echo "UPDATE tbl_user_rights SET user_id = '$user_id', m_menu_id = '$m_menu_id',s_submenu_id='$s_submenu_id',add_right='$add_right',edit_right='$edit_right',
             view_right='$view_right',delete_right='$delete_right'   WHERE m_menu_id = '$m_menu_id' and user_id='$user_id' and s_submenu_id='$s_submenu_id'";	*/	 
			 
			 mysql_query("UPDATE tbl_user_rights SET user_id = '$user_id', m_menu_id = '$m_menu_id',s_submenu_id='$s_submenu_id',add_right='$add_right',edit_right='$edit_right',
             view_right='$view_right',delete_right='$delete_right'   WHERE m_menu_id = '$m_menu_id' and user_id='$user_id' and s_submenu_id='$s_submenu_id'");
		  }
		   
	}
}





		?>
		
		

