<?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");
include_once (PATH_TO_ROOT."CRM/class/class.Permissions.php");
//ini_set("display_errors",1);
$permissions = new Permissions($db);
$Logger = new LoggerX($db);

if($_POST['action'] == "canViewActivities"){
	$cati_location = StringChecker::Check($_POST['cati_location']);
	$dan = StringChecker::Check($_POST['dan']);
	$ura = StringChecker::Check($_POST['ura']);
	$projectId = StringChecker::Check($_POST['project_id']);
	$now = strtotime(date("d.m.Y H:i:s"));
	$sql = "SELECT  activite_lock_time,
					presentation_date,
					presentation_time
			FROM ".WARPIT_CRM."._PresentatorPermissions 
			WHERE id_project = ".$projectId." 
			AND cati_location = ".$cati_location." 
			AND cati_hour = ".$ura. " 
			AND cati_day = ".$dan;
	$res = $db->SQLexecute($sql);
	$dat = $db->fetchAssoc($res);
	$lock = $dat['activite_lock_time'];
	
	$date = $dat['presentation_date']. " ".$dat['presentation_time'];
	$added = strtotime($date);
	if($now > $added && $now < strtotime("+".$lock." hours",$added)){
		$r['show'] = true;
	}elseif($permissions->hasCRMPermission(32)){
		$r['show'] = true;
	}else{
		$r['show'] = false;
	}
	$r['success'] = true;
	echo json_encode($r);
}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";
	$prisotni = isset($_POST['prisotni']) ? StringChecker::Check($_POST['prisotni']) : NULL;
	if(isset($_POST['id_tel']) && !empty($_POST['id_tel'])){
	    $sql = "SELECT * FROM ".WARPIT_CRM."._visitStatistic WHERE id_tel = ".$_POST['id_tel'];
        $res = $db->SQLexecute($sql);
        while($row = $db->fetchAssoc($res)){
            $row['oseba_1'] = $row['firstname']." ".$row['surname'];
            $row['oseba_2'] = $row['address'];
            $row['kraj'] = $row['city'];
            $row['posta'] = $row['post_code'];
            $row['odnos'] = $row['odnos'];
            $dat[] = $row;
        }
        $results['success'] = true;
        $results['results'] = sizeof($dat);
        $results['rows'] = $dat != null ? $dat : '';
        echo json_encode($results);
	}
    else{
	
	
    	$filter = $_POST['filter'] ? " oseba_1 LIKE '%".StringChecker::Check($_POST['filter'])."%' AND" : "";
    	$projectId = StringChecker::Check($_POST['project_id']);
    	
    	$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);
    	}
    	$chang = $projectId."_".StringChecker::Check($_POST['cati_location'])."_".StringChecker::Check($_POST['dan'])."_".StringChecker::Check($_POST['ura']);
    	$changedPresentation = array();
    	$sql = "SELECT id_tel, id_project FROM ".WARPIT_CRM."._visitStatistic WHERE changedPresentation = '".$chang."'";
    	
    	$res = $db->SQLexecute($sql);
    	while($row = $db->fetchAssoc($res)){
    		$changedPresentation[] = $row['id_tel'];
    	}
    	$id_tel = array();
    	
    	$sql = "SELECT v.*, CONCAT_WS(' ', i.Firstname, i.Surname) AS agent 
    				FROM ".WARPIT_CRM."._visitStatistic AS v
    				LEFT JOIN ".DB_WARPIT_WEBCATI_BASE.".".$ProjectName."_structure AS t
    				ON t.id_tel = v.id_tel
    				LEFT JOIN ".DB_WARPIT_WEBCATI_BASE."._Interviewer AS i
    				ON t.user_id = i.id
    				WHERE v.id_project = ".$projectId." 
    				AND v.dan = ".(StringChecker::Check($_POST['dan']))." 
    				AND v.ura = " . StringChecker::Check($_POST['ura']). " 
    				AND v.cati_location = ".(StringChecker::Check($_POST['cati_location']))." 
    				AND (v.firstname LIKE '%".StringChecker::Check($_POST['filter'])."%' 
    					OR v.surname LIKE '%".StringChecker::Check($_POST['filter'])."%') 
    				AND v.id_sub_tel = 0
    				AND changedPresentation = 0";
    				
    	if(isset($prisotni) && $prisotni == "true"){
    		$sql .= " AND v.nacin_pristopa <> 0";
    	} 
    	$sql .= "	ORDER BY v.nacin_pristopa";
    	$res = $db->SQLexecute($sql);
      
         
    	if($db->NumRows($res) != 0){
    		while($row = $db->fetchAssoc($res)){
    			$row['oseba_1'] = $row['firstname']." ".$row['surname'];
    			$row['oseba_2'] = $row['address'];
    			$row['kraj'] = $row['city'];
    			$row['posta'] = $row['post_code'];
    			array_push($id_tel, $row['id_tel']);
    			$row['odnos'] = $row['odnos'];
    			$data[] = $row;
    		}
    	}
    	$sql = "SELECT v.id_tel, nacin_pristopa, SUBSTRING_INDEX(changedPresentation, '_', 1) AS id_project
                FROM ".WARPIT_CRM."._visitStatistic AS v
                WHERE v.id_project = ".$projectId." 
                AND v.dan = ".(StringChecker::Check($_POST['dan']))." 
                AND v.ura = " . StringChecker::Check($_POST['ura']). " 
                AND v.cati_location = ".(StringChecker::Check($_POST['cati_location']))." 
                AND (v.firstname LIKE '%".StringChecker::Check($_POST['filter'])."%' 
                    OR v.surname LIKE '%".StringChecker::Check($_POST['filter'])."%') 
                AND v.id_sub_tel = 0
                AND changedPresentation > 0";
            if(isset($prisotni) && $prisotni == "true"){
                $sql .= " AND v.nacin_pristopa <> 0";
            } 
            $res = $db->SQLexecute($sql);
            
            if($db->NumRows($res) > 0){
                while($row = $db->fetchAssoc($res)){
                    $sql = "SELECT proj_name 
                    FROM ".DB_WARPIT_WEBCATI_BASE.".`_Projects` 
                    WHERE id = ".$row['id_project'];
                    $rez = $db->SQLexecute($sql);
                    $dat = $db->fetchAssoc($rez);
                    $ProjectNameTemp = $dat['proj_name'];
                    
                    $sql = "SELECT t.*, CONCAT_WS(' ', i.Firstname, i.Surname) AS agent
                            FROM ".DB_WARPIT_WEBCATI_BASE.".".$ProjectNameTemp."_structure AS t
                            LEFT JOIN ".DB_WARPIT_WEBCATI_BASE."._Interviewer AS i
                            ON t.user_id = i.id
                            WHERE t.id_tel = ".$row['id_tel'];
                            
                    $rez = $db->SQLexecute($sql);
                    $dat = $db->fetchAssoc($rez);
                    
                    $kraj = explode(" ", trim($dat['oseba_3']), 2);
                    $dat['posta'] = $kraj[0];
                    $dat['phone1'] = $dat['dodatno_3'];
                    $dat['kraj'] = $kraj[1];
                    $dat['dogodek'] = $dat['dogodek_2'];
                    $dat['ura'] = $dat['ura'];
                    $dat['dan'] = $dat['dan'];
                    $dat['nacin_pristopa'] = $row['nacin_pristopa'];
                    $data[] = $dat;
                }
            }
        
    	$sql = "SELECT changedPresentation FROM ".WARPIT_CRM."._visitStatistic WHERE changedPresentation > 0 AND id_project = ".$projectId;
    	$res = $db->SQLexecute($sql);
    	while($row = $db->fetchAssoc($res)){
    		$temp = explode("_",$row['changedPresentation']);
    		if(is_array($temp) && count($temp) == 4){
    			$sql = "SELECT id_tel 
    					FROM ".WARPIT_CRM."._visitStatistic 
    					WHERE changedPresentation > 0 
    					AND id_project = ".$temp[0]." 
    					AND cati_location = ".$temp[1]. " 
    					AND dan = ".$temp[2]." 
    					AND ura=".$temp[3];
    			$zad = $db->SQLexecute($sql);
    			while($dat = $db->fetchAssoc($zad)){
    				$id_tel[] = $dat['id_tel'];
    			}
    		}
    	}
    	
    	$id_tel = array_merge($id_tel, $changedPresentation);
    	if(!isset($prisotni) || $prisotni == "false"){
    		if(is_array($id_tel) && count($id_tel) != 0)
    			$sql = "SELECT t.*, CONCAT_WS(' ', i.Firstname, i.Surname) AS agent 
    					FROM ".DB_WARPIT_WEBCATI_BASE.".".$ProjectName."_structure AS t
    					JOIN ".DB_WARPIT_WEBCATI_BASE."._Interviewer AS i
    					ON i.id = t.user_id
    					WHERE ".ZIP." = ".StringChecker::Check($_POST['zipId'])." 
    					AND id_tel NOT IN (".implode(",",$id_tel).") 
    					AND $filter $mojFilter `status` = 1 
    					AND status_rec = 1";
    		else
    			$sql = "SELECT t.*, CONCAT_WS(' ', i.Firstname, i.Surname) AS agent
    					FROM ".DB_WARPIT_WEBCATI_BASE.".".$ProjectName."_structure AS t
    					JOIN ".DB_WARPIT_WEBCATI_BASE."._Interviewer AS i
    					ON i.id = t.user_id 
    					WHERE ".ZIP." = ".StringChecker::Check($_POST['zipId'])." 
    					AND $filter $mojFilter `status` = 1 
    					AND status_rec = 1";
    			
    		$res = $db->SQLexecute($sql);
    		while($dat = $db->fetchAssoc($res)){
    			$kraj = explode(" ", trim($dat['oseba_3']), 2);
    			$dat['posta'] = $kraj[0];
    			$dat['phone1'] = $dat['dodatno_3'];
    			$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'] == "filter"){
	//ini_set("display_errors", 1);
	$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 .= " cati_location = ".(StringChecker::Check($_POST['cati_location']))." AND";
	
	
	
	$sql = "SELECT * FROM ".WARPIT_CRM."._visitStatistic WHERE CONCAT(firstname, ' ', surname) LIKE '%".StringChecker::Check($_POST['filter'])."%' OR CONCAT (surname, ' ', firstname) LIKE '%".StringChecker::Check($_POST['filter'])."%'";
	$res = $db->SQLexecute($sql);
	while($row = $db->fetchAssoc($res)){
		$row['id_tel'] = $row['id_tel'];
		$row['oseba_1'] = $row['firstname']." ".$row['surname'];
		$row['oseba_2'] = $row['address'];
		$row['kraj'] = $row['city'];
		$row['posta'] = $row['post_code'];
		$row['odnos'] = $row['odnos'];
		$row['projectName'] = "_visitStatistic";
		$row['project'] = "-1";
		$data[] = $row;
	}
	
	$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'];
	}
	
	$sqlAll = array();
	foreach ($projects as $project) {
		$project .= "_structure";
		$sql = "(SELECT id, smpl_var, oseba_1, oseba_2, oseba_3, dogodek_2, (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 '%".StringChecker::Check($_POST['filter'])."%')";
		array_push($sqlAll, $sql);
	}
	$sql = implode(" UNION ", $sqlAll);
	//print_r($sql);
	
	$res = $db->SQLexecute($sql);
	while($row = $db->fetchAssoc($res)){
		$row['id_tel'] = -1;
		$data[] = $row;
	}
	
	$results['success'] = true;
	$results['results'] = sizeof($data);
	$results['rows'] = $data != null ? $data : '';
	echo json_encode($results); 
}elseif($_POST['action'] == "getSub"){
	if(isset($_POST['id_tel']) && $_POST['id_tel'] != ""){
		$sql = "SELECT * FROM ".WARPIT_CRM."._visitStatistic WHERE $filter id_sub_tel = ".$_POST['id_tel'];
		$res = $db->SQLexecute($sql);
		while($dat = $db->fetchAssoc($res)){
			$data[] = $dat;
		}
	}elseif(isset($_POST['filter']) && $_POST['filter'] != null){
		$filter = StringChecker::Check($_POST['filter']) != null ? " id_project = ".StringChecker::Check($_POST['project_id'])." AND (CONCAT(firstname, ' ', surname) LIKE '%".StringChecker::Check($_POST['filter'])."%' OR CONCAT(surname, ' ', firstname) LIKE '%".StringChecker::Check($_POST['filter'])."%') AND ura = ".StringChecker::Check($_POST['ura'])." AND dan = ".StringChecker::Check($_POST['dan']). " AND cati_location = ".(StringChecker::Check($_POST['cati_location']))."" : "";
		$sql = "SELECT * FROM ".WARPIT_CRM."._visitStatistic WHERE $filter AND id_sub_tel <> 0";
		$res = $db->SQLexecute($sql);
		while($dat = $db->fetchAssoc($res)){
			$data[] = $dat;
		}
	}
	$results['success'] = true;
	$results['results'] = sizeof($data);
	$results['rows'] = $data != null ? $data : '';
	echo json_encode($results); 
}elseif($_POST['action'] == "updateDate"){
	$projectId = StringChecker::Check($_POST['projectId']);
	$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 ".WARPIT_CRM."._visitStatistic WHERE id_tel=".StringChecker::Check($_POST['id_tel']);
	$res = $db->SQLexecute($sql);
	if($db->NumRows($res) == 0){
		$sql = "SELECT * FROM ".DB_WARPIT_WEBCATI_BASE.".".$ProjectName."_structure WHERE id_tel = ".StringChecker::Check($_POST['id_tel']);
		
		$res = $db->SQLexecute($sql);
		$data = $db->fetchAssoc($res);
		$array1 = explode(' ', $data['oseba_1'], 2);
		$array2 = explode(' ', $data['oseba_3'], 2); 
		$ime = $array1[0];
		$priimek = $array1[1];
		$naslov = $data['oseba_2'];
		$postnaSt = $array2[0];
		$kraj = $array2[1];
		$dan = $data['dan'];
		$ura = $data['ura'];
		$dogodek = $data['dogodek_2'];
        $spol = $data['spol'];
		//$projectZip = "0".$data['TP_OMS'].$data['TP_telephone'];
		$projectZip = $data['dodatno_3'];
		$cati_location = StringChecker::Check($_POST['cati_location']);
		
		$sql = "INSERT INTO ". WARPIT_CRM."._visitStatistic (id_project, firstname, surname, address, post_code, city, dan, ura, dogodek, id_tel, cati_location, phone1, gender) 
				   	  VALUES ('$projectId', '$ime', '$priimek', '$naslov', '$postnaSt', '$kraj', '$dan', '$ura', '$dogodek', '".StringChecker::Check($_POST['id_tel'])."', '$cati_location', '$projectZip', '$spol')";
					  
		$Logger->Log('dodajanje_uporabnika', 'multiple-filed-insert', 'id_project, firstname, surname, address, post_code, city, dan, ura, dogodek, id_tel, cati_location', 
					  WARPIT_CRM."._visitStatistic", " ", addslashes($sql));
					  
		$db->SQLexecute($sql);
		$type = $_POST['type'] == 1 ? "cas_prihoda" : "cas_odhoda";
		$sql = "UPDATE ".WARPIT_CRM."._visitStatistic SET $type = NOW() WHERE id_tel = ".StringChecker::Check($_POST['id_tel']);
		$Logger->Log('sprememba_'.$type, 'single-field-update', $type, WARPIT_CRM."._visitStatistic", " ", addslashes($sql));
		$db->SQLexecute($sql);
		
	}else{
		$type = $_POST['type'] == 1 ? "cas_prihoda" : "cas_odhoda";
		$sql = "UPDATE ".WARPIT_CRM."._visitStatistic SET $type = NOW() WHERE id_tel = ".StringChecker::Check($_POST['id_tel']);
		$Logger->Log('sprememba_'.$type, 'single-field-update', $type, WARPIT_CRM."._visitStatistic", " ", addslashes($sql));
		$db->SQLexecute($sql);
	}
}elseif($_POST['action'] == "update"){
	
	$projectId = StringChecker::Check($_POST['projectId']);
	$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'];
	
	$field = $_POST['field'];
	if($field == 'oseba_2') $field = "address";
	if($field == 'posta') $field = "post_code"; 
	if($field == 'kraj') $field = "city";
	
	$sql = "SELECT * FROM ".WARPIT_CRM."._visitStatistic WHERE id_sub_tel = ".StringChecker::Check($_POST['id_tel']);
	$res = $db->SQLexecute($sql);
	if($db->NumRows($res) > 0){
		$dat = $db->fetchAssoc($res);
		$sql = "UPDATE ".WARPIT_CRM."._visitStatistic SET id_tel = (id*-1) WHERE id = ".$dat['id'];
		$Logger->Log('dodajanje_uporabnika', 'id_tel_fix', 'id_tel', 
					  WARPIT_CRM."._visitStatistic", "'".$dat['id_tel']."'", addslashes($sql));
		$db->SQLexecute($sql);
	}
	
	$sql = "SELECT * FROM ".WARPIT_CRM."._visitStatistic WHERE id_tel=".StringChecker::Check($_POST['id_tel']);
	$res = $db->SQLexecute($sql);
	if($db->NumRows($res) == 0){
		$sql = "SELECT * FROM ".DB_WARPIT_WEBCATI_BASE.".".$ProjectName."_structure WHERE status = 1 AND status_rec = 1 AND id_tel = ".StringChecker::Check($_POST['id_tel']);
		
		$res = $db->SQLexecute($sql);
		$data = $db->fetchAssoc($res);
		$array1 = explode(' ', trim($data['oseba_1']), 2);
		$array2 = explode(' ', trim($data['oseba_3']), 2); 
		$ime = $array1[0];
		$priimek = $array1[1];
		$naslov = $data['oseba_2'];
		$postnaSt = $array2[0];
		$kraj = $array2[1];
		$dan = $data['dan'];
		$ura = $data['ura'];
		//$projectZip = "0".$data['TP_OMS'].$data['TP_telephone'];
		$spol = $data['spol'];
		$projectZip = $data['dodatno_3'];
		$dogodek = $data['dogodek_2'];
		$cati_location = StringChecker::Check($_POST['cati_location']);
		
		
		$sql = "INSERT INTO ". WARPIT_CRM."._visitStatistic (id_project, firstname, surname, address, post_code, city, dan, ura, dogodek, id_tel, cati_location, phone1, gender) 
				   	  VALUES ('$projectId', '$ime', '$priimek', '$naslov', '$postnaSt', '$kraj', '$dan', '$ura', '$dogodek', '".StringChecker::Check($_POST['id_tel'])."', '$cati_location', '$projectZip', '$spol')";
        
		$Logger->Log('dodajanje_uporabnika', 'multiple-filed-insert', 'id_project, firstname, surname, address, post_code, city, dan, ura, dogodek, id_tel, cati_location', 
					  WARPIT_CRM."._visitStatistic", " ", addslashes($sql));
		$db->SQLexecute($sql);
		
		if($field == 'nacin_pristopa'){
			$sql = "UPDATE ".StringChecker::Check($_POST['table'])." SET cas_prihoda = NOW() WHERE id_tel = ".StringChecker::Check($_POST['id_tel']);
			$Logger->Log('sprememba_časa_prihoda', 'single-field-update', 'cas_prihoda', StringChecker::Check($_POST['table']), StringChecker::Check($_POST['oldValue']), addslashes($sql));
			
		}
		if($field == 'oseba_1'){
			$ime = explode(" ", StringChecker::Check($_POST['value']), 2);
			$fileds = array("firstname", "surname");
			for($i=0;$i<2;$i++){
				$sql = "UPDATE ".$_POST['table']." SET ".$fileds[$i]." = '".StringChecker::Check($ime[$i])."' WHERE id_tel = ".StringChecker::Check($_POST['id_tel']);
				$Logger->Log('sprememba_'.$fileds[$i], 'single-field-update', $fileds[$i], $_POST['table'], StringChecker::Check($_POST['oldValue']), addslashes($sql));	
				$db->SQLexecute($sql);
			}
		}else{
			$sql = "UPDATE ".$_POST['table']." SET ".$field." = '".StringChecker::Check($_POST['value'])."' WHERE id_tel = ".StringChecker::Check($_POST['id_tel']);
			$Logger->Log('sprememba_'.$field, 'single-field-update', $field, $_POST['table'], StringChecker::Check($_POST['oldValue']), addslashes($sql));
			$db->SQLexecute($sql);
		}
		
	}else{
		if($field == 'oseba_1'){
			$ime = explode(" ", StringChecker::Check($_POST['value']), 2);
			$fileds = array("firstname", "surname");
			for($i=0;$i<2;$i++){
				$sql = "UPDATE ".$_POST['table']." SET ".$fileds[$i]." = '".StringChecker::Check($ime[$i])."' WHERE id_tel = ".StringChecker::Check($_POST['id_tel']);
				$Logger->Log('sprememba_'.$fileds[$i], 'single-field-update', $fileds[$i], $_POST['table'], StringChecker::Check($_POST['oldValue']), addslashes($sql));	
				$db->SQLexecute($sql);
			}
		}else{
			$sql = "UPDATE ".$_POST['table']." SET ".$field." = '".StringChecker::Check($_POST['value'])."' WHERE id_tel = ".StringChecker::Check($_POST['id_tel']);
			$Logger->Log('sprememba_'.$field, 'single-field-update', $field, $_POST['table'], " ", addslashes($sql));		
			$db->SQLexecute($sql);
			if($field == 'nacin_pristopa'){
				$sql = "UPDATE ".$_POST['table']." SET cas_prihoda = NOW() WHERE id_tel = ".StringChecker::Check($_POST['id_tel']);
				$Logger->Log('sprememba_časa_prihoda', 'single-field-update', 'cas_prihoda', $_POST['table'], StringChecker::Check($_POST['oldValue']), addslashes($sql));
				$db->SQLexecute($sql);
			}
		}
	}
	echo "{success: true}";
}elseif($_POST['action'] == "updateAction"){
	$project_id = StringChecker::Check($_POST['project_id']);
	$filter_id = isset($_POST['zipId']) ? StringChecker::Check($_POST['zipId']) : 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 = 1";
	$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;
}else if($_POST['action'] == 'delUser'){
	$idTel = StringChecker::Check($_POST['id_tel']);
	$sql = "DELETE FROM ".WARPIT_CRM."._visitStatistic WHERE id_tel = $idTel";
	$Logger->Log('brisanje_visitStatistic', 'DELETE', 'id_tel = $idTel', 
				WARPIT_CRM."._visitStatistic", " ", addslashes($sql));
	$db->SQLexecute($sql);
}elseif($_POST['action'] == 'canDelete'){
	$id_tel = StringChecker::Check($_POST['id_tel']);
	$sql = "SELECT * FROM ".WARPIT_CRM."._visitStatistic WHERE id_tel = ".$id_tel;
	$res = $db->SQLexecute($sql);
	$r['success'] = $db->NumRows($res) > 0;
	echo json_encode($r);
}
?>