<?php 
$form_name = "User Privilages";
$table_name = "custom_menu_access";
$primary_key = "mnu_acs_id";

$mode = isset($_REQUEST['mode']) ? $_REQUEST['mode'] : "";
	
include_once("includes/config.php");
include_once("includes/common_functions.php");

$common = new General_Functions();
$css_class = "x-gray";
// $common->SetCssClass($css_class);

$mode = isset($_REQUEST['mode']) ? $_REQUEST['mode'] : "";
$act_type = isset($_REQUEST['act_type']) ? $_REQUEST['act_type'] : "";
$cancel_link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'];
$action = $_SERVER['SCRIPT_NAME']."?page=".$_REQUEST['page']."&mode=submit";

switch($mode)
{
	case "submit":
        $mnu_id_all = $_REQUEST['min_mnu_all'];
		$mnu_id = $_REQUEST['min_mnu'];
		
		for($i=0; $i<count($mnu_id_all); $i++)
		{
			$sub_id_all = $_REQUEST[$mnu_id_all[$i].'_all'];
			$sub_id = $_REQUEST[$mnu_id_all[$i]];
			
			$add_id = $_REQUEST[$mnu_id_all[$i].'_add'];
			$edt_id = $_REQUEST[$mnu_id_all[$i].'_edit'];
			$dlt_id = $_REQUEST[$mnu_id_all[$i].'_delete'];
			
			for($j=0; $j<count($sub_id_all); $j++)
			{
				$query = "Select * FROM ".$table_name." WHERE mnu_acs_usr_id_fk = '".$_REQUEST['usr_id']."' AND mnu_acs_mnu_id_fk = '".$mnu_id_all[$i]."' AND mnu_acs_sub_mnu_id_fk = '".$sub_id_all[$j]."'";
				$common->sql->query($query, SQL_ALL, SQL_ASSOC);
				$rec = $common->sql->record;
				
				$_query = sprintf("insert into ".$table_name." values(0, '%s', '%s', '%s', 0, 0, 0, 0, 'Y', now(), '%s', 0, 0)",
					mysql_escape_string($_REQUEST['usr_id']),
					mysql_escape_string($mnu_id_all[$i]),
					mysql_escape_string($sub_id_all[$j]),
					mysql_escape_string(USR)
				);
				
				if(count($rec) == 0)
				{
					$common->sql->query($_query);
				}
				else
				{
					$query = "UPDATE ".$table_name." SET mnu_acs_usr_status = 0, mnu_acs_add = 0, mnu_acs_edit = 0, mnu_acs_delete = 0 WHERE mnu_acs_usr_id_fk = '".mysql_escape_string($_REQUEST['usr_id'])."' AND mnu_acs_mnu_id_fk = '".mysql_escape_string($mnu_id_all[$i])."' AND mnu_acs_sub_mnu_id_fk = '".$sub_id_all[$j]."'";
					$common->sql->exeQuery($query);
				}
			}
			
			$query = "UPDATE ".$table_name." SET mnu_acs_usr_status = 1 WHERE mnu_acs_usr_id_fk = '".mysql_escape_string($_REQUEST['usr_id'])."' AND mnu_acs_mnu_id_fk = '".mysql_escape_string($mnu_id_all[$i])."' AND mnu_acs_sub_mnu_id_fk IN ('".implode("', '", $sub_id)."')";
			$common->sql->exeQuery($query);
			
			$query = "UPDATE ".$table_name." SET mnu_acs_add = 1 WHERE mnu_acs_usr_id_fk = '".mysql_escape_string($_REQUEST['usr_id'])."' AND mnu_acs_mnu_id_fk = '".mysql_escape_string($mnu_id_all[$i])."' AND mnu_acs_sub_mnu_id_fk IN ('".implode("', '", $add_id)."')";
			$common->sql->exeQuery($query);
			
			$query = "UPDATE ".$table_name." SET mnu_acs_edit = 1 WHERE mnu_acs_usr_id_fk = '".mysql_escape_string($_REQUEST['usr_id'])."' AND mnu_acs_mnu_id_fk = '".mysql_escape_string($mnu_id_all[$i])."' AND mnu_acs_sub_mnu_id_fk IN ('".implode("', '", $edt_id)."')";
			$common->sql->exeQuery($query);
			
			$query = "UPDATE ".$table_name." SET mnu_acs_delete = 1 WHERE mnu_acs_usr_id_fk = '".mysql_escape_string($_REQUEST['usr_id'])."' AND mnu_acs_mnu_id_fk = '".mysql_escape_string($mnu_id_all[$i])."' AND mnu_acs_sub_mnu_id_fk IN ('".implode("', '", $dlt_id)."')";
			$common->sql->exeQuery($query);
		}
		$link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'];
		header("location:".$link);
		
		exit;
                $status = isset($_REQUEST['status']) ? $_REQUEST['status'] : 0;
                
                if($_REQUEST['id'] != '')
                    $whr = ' AND'.$primary_key." != '".$_REQUEST['id']."'";
                else
                    $whr = '';
                
                $qry = "SELECT * FROM ".$table_name." WHERE mnu_acs_usr_id_fk = '".$_REQUEST['usr_id']."' AND mnu_acs_mnu_id_fk = '".$_REQUEST['min_mnu_id']."' AND mnu_acs_sub_mnu_id_fk = '".$_REQUEST['sub_title']."' ".$whr;
                $common->sql->query($qry, SQL_ALL, SQL_ASSOC);
                $rec = $common->sql->record;
                
                if(count($rec) == 0)
                {
                    if($_REQUEST['id'] == '')
                    {
                        $_query = sprintf("insert into ".$table_name." values(0, '%s', '%s', '%s', '%s', '%s', now(), '%s', 0, 0)",
                                mysql_escape_string($_REQUEST['usr_id']),
                                mysql_escape_string($_REQUEST['min_mnu_id']),
                                mysql_escape_string($_REQUEST['sub_name']),
                                mysql_escape_string($_REQUEST['sub_title']),
                                mysql_escape_string($_REQUEST['sub_pref']),
                                mysql_escape_string($status),
                                mysql_escape_string(USR)
                        );
                        $rec = $common->sql->query($_query);
                        $_SESSION['msg'] = 'Sub Menu Created Successfully';
                    }
                    else
                    {
                        $_query = "UPDATE ".$table_name." SET   min_mnu_id_fk       = '".$_REQUEST['min_mnu_id']."',
                                                                sub_mnu_name        = '".$_REQUEST['sub_name']."',
                                                                sub_mnu_desc        = '".$_REQUEST['sub_title']."',
                                                                sub_mnu_preference  = '".$_REQUEST['sub_pref']."',
                                                                status              = '".$status."',
                                                                update_time         = now(),
                                                                update_user         = '".USR."'
                                                          WHERE ".$primary_key."    = '".$_REQUEST['id']."'";
                        $rec = $common->sql->exeQuery($_query);
                        $_SESSION['msg'] = 'Sub Menu Updated Successfully';
                    }
                }
                else
                {
                    $_SESSION['msg'] = 'Sub Menu Already Exsists';
                }
		$link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'];
		header("location:".$link);
	break;
	
	case "edit":
		$query = "Select * FROM ".$table_name." 
                            INNER JOIN custom_participant_registration 
                                ON (custom_participant_registration.reg_id = ".$table_name.".lab_smple_part_id)
                          WHERE lab_smple_part_id = '".$_REQUEST['id']."'";
		$common->sql->query($query, SQL_ALL, SQL_ASSOC);
		$rec = $common->sql->record;
		
		$id = $_REQUEST['id'];
		$partcpnt_id = $rec[0]['reg_participant_id'];
		$survey_id = $rec[0]['lab_smple_sur_id'];
		$accs_no = $rec[0]['lab_smple_acc_no'];
		include("edit.php");
	break;
        
        case "delete":
                if(isset($_REQUEST[$prefix.'id']))
                        $id = $_REQUEST[$prefix.'id'];
                else
                        $id = implode("', '", $_REQUEST[$primary_key]);

                $query = "DELETE FROM ".$table_name." WHERE ".$primary_key." IN ('".$id."')";

                $rec = $common->sql->exeQuery($query);

                if($rec)
                {
                        $_SESSION['msg'] = 'Selected record has been deleted successfully';
                        if(isset($_REQUEST['id']))
                                $common->UserLog($form_name, $table_name, $_REQUEST['id'], 'Delete');
                        else
                                for($i=0; $i<count($_REQUEST[$primary_key]); $i++)
                                        $common->UserLog($form_name, $table_name, $_REQUEST[$primary_key][$i], 'Delete');
                }

                $query_string = str_replace("&mode=delete","",$_SERVER['QUERY_STRING']);
                $link = $_SERVER['SCRIPT_NAME'].'?page='.$_REQUEST['page'];
                header("location:".$link);
        break;
	
	default:
		echo '<div class="content_bg">';
			include("add.php");
		echo '</div>';
	break;
}
?>