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

include_once (PATH_TO_ROOT."init.php");

class TreeDataManipulator{
	/*
	 * Load tree node.
	 */	
				
	function __construct($db,$table) 
	{
		$this->db = $db;
		$this->table = $table;
	}
		
	function selectAction()
	{
		$parent = $_POST['node'];
		$parent = explode("_", $parent);
		//$sql = 'SELECT * FROM '.$_POST['RMtable'].' WHERE id_sup = '.$parent.' ';
		
		$used_lang = 1;
		
		$faq_res = $this->db->SQLexecute('SELECT * FROM '.$this->table.' WHERE id = '.$parent[0]);
		$faq = $this->db->fetchAssoc($faq_res);
		
		/*if($faq['faq']=='1' && $faq['topic']=='0'){
			$sql = 'SELECT a.node_img,a.id_sup,a.id,b.title AS idLab FROM '.$_POST['RMtable'].' AS a, '.$_POST['RMtable'].'Lang AS b WHERE a.id = b.id_sup AND b.id_lang = "'.$used_lang.'" AND a.topic = "1" AND a.topic_sub = "0" AND a.faq = "1" ORDER BY a.orderBy ';
		}else{
			$sql = 'SELECT a.node_img,a.id_sup,a.id,b.title AS idLab,a.topic FROM '.$_POST['RMtable'].' AS a, '.$_POST['RMtable'].'Lang AS b WHERE a.id_sup = '.$parent[0].' AND a.id = b.id_sup AND b.id_lang = "'.$used_lang.'" ORDER BY a.topic ASC, a.orderBy ';
		}*/
		
		$sql = 'SELECT a.node_img,a.id_sup,a.id,b.title AS idLab,a.topic FROM '.$_POST['RMtable'].' AS a, '.$_POST['RMtable'].'Lang AS b WHERE a.id_sup = '.$parent[0].' AND a.id = b.id_sup AND b.id_lang = "'.$used_lang.'" ORDER BY a.topic ASC, a.orderBy ';
	
		/*if (isset($_POST['RMorder'])) {
			$this->order[] = $_POST['RMorder'];
		} else {
			$this->order[] = 'orderBy';
		}
		if (isset($this->order))
		{
			$sql .= 'ORDER BY '.implode(',',$this->order);
		}*/
		
		
		If (!$res = $this->db->SQLexecute($sql)) 
		{			
				Echo '{success:false,error:"'.$this->db->GetError().'", STATEMENT: "'.$sql.'"}';
		}
		else 
		{
		
			// display each child
			$i = 0;
			while ($row = $this->db->fetchAssoc($res)) {
				$i++;
				// Response parameters.
				$path = array();
				
				
				if($_REQUEST['boldRoots'] && $parent == 0)
				{
					$path['cls']		= 'treeFontBold';
					//$path['children'][] = array(text=>'1');
					//$path['children'][] = array(text=>'2');
				}
				
				/*$path['text']		=' '.html_entity_decode($row['idLab']);
				
				$path['id']			= $row['id'];
				$path['position']	= $row['orderBy'];*/
				
				$path['text']		= ''.html_entity_decode($row['idLab']);
				$path['id']	= $row['id'].'_'.time().'_'.$i;	
				$path['position']	= $row['orderBy'];
							
				//$path['cls'] = 'textSize';
			
				// 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;
						
					} 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
				*/
				
				if($row['topic']=='0'){
					// Check if node is a leaf or a folder.
					$path['leaf']	= false;					
					//$path['iconCls']	= 'folder';
					$path['iconCls']	= 'topic';
										
				}elseif($row['topic_sub']=='1'){
					// Check if node is a leaf or a folder.
					//$cRes = $this->db->SQLexecute ('SELECT * FROM '.$this->table.' AS a, '.$this->table.'Lang AS b WHERE a.id_sup = '.$row['id'].' AND a.id = b.id_sup AND b.id_lang = "'.$used_lang.'" AND a.id_user = "0" LIMIT 2');
													
					$path['leaf']	= true;
					$path['iconCls']	= 'topicSub';
										
				}else{
					//it is allways leaf and cls is topic
					if($cCount>0){						
						$path['leaf']	= false;
					}else{
						$path['leaf']	= true;
					}
					$path['iconCls']	= 'topic';
						
				}
				
				
				if(isset($row['node_img']) && $row['node_img']!= '')
				{
					$path['icon'] = $row['node_img'];
				}
				if(isset($row['node_img']) && $row['node_img']!= '')
				{
					$path['icon'] = $row['node_img'];
				}
	 			$path['href']   = "";
				
				
				$nodes[] = $path;
			}
			
			//return $nodes;
		}
		echo json_encode($nodes);
	}
 		
	function deleteAction()
	{
		$parent = $_POST[nodeId];
		$parent = explode("_", $parent);
		
		$sql_t_res = $this->db->SQLexecute("SELECT * FROM ".$this->table." WHERE id_sup = ".$parent[0]);
		$t_num = $this->db->NumRows($sql_t_res);
		
		$sql_a_res = $this->db->SQLexecute("SELECT * FROM ".$this->table." WHERE id = ".$parent[0]);
		$row_a = $this->db->fetchAssoc($sql_a_res);
		
		
		if($parent[0]>0 && $t_num==0 && $row_a['id_sup']>0 ){
			$sql = "DELETE FROM  ".$this->table." WHERE id = ".$parent[0];
			//echo $sql;
			
			if($this->db->SQLexecute($sql)){
				echo " { success: 1  }";
				$this->db->SQLexecute($sql = "DELETE FROM  ".$this->table."Lang WHERE id_sup = ".$parent[0]);
				$this->db->SQLexecute('DELETE FROM '.$_REQUEST['RMtable'].'Also WHERE id_sup_1 = '.$parent[0].' OR id_sup_2 = '.$parent[0]);			
			
			}else 
			{
				echo "{success:false,STATEMENT:'$sql'}";
			}
		}else{
			echo "{success:false,STATEMENT:'This item cannot be deleted!'}";
		}
	}
	function editAction()
	{
		$parent = $_POST[nodeId];
		$parent = explode("_", $parent);
		
		$used_lang = 1;
		
		$sql_a_res = $this->db->SQLexecute("SELECT * FROM ".$this->table." WHERE id = ".$parent[0]);
		$row_a = $this->db->fetchAssoc($sql_a_res);
		
		if($row_a['id_sup']!='0'){
		
			$this->db->SQLexecute($sql = "UPDATE  ".$this->table." SET idLab = '".$_POST[newValue]."' WHERE id_sup != '0' AND id = ".$parent[0]);
			$sql = "UPDATE  ".$this->table."Lang SET title = '".$_POST[newValue]."' WHERE id_sup != '0' AND id_sup = ".$parent[0]." AND id_lang = ".$used_lang;
			
			if($this->db->SQLexecute($sql))
				echo " { success: 1  }";
			else 
			{
				echo "{success:false,STATEMENT:'$sql'}";
			}
		}else{
			
			echo "{success:false,STATEMENT:'$sql'}";
		}
	}
	
	function insertAction()
	{
		$parent = $_POST[nodeId];
		$parent = explode("_", $parent);
		
		$used_lang = 1;
		
			$sql = "INSERT INTO  ".$this->table." SET id_sup = ".$parent[0].", idLab='".$_POST[nodeText]."'";
			$this->db->SQLexecute($sql);
			$last = $this->db->GetLastInsertId();
			$sql = "UPDATE  ".$this->table." SET orderBy = ".$last." WHERE id = ".$last;  
			$this->db->SQLexecute($sql);
			$sql = "INSERT INTO  ".$this->table."Lang SET id_sup = ".$last.", title='".$_POST[nodeText]."', text='Empty!', id_lang='".$used_lang."'";
			$this->db->SQLexecute($sql);
			
		
		echo " { success:true,insertId: $last  }";
	}
	
	function insertTopicAction()
	{
		$parent = $_POST[nodeId];
		$parent = explode("_", $parent);
		
		$used_lang = 1;
		
		//preverim ce kliknem na topic
		$t_s = '';
		$sql_t = "SELECT * FROM ".$this->table." WHERE id = ".$parent[0];
		$sql_t_res = $this->db->SQLexecute($sql_t);
		$row_t = $this->db->fetchAssoc($sql_t_res);
		
		if($row_t['topic_sub']=='0'){
			
			if($row_t['topic']=='1'){
				$t_s = ", topic_sub='1'";
				$t_s .= ", node_img='webhelp/_icons/Text_Document.ico'";
			}else{
				$t_s = ", node_img='webhelp/_icons/Documents.ico'";
			}
			
			$sql = "INSERT INTO  ".$this->table." SET id_sup = ".$parent[0].", idLab='".$_POST[nodeText]."', topic='1'".$t_s;
			$this->db->SQLexecute($sql);
			$last = $this->db->GetLastInsertId();
			$sql = "UPDATE  ".$this->table." SET orderBy = ".$last." WHERE id = ".$last;  
			$this->db->SQLexecute($sql);
			$sql = "INSERT INTO  ".$this->table."Lang SET id_sup = ".$last.", title='".$_POST[nodeText]."', text='Empty!', id_lang='".$used_lang."', date_m='".date('Y-m-d H:i:s')."'";
			$this->db->SQLexecute($sql);
			
		
			echo " { success:true,insertId: $last  }";
		}else{
			echo " { success:false,reason: 'Already subtopic'  }";
		}
		
			
	}
	
	function insertCatAction()
	{
		$parent = $_POST[nodeId];
		$parent = explode("_", $parent);
		
		
		$sql = "SELECT * FROM ".$this->table." WHERE id = ".$parent[0];
		$sql_res = $this->db->SQLexecute($sql);
		$row = $this->db->fetchAssoc($sql_res);	
		
		if($row['topic']=='0'){
			$used_lang = 1;
		
			$sql = "INSERT INTO  ".$this->table." SET id_sup = ".$parent[0].", idLab='".$_POST[nodeText]."', topic='0', node_img='webhelp/_icons/File_and_Folder.ico'";
			$this->db->SQLexecute($sql);
			$last = $this->db->GetLastInsertId();
			$sql = "UPDATE  ".$this->table." SET orderBy = ".$last." WHERE id = ".$last;  
			$this->db->SQLexecute($sql);
			$sql = "INSERT INTO  ".$this->table."Lang SET id_sup = ".$last.", title='".$_POST[nodeText]."', text='Empty!', id_lang='".$used_lang."', date_m='".date('Y-m-d H:i:s')."'";
			$this->db->SQLexecute($sql);
			
		
			echo " { success:true,insertId: $last  }";
		}else{
			echo " { success:false, reason: 'Category cannot be added under topic!'  }";
		}
			
	}
		
	function moveNodeAction()
	{
		$parent = $_POST[nodeId];
		$parent = explode("_", $parent);
		
		 $sql = "UPDATE  ".$this->table." SET  orderBy = orderBy + 1 WHERE orderBy = ".$_POST[orderBy]." AND id_sup = ".$_POST[newParent];
			$this->db->SQLexecute($sql);
			$sql = "UPDATE  ".$this->table." SET id_sup = ".$_POST[newParent].",orderBy = ".$_POST[orderBy]."  WHERE id = ".$parent;
			$this->db->SQLexecute($sql);
			if($this->db->isError())
			{
				echo "{error:'".$this->db->getError()."',statement:'$sql' }";
			}
			else 
				echo " { success: 1  }";
			
	}
 
}




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

$tableName = $_POST['RMtable'];
$manipulator = new TreeDataManipulator($db,$tableName);
$comm = $_POST['action'].'Action';
if(method_exists($manipulator,$comm))
	$manipulator->$comm();
else 
{
	
	Echo '{success:false,error:"Unknown action!"}';	
	
}
?>