<?php
error_reporting(0);
function get_Parent($id,$user1,$cType){
	include'config.php'; 
	$querry3="Select * from tbl_features where parent ='".$id."'group by fnid order by id";
	$result3 =  mysqli_query($con, $querry3);
	while($row3 = mysqli_fetch_array($result3)) 
	{
		$querry2="Select * from tbl_permission where desi='".$user1."' and cType='".$cType."'";
		$result2 =  mysqli_query($con, $querry2);
		while($row2= mysqli_fetch_array($result2)) 
		{
			$version= substr($row2[$row3['fnid']], 0,1);
			$fetch[] = array("id"=>$row3['id'],"version"=>$version,"file_name"=>$row3['file_name'],"fnid"=>$row3['fnid']);
		}
	}
	foreach($fetch as $index => $data){
		if ($data['version']!==0) {
			$version=$data['version'];
			$querry="Select * from tbl_features where fnid ='".$data['fnid']."' and status='0' and version='".$version."' group by fnid order by id";
			$result =  mysqli_query($con, $querry);
			while($row = mysqli_fetch_array($result)) {
				$categories[]=$row;
			}
		}
	}
	$i=0;
	foreach($categories as $p_cat){
		$categories[$i]['sub'] = get_Child($p_cat['id'],$user1,$cType);
		$i++;
	}
	return $categories;   
}
function get_Child($id,$user1,$cType){
	include'config.php'; 
	$querry3="Select * from tbl_features where parent ='".$id."'group by fnid order by id";
	$result3 =  mysqli_query($con, $querry3);
	while($row3 = mysqli_fetch_array($result3)) 
	{
		$querry2="Select * from tbl_permission where desi='".$user1."' and cType='".$cType."' ";
		$result2 =  mysqli_query($con, $querry2);
		while($row2= mysqli_fetch_array($result2)) 
		{
			$version= substr($row2[$row3['fnid']], 0,1);
			$fetch[] = array("id"=>$row3['id'],"version"=>$version,"file_name"=>$row3['file_name'],"fnid"=>$row3['fnid']);
		}
	}
	foreach($fetch as $index => $data){
		if ($data['version']!==0) {
			$version=$data['version'];
			$querry="Select * from tbl_features where fnid ='".$data['fnid']."' and status='0' and version='".$version."' group by fnid order by id";
			$result =  mysqli_query($con, $querry);
			while($row = mysqli_fetch_array($result)) {
				$categories[]=$row;
			}
		}
	}
	$i=0;
	foreach($categories as $p_cat){
		$categories[$i]['sub'] = get_Child($p_cat['id'],$user1,$cType);
		$i++;
	}
	return $categories;       
}      
$fetch=get_Parent(0,'aatram','18 HR');
// print_r($fetch);
tree($fetch,2);
function tree($data,$level){
	include'config.php'; 
	foreach ($data as $row => $row1){
		if(isset($row1['sub'])){
			if(!empty($row1['sub'])){
				echo "<li>";
			}
		}
		else
		{
			echo "<li>";
		}
		if ($row1['file_name']=="#!setting_pass"||$row1['fname']=="Setting") {
			if (!isset($_SESSION['owner_password2'])&&$row1['file_name']=="#!setting_pass") {
				$name='href="'.$row1['file_name'].'" ';
				?>
				<a <?php print_r($name);?> ><i class="material-icons"><?php echo $row1['icon'] ;?></i><span title="<?php echo $row1['disc'];?>"><?php echo ($row1['fname']);?></span></a>
				<?php
			}
			if (isset($_SESSION['owner_password2'])&&$row1['fname']=="Setting"){
				$name='href="javascript:void(0);" class="menu-toggle waves-effect waves-block"';
				?>
				<a <?php print_r($name);?> ><i class="material-icons"><?php echo $row1['icon'] ;?></i><span title="<?php echo $row1['disc'];?>"><?php echo ($row1['fname']);?></span></a>
				<?php
			}
		}else{
			if ($row1['file_name']==" "||$row1['file_name']==""||$row1['file_name']=="  "||$row1['file_name']=="0"||$row1['file_name']=="javascript:void(0);") {
				$name='href="javascript:void(0);" class="menu-toggle waves-effect waves-block"';
			} else{
				$name='href="'.$row1['file_name'].'"';
			}
			?>
			<a <?php print_r($name);?> ><i class="material-icons"><?php echo $row1['icon'] ;?></i><span title="<?php echo $row1['disc'];?>"><?php echo ($row1['fname']);?></span>
			</a>
			<?php
		}
		if(isset($row1['sub'])){
			if(!empty($row1['sub'])){
				if ($level==4) {
					$level=3;
					echo '<ul class="ml-menu">';
					tree($row1['sub'],$level);
					echo "</ul>"; 
				}elseif ($level==3) {
					$level =2;
					echo '<ul class="ml-menu">'; 
					tree($row1['sub'],$level);

					echo "</ul>";
				}elseif ($level==2) {
					$level =1;
					echo '<ul class="ml-menu">'; 
					tree($row1['sub'],$level);
					echo "</ul>";
				} else {
					$level ++;
					echo '<ul class="ml-menu">';
					tree($row1['sub'],$level);
					echo "</ul>";
				}
			}
		}else{
			tree($row1['sub'],0);
		}
		?>
	</li>
	<?php
	
}
}
?>