<?php 
include '_dirinfo.php';
include_once(PATH_TO_ROOT."_support/class.db.php");


$db = new db;
$db->SetDatabaseConnection(test,test,localhost);

function ob_file_callback($buffer)
{
  global $ob_file;
  fwrite($ob_file,$buffer);
}

function getLabel($type)
	{
		global $db;
		if($type)
		{
		$sql = "SELECT idLab FROM _DesignerElements WHERE type = ".$type;
		if(!$rs = $db->SQLexecute($sql)) return '{success:false,error:"'.$db->GetError().'", STATEMENT: "'.$sql.'"}';
			if ($row = $db->fetchArray($rs))
			{
				return $row[0];
			}
		}
		return 'Panel';
		
	}

function readXML($xmlObj,$parType,$offSet,$chNum)
{
	global $qVar;
	foreach ($xmlObj->attributes() as $attr => $value) {
			$prop[$attr] = "$value";
		}
	  	$prop['text'] = getLabel($prop['type']).' ';

	if($prop['id'] == 0 )
		{
		/* foreach($xmlObj->children() as $child) {
		    readXML($child,$prop['type'],$offSet);
		    $offSet+=100;
		  }
		  return;*/
			$prop['type'] = 2;
			$prop['position'] = 'relative';
			$prop['height'] = 100;
		}	
	  	
	  	
	  if($parType == 1)
	  {
	  	$pos = "position:absolute;left:$offSet;right:0";
	  	$mainSize = 'width';
	  	$secSize = 'height';
	  }
	  else
	  {
	  	$pos = "top:$offSet;width:100%";
	  	$mainSize = 'height';
	  	$secSize = 'width';
	  }
	  $size = "position:absolute;$secSize:100%;$mainSize:".$prop[$mainSize];
	  if($prop['position'] == 'relative')
	  {
	  	$size .= '%';
	  	//$height = $prop['height']."%";
	  	//$width = $prop['width']."%";	
	  }
	  
	  else if($prop['position'] == 'auto')
	  {
	  	$size = "$secSize:100%;";
	  }
	else
	  {
	  	$size .= 'px';
	  	//$height = $prop['height']."px";	
	  	//$width = $prop['width']."px";	
	  }
	  //we encose each "brother" in its own div to keep the positions!
	  echo "<div test=11 style=\"$pos;height:100%;\">"; //we close it with a single string after the querry!
	  echo "<div id=\"".$prop['id']."\" style=\"$size;";//."border:1px solid blue"
	  if($prop['type'] != 1 && $prop['type'] != 2)
	  {
	  	echo "border-left:1px inset blue;border-top:1px inset blue;";
	  }
	//  echo "text-align:center;";
	  echo "\">";
	  //lets echo the proper text dependend on type
	  switch ($prop['type'])
	  {
	  	case 3:
	  	//question
	  	echo $qVar['vpr_sql'];
	  	break;
	  	case 4:
	  	//instruction	
	  	echo $qVar['nav_sql'];
	  	
	  	break;
	  	case 6:
	  	//answers
	  	echo $qVar['odg_sql'];
	  	break;
	  	case 7:
	  		//variable
	  	echo $qVar['q_name'];
	  	break;
	  	case 7:
	  	break;
	  	
	  	
	  }
	  $lala=0;
	  $numCh = 0;
	  foreach($xmlObj->children() as $child) {
	  	$numCh++;
	    readXML($child,$prop['type'],$lala,$nu);
	    $closeDivs.= "</div>";
	    $lala=  $child[$secSize]. ($child['position'] == 'relative' ? '%' : 'px');
	   			if($child['position'] == 'relative' && $prop['type'] == 2)
			  {
			  	$lala .= ';position:absolute;';
			  	//$height = $prop['height']."%";
			  	//$width = $prop['width']."%";	
			  }
	  			else if($child['position'] == 'auto')
			  {
			  	$lala = "";
			  }
			  else if($prop['type'] == 2)
			  {
			  	$lala .= ';position:absolute;';
			  	//$height = $prop['height']."px";	
			  	//$width = $prop['width']."px";	
			  }
			  
	  }
	  echo $closeDivs;
	 echo "</div>";
	 
}

$db->ChangeDatabase('WarpitDevel_Webcati');
$sql = "SELECT * FROM testIG1 WHERE id=22";
if(!$rs = $db->SQLexecute($sql)) echo $db->GetError();
	$qVar = $db->fetchArray($rs);
//$ob_file = fopen('test.txt','w'); ob_start('ob_file_callback');
	
	
	
$db->ChangeDatabase('WarpitDevel_Webcati_base');
	$sql = "SELECT template_xml FROM _DesignerTemplates WHERE id = 1";
	if(!$rs = $db->SQLexecute($sql)) echo $db->GetError();
	$row = $db->fetchArray($rs);
	$curXML = $row[0];
	if($curXML)
	{
		echo "<body style=\"height:100%;width:100%;border: 0 none;margin:0;padding:0;overflow:hidden; \">";
		$xml = simplexml_load_string( $curXML );
		$numCh = 0;
		 foreach($xml->children() as $child) {
		  	$numCh++;
		    readXML($child,0,$lala,$numCh);
		    $closeDivs.= "</div>";
		    $lala=  $child['height']. ($child['position'] == 'relative' ? '%' : 'px');
		 if($child['position'] == 'relative')
			  {
			  	$lala .= ';position:absolute;';
			  	//$height = $prop['height']."%";
			  	//$width = $prop['width']."%";	
			  }
	  			else if($child['position'] == 'auto')
			  {
			  	$lala = "";
			  }
			  else 
			  {
			  	$lala .= ';position:absolute;';
			  	//$height = $prop['height']."px";	
			  	//$width = $prop['width']."px";	
			  }
		  }
		 echo $closeDivs;
		
	}
	
//ob_end_flush();
	
	
	
?>
</body>
