<?php
include_once ("_dirinfo.php");
include_once (PATH_TO_ROOT."CRM/_CRM_config.php");
include_once (PATH_TO_ROOT."init.php");
include_once (PATH_TO_ROOT."config.php");
include_once (PATH_TO_ROOT.'CRM/class/class.logger.php');
include_once(PATH_TO_ROOT."CRM/class/class.checkString.php");

$Logger = new LoggerX($db);

if($_POST['action'] == "get"){
	$mojFilter = "";
	if(isset($_POST['dan'])) $mojFilter .= " dan = ".(StringChecker::Check($_POST['dan']))." AND";
	if(isset($_POST['ura'])) $mojFilter .= " ura = ".(StringChecker::Check($_POST['ura']))." AND";
	if(isset($_POST['cati_location'])) $mojFilter .= " smpl_var = ".StringChecker::Check($_POST['cati_location'])." AND";
	
	$filter = $_POST['filter'] ? " oseba_1 LIKE '%".StringChecker::Check($_POST['filter'])."%' AND " : "";
	$projectId = StringChecker::Check($_POST['project_id']);
	
	if(isset($_POST['cati_location']))
		$sql = "SELECT * FROM ".WARPIT_CRM."._actionLogger WHERE id_project = $projectId AND id_filter = ".StringChecker::Check($_POST['cati_location'])." AND id_action = 1";
	else
		$sql = "SELECT * FROM ".WARPIT_CRM."._actionLogger WHERE id_project = $projectId AND id_action = 1";
	$results['sql'] = $sql;

	$res = $db->SQLexecute($sql);
	while($dat = $db->fetchAssoc($res)){
		$log[] = $dat['id_tel'];
	}
	$sql = "SELECT proj_name FROM ".DB_WARPIT_WEBCATI_BASE.".`_Projects` WHERE id = $projectId";
	$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);
	}
		
	
	
	if(is_array($log)){
		if(isset($_POST['cati_location']))
			$sql = "SELECT * FROM ".DB_WARPIT_WEBCATI_BASE.".".$ProjectName."_structure WHERE ".ZIP." = ".StringChecker::Check($_POST['cati_location'])." AND $filter $mojFilter `status` = 1 AND id_tel NOT IN (".implode(",", $log).") AND status_rec = 1";
		else
			$sql = "SELECT * FROM ".DB_WARPIT_WEBCATI_BASE.".".$ProjectName."_structure WHERE $filter $mojFilter `status` = 1 AND id_tel NOT IN (".implode(",", $log).") AND status_rec = 1";
	}
	else{
		if(isset($_POST['cati_location']))
			$sql = "SELECT * FROM ".DB_WARPIT_WEBCATI_BASE.".".$ProjectName."_structure WHERE ".ZIP." = ".StringChecker::Check($_POST['cati_location'])." AND $filter $mojFilter `status` = 1 AND status_rec = 1";
		else
			$sql = "SELECT * FROM ".DB_WARPIT_WEBCATI_BASE.".".$ProjectName."_structure WHERE $filter $mojFilter `status` = 1 AND status_rec = 1";
	}
	$sql .= " ORDER BY dan, ura, smpl_var";
	//echo $sql;
	//echo "/*".$sql."*/";
	$res = $db->SQLexecute($sql);
	while($dat = $db->fetchAssoc($res)){
		$kraj = explode(" ", $dat['oseba_3'], 2);
		$dat['posta'] = $kraj[0];
		$dat['kraj'] = $kraj[1];
		$dat['dogodek'] = $dat['dogodek_2'];
		$dat['ura'] = $dat['ura'];
		$dat['dan'] = $dat['dan'];
		$data[] = $dat;
	}
	$results['success'] = true;
	$results['results'] = sizeof($data);
	$results['rows'] = $data != null ? $data : '';
	echo json_encode($results);
}elseif($_POST['action'] == "updateAction"){
	$project_id = StringChecker::Check($_POST['project_id']);
	$filter_id = isset($_POST['cati_location']) ? StringChecker::Check($_POST['cati_location']) : 0;
	$id_tel = StringChecker::Check($_POST['id_tel']);
	$print_id = StringChecker::Check($_POST['print_id']);
	
	$max = $print_id;
	$sql = "SELECT * FROM ".WARPIT_CRM."._actionLogger WHERE id_print = ".StringChecker::Check($_POST['print_id']);
	$res = $db->SQLexecute($sql);
	if($db->NumRows($res) == 0){
		$sql = "SELECT * FROM ".WARPIT_CRM."._actionLogger WHERE id_project = $project_id AND id_tel IN ($id_tel) AND id_filter = $filter_id AND id_action = 1";
		$res = $db->SQLexecute($sql);
		if($db->NumRows($res) == 0){
			if(strpos(",", $id_tel)){
				$sql = "INSERT INTO ".WARPIT_CRM."._actionLogger (id_project, id_filter, id_tel, id_action, actionDate, id_print) VALUES ($project_id, $filter_id, $id_tel, 1, NOW(), 1)";
				$Logger->Log('dodajanje_v_actionLogger', 'multiple-filed-insert', 'id_project, id_filter, id_tel, id_action, actionDate, id_print', 
		  		  			  WARPIT_CRM."._actionLogger", " ", addslashes($sql));	
				$db->SQLexecute($sql);
			}else{
				$tel = explode(",", $id_tel);
				foreach ($tel as $item) {
					$sql = "SELECT * FROM ".WARPIT_CRM."._actionLogger WHERE id_project = $project_id AND id_tel IN ($item) AND id_filter = $filter_id AND id_action = 1";
					$res = $db->SQLexecute($sql);
					if($db->NumRows($res) == 0){
						$sql = "INSERT INTO ".WARPIT_CRM."._actionLogger (id_project, id_filter, id_tel, id_action, actionDate, id_print) VALUES ($project_id, $filter_id, $item, 1, NOW(), 1)";
						$Logger->Log('dodajanje_v_actionLogger', 'multiple-filed-insert', 'id_project, id_filter, id_tel, id_action, actionDate, id_print', 
		  		  			  		  WARPIT_CRM."._actionLogger", " ", addslashes($sql));
						$db->SQLexecute($sql);
					}
				}
			}	
		}
	}else{
		$sql = "SELECT * FROM ".WARPIT_CRM."._actionLogger WHERE id_project = $project_id AND id_tel IN ($id_tel) AND id_filter = $filter_id AND id_action = 1";
		$res = $db->SQLexecute($sql);
		if($db->NumRows($res) == 0){
			$sql = "SELECT MAX(id_print) as max FROM ".WARPIT_CRM."._actionLogger";
			$rez = $db->SQLexecute($sql);
			$dat = $db->fetchAssoc($rez);
			$max = intval($dat['max'])+1;
			if(strpos(",", $id_tel)){
				$sql = "INSERT INTO ".WARPIT_CRM."._actionLogger (id_project, id_filter, id_tel, id_action, actionDate, id_print) VALUES ($project_id, $filter_id, $id_tel, 1, NOW(), $max)";
				$Logger->Log('dodajanje_v_actionLogger', 'multiple-filed-insert', 'id_project, id_filter, id_tel, id_action, actionDate, id_print', 
  		  			  		  WARPIT_CRM."._actionLogger", " ", addslashes($sql));
				$db->SQLexecute($sql);
			}else{
				$tel = explode(",", $id_tel);
				foreach ($tel as $item) {
					$sql = "SELECT * FROM ".WARPIT_CRM."._actionLogger WHERE id_project = $project_id AND id_tel IN ($item) AND id_filter = $filter_id AND id_action = 1";
					$res = $db->SQLexecute($sql);
					if($db->NumRows($res) == 0){
						$sql = "INSERT INTO ".WARPIT_CRM."._actionLogger (id_project, id_filter, id_tel, id_action, actionDate, id_print) VALUES ($project_id, $filter_id, $item, 1, NOW(), $max)";
						$Logger->Log('dodajanje_v_actionLogger', 'multiple-filed-insert', 'id_project, id_filter, id_tel, id_action, actionDate, id_print', 
		  		  			  		  WARPIT_CRM."._actionLogger", " ", addslashes($sql));
						$db->SQLexecute($sql);
					}
				}
			}	
		}
	}
	echo $max;
}elseif($_POST['action'] == 'getHistory'){
				
	$sql = "SELECT proj_name FROM ".DB_WARPIT_WEBCATI_BASE.".`_Projects` WHERE id = ".StringChecker::Check($_POST['projectId']);
	$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);
	}
		
	$mojFilter = "";
	if(isset($_POST['dan'])) $mojFilter .= " dan = ".(StringChecker::Check($_POST['dan']))." AND";
	if(isset($_POST['ura'])) $mojFilter .= " ura = ".(StringChecker::Check($_POST['ura']))." AND";
	if(isset($_POST['cati_location'])) $mojFilter .= " smpl_var = '".StringChecker::Check($_POST['cati_location'])."' AND";	
	
	$idFilter = "";
	if(isset($_POST['filter'])){
		$filter = $_POST['filter'] ? " oseba_1 LIKE '%".StringChecker::Check($_POST['filter'])."%' AND " : "";	
		$sql = "SELECT * FROM ".DB_WARPIT_WEBCATI_BASE.".".$ProjectName."_structure WHERE Status = 1 AND $filter status_rec = 1";
		$res = $db->SQLExecute($sql);
		while($dat = $db->fetchAssoc($res)){
			$ids[] = $dat['id_tel'];
		}
		if(is_array($ids)){
			$idFilter = implode(",",$ids);
		}
	}
	$dateFilter = "";
	if(isset($_POST['date']) && $_POST['date'] != ""){
		$dateFilter = " AND DATE(actionDate) = STR_TO_DATE('".StringChecker::Check($_POST['date'])."', '%d.%m.%Y')";
	}
	
	$filter = $idFilter != "" ? "AND id_tel IN ($idFilter)" : "";
	$zip = isset($_POST['cati_location']) ? "AND id_filter = ".StringChecker::Check($_POST['cati_location']) : ""; 
	$sql = "SELECT * FROM ".WARPIT_CRM."._actionLogger WHERE id_project = ".StringChecker::Check($_POST['projectId'])." $zip $filter $dateFilter ORDER BY actionDate DESC LIMIT ". StringChecker::Check($_POST['start']). ", ".StringChecker::Check($_POST['limit']);
	$row = "SELECT * FROM ".WARPIT_CRM."._actionLogger WHERE id_project = ".StringChecker::Check($_POST['projectId'])." $zip $filter $dateFilter ORDER BY actionDate DESC";
	
	$res = $db->SQLExecute($sql);
	while($dat = $db->fetchAssoc($res)){
		$sql = "SELECT * FROM ".DB_WARPIT_WEBCATI_BASE.".".$ProjectName."_structure WHERE $mojFilter Status = 1 AND id_tel = ".$dat['id_tel']." AND status_rec = 1";
		
		$rez  = $db->SQLexecute($sql);
		$zad = $db->fetchAssoc($rez);
		$dat['name'] = $zad['oseba_1'];
		$dat['address'] = $zad['oseba_2'];
		$kraj = explode(" ", $zad['oseba_3'], 2);
		$dat['posta'] = $kraj[0];
		$dat['kraj'] = $kraj[1];
		$dat['dogodek'] = $zad['dogodek_2'];
		$dat['ura'] = $time[$zad['ura']]['label'];
		$dat['dan'] = $days[$zad['dan']]['label'];
		$data[] = $dat;
	}
	$result['success'] = true;
	$result['results'] = $db->NumRows($db->SQLexecute($row));
	$result['rows'] = $data != null ? $data : "";
	echo json_encode($result);
}
?>