<?php 	
//ini_set("display_errors", "1");
include_once (PATH_TO_ROOT."_ajaxManipulators/_class/class.treeData.php");
include_once (PATH_TO_ROOT."_ajaxManipulators/_class/class.gridData.php");
include_once (PATH_TO_ROOT."CRM/class/class.checkString.php");

class wcGroupsTreeDataMan extends TreeDataManipulator 
{
	
	function selectAction()
	{
		
		if ($_POST['database']) $addIt = StringChecker::Check($_POST['database'])."."; else $addIt = "";
		
		$parent = StringChecker::Check($_POST['node']);
		$sql = 'SELECT * FROM '. $addIt.$this->tableTree.' WHERE ';
		
		if(isset($_POST['filter']) && $_POST['filter'] != ""){
			if(isset($_POST['filter']) && strlen($_POST['filter']) > 0 )
			{
				$fil = $_POST['filter'];
				$sql.= " (idLab LIKE '%". StringChecker::Check($fil) ."%' AND is_leaf = 1) ";	
			} 
		} else 
			
		$sql.= " id_sup = ".StringChecker::Check($parent);
		$sql .= ' ORDER BY is_leaf ASC, idLab ASC';
		
		if (!$res = $this->db->SQLexecute($sql)) 
		{	
			Echo '{success:false,error:"'.$this->db->GetError().'", STATEMENT: "'.$sql.'"}';
		}
		else 
		{
		//echo $sql;
			// display each child
			if($this->db->NumRows($res) != 0){
				while ($row = $this->db->fetchAssoc($res)) {
					//echo $sql;
					// Response parameters.
					$path = array();
					
					if(isset($_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['isProject'] 	= $row['is_leaf']; 
					$path['id_project'] = $row['id_project']; 
				//	$path['cls'] = 'textSize';
					// Check if node is a leaf or a folder.
				
					if(isset($row['node_img']) && $row['node_img']!= '')
					{
						$path['icon'] = $row['node_img'];
					}
	
					if (isset($row['q_pos']))
		 				$path['q_pos'] = $row['q_pos'];
					
					$path['leaf']	= true; 
					if($path['isProject'] != 1){
						$path['leaf']	= false;  
					} else {
						$path['icon'] = "extjs/resources/images/default/tree/leaf.gif";
						if(isset($_POST['filter']) && strlen($_POST['filter']) > 0 ){
						//	ini_set("display_errors",1);
							
							$sqlFilter = "SELECT proj_name FROM $addIt`_Projects` WHERE id = ".StringChecker::Check($row['id_project']);
							$resFilter = $this->db->SQLexecute($sqlFilter);
							$datFilter = $this->db->fetchAssoc($resFilter);
							$ProjectName = $datFilter['proj_name'];
							$tableOk=false;
							if(isset($ProjectName) && $ProjectName != ""){
								$tableName = $ProjectName."_structure";
								$langName = $ProjectName."_language";
								
								if($this->db->TableExists($tableName, DB_WARPIT_WEBCATI_BASE) && $this->db->TableExists($langName, DB_WARPIT_WEBCATI)){
									
									$resFilter = $this->db->SQLexecute("DESCRIBE ".DB_WARPIT_WEBCATI_BASE.".".$tableName);
									$fields = array();
									while($datFilter = $this->db->fetchAssoc($resFilter)){
										array_push($fields, $datFilter['Field']);
									}
									
									if(in_array("dan", $fields) && in_array("ura", $fields) && in_array("smpl_var", $fields))
										$tableOk = true;
									
									
									if($tableOk){
										$path['leaf'] = false;
										$sqlFilter = "SELECT * FROM ".DB_WARPIT_WEBCATI.".`". StringChecker::Check($langName) ."` WHERE `id_question` IN ( 9, 10 ) AND `id_text_type` = 3";
										$resFilter = $this->db->SQLexecute($sqlFilter);
										while($datFilter = $this->db->fetchAssoc($resFilter)){
											$lang[] = explode("/", $datFilter['lngText']);
										}
										foreach ($lang[0] as $day) {
											$tmp = explode(" ", $day);
											$days[$tmp[0]]['label'] = str_replace($tmp[0]." ", "", $day);	
										}
										foreach ($lang[1] as $t) {
											$tmp = explode(" ", $t);
											$time[$tmp[0]]['label'] = str_replace($tmp[0]." ", "", $t);
										}	
									
										$sqlFilter = "SELECT * FROM $addIt".$tableName." WHERE status = 1 AND status_rec = 1 AND smpl_var IS NOT NULL GROUP BY dan, ura, smpl_var";
										//echo $sqlFilter;
										$resFilter = $this->db->SQLexecute($sqlFilter);
										if($this->db->NumRows($resFilter) > 0){
											$i=0;
											while($datFilter = $this->db->fetchAssoc($resFilter)){
												$ch = array();
												
												$sql = "SELECT DISTINCT(presentation_location) FROM ".WARPIT_CRM."._PresentatorPermissions WHERE cati_location = '".$datFilter['smpl_var']."' AND id_project=".$parent;
								
												$rez = $this->db->SQLexecute($sql);
												$da = $this->db->fetchAssoc($rez);
												$tmp = explode(",", $da['presentation_location']);
												$dogodek = $tmp[0];
	
												$ch['text']	= $days[$datFilter['dan']]['label'].", ".$time[$datFilter['ura']]['label'].", ".$dogodek;
													
												$dogodek = str_replace(" ", "|", $dogodek[0]);	
												$ch['id']			= $row['id_project']."_".$datFilter['smpl_var']."_".($datFilter['dan'])."_".($datFilter['ura'])."_".$datFilter['smpl_var'];
												$ch['position']	= 1;
												$ch['isProject'] = 0; 
												$ch['id_project'] = $row['id_project']."_".$datFilter['smpl_var']."_".($datFilter['dan'])."_".($datFilter['ura'])."_".$datFilter['smpl_var']; 
												$ch['leaf']	= true;
												$ch['iconCls'] = 'RMicon_time_statistics'; 
												
												$path['children'][$i]= $ch;
												$i++;
											}
										}
										else{
											$path['leaf']	= true;  
										}
									}
								}
							}
						}
						else
							$path['leaf'] = false;
					}

					$nodes[] = $path;
					
					// call this function again to display this
					// child's children
				}
				
				//return $nodes;
			}else{
				$sql = "SELECT proj_name FROM $addIt`_Projects` WHERE id = $parent";
				$res = $this->db->SQLexecute($sql);
				$dat = $this->db->fetchAssoc($res);
				$ProjectName = $dat['proj_name'];
				if(isset($ProjectName) && $ProjectName != ""){
					$tableName = $ProjectName."_structure";
					$langName = $ProjectName."_language";
					if($this->db->TableExists($tableName, DB_WARPIT_WEBCATI_BASE) && $this->db->TableExists($langName, DB_WARPIT_WEBCATI)){
						$resFilter = $this->db->SQLexecute("DESCRIBE ".DB_WARPIT_WEBCATI_BASE.".".$tableName);
						$fields = array();
						while($datFilter = $this->db->fetchAssoc($resFilter)){
							array_push($fields, $datFilter['Field']);
						}
						
						if(in_array("dan", $fields) && in_array("ura", $fields) && in_array("smpl_var", $fields))
							$tableOk = true;
						
						if($tableOk){
							$sql = "SELECT * FROM ".DB_WARPIT_WEBCATI.".`".$ProjectName."_language` WHERE `id_question` IN ( 9, 10 ) AND `id_text_type` = 3";
							$res = $this->db->SQLexecute($sql);
							while($row = $this->db->fetchAssoc($res)){
								$lang[] = explode("/", $row['lngText']);
							}
							foreach ($lang[0] as $day) {
								$tmp = explode(" ", $day);
								$days[$tmp[0]]['label'] = str_replace($tmp[0]." ", "", $day);	
							}
							foreach ($lang[1] as $t) {
								$tmp = explode(" ", $t);
								$time[$tmp[0]]['label'] = str_replace($tmp[0]." ", "", $t);
							}	
									
							$sql = "SELECT * FROM $addIt`".$ProjectName."_structure` WHERE status = 1 AND status_rec = 1 AND smpl_var IS NOT NULL GROUP BY dan, ura, smpl_var";
							$res = $this->db->SQLexecute($sql);
							while($row = $this->db->fetchAssoc($res)){
								$path = array();
								
								$sql = "SELECT DISTINCT(presentation_location) FROM ".WARPIT_CRM."._PresentatorPermissions WHERE cati_location = '".$row['smpl_var']."' AND id_project=".$parent;
								
								$rez = $this->db->SQLexecute($sql);
								$da = $this->db->fetchAssoc($rez);
								$tmp = explode(",", $da['presentation_location']);
								$dogodek = $tmp[0];
								
								
								$path['text']		= $days[$row['dan']]['label'].", ".$time[$row['ura']]['label'].", ".$dogodek;
									
								$path['id']			= $parent."_".$row['smpl_var']."_".($row['dan'])."_".($row['ura'])."_".$row['smpl_var'];
								$path['position']	= 1;
								$path['isProject'] 	= 0; 
								$path['id_project'] = $parent.".".$row['smpl_var']."_".($row['dan'])."_".($row['ura'])."_".$row['smpl_var']; 
								$path['leaf']	= true;
								$path['iconCls'] = 'RMicon_time_statistics'; 
								$nodes[] = $path;
							}
						}
					}
				}
				if(!isset($nodes)) $nodes[] = array("id"=>-1, "text" => "Ni rezultatov", "leaf" => true);
			}
			include_once (PATH_TO_ROOT."_support/include.projectRights.php");
			
			return json_encode($bla);
		}
	}
}
?>