<?php require "commonincludefiles.php";

if($_SESSION["cCompany_id"]!="" && isset($_SESSION["cCompany_id"]))
{
ini_set("display_errors", true);
ini_set("html_errors", true);
	
$user_id 	= $_REQUEST["userid"];       

$menu_ids 	= $_REQUEST["menuid"]; 
$page_ids	= $_REQUEST["pageid"]; 
$subpageids	= $_REQUEST["subpageids"]; 

$formMode 	= ($user_id != 0 ? "edit" : "add");


$arrmenuids = explode(",",$menu_ids);
$arrpageids = explode(",",$page_ids);
$arrsubpageids = explode(",",$subpageids);

class TreeNode{	
	public $text = "";
	public $id = "";
	public $iconCls = "";
	public $leaf = true;
	public $draggable = false;
	public $checked = false;
	public $singleClickExpand = true;

	function  __construct($id,$text,$iconCls,$leaf,$draggable,$checked,$singleClickExpand) {
		$this->id = $id;
		$this->text = $text;
		$this->iconCls = $iconCls;
		$this->leaf = $leaf;
		$this->draggable = $draggable;
		$this->checked = $checked;
		$this->singleClickExpand = $singleClickExpand;	
	}	
}

class TreeNodes {
	
	protected $nodes = array();
	
	function add($id,$text,$iconCls,$leaf,$draggable,$checked,$singleClickExpand){
		$n = new TreeNode($id,$text,$iconCls,$leaf,$draggable,$checked,$singleClickExpand);		
		$this->nodes[] = $n;
	}
	
	function toJson() {
		return json_encode($this->nodes);
	}
}

$requestedNode = "";

if (isset($_REQUEST["node"])) {
	$requestedNode = $_REQUEST["node"];
}

$treeNodes = new TreeNodes();

if('node-root' != $requestedNode){
	$parent = $requestedNode;
	$mastercount = (substr_count ($requestedNode,"/"));
	if($mastercount == 0){
		$requestedNode_array = explode("-",$requestedNode);
		$tempNode = $requestedNode_array[0];
		$ids =  $requestedNode_array[1];
	}
	if($mastercount == 1){
		$temp_array = explode("/",$requestedNode);
		$requestedNode_array = explode("-",$temp_array[1]);
		$tempNode = $requestedNode_array[0];
		$ids =  $requestedNode_array[1];
	}
	if($mastercount == 2){
		$temp_array = explode("/",$requestedNode);
		$requestedNode_array = explode("-",$temp_array[2]);
		$tempNode = $requestedNode_array[0];
		$ids =  $requestedNode_array[1];
	}
}

if('node-root' == $requestedNode){
	$qry = "SELECT * FROM lms_menumaster where  menu_active = 1 ORDER BY menu_order ASC ";
	$out=dbQuery($qry);
    if (dbRows($out)>0) {
    	while($row=dbArray($out)) {
			if(in_array($row["menu_id"],$arrmenuids) && $formMode != "add")
			{
				$chk = true;
			}
			else
			{
				$chk = false;
			}	
			
			$treeNodes->add($parent."/menu-".$row["menu_id"],$row["menu_name"],"",false,false,$chk,true);
		}
    }
	
} else if('menu' == $tempNode){
	$men = explode("-",$parent);
	$qry = "SELECT * FROM lms_pagemaster where 1=1 and  menu_id= $ids AND page_active = 1 ORDER BY page_order ASC ";
	$out=dbQuery($qry);
    if (dbRows($out)>0) {
    	while($row=dbArray($out)) {
			if(in_array($row["page_id"],$arrpageids) && $formMode != "add")
			{
				$chk = true;
			}
			else
			{
				$chk = false;
			}	
			if($row["page_link"]!= "")
			{
				$leaf = true;
			}
			else
			{
				$leaf = false;
			}
			$treeNodes->add($parent."/page-".$row["page_id"],$row["page_name"],"",$leaf,false,$chk,true);
		}
    }	

} 
else if('page' == $tempNode){
	$men = explode("-",$parent);
	$qry = "SELECT * FROM lms_subpagemaster where 1=1 and  page_id= $ids AND subpage_active = 1 ORDER BY subpage_order ASC ";
	$out=dbQuery($qry);
    if (dbRows($out)>0) {
    	while($row=dbArray($out)) {
			if(in_array($row["subpage_id"],$arrsubpageids) && $formMode != "add")
			{
				$chk = true;
			}
			else
			{
				$chk = false;
			}	
			if($row["subpage_link"]!= "")
			{
				$leaf = true;
			}
			else
			{
				$leaf = false;
			}
			$treeNodes->add($parent."/subpage-".$row["subpage_id"],$row["subpage_name"],"",$leaf,false,$chk,true);
		}
    }	

}
/*else if('page' == $tempNode){

		$men = explode("-",$temp_array[0]);
		
		$qry = "SELECT access FROM lms_pagemaster where page_id = '".$ids."' AND menu_id = '".$men[1]."' ";
			$out=dbQuery($qry);
			if (dbRows($out)>0)
			{
				$chkAdd = false;
				$chkEdit = false;
				$chkDelete = false;
				
				$row=dbArray($out);
				/*if(in_array($ids,$arrpageids))
				{	
					$qry2 = "SELECT * FROM lms_useraccess where 1=1 AND page_id = '".$ids."'  and user_id = '".$user_id."' AND menu_id = '".$men[1]."' ";
					$out2=dbQuery($qry2);
					if (dbRows($out2)>0)
					{
						while($row2=dbArray($out2)) {
							$chkAdd = $row2["useraccess_add"];
							$chkEdit = $row2["useraccess_edit"];
							$chkDelete = $row2["useraccess_delete"];
							if ($chkAdd==1 && $formMode != "add"){
							$chkAdd = true;
							}
							else
							{
								$chkAdd = false;
							}
							if ($chkEdit==1 && $formMode != "add"){
							$chkEdit = true;
							}
							else
							{
								$chkEdit = false;
							}
							if ($chkDelete==1 && $formMode != "add"){
							$chkDelete = true;
							}
							else
							{
								$chkDelete = false;
							}
						}
					}
				}
				//if(in_array($ids,$arrpageids)) ends	
				$access = explode(",",$row["access"]);
				
				if($access[0] == 1){
					$treeNodes->add($parent."/page-".$ids."-"."add","Add","",true,false,$chkAdd,true);
				}
				if($access[1] == 1){
					$treeNodes->add($parent."/page-".$ids."-"."edit","Edit","",true,false,$chkEdit,true);
				}
				if($access[2] == 1){
					$treeNodes->add($parent."/page-".$ids."-"."delete","Delete","",true,false,$chkDelete,true);
				}*/
				 	
				/*
				module-1/section-1/menu-1/page-1/page-1-add
			
		}//if (dbRows($out)>0)  ends
	
}*/
echo $treeNodes->toJson();
}
?>