<?php
ob_start();
if(!(is_readable(realpath(dirname(__FILE__).'/../')."/include/loginroutines.inc"))) {
	die("Error - Could not open or read file ".realpath(dirname(__FILE__).'/../')."/include/loginroutines.inc");
} else {
	include_once(realpath(dirname(__FILE__).'/../')."/include/loginroutines.inc");
}

// параметр фасад передается как часть пути
if(!(is_readable(realpath(dirname(__FILE__).'/../')."/include/db/mysqlroutines.inc"))) {
	die("Error - Could not open or read file ".realpath(dirname(__FILE__).'/../')."/include/db/mysqlroutines.inc");
} else {
	include_once(realpath(dirname(__FILE__).'/../')."/include/db/mysqlroutines.inc");
}

if(!(is_readable(realpath(dirname(__FILE__).'/../')."/include/translate.inc"))) {
	die("Error - Could not open or read file ".realpath(dirname(__FILE__).'/../')."/include/translate.inc");
} else {
	include_once(realpath(dirname(__FILE__).'/../')."/include/translate.inc");
}

if(!(is_readable(realpath(dirname(__FILE__).'/../')."/include/content.inc"))) {
	die("Error - Could not open or read file ".realpath(dirname(__FILE__).'/../')."/include/content.inc");
} else {
	include_once(realpath(dirname(__FILE__).'/../')."/include/content.inc");
}

if(!(is_readable(realpath(dirname(__FILE__).'/../').'/classes/tree.php'))) {
	die('Error - Could not open or read file '.realpath(dirname(__FILE__).'/../').'/classes/tree.php');
} else {
	include_once(realpath(dirname(__FILE__).'/../').'/classes/tree.php');
}


if ( GetSetting('portal','useRights',0) == 1 && !IsUserLogin()){
	ob_end_flush();
	exit;
}

if (!isset($_SESSION['user_idlanguagecontent'])){
	checkLanguage();
}

$cn = db_connect();
$user_idlanguagecontent = $_SESSION['user_idlanguagecontent'];
session_write_close();

if (isset($_REQUEST['idbranch'])) {
	$idbranch = $_REQUEST['idbranch'];
	$skbranch = db_getSKBranch($cn,$idbranch);	
} else if (isset($_REQUEST['skbranch'])) {
	$skbranch = $_REQUEST['skbranch'];
	$idbranch = db_getIDBranch($cn, $skbranch );	
} else {
	$skbranch = db_getMainSKBranch($cn);
	$idbranch = db_getMainIDBranch($cn);	
}
$tr_arr = db_getObjectTreetoSearchtoUser($cn,
		isset($_REQUEST['root']) ? $_REQUEST['root'] : 0,
		GetUserIDs(), $user_idlanguagecontent, $skbranch);
echo '[ ';
$_cnt = 0;
foreach ($tr_arr as $node) {
	echo $_cnt == 0 ? '{' : ',{ ';
	echo '"text": "'.prepareTextToTree(nl2br($node['o_name'])).'",';
	echo '"icon":  "'.getImgIcon($node['o_icon']).'",';
	echo '"expanded": false, ';
	echo '"name": "'.str_replace('"', "&quot;", $node['o_guid']).'",';
	echo '"id": "'.$node['idobject'].'",';
	if (sizeof(db_getObjectTreetoSearchtoUser($cn, $node['idobject'], GetUserIDs(), $user_idlanguagecontent, $skbranch)) > 0){
		echo '"hasChildren": true';
	} else
		echo '"hasChildren": false';
	echo '} ';
	$_cnt = $_cnt + 1;
}
echo '] ';
db_disconnect($cn);
ob_end_flush();
?>
