<?php 
include_once ("_dirinfo.php");

include_once (PATH_TO_ROOT."init.php");
include (PATH_TO_ROOT."webcati/init.php");
include_once (PATH_TO_ROOT."_ajaxManipulators/_class/class.gridData.php");
include_once (PATH_TO_ROOT."_ajaxManipulators/_class/class.treeData.php");

class ClassTreeMan extends TreeDataManipulator {
	public function deleteAction()
	{
		$sql = "DELETE FROM  _DesignerClsPropLink WHERE id_class = ".$_POST['nodeId'];
		$this->db->SQLexecute($sql);
		return parent::deleteAction();
	}
}

class DesignerPanelCssManipulator extends GridDataManipulator{
	
	public function selectAction()
	{
	
			$sql = "SELECT name,value,prop_value FROM ".WC_DESIGNER_CSS_PROPERTIES." a INNER JOIN ".WC_DESIGNER_CLS_PROP_LINK." b ON a.id = b.id_property WHERE id_class = ".$_POST['node'];
			if(!$rs = $this->db->SQLexecute($sql)) return '{success:false,error:"'.$this->db->GetError().'", STATEMENT: "'.$sql.'"}';
			while ($row = $this->db->fetchArray($rs))
			{
				$attr[] = $row;
			}
			if(!is_array($attr)) return "{success:true,object:[],editors:{}}";
			//we create an object for the property grid!!!
			$rows = array();
			$editors = array();
			
			foreach ($attr as $name) {
				$value = $name[2];
				$rows[$name[0]] = "$value";
			//	$rows[] = $r ;
				if($name[1])
				{//we set up a combobox
					if(is_array($propValues = explode(',',$name[1])  ) )
						$editors[] = "'".$name[0]."':new Ext.grid.GridEditor (new Ext.form.ComboBox({store:['".implode("','",$propValues)."'],triggerAction:'all',editable:true}))"; 
					//$editors[$name[0]] = "new Ext.grid.GridEditor (new Ext.form.DateField())";
				}
			}
					
			return "{success:true,object:".json_encode($rows).",editors:{".implode(',',$editors)."},sql:'$sql'}";
			
	}
	public function addStyleAction()
	{
		$clsId = $_POST['node'];
		$propId = $_POST['propId'];
		
		$sql = "INSERT INTO ".WC_DESIGNER_CLS_PROP_LINK." (id_class,id_property) VALUES($clsId,$propId)";
		If (!$this->db->SQLexecute($sql)) {	return '{success:false,error:"'.$this->db->GetError().'", STATEMENT: "'.$sql.'"}';}
		
		return "{success:true}";
	}
	public function setStyleAction()
	{
		$clsId = $_POST['node'];
		$prop = $_POST['prop'];
		$value = $_POST['value'];
		
		$sql = "SELECT id FROM ".WC_DESIGNER_CSS_PROPERTIES." WHERE name = '$prop'";
		if(!$rs = $this->db->SQLexecute($sql)) return '{success:false,error:"'.$this->db->GetError().'", STATEMENT: "'.$sql.'"}';
		while ($row = $this->db->fetchArray($rs))
		{
			$propId = $row[0];
		}
		
		
		$sql = "UPDATE ".WC_DESIGNER_CLS_PROP_LINK." SET prop_value = '$value' WHERE id_class = $clsId AND id_property = $propId";
		If (!$this->db->SQLexecute($sql)) {	return '{success:false,error:"'.$this->db->GetError().'", STATEMENT: "'.$sql.'"}';}
		
		return "{success:true}";
	}
	public function deleteAction()
	{
	
		
		$sql = "DELETE FROM _DesignerClsPropLink WHERE id_property = (SELECT id FROM _DesignerCssProperties WHERE name = '".$_POST['name']."') AND id_class = ".$_POST['node'];
		If (!$this->db->SQLexecute($sql)) {return '{success:false,error:"'.$this->db->GetError().'", STATEMENT: "'.$sql.'"}';}	
		return '{success:true }';
		
	}
}
	
	
	$db->ChangeDatabase(DB_WARPIT_WEBCATI_BASE);
	if($_POST['RMcomp'] == 'tree')
		$manipulator = new ClassTreeMan($db,$_POST['RMtable']);
	else
		$manipulator = new DesignerPanelCssManipulator($db,'nimaveze');
	$comm = $_POST['action'].'Action';
	if(method_exists($manipulator,$comm))
		echo $manipulator->$comm();
	else 
	{
		echo '{success:false,error:"Unknown action!"}';	
	}

?>