<?php


function USERS_FILTER_PROBLEM() {
	global $thiss;//,$USERS_FILTER_PROBLEM;

	$USERS_FILTER_PROBLEM = (isset($_REQUEST['USERS_FILTER_PROBLEM']))? $_REQUEST['USERS_FILTER_PROBLEM'] : '';

	if (!isset($_REQUEST['USERS_FILTER_PROBLEM']) && !empty($_SESSION['USERS_FILTER_PROBLEM_SQL_FUNC'])) $USERS_FILTER_PROBLEM = $_SESSION['USERS_FILTER_PROBLEM_SQL_FUNC'] ;

	if ($USERS_FILTER_PROBLEM) {
		$_SESSION['USERS_FILTER_PROBLEM_SQL_FUNC'] = $USERS_FILTER_PROBLEM;

		if ($USERS_FILTER_PROBLEM == 'PROBLEM') $_SESSION['USERS_FILTER_PROBLEM_SQL']="and $thiss->DETECT_TABLE_NAME.A_PROBLEM!=''";
		if ($USERS_FILTER_PROBLEM == 'NORMAL') $_SESSION['USERS_FILTER_PROBLEM_SQL']="and $thiss->DETECT_TABLE_NAME.A_PROBLEM=''";
	} else {
		unset($_SESSION['USERS_FILTER_PROBLEM_SQL']);
		unset($_SESSION['USERS_FILTER_PROBLEM_SQL_FUNC']);
	}
}


function USERS_FILTER_STATUS() {
	global $thiss;//,$USERS_FILTER_STATUS;

	$USERS_FILTER_STATUS = (isset($_REQUEST['USERS_FILTER_STATUS']))? $_REQUEST['USERS_FILTER_STATUS'] : '';

	if (!isset($_REQUEST['USERS_FILTER_STATUS']) && !empty($_SESSION['USERS_FILTER_STATUS_SQL_FUNC'])) $USERS_FILTER_STATUS = $_SESSION['USERS_FILTER_STATUS_SQL_FUNC'] ;

	if ($USERS_FILTER_STATUS) {
		$_SESSION['USERS_FILTER_STATUS_SQL_FUNC'] = $USERS_FILTER_STATUS;

		if ($USERS_FILTER_STATUS == 'WAITING') $_SESSION['USERS_FILTER_STATUS_SQL']="and $thiss->DETECT_TABLE_NAME.A_STATUS='WAITING'";
		if ($USERS_FILTER_STATUS == 'AKTYWNI') {
			$_SESSION['USERS_FILTER_STATUS_SQL']="and (( $thiss->DETECT_TABLE_NAME.A_STATUS='NORMAL' or $thiss->DETECT_TABLE_NAME.A_STATUS='WARNING'  ) or ( $thiss->DETECT_TABLE_NAME.A_STATUS='OFF_SOFT' and $thiss->DETECT_TABLE_NAME.A_PROBLEM_DESC not like '%odla%fizy%' and $thiss->DETECT_TABLE_NAME.A_PROBLEM!='' ) or ( $thiss->DETECT_TABLE_NAME.A_STATUS='OFF_SOFT' and $thiss->DETECT_TABLE_NAME.A_PROBLEM='' )) ";
			if ($thiss->DETECT_TABLE_NAME == 'KSIEG_DOKUMENTY') $_SESSION['USERS_FILTER_STATUS_SQL']="and ( $thiss->DETECT_TABLE_NAME.A_STATUS='NORMAL' or $thiss->DETECT_TABLE_NAME.A_STATUS='WARNING'  )  ";
		}
	} else {
		unset($_SESSION['USERS_FILTER_STATUS_SQL']);
		unset($_SESSION['USERS_FILTER_STATUS_SQL_FUNC']);
	}
}


