<?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.checkString.php");

//ini_set("display_errors",1);

if($_POST['action'] == "get"){
	$filter = array();
	
	if(isset($_POST['dateFrom']) && !empty($_POST['dateFrom'])) $filter[] = "Added >= STR_TO_DATE('".StringChecker::Check($_POST['dateFrom'])."', '%d.%m.%Y')";
	if(isset($_POST['dateTo']) && !empty($_POST['dateTo'])) $filter[] = "Added <= STR_TO_DATE('".StringChecker::Check($_POST['dateTo'])."', '%d.%m.%Y')";
	if(isset($_POST['userId']) && !empty($_POST['userId'])) $filter[] = "UserId = ".StringChecker::Check($_POST['userId']);
	if(isset($_POST['ChangeType']) && !empty($_POST['ChangeType'])) $filter[] = " ChangeType = '".StringChecker::Check($_POST['ChangeType'])."'";
	if(isset($_POST['area']) && !empty($_POST['area'])) $filter[] = " area = '".StringChecker::Check($_POST['area'])."'";
	if(isset($_POST['newValueSearch']) && !empty($_POST['newValueSearch'])) $filter[] = " NewValue LIKE '%".StringChecker::Check($_POST['newValueSearch'])."%'";
    if(isset($_POST['postValueSearch']) && !empty($_POST['postValueSearch'])) $filter[] = " POST LIKE '%".StringChecker::Check($_POST['postValueSearch'])."%'";
	
	if(count($filter) > 0)
		$sql = "SELECT * FROM ".WARPIT_CRM."._logger WHERE ".implode(" AND ", $filter) ." ORDER BY id DESC";
	else
		$sql = "SELECT * FROM ".WARPIT_CRM."._logger ORDER BY id DESC";
	$rows = $sql;
	$sql .= " LIMIT ".StringChecker::Check($_POST['start']).", ".StringChecker::Check($_POST['limit']);
	
    $r['sql'] = $sql;
	$res = $db->SQLexecute($sql);
	while($row = $db->fetchAssoc($res)){
	    if($row['userType'] == 0)
    		$sql = "SELECT username FROM ".DB_WARPIT_WEBCATI_BASE."._Interviewer WHERE id = ".$row['UserId'];
        elseif($row['userType'] == 1)
            $sql = "SELECT username FROM ".DB_WARPIT_MAIN."._user WHERE id=".$row['UserId'];
		$raz = $db->SQLexecute($sql);
		$dat = $db->fetchAssoc($raz);
		$row['UserId'] = $dat['username'];
		$date = new DateTime($row['Added']);
		$row['Added'] = $date->format("d.m.Y H:i:s");
		$data[] = $row;
	}
	$r['success'] = true;
	$r['results'] = $db->NumRows($db->SQLexecute($rows));
	$r['rows'] = $data != null ? $data : "";
	
	echo json_encode($r);
}elseif($_POST['action'] == "getUsers"){
	$sql = "SELECT DISTINCT(l.UserId) as id, u.username FROM ".WARPIT_CRM."._logger l JOIN ".DB_WARPIT_WEBCATI_BASE."._Interviewer u ON l.UserId = u.id";
	$res = $db->SQLexecute($sql);
	while($row = $db->fetchAssoc($res)){
		$data[] = array("id" => $row['id'], "username" => $row['username']);
	}
	$r['success'] = true;
	$r['results'] = sizeof($data);
	$r['rows'] = $data != null ? $data : "";
	
	echo json_encode($r);
}elseif($_POST['action'] == "getArea"){
	$sql = "SELECT DISTINCT(area) as area FROM ".WARPIT_CRM."._logger";
	$res = $db->SQLexecute($sql);
	$i = 1;
	while($row = $db->fetchAssoc($res)){
		$data[] = array("id" => $i++, "text" => $row['area']);
	}
	$r['success'] = true;
	$r['results'] = sizeof($data);
	$r['rows'] = $data != null ? $data : "";
	
	echo json_encode($r);
}elseif($_POST['action'] == "getChangeType"){
	$sql = "SELECT DISTINCT(ChangeType) as ChangeType FROM ".WARPIT_CRM."._logger";
	$res = $db->SQLexecute($sql);
	$i = 1;
	while($row = $db->fetchAssoc($res)){
		$data[] = array("id" => $i++, "text" => $row['ChangeType']);
	}
	$r['success'] = true;
	$r['results'] = sizeof($data);
	$r['rows'] = $data != null ? $data : "";
	
	echo json_encode($r);
}
?>
