<?php require "commonincludefiles.php";
//error_reporting(1);
if($_SESSION["VedantnrCollegeId"]!="" && isset($_SESSION["VedantnrCollegeId"])){
	$objMenuMgmt = new MenuMgmt();
	$objPageMgmt= new PageMgmt();
ini_set("display_errors", true);
ini_set("html_errors", true);
set_time_limit(600);
if($_REQUEST['hod'] == 1)
{
   $menu_ids 	= $HODMenuRights ;
   $page_ids	= $HODPageRights;
}
if($_REQUEST['faculty'] == 1)
{
   $menu_ids 	= $FacultyMenuRights ;
   $page_ids	= $FacultyPageRights;
}
else
{
    $menu_ids 	= $_REQUEST["menuid"];
    $page_ids	= $_REQUEST["pageid"];
}
$user_id 	= $_REQUEST["userid"];       
$module_ids = $_REQUEST["moduleid"];

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


$arrmodids 	= explode(",",$module_ids);
$arrmenuids = explode(",",$menu_ids);
$arrpageids = explode(",",$page_ids);

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 cms_modulemaster where module_active = 1";
	//$out=dbQuery($qry);
	
	$out = $objMenuMgmt->getAllModule($condition="");
	
    if (dbRows($out)>0) 
    {
    	$chk = false;
    	//print_r($arrmodids) ;
    
    	while($row= dbArray($out)) 
    	{	
			if(in_array($row["module_id"],$arrmodids) )
			{
				$chk = true;
				
				
			}
			else
			{
				$chk = false;
			}	
			$treeNodes->add("module-".$row["module_id"],$row["module_fullname"],"",false,false,$chk,true);
			
		}
    }	 	
	
} else if('module' == $tempNode){
	
	$mod_id = explode("-",$parent);
	//$qry = "SELECT * FROM cms_menumaster where module_id = $mod_id[1] AND menu_active = 1 ORDER BY menu_order ASC ";
	//$out=dbQuery($qry);

	$condition1 ="";
	$condition1 .= " and module_id = $mod_id[1] ";
	$out = $objMenuMgmt->getmenu($condition1);
    if (dbRows($out)>0) {
    	while($row=dbArray($out)) {
			if(in_array($row["menu_id"],$arrmenuids) )
			{
				$chk = true;
			}
			else
			{
				$chk = false;
			}	
			
			$treeNodes->add($parent."/menu-".$row["menu_id"],$row["menu_name"],"",false,false,$chk,true);
		}
    }
}else if('menu' == $tempNode){

	//$qry = "SELECT * FROM cms_pagemaster where 1=1 and  menu_id= $ids AND page_active = 1 ORDER BY page_order ASC ";
	//$out=dbQuery($qry);
	$condition2 = "";
	$condition2 .= " and menu_id = $ids ORDER BY page_order ASC";
	
    $out = $objPageMgmt->getpage($condition2);
	
	
	if (dbRows($out)>0) {
    	while($row=dbArray($out)) {
			if(in_array($row["page_id"],$arrpageids) )
			{
				$chk = true;
			}
			else
			{
				$chk = false;
			}	
			if($row["access"] == "0,0,0")
			{
				$leaf = true;
			}
			else
			{
				$leaf = false;
			}
			$treeNodes->add($parent."/page-".$row["page_id"],$row["page_name"],"",$leaf,false,$chk,true);
		}
    }	
} 
else if('page' == $tempNode)
{
		$mod = explode("-",$temp_array[0]);
		$men = explode("-",$temp_array[1]);

		//$qry = "SELECT access FROM cms_pagemaster where page_id = '".$ids."' AND menu_id = '".$men[1]."' ";
		//$out=dbQuery($qry);

		$condition3="";
		$condition3 .=" and page_id = '".$ids."' AND menu_id = '".$men[1]."' ";
		$out = $objPageMgmt->getpageaccess($condition3);
		if (dbRows($out)>0)
		{
			$chkAdd = false;
			$chkEdit = false;
			$chkDelete = false;
			
			$row = dbArray($out);
			
			//echo "<pre>";
			//print_r($row['access']);
			
			if(in_array($ids,$arrpageids))
			{	
			$chkAdd = true;
			$chkEdit = true;
			$chkDelete = true;
				
				
				//$qry2 = "SELECT * FROM cms_useraccess where 1=1 AND page_id = '".$ids."'  and user_id = '".$user_id."' AND menu_id = '".$men[1]."' ";
				//$out2=dbQuery($qry2);
 
				$objUserAccessMgmt= new UserAccessMgmt();

			$condition4 ="";
			$condition4 .=" AND page_id = '".$ids."'  and user_id = '".$user_id."' AND menu_id = '".$men[1]."' ";
			$out2 = $objUserAccessMgmt->getuseraccess($condition4);
				if (dbRows($out2)>0)
				{
					//echo "nirav-p";
						while($row2=dbArray($out2)) 
						{
							$chkAdd = $row2["useraccess_add"];
							$chkEdit = $row2["useraccess_edit"];
							$chkDelete = $row2["useraccess_delete"];
							if ($chkAdd==1 ){
							$chkAdd = true;
							}
							else
							{
								$chkAdd = false;
							}
							if ($chkEdit==1 ){
							$chkEdit = true;
							}
							else
							{
								$chkEdit = false;
							}
							if ($chkDelete==1){
							$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();
}
?>