<?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");
require_once (PATH_TO_ROOT."CRM/_lib/excel/PHPExcel.php");
$Logger = new LoggerX($db);

if($_POST['action'] == 'changeDate'){
	$id = StringChecker::Check($_POST['id']);
	$date = StringChecker::Check($_POST['datumDostave']);
	$field = StringChecker::Check($_POST['field']);
	
	
	$dateField = substr($field, 0, $field.lenght-5)."Date";
	
	$oldSql = "SELECT ".$field.", ".$dateField." FROM ".WARPIT_CRM."._visitStatistic WHERE id=".$id;
	$res = $db->SQLexecute($oldSql);
	$dat = $db->fetchAssoc($res);
	
	$sql = "UPDATE ".WARPIT_CRM."._visitStatistic SET ".$field." = 1, ".$dateField." = STR_TO_DATE('".$date."','%d.%m.%Y') WHERE id = ".$id;
	$Logger->Log('sprememba_'.$field, 'single-field-update', $field, WARPIT_CRM."._visitStatistic", json_encode($dat) , StringChecker::Check($sql));
	$db->SQLexecute($sql);
}elseif($_POST['action'] == "update"){
	$id = StringChecker::Check($_POST['id']);
	$field = StringChecker::Check($_POST['field']);
	$table = StringChecker::Check($_POST['table']);
	
	$dateField = substr($field, 0, $field.lenght-5)."Date";
	$oldSql = "SELECT ".$field.", ".$dateField." FROM ".WARPIT_CRM."._visitStatistic WHERE id=".$id;
	$res = $db->SQLexecute($oldSql);
	$dat = $db->fetchAssoc($res);
	
	$sql = "UPDATE ".$table." SET ".$field." = 0, ".$dateField." = '0000-00-00' WHERE id = ".$id;
	$Logger->Log('sprememba_'.$field, 'single-field-update', $field, WARPIT_CRM."._visitStatistic", json_encode($dat) , StringChecker::Check($sql));
	$db->SQLexecute($sql);
}

?>