<?php
 	require "commonincludefiles.php";
	$msg = $_REQUEST["cn"];
	$msg = str_replace("[","",$msg);
	$msg = str_replace("]","",$msg);
	$msg = str_replace("Node ","",$msg);
	$objUserMgmt = new UserMgmt();
if($_SESSION["cCompany_id"]!="" && isset($_SESSION["cCompany_id"]))
{	
	$objUserMgmt ->user_id          = $_REQUEST["user_id"];
	$objUserMgmt ->user_name		= $_REQUEST['username'];
	$objUserMgmt ->user_password	= $_REQUEST['password'];
	$objUserMgmt ->company_id		= $_REQUEST['company_heden'];
	
	if($_REQUEST["user_id"] > 0 && isset($_REQUEST["user_id"])){   
					$objUserMgmt->user_id = $_REQUEST["user_id"];
					$objUserMgmt-> editLogin();
					$user_id = $_REQUEST["user_id"];
					if($objUserMgmt->user_id!=0) {			
						$user_id =$objUserMgmt->user_id;
					}
				
	}
	else if($_REQUEST["user_id"] ==0 && isset($_REQUEST["user_id"])){    
		$objUserMgmt->add();
		if ($objUserMgmt->user_id > 0){				
			$user_id =$objUserMgmt->user_id;
		}	
	}
	
	if($_REQUEST["poweruser"]=="on")
	{
	 	$qry="SELECT group_concat(menu_id )As menuids FROM `lms_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 `lms_pagemaster` Where page_active=1 order by page_id " ;
		$resut=dbQuery($qry);
		$row=dbArray($resut);
		$pageids=$row["pageids"];
		
		$qry="SELECT group_concat(subpage_id )As subpageids FROM `lms_subpagemaster` Where subpage_active=1 order by subpage_id " ;
		$resut=dbQuery($qry);
		$row=dbArray($resut);
		$subpageids=$row["subpageids"];
		
		
		$objUserMgmt->menu_ids		= $menuids;
		$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;
					
					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);
				
					
					//print_r($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;
						$flg_subpage_id   	= 0;
						$str_subpage="";
						
						
						
						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!="") {
									$str_subpage.=$str_temp;
									
								}
							}
							else{
							
							
								$tempary   = explode("/",$str_store);		
								$menu_id   = substr($tempary[1],5);
								$page_id   = substr($tempary[2],5);
								
													
								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;
													
								$str_store = $str_new_cut;
							}
						}
						$str_subpage=str_replace("subpage-",",",$str_subpage);
						$str_subpage=substr($str_subpage,1);
						//print_r($str_subpage);	
						
						if($i == count($MainArray)){
							$tempary	= explode("/",$str_store);	
							$menu_id   	= substr($tempary[1],5);
							$page_id   	= substr($tempary[2],5);
									
							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
						
						//print $strModule_ids;
						
						//print "aaa ".$strMenu_ids." ".$strPage_ids;
						
						//$objUserMgmt = new UserMgmt();
						$objUserMgmt->menu_ids		= $strMenu_ids;
						$objUserMgmt->page_ids		= $strPage_ids;
						$objUserMgmt->subpage_ids		= $str_subpage;
			  
						if($user_id > 0){   
							$objUserMgmt->user_id = $user_id;
							$objUserMgmt-> editRight();
						}
					} 
					echo '{"success":"true"}';
				}
				else{
					echo '{"success":"false"}';	
				}
				}


}

	
?>