<?
////////////////////////////////////////////////////
// Conciliación: gestión de conciliación de activos
// Version: 1.0
//
// Copyright (C) 2014  Eustotem Multimedia, S.L.
//
// License: MIT, see LICENSE
////////////////////////////////////////////////////


include("../conf/preferences.php");
session_name(SYS_APP_SESSNAME);
session_start();
include("../conf/langs/".$langs[$_SESSION['userlang']][0]."/MAIN.php");
include("../conf/connections.php");
include("../phplib/auth.php");
require_once("../phplib/utils.php");
require_once("../core/plugins.php");

$accion=$_GET['action'];

$obj=NULL;		
$result="";		

function getNavTree($padre){
	$cadena="";
	$query="SELECT a.*, (SELECT COUNT(id) FROM " . SYS_RESOURCES_TABLE . " WHERE parent=a.idResource) AS hijos FROM " . SYS_RESOURCES_TABLE . " a where parent='" . $padre . "' order by orden,idResource";			
	$q=mysql_query($query);
	while($f=mysql_fetch_array($q)){
		$id=$f['idResource'];
				$permiso=checkPermissions($id,$f['access']);
				if($permiso[0]==0) continue;
		
		$tiponodo= $f['hijos']>0 ? "folder" : "item";
		$parent= $padre=='0' ? '' : $padre;
		$nombre='root'.$id;
		$titulo=$f['label'];
		$masterview=$f['master'];
		$detailview=$f['detail'];
		$icono=$f['icon'];
		$tempicono=explode(".",$icono);
		$nombreicono=$tempicono[0];
		if($f['href']!='') $link=$link="../".$f['href'] . ($tiponodo!="folder" ? '?resId=' . $id : '#');
		$marco=$f['target']!='' ? $f['target'] : 'mainFrame';
		
		if($f['access']<4 && $f['menu']==1) $cadena.="<li id='phtml_{$id}' rel='{$nombreicono}'><a href='{$link}' rel='{$tiponodo}' target='{$marco}'><img src='../resources/icons16/{$icono}' class='iconomenu' /> {$titulo}</a>\n";

		
		if($tiponodo=="folder"){
			$cadena.="<ul>\n";
			if($f['hijos']>0) $cadena.=getNavTree($id);
			$cadena.="</ul>\n";
		}else if($tiponodo=="item"){
			$_SESSION['conf'][(string)$id]->id=$id;
			$_SESSION['conf'][(string)$id]->title=$titulo;
			$_SESSION['conf'][(string)$id]->href=$link;
			$_SESSION['conf'][(string)$id]->target=$marco;
			$_SESSION['conf'][(string)$id]->masterview=$masterview;
			$_SESSION['conf'][(string)$id]->detailview=$detailview;
		}
		if($f['access']<4 && $f['menu']==1) $cadena.="</li>\n";
	}
	return $cadena;
}


switch($accion){
	case "getMenu":
		$result="<ul>";
		$result.=getNavTree('0');
		$result.="</ul>";
		break;
}
echo $result;
?>