<?php
 	require "commonincludefiles.php";
	if($_REQUEST["cn"] == '')
	{
		$msg = $_REQUEST["msgv"]; 		
	}
	else
	{
		$msg = $_REQUEST["cn"];
		$msg = str_replace("[","",$msg);
		$msg = str_replace("]","",$msg);
		$msg = str_replace("Node ","",$msg);
	}
 	
	$username	= $_REQUEST["username"];
	$password   = $_REQUEST["password"];
	
	//// start user add
	$objUserMgmt = new UserMgmt();
	
	$objUserMgmt->user_name		= $username;
	
	if($_REQUEST['faculty'] == "on")
	{
		$objUserMgmt->user_role		= "Faculty";
	}
	else if($_REQUEST['hod'] == "on")
	{
		$objUserMgmt->user_role		= "Hod";
	}
	else
	{
		$objUserMgmt->user_role		= "user";
	}
	$objUserMgmt->user_password	= $password;
	$objUserMgmt->college_id		= $_SESSION["VedantnrCollegeId"];
	if($_REQUEST["ID"] > 0 && isset($_REQUEST["ID"])){   
		$objUserMgmt->user_id = $_REQUEST["ID"];
		$objUserMgmt-> editLogin();
		$user_id = $_REQUEST["ID"];
		if($objUserMgmt->user_id!=0) {			
			$user_id =$objUserMgmt->user_id;
		}
	}
	else if($_REQUEST["ID"] == 0 && isset($_REQUEST["ID"])){
		$objUserMgmt->add();
		if ($objUserMgmt->user_id > 0){				
			$user_id =$objUserMgmt->user_id;
		}	
	}
	//// end user add
	if($_REQUEST["poweruser"]=="on")
	{
	 	$qry="SELECT group_concat(menu_id )As menuids FROM `cms_menumaster` Where menu_active=1 order by menu_id " ;
		$resut=dbQuery($qry);
		$row=dbArray($resut);
		$menuids=$row["menuids"];
		
		$qry="SELECT group_concat(page_id )As pageids FROM `cms_pagemaster` Where page_active=1 order by page_id " ;
		$resut=dbQuery($qry);
		$row=dbArray($resut);
		$pageids=$row["pageids"];
		
		$qry="SELECT group_concat(module_id )As moduleids FROM `cms_modulemaster` Where module_active=1 order by module_id" ;
		$resut=dbQuery($qry);
		$row=dbArray($resut);
		$moduleids=$row["moduleids"];
		
		$objUserMgmt->user_role		= "PowerUser";
		$objUserMgmt->menu_ids		= $menuids;
		$objUserMgmt->module_ids	= $moduleids;
		$objUserMgmt->page_ids		= $pageids;
		$objUserMgmt->subpage_ids	= $subpageids;

		if($user_id > 0){   
			$objUserMgmt->user_id = $user_id;
			$objUserMgmt-> editRight();
		}
		
		echo '{"success":"true"}';
		
	}
	else
	{
        if($msg != ""){
		$arrmsg = explode(",",$msg);			
		
		$arraccess 	= array();
		$arraccess2 = array();
		$MainArray 	= array();
			
		$tempchar   = "";
		$mmenu		= "";
		$smenu		= "";
		$j			= 0;
		$len		= 0;
		$k			= 0;
		
		$objUser = new UserAccessMgmt();
		
		if($user_id > 0){
			$objUser->deleteUser_Id = $user_id;
			$objUser->delete();
		}

		for($i = 0,$j = 0,$k = 0 ; $i < count ($arrmsg); $i++)
		{
			$mastercount = (substr_count($arrmsg[$i],"/"));
			if($mastercount ==2)
			{
				$arraccess[$j++] = $arrmsg[$i];
			}
			if($mastercount ==3)
			{
				$arraccess2[$k++] = $arrmsg[$i];
			}
		}
		
		$MainArray = array_merge($arraccess,$arraccess2);
		sort($MainArray);
	
		if(count($MainArray)>0){
			
			$str_store = $MainArray[0];
			//$str_store = substr($str_store,6);
			$str_new = "";
			$arr_temp = array();
	
			$module_id 			= 0;
			$menu_id   			= 0;
			$page_id   			= 0;
			
			$flg_module_id		= 0;
			$flg_menu_id   		= 0;
			$flg_page_id   		= 0;
			
			$useraccess_add 	= 0;
			$useraccess_edit 	= 0; 
			$useraccess_delete 	= 0;
			
			for($i=0; $i<count($MainArray); $i++){
				$str_new = $MainArray[$i];
				$str_new = explode("/",$MainArray[$i]);				
				unset($str_new[3]);
				$str_new_cut = implode("/",$str_new);				
							
				$str_store_len = strlen($str_store);
								
				if($str_store == $str_new_cut){								
					
					$str_temp = substr($MainArray[$i],$str_store_len+1);
					
					if($str_temp!="") {
						$arr_temp = explode("-",$str_temp);				
						if(count($arr_temp) > 0) {
							if($arr_temp[2] == "add"){
								$useraccess_add = 1;
							} else if($arr_temp[2] == "edit"){
								$useraccess_edit = 1;
							} else if($arr_temp[2] == "delete"){
								$useraccess_delete = 1;
							}
						}
					}
				}
				else{
					
					$tempary   = explode("/",$str_store);			
					
					$module_id = substr(trim($tempary[0]),7);
					$menu_id   = substr(trim($tempary[1]),5);
					$page_id   = substr(trim($tempary[2]),5);
					
					$objUser->user_id 			= $user_id;
					$objUser->module_id 		= $module_id;
					$objUser->menu_id 			= $menu_id;
					$objUser->page_id 			= $page_id;
					$objUser->useraccess_add 	= $useraccess_add;
					$objUser->useraccess_edit 	= $useraccess_edit;
					$objUser->useraccess_delete = $useraccess_delete;
					$objUser->useraccess_view 	= 1;
					$objUser->add();
					
					if($module_id != "" && $module_id != $flg_module_id){
						$strModule_ids .= $module_id.",";
						$flg_module_id = $module_id;
					}
					
					if($menu_id != "" && $menu_id != $flg_menu_id){
						$strMenu_ids   .= $menu_id.",";
						$flg_menu_id = $menu_id;
					}
					
					if($page_id != "" && $page_id != $flg_page_id){				
						$strPage_ids   .= $page_id.",";		
						$flg_page_id = $page_id;				
					}

					$module_id 			= 0;
					$menu_id   			= 0;
					$page_id   			= 0;
					$useraccess_add 	= 0;
					$useraccess_edit 	= 0; 
					$useraccess_delete 	= 0;
					
					$str_store = $str_new_cut;
				}
			}
			
			if($i == count($MainArray)){
				
				$tempary	= explode("/",$str_store);			
				$module_id  = substr(trim($tempary[0]),7);
				$menu_id   	= substr(trim($tempary[1]),5);
				$page_id   	= substr(trim($tempary[2]),5);
				
			
				$objUser->user_id 			= $user_id;
				$objUser->module_id 		= $module_id;
				$objUser->menu_id 			= $menu_id;
				$objUser->page_id 			= $page_id;
				$objUser->useraccess_add 	= $useraccess_add;
				$objUser->useraccess_edit 	= $useraccess_edit;
				$objUser->useraccess_delete = $useraccess_delete;
				$objUser->useraccess_view 	= 1;
				$objUser->add();
				
				if($module_id != "" && $module_id != $flg_module_id){
					$strModule_ids .= $module_id.",";
					$flg_module_id = $module_id;
				}
				
				if($menu_id != "" && $menu_id != $flg_menu_id){
					$strMenu_ids   .= $menu_id.",";
					$flg_menu_id = $menu_id;
				}
				
				if($page_id != "" && $page_id != $flg_page_id){				
					$strPage_ids   .= $page_id.",";		
					$flg_page_id = $page_id;				
				}

				
				$strModule_ids  = substr($strModule_ids,0,strlen($strModule_ids)-1);
				$strMenu_ids 	= substr($strMenu_ids,0,strlen($strMenu_ids)-1);
				$strPage_ids 	= substr($strPage_ids,0,strlen($strPage_ids)-1);										
			}	
	
			// update user
			$objUserMgmt = new UserMgmt();
			$objUserMgmt->module_ids	= $strModule_ids;
			$objUserMgmt->menu_ids		= $strMenu_ids;
			$objUserMgmt->page_ids		= $strPage_ids;
  
  			if($user_id > 0){
                $objUserMgmt->user_id = $user_id;
				$objUserMgmt-> editRight();
			}
			 
		echo '{"success":"true"}';
	}else{
		echo '{"success":"false"}';	
	}
		/*if($msg != ""){
		$arrmsg = explode(",",$msg);			
		
		$arraccess 	= array();
		$arraccess2 = array();
		$MainArray 	= array();
			
		$tempchar   = "";
		$mmenu		= "";
		$smenu		= "";
		$j			= 0;
		$len		= 0;
		$k			= 0;
		
		$objUser = new UserAccessMgmt();
		
		if($user_id > 0){
			$objUser->deleteUser_Id = $user_id;
			$objUser->delete();
		}

		for($i = 0,$j = 0,$k = 0 ; $i < count ($arrmsg); $i++){
			$mastercount = (substr_count($arrmsg[$i],"/"));
			if($mastercount ==2){
				$arraccess[$j++] = $arrmsg[$i];
			}
			if($mastercount ==3){
				$arraccess2[$k++] = $arrmsg[$i];
			}
		}
		
		$MainArray = array_merge($arraccess,$arraccess2);
		sort($MainArray);
	
		if(count($MainArray)>0){
			$str_store = $MainArray[0];
			//$str_store = substr($str_store,6);
			$str_new = "";
			$arr_temp = array();
	
			$menu_id   			= 0;
			$page_id   			= 0;
			$flg_menu_id   		= 0;
			$flg_page_id   		= 0;
			$useraccess_add 	= 0;
			$useraccess_edit 	= 0; 
			$useraccess_delete 	= 0;
			
			for($i=0; $i<count($MainArray); $i++){
				$str_new = $MainArray[$i];
				$str_new = explode("/",$MainArray[$i]);				
				unset($str_new[3]);
				$str_new_cut = implode("/",$str_new);				
				$str_store_len = strlen($str_store);

				if($str_store == $str_new_cut){								
					
					$str_temp = substr($MainArray[$i],$str_store_len+1);
									
					if($str_temp!="") {
						$arr_temp = explode("-",$str_temp);				
						if(count($arr_temp) > 0) {
							if($arr_temp[2] == "add"){
								$useraccess_add = 1;
							} else if($arr_temp[2] == "edit"){
								$useraccess_edit = 1;
							} else if($arr_temp[2] == "delete"){
								$useraccess_delete = 1;
							}
						}
					}
				}
				else{
					$tempary   = explode("/",$str_store);			
					$menu_id   = substr($tempary[1],5);
					$page_id   = substr($tempary[2],5);
					
					$objUser->user_id 			= $user_id;
					$objUser->menu_id 			= $menu_id;
					$objUser->page_id 			= $page_id;
					$objUser->useraccess_add 	= $useraccess_add;
					$objUser->useraccess_edit 	= $useraccess_edit;
					$objUser->useraccess_delete = $useraccess_delete;
					$objUser->useraccess_view 	= 1;
					$objUser->add();
					
					if($menu_id != "" && $menu_id != $flg_menu_id){
						$strMenu_ids   .= $menu_id.",";
						$flg_menu_id = $menu_id;
					}
					
					if($page_id != "" && $page_id != $flg_page_id){				
						$strPage_ids   .= $page_id.",";		
						$flg_page_id = $page_id;				
					}

					$menu_id   			= 0;
					$page_id   			= 0;
					$useraccess_add 	= 0;
					$useraccess_edit 	= 0; 
					$useraccess_delete 	= 0;
					
					$str_store = $str_new_cut;
				}
			}
			
			if($i == count($MainArray)){
				$tempary	= explode("/",$str_store);			
				$menu_id   	= substr($tempary[1],5);
				$page_id   	= substr($tempary[2],5);
			
				$objUser->user_id 			= $user_id;
				$objUser->menu_id 			= $menu_id;
				$objUser->page_id 			= $page_id;
				$objUser->useraccess_add 	= $useraccess_add;
				$objUser->useraccess_edit 	= $useraccess_edit;
				$objUser->useraccess_delete = $useraccess_delete;
				$objUser->useraccess_view 	= 1;
				$objUser->add();
				
				if($menu_id != "" && $menu_id != $flg_menu_id){
					$strMenu_ids   .= $menu_id.",";
					$flg_menu_id = $menu_id;
				}
				
				if($page_id != "" && $page_id != $flg_page_id){				
					$strPage_ids   .= $page_id.",";		
					$flg_page_id = $page_id;				
				}
				
				$strMenu_ids 	= substr($strMenu_ids,0,strlen($strMenu_ids)-1);
				$strPage_ids 	= substr($strPage_ids,0,strlen($strPage_ids)-1);										
			}	
			
			// update user
			$objUserMgmt = new UserMgmt();
			$objUserMgmt->menu_ids		= $strMenu_ids;
			$objUserMgmt->page_ids		= $strPage_ids;
  
  			if($user_id > 0){   
			 	$objUserMgmt->user_id = $user_id;
				$objUserMgmt-> editRight();
			}
		} 
		echo '{"success":"true"}';
	}else{
		echo '{"success":"false"}';	
	          }*/
        }
	}
?>