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

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


class fieldEditManipulator extends TreeDataManipulator
{
	//function to get all nodes in a tree like format!
function selectAllAction()
	{
		
		$parent = $_POST['node'];
		
		$lab = $_POST['nameField'];
				if(!$lab) $lab = 'idLab';
		
		
		$sql = 'SELECT * FROM '.$this->table.' WHERE id_sup = '.$parent.' ';
		if(isset($_POST['filter']) && $_POST['filter'] != '')
		{
			$fil = $_POST['filter'];
			$sql.= "AND id_lab LIKE '%$fil% '";	
		}
		
		if (isset($_POST['RMorder'])) {
			$this->order[] = $_POST['RMorder'];
		} else {
			$this->order[] = 'orderBy';
		}
		if (isset($this->order))
		{
			$sql .= 'ORDER BY '.implode(',',$this->order);
		}
		
		/*echo $sql;
		exit;
		*/
		If (!$res = $this->db->SQLexecute($sql)) 
		{
			
				return '{success:false,error:"'.$this->db->GetError().'", STATEMENT: "'.$sql.'"}';
				
		}
		else 
		{
		
			// display each child
			while ($row = $this->db->fetchAssoc($res)) {
				// Response parameters.
				$path = array();
				
				
				if($_POST['boldRoots'] && $parent == 0)
				{
					$path['cls']		= 'treeFontBold';
					$path['unselectable'] = true;
					//$path['children'][] = array(text=>'1');
					//$path['children'][] = array(text=>'2');
				}
				
				$path['text']		=html_entity_decode($row[$lab]);
				
				$path['id']			= $row[$lab];
				$path['idOrg']      = $row['id'];
				$path['position']	= $row['orderBy'];
				
				if($this->isField)
				{
					$path['isField'] = true;
					$path['displayType'] = $row['field_type'];
				}
				
			//	$path['cls'] = 'textSize';
				
				//personal information fetches a querry from _InterviewerStructure
				if($parent == 0 && $row['id'] == 2 && $this->table == '_QueryMachineFields')
				{
					$this->table = '_InterviewerStructure';
					$_POST['node'] = 0;
					$_POST['nameField'] = 'field_name';
					$_POST['boldRoots'] = false;
					$this->isField = true;
						
					$path['children'] = json_decode( $this->selectAllAction(),true );
					
					$path['leaf'] = false;
					$this->table = '_QueryMachineFields';
					$_POST['nameField'] = 'idLab';
					$this->isField = false;
				}
				else 
				{
					$_POST['node'] = $row['id'];
				
					if($path['children'] = json_decode( $this->selectAllAction(),true ))
						{
							$path['leaf']	= false;
							
						} else{
							$path['leaf']	= true;
						}
				}
				if(isset($row['node_img']) && $row['node_img']!= '')
				{
					$path['icon'] = $row['node_img'];
				}
				else 
				{			
					if($cCount > 0){
						$path['iconCls']	= 'folder';
						
					} else{
						$path['iconCls']	= 'file';
						
					}
				}
				if(isset($row['node_img']) && $row['node_img']!= '')
				{
					$path['icon'] = $row['node_img'];
				}
	 			$path['href']   = "";
				// call this function again to display this
				// child's children
				$nodes[] = $path;
			}
			
			//return $nodes;
		}
		return json_encode($nodes);
	}
}


class queryGridManipulator extends GridDataManipulator
{
	function selectAction()
	{
	
			$sql = "SELECT a.*,b.idLab as groupName,d.lab_label as comboName FROM `_QueryMachineConstruction` a 
				LEFT JOIN _groups b ON a.valueField = b.id 
				LEFT JOIN _InterviewerStructure c ON a.field_id = c.field_name 
				LEFT JOIN user_input_labels d ON c.id = d.lab_field AND a.valueField = d.lab_code";
		/*echo $sql;
		exit;*/
		//custom where statement send by parameters
		$postWhere = $_POST['SQLwhere'];
		if($postWhere)
		{
			$this->where[] = $postWhere;
		}
		
		if (is_array($this->where))
		{
			$sql .= ' WHERE '.implode(' AND ',$this->where);		
		}
		
			$sql .= ' ORDER BY orderBy';
		
		$this->arr = array();
		If (!$this->rs = $this->db->SQLexecute($sql)) {	return '{success:false,error:"'.$this->db->GetError().'", STATEMENT: "'.$sql.'"}';}

		while($obj = $this->db->fetchAssoc($this->rs)){
			$this->arr[] = $obj;
		}
		$echo = '{success:true';

		$echo.= ',rows: '.json_encode($this->arr);
		$echo.='}';
		return $echo;
				
		
	}
	
	function deleteAction()
		{
			$sql = "SELECT id_template,orderBy FROM $this->table WHERE id = ".$_POST['ids'];
			If (!$rs = $this->db->SQLexecute($sql))
						{
						
							return '{success:false,error:"'.$this->db->GetError().'", STATEMENT: "'.$sql.'"}';
						}
			$row = $this->db->fetchArray($rs);

			$fld = $row[0];
			$pos = $row[1];
			
			$sql = "UPDATE $this->table SET orderBy = orderBy -1 WHERE id_template = $fld AND orderBy > $pos";
			If (!$this->db->SQLexecute($sql))
					{
					
						return '{success:false,error:"'.$this->db->GetError().'", STATEMENT: "'.$sql.'"}';
					}
			
			return parent::deleteAction();
		}
}

$db->ChangeDatabase($_POST['database']);

if($_POST['RMcomp']=='tree')
	$manipulator = new fieldEditManipulator($db,$_POST['RMtable']);
else 
{
	$manipulator = new queryGridManipulator($db,$_POST['RMtable']);
}
$comm = $_REQUEST['action'].'Action';
if(method_exists($manipulator,$comm))
	echo $manipulator->$comm();
else 
{
	
	echo '{success:false,error:"Unknown action!"}';	
	
}


?>