function USERS_FILTER_APPOINTMENT() {
	global $thiss;//,$USERS_FILTER_APPOINTMENT;

	$USERS_FILTER_APPOINTMENT = (isset($_REQUEST['USERS_FILTER_APPOINTMENT']))? $_REQUEST['USERS_FILTER_APPOINTMENT'] : '';

	if (!isset($_REQUEST['USERS_FILTER_APPOINTMENT']) && !empty($_SESSION['USERS_FILTER_APPOINTMENT_SQL_FUNC'])) $USERS_FILTER_APPOINTMENT=$_SESSION['USERS_FILTER_APPOINTMENT_SQL_FUNC'] ;

	if ($USERS_FILTER_APPOINTMENT) {
		$_SESSION['USERS_FILTER_APPOINTMENT_SQL_FUNC']=$USERS_FILTER_APPOINTMENT;

		if ($USERS_FILTER_APPOINTMENT == 'TODAY') {
			$tomorrow  = mktime (0,0,0,date("m")  ,date("d")+1,date("Y"));
			$today  = mktime (0,0,0,date("m")  ,date("d"),date("Y"));
			$_SESSION['USERS_FILTER_APPOINTMENT_SQL']="and UNIX_TIMESTAMP($thiss->DETECT_TABLE_NAME.L_APPOITMENT_DATE) > '$today' and UNIX_TIMESTAMP($thiss->DETECT_TABLE_NAME.L_APPOITMENT_DATE) < '$tomorrow'";
		}
		if ($USERS_FILTER_APPOINTMENT == 'TOMORROW') {
			$tomorrow  = mktime (0,0,0,date("m")  ,date("d")+2,date("Y"));
			$today  = mktime (0,0,0,date("m")  ,date("d")+1,date("Y"));
			$_SESSION['USERS_FILTER_APPOINTMENT_SQL']="and UNIX_TIMESTAMP($thiss->DETECT_TABLE_NAME.L_APPOITMENT_DATE) > '$today' and UNIX_TIMESTAMP($thiss->DETECT_TABLE_NAME.L_APPOITMENT_DATE) < '$tomorrow'";
		}
		if ($USERS_FILTER_APPOINTMENT == 'YESTERDAY') {
			$tomorrow  = mktime (0,0,0,date("m")  ,date("d")-1,date("Y"));
			$today  = mktime (0,0,0,date("m")  ,date("d")-2,date("Y"));
			$_SESSION['USERS_FILTER_APPOINTMENT_SQL']="and UNIX_TIMESTAMP($thiss->DETECT_TABLE_NAME.L_APPOITMENT_DATE) > '$today' and UNIX_TIMESTAMP($thiss->DETECT_TABLE_NAME.L_APPOITMENT_DATE) < '$tomorrow'";
		}
		if ($USERS_FILTER_APPOINTMENT == 'OLD') {
			$tomorrow  = mktime (0,0,0,date("m")  ,date("d")-1,date("Y"));
			$today  = mktime (0,0,0,date("m")  ,date("d")-2,date("Y"));
			$_SESSION['USERS_FILTER_APPOINTMENT_SQL']="and UNIX_TIMESTAMP($thiss->DETECT_TABLE_NAME.L_APPOITMENT_DATE) < UNIX_TIMESTAMP(now()) and $thiss->DETECT_TABLE_NAME.L_APPOITMENT_DATE!='' ";
		}
		if ($USERS_FILTER_APPOINTMENT == 'NOW') {
			$tomorrow  = mktime (0,0,0,date("m")  ,date("d")-1,date("Y"));
			$today  = mktime (0,0,0,date("m")  ,date("d")-2,date("Y"));
			$_SESSION['USERS_FILTER_APPOINTMENT_SQL']="and UNIX_TIMESTAMP($thiss->DETECT_TABLE_NAME.L_APPOITMENT_DATE) < UNIX_TIMESTAMP(now())+3600 and UNIX_TIMESTAMP($thiss->DETECT_TABLE_NAME.L_APPOITMENT_DATE)  > UNIX_TIMESTAMP(now())-3600 ";
		}
	} else {
		unset($_SESSION['USERS_FILTER_APPOINTMENT_SQL']);
		unset($_SESSION['USERS_FILTER_APPOINTMENT_SQL_FUNC']);
	}
}
