<?php 
error_reporting(0);
$_SESSION['user_array']=array();
function getUnderRoles($role_id,$company_id)
{
	global $db;
	 $sql="Select * from securityroles where rolepid='".$role_id."' and companyid='".$company_id."'";
	//echo $sql."<br>";
	$result=DB_query($sql,$db);
	while($row=DB_fetch_array($result))
	{
		//echo "<br>".$row['secroleid']."<br>";
		$user="Select * from www_users where rolepid='".$row['secroleid']."'";
		$user_result=DB_query($user,$db);
		if(DB_num_rows($user_result)>0)
		{
			//echo "manvi";
			while($user_row=DB_fetch_array($user_result))
			{
				$_SESSION['user_array'][]=$user_row['id'];
			}
		}
		getUnderRoles($row['secroleid'],$_SESSION['ACGCompanyId']);
	}
	return($_SESSION['user_array']);
}


$_SESSION['ACGUnderusers']=array();

function getUnderUsers($uid,$company_id,$dept='')
{
	global $db;
	$s=new db_query();
	//echo $_SESSION['listarr'];
	$a=implode("' , '",$_SESSION['listarr']);
	  $a="'".$a."'";
        	 $x2= $s->GetFieldValue('www_users'," id='".$uid ."' " ,'mst_dpt_heirarchy');
			$x=implode("' , '",$dept);
			 $x="'".$x."'";
	        $x1= $s->GetFieldValue('www_users'," id='".$uid ."' " ,'assigned_sites');
	//
//echo $x1;
 
// or FIND_IN_SET($x1, assigned_sites)//assigned_sites in($x1)
   	   $sql="Select id,mst_dpt_heirarchy ,assigned_sites from www_users where id='".$uid."' or  ( (mst_dpt_heirarchy in($x) or mst_dpt_heirarchy <=$x2)    ) and companyid='".$company_id."'";
 	//echo "<br>----------------<br>";
	$result=DB_query($sql,$db);
	while($row=DB_fetch_array($result))
	{
		 $d=explode(",",$row['assigned_sites']);
 		 if(in_array($x1, $d))
		{
			 echo "<br>".$row['assigned_sites']."-------".$x1."<br>";
 		}	
 		$_SESSION['ACGUnderusers'][]=$row['id'];	
   		//getUnderUsers($row['id'],$_SESSION['ACGCompanyId']);
	}
	return($_SESSION['ACGUnderusers']);
}


//------------------------------------------------------------------------------------------------
$_SESSION['ACGUpperUserList']=array();
function GETUpperDeptUser($uid,$hid,$company_id)
{
	 	global $db;
	$s=new db_query();
       $sql="Select parentid from deptmaster_hierarchy where  id='".$hid."' and companyid='".$company_id."' ";
	//	echo "<br>----------------<br>";
	$result=DB_query($sql,$db);
	while($row=DB_fetch_array($result))
	{ 
 		$_SESSION['ACGUpperUserList'][]=$row['parentid'];		
		GETUpperDeptUser($uid,$row['parentid'],$company_id);
		
		//echo $row['parentid'].'<br>' ;
	}
	  $x2= $s->GetFieldValue('www_users'," id='".$uid ."' " ,'mst_dpt_heirarchy');
	  $_SESSION['ACGUpperUserList'][]=$x2;
	 return(array_unique($_SESSION['ACGUpperUserList']));
}

//------------------------------------------------------------------------------------------------
$_SESSION['ACGLowerUserList']=array();
function GETLowerDeptUser($uid,$hid,$company_id)
{
	 	global $db;
	 $s=new db_query();
	//  echo "<br>***********Lower Users****************************************<br>";
	if($_SESSION['UserType']!='SuperAdmin')
	{
 	   $sql="Select id from deptmaster_hierarchy where  parentid='".$hid."' and companyid='".$company_id."' ";
	   }
	   else
	   {
	   	$sql="Select id from deptmaster_hierarchy where  parentid='".$hid."'";
	   }
	//echo "<br>----------------<br>";
	$result=DB_query($sql,$db);
	while($row=DB_fetch_array($result))
	{ 
 		$_SESSION['ACGLowerUserList'][]=$row['id'];		
		GETLowerDeptUser($uid,$row['id'],$company_id);
	}
 	 return(array_unique($_SESSION['ACGLowerUserList'])); 
}
 
//----------------------------------------------------------
function UserDeptList($list)
{
	$s=new db_query();
	$l=array();global $db;
	//    echo "<br>******----------------------***********************<br>";
   // print_r($list);
if(count($list)>0)	
{
 $lop=implode(',',$list);
  //   echo "<br>*********0000000000000000000000000000000000********************<br>";
  if($_SESSION['UserID']!='44')
	{
         $sql2="Select id from www_users where  mst_dpt_heirarchy in($lop) and  companyid='".$_SESSION['ACGCompanyId']."' ";
		 }else
		 {
		 	$sql2="Select id from www_users where  mst_dpt_heirarchy in($lop) ";
		 }
		   $result=DB_query($sql2,$db);
			while($row=DB_fetch_array($result))
		    { 
  			 	  $l[]=$row['id'];
    		 }
    // echo "<br>***************-------------------------------------------**************<br>";
 	 //print_r($l);
	
 //   echo "<br>****************33333333333333*************<br>";
} 
 	  return($l);

}

//********************************************************
function GETFinalUserList($list)
{
	
	 
	$s=new db_query();
		$siteasin= $s->GetFieldValue('www_users'," id='". $_SESSION['UserID'] ."' " ,'assigned_sites');
	$l=array();global $db;
	 
	    $pos = strpos($list, ',');
	//  echo "<br>-----".$siteasin."-----<br>";
 
	if($pos>0)
	{
	 $lop=explode(',',$list);
   // print_r($lop);

    for($i=0;$i<count($lop);$i++)
   {
   	if($_SESSION['UserID']!='44'){
    
            $sql2="Select id from www_users where  FIND_IN_SET(".$lop[$i].",assigned_sites) AND     companyid='".$_SESSION['ACGCompanyId']."' ";
			}else
			{
				   $sql2="Select id from www_users where  FIND_IN_SET(".$lop[$i].",assigned_sites) ";
			}
     //  echo "<br>-----".$lop[$i]."-----<br>";
		   $result=DB_query($sql2,$db);
			while($row=DB_fetch_array($result))
		    { 
  			 	  $l[]=$row['id'];
    		 }
 	}
	}
	else{
	          $sql2="Select id from www_users where  FIND_IN_SET($siteasin,assigned_sites) AND     companyid='".$_SESSION['ACGCompanyId']."' ";
		   $result=DB_query($sql2,$db);
			while($row=DB_fetch_array($result))
		    { 
  			 	  $l[]=$row['id'];
    		 }

	
	}		 
  	  return(array_unique($l));

	 

}
//**********

function calculateBackDate($role_id)
{
	global $db;
	$days_sql="Select back_days from securityroles where secroleid='".$role_id."'";
	$days_result=DB_query($days_sql,$db);
	$days_row=DB_fetch_array($days_result);
	$back_days=$days_row['back_days'];
	return $back_days;
}
?>