<?php
include("../init.php");
include("../_support/class.db.php");

$db = new db;
$db->SetDatabaseConnection($MySQL_User,$MySQL_Pass,'Warpit_Webuserspool',"localhost");



class DataTree{
	/*
	 * Load tree node.
	 */	
	
	function __construct($db,$table) {
		$this->db = $db;
		$this->table = $table;
	}
	
	function getTree($node){
		$nodes = $this->display_children($node);
		print json_encode($nodes);
	}
 	
	function display_children($parent) {
		// retrieve all children of $parent
		$res = $this->db->SQLexecute('SELECT id_sup,id,idLab FROM '.$this->table.' WHERE id_sup = '.$parent.' ORDER BY orderBy ');
		
		// display each child
		while ($row = $this->db->fetchAssoc($res)) {
			// Response parameters.
			$path['text']		= html_entity_decode($row['idLab']);
			$path['id']			= $row['id'];
			$path['position']	= $row['orderBy'];
			// Check if node is a leaf or a folder.
			$cRes = $this->db->SQLexecute ('SELECT * FROM '.$this->table.' WHERE id_sup = '.$row['id'].' LIMIT 2');
						
				// $cCount = mysql_num_rows($cRes);
			$cCount = $this->db->NumRows($cRes);
			if($cCount > 0){
				$path['leaf']	= false;
				$path['cls']	= 'folder';
				
			} else{
				$path['leaf']	= true;
				$path['cls']	= 'file';
				
			}
 			$path['href']   = "";
			// call this function again to display this
			// child's children
			$nodes[] = $path;
		}
		
		return $nodes;
	}
 
 
}


$treeDataModel = new DataTree($db,$_POST[table]);

$node = $_POST['node'];
$treeDataModel->getTree($node);
				
?>