<?php
include_once ("_dirinfo.php");
include_once (PATH_TO_ROOT."init.php");
include_once (PATH_TO_ROOT."config.php");
include_once (PATH_TO_ROOT."CRM/_CRM_config.php");
include_once (PATH_TO_ROOT."CRM/class/class.Language-Pack.php");
include_once (PATH_TO_ROOT."CRM/class/class.checkString.php");
$LangPack = new LanguagePack($db, $aLanguage);
//ini_set("display_errors", 1);

$LangPack = new LanguagePack($db, $aLanguage);
$sql = "SELECT proj_name FROM ".DB_WARPIT_WEBCATI_BASE."._Projects WHERE proj_name LIKE '".WEEK_NAME."%'";
$res = $db->SQLexecute($sql);
while($row = $db->fetchAssoc($res)){
	$projects[] = $row['proj_name'];
}
if($_POST['action'] == "findPerson"){
	$filter = StringChecker::Check($_POST['filter']);
	$sqlAll = array();
	foreach ($projects as $project) {
		$project .= "_structure";
		$sql = "(SELECT oseba_1, oseba_2, oseba_3, id_tel, (SELECT table_name FROM information_schema.tables WHERE table_name = '".$project."'
				) as projectName  FROM ".DB_WARPIT_WEBCATI_BASE.".".$project." WHERE status = 1 AND status_rec = 1 AND oseba_1 LIKE '%".$filter."%')";
		$sqlAll[] = $sql;
	}
	$sql = implode(" UNION ", $sqlAll);
	$zad = $db->SQLexecute($sql);
	while($row = $db->fetchAssoc($zad)){
		$row['ime'] = $row['oseba_1'];
		$row['naslov'] = $row['oseba_2'];
		$t = explode(" ", $row['oseba_3']);
		$row['posta'] = $t[0];
		$row['kraj'] = $t[1];
		$data[] = $row;
	}
	$r['success'] = true;
	$r['results'] = sizeof($data);
	$r['rows'] = $data != null ? $data : "";
	echo json_encode($r);
}elseif($_POST['action'] == "get"){
	$sql = "SELECT * FROM ".WARPIT_CRM."._visitStatistic WHERE changedPresentation > 0";
	$res = $db->SQLexecute($sql);
	while($row = $db->fetchAssoc($res)){
		$d['id'] = $row['id'];
		
		$d['ime'] = $row['firstname']." ".$row['surname'];
		$d['spol'] = $row['gender'] == 1 ? $LangPack->GetLabel("CRM_all", "male_gender_title","Moški") : $LangPack->GetLabel("CRM_all", "female_gender_title","Ženski");
		$d['posta'] = $row['post_code'];
		$d['kraj'] = $row['city'];
		$d['naslov'] = $row['address'];
		$d['prisel'] = "<b>".getProjectName($db, $row['id_project'], $row['dan'], $row['ura'], $row['dogodek'])."</b>";
		$sqlAll = array();
		foreach ($projects as $project) {
			$project .= "_structure";
			$sql = "(SELECT ura, dan, dogodek_2, id_tel, (SELECT table_name FROM information_schema.tables WHERE table_name = '".$project."'
					) as projectName FROM ".DB_WARPIT_WEBCATI_BASE.".".$project." WHERE status = 1 AND status_rec = 1 AND id_tel = '".$row['id_tel']."')";
			$sqlAll[] = $sql;
		}
		$sql = implode(" UNION ", $sqlAll);
		$zad = $db->SQLexecute($sql);
		if($db->NumRows($zad) > 0){
			$dat = $db->fetchAssoc($zad);
			$projectName = strrev($dat['projectName']);
			$pUra = $dat['ura'];
			$pDan = $dat['dan'];
			$pIdTel = $dat['id_tel'];
			$pLoc = $dat['dogodek_2'];
			$temp = explode("_", $projectName,2);
			$name = strrev($temp[1]);
			
			$sql = "SELECT id FROM ".DB_WARPIT_WEBCATI_BASE."._Projects WHERE proj_name = '".$name."'";
			$zad = $db->SQLexecute($sql);
			$dat = $db->fetchAssoc($zad);
			$pId = $dat['id'];
			$d['id_tel'] = $pIdTel;
			$d['mogo_priti'] = getProjectName($db, $pId, $pDan, $pUra, $pLoc);
		}
		
		
		$data[] = $d;
	}
	
	$r['success'] = true;
	$r['results'] = sizeof($data);
	$r['rows'] = $data;
	echo json_encode($r);
}elseif($_POST['action'] == "getPresentations"){
	$idProject = StringChecker::Check($_POST['idProject']);
	
	if ( $_SESSION['CRM_login'] == "1" ) {
		$sql = "SELECT * FROM ".WARPIT_CRM."._PresentatorPermissions 
					WHERE id_project = '".$idProject."' 
					AND presentation_team IN (SELECT id_group  FROM  ".DB_WARPIT_WEBCATI_BASE."._group_link  WHERE id = ".$_SESSION['UserInfo']['id'].")
					AND presentation_team_lock > NOW()";
	} else {
		$sql = "SELECT * FROM ".WARPIT_CRM."._PresentatorPermissions WHERE id_project = '".$idProject."'";
	}
	
	$sql .= " ORDER BY presentation_date, presentation_location, presentation_time";
	$res = $db->SQLexecute($sql);
	
	while($zad = $db->fetchAssoc($res)){
		$label = $zad['presentation_date']." ".$zad['presentation_time']." ".$zad['presentation_location']." ".$zad['presentation_city'];
		$data[] = array("id" => $zad['id'], "projectId" => $zad['id_project'], "project" => $label, "hotel" => $zad['presentation_location'], "dan" => $zad['cati_day'], "ura" => $zad['cati_hour'], "cati_location" => $zad['cati_location']);
	}
	
	$results['success'] = true;
	$results['results'] = sizeof($data);
	$results['rows'] = $data != null ? $data : '';
	echo json_encode($results);
}elseif($_POST['action'] == "fixTermine"){
	$id = StringChecker::Check($_POST['id']);
	$autoFix = StringChecker::Check($_POST['autoFix']);
	$id_tel = StringChecker::Check($_POST['id_tel']);
	if($autoFix == "true"){
		$sqlAll = array();
		foreach ($projects as $project) {
			$project .= "_structure";
			$sql = "(SELECT ura, dan, dogodek_2, smpl_var, id_tel, (SELECT table_name FROM information_schema.tables WHERE table_name = '".$project."'
					) as projectName FROM ".DB_WARPIT_WEBCATI_BASE.".".$project." WHERE status = 1 AND status_rec = 1 AND id_tel = '".$id_tel."')";
			$sqlAll[] = $sql;
		}
		$sql = implode(" UNION ", $sqlAll);
		$zad = $db->SQLexecute($sql);
		if($db->NumRows($zad) > 0){
			$dat = $db->fetchAssoc($zad);
			$projectName = strrev($dat['projectName']);
			$temp = explode("_", $projectName,2);
			
			$cati_location = $dat['smpl_var'];
			$dan = $dat['dan'];
			$ura = $dat['ura'];
			$sql = "SELECT id FROM ".DB_WARPIT_WEBCATI_BASE."._Projects WHERE proj_name = '".strrev($temp[1])."'";
			$zad = $db->SQLexecute($sql);
			$dat = $db->fetchAssoc($zad);
			$pId = $dat['id'];
			
			$sql = "UPDATE ".WARPIT_CRM."._visitStatistic SET changedPresentation = 0, cati_location = ".$cati_location.", dan=".$dan.", ura=".$ura.", id_project=".$pId." WHERE id = ".$id;
			$db->SQLexecute($sql);
		}
	}else{
		$table = StringChecker::Check($_POST['projectName']);
		$sql = "SELECT ura, dan, dogodek_2, smpl_var FROM ".DB_WARPIT_WEBCATI_BASE.".".$table." WHERE id_tel = ".$id_tel;
		$res = $db->SQLexecute($sql);
		$dat = $db->fetchAssoc($res);
		$cati_location = $dat['smpl_var'];
		$dan = $dat['dan'];
		$ura = $dat['ura'];
		
		$projectName = strrev($table);
		$temp = explode("_", $projectName,2);
		$sql = "SELECT id FROM ".DB_WARPIT_WEBCATI_BASE."._Projects WHERE proj_name = '".strrev($temp[1])."'";
		$zad = $db->SQLexecute($sql);
		$dat = $db->fetchAssoc($zad);
		$pId = $dat['id'];
		$sql = "UPDATE ".WARPIT_CRM."._visitStatistic SET changedPresentation = 0, cati_location = ".$cati_location.", dan=".$dan.", ura=".$ura.", id_project=".$pId." WHERE id = ".$id;
		$db->SQLexecute($sql);
	}
	echo "{ success: true}";
}
function getProjectName($db, $id, $dan, $ura, $location){
	$sql = "SELECT proj_name FROM ".DB_WARPIT_WEBCATI_BASE.".`_Projects` WHERE id = $id";
    $res = $db->SQLexecute($sql);
 	$dat = $db->fetchAssoc($res);
 	$ProjectName = $dat['proj_name'];
	
	$sql = "SELECT * FROM ".DB_WARPIT_WEBCATI.".`".$ProjectName."_language` WHERE `id_question` IN ( 9, 10 ) AND `id_text_type` = 3";
	$res = $db->SQLexecute($sql);
	while($row = $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);
	}
	$t = explode(",", $location);
	return $ProjectName." ". $days[$dan]['label']." ".$time[$ura]['label']." ".$t[0];
}
?>