<?php
//@2012-08-28 sqix: added in line 27:  $_SESSION['USERS_COLUMN']['SPECIALFILTER'] problem with WHERE and WHERE2


function GETZAPSQL($OPT_EDIT_ID_RECORD = 0) {
global $GETZAPSQL,$thiss,$SQL_USER,$SQL_DATABASE,$INTERFACE_ADDR,$SQL_USER_PASS,$SQL_WIEV,$USERS_COLUMN,$USERS_COLUMN_DESC,$USERS_HIDECOLUMN;

if($_SESSION['DEBUG']) echo "<br>\nfunction GETZAPSQL()<br>";
USERS_FILTER_PROBLEM();
USERS_FILTER_STATUS();
USERS_FILTER_APPOINTMENT();
//FILTER_SEARCH();


//$GETZAPSQL[WHAT]="";
$GETZAPSQL['FROM']="";
$GETZAPSQL['FROM_USED']="";
$GETZAPSQL['WHERE'] = "1=1 ";
//ADDED BY SQIX at 2008-02-21
if (!empty($_SESSION[$thiss->DETECT_TABLE_COLUMN]['TYPE']['A_ADM_COMPANY'])) {
	$GETZAPSQL['WHERE'] .= " and ".$thiss->DETECT_TABLE_NAME.".A_ADM_COMPANY like '".$_SESSION['ADM_COMPANY']."' ";
	if (!empty($_SESSION['USERS_FILTER_PROBLEM_SQL'])) $GETZAPSQL['WHERE'] .= " ".$_SESSION['USERS_FILTER_PROBLEM_SQL'];
	if (!empty($_SESSION['USERS_FILTER_STATUS_SQL'])) $GETZAPSQL['WHERE'] .= " ".$_SESSION['USERS_FILTER_STATUS_SQL'];
}
if (!empty($_SESSION['USERS_FILTER_APPOINTMENT_SQL'])) $GETZAPSQL['WHERE'] .= " " . $_SESSION['USERS_FILTER_APPOINTMENT_SQL'];
if (!empty($_SESSION[$thiss->DETECT_TABLE_COLUMN]['SPECIALFILTER'])) $GETZAPSQL['WHERE'] .= " " . $_SESSION[$thiss->DETECT_TABLE_COLUMN]['SPECIALFILTER'];
if (!empty($_SESSION[$thiss->DETECT_TABLE_COLUMN]['FINDSQL'])) $GETZAPSQL['WHERE'] .= " " . $_SESSION[$thiss->DETECT_TABLE_COLUMN]['FINDSQL'];
if (!empty($_SESSION[$thiss->DETECT_TABLE_COLUMN]['USERS_SORTER_SQL'])) $GETZAPSQL['WHERE'] .= " " . $_SESSION[$thiss->DETECT_TABLE_COLUMN]['USERS_SORTER_SQL'];

$GETZAPSQL['WHERE2'] = "" . $thiss->DETECT_TABLE_NAME.".A_ADM_COMPANY like '".$_SESSION['ADM_COMPANY']."' ";
if (!empty($_SESSION['USERS_FILTER_PROBLEM_SQL'])) $GETZAPSQL['WHERE2'] .= " " . $_SESSION['USERS_FILTER_PROBLEM_SQL'];
if (!empty($_SESSION['USERS_FILTER_STATUS_SQL'])) $GETZAPSQL['WHERE2'] .= " " . $_SESSION['USERS_FILTER_STATUS_SQL'];
if (!empty($_SESSION['USERS_FILTER_APPOINTMENT_SQL'])) $GETZAPSQL['WHERE2'] .= " " . $_SESSION['USERS_FILTER_APPOINTMENT_SQL'];
if (!empty($_SESSION[$thiss->DETECT_TABLE_COLUMN]['SPECIALFILTER'])) $GETZAPSQL['WHERE2'] .= " " . $_SESSION[$thiss->DETECT_TABLE_COLUMN]['SPECIALFILTER'];
if (!empty($_SESSION[$thiss->DETECT_TABLE_COLUMN]['FINDSQL'])) $GETZAPSQL['WHERE2'] .= " " . $_SESSION[$thiss->DETECT_TABLE_COLUMN]['FINDSQL'];
if (!empty($_SESSION[$thiss->DETECT_TABLE_COLUMN]['USERS_SORTER_SQL'])) $GETZAPSQL['WHERE2'] .= " " . $_SESSION[$thiss->DETECT_TABLE_COLUMN]['USERS_SORTER_SQL'];

if ($OPT_EDIT_ID_RECORD > 0) $GETZAPSQL['WHERE2'] .=" and ".$thiss->DETECT_TABLE_NAME.".ID='".$OPT_EDIT_ID_RECORD."'";

//echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">';print_r($GETZAPSQL);echo'</pre>';

$I = 0;
$IJ = 0;
//echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">';print_r($_SESSION[$thiss->DETECT_TABLE_COLUMN]);echo'</pre>';
foreach ($_SESSION[$thiss->DETECT_TABLE_COLUMN]['DESC'] as $value) {
	if (
		strstr($_SESSION[$thiss->DETECT_TABLE_COLUMN]['PERMEDIT']["$value"], 'R')
		&&
		(	!(
					!empty($_SESSION[$thiss->DETECT_TABLE_COLUMN]['HIDE'][$_SESSION[$thiss->DETECT_TABLE_COLUMN]['DESC_TO_KEY'][$value]])
					&&
					$_SESSION[$thiss->DETECT_TABLE_COLUMN]['HIDE'][$_SESSION[$thiss->DETECT_TABLE_COLUMN]['DESC_TO_KEY'][$value]]=='HIDE'
		) || (
		$OPT_EDIT_ID_RECORD > 0 //@2012-10-05 - by sqix - jezeli jest tryb wyzwolenia z edycji, to nie interesuja nas ukryte - pokazujemy wszystkie...
		)	)
	) {
		$GETZAPSQL['WHAT_DESC'][$I] = $value ;
		$GETZAPSQL['ID'][$I] = $I ;
		$GETZAPSQL['DESC_TO_KEY'][$value] = $I ;
if (!empty($_SESSION[$thiss->DETECT_TABLE_COLUMN]['RELATIVEDB']['SRC_COLUMN'][$value])) {
// $GETZAPSQL[WHAT][$I] = $_SESSION[$thiss->DETECT_TABLE_COLUMN][RELATIVEDB][SRC_COLUMN][$value] .".". $_SESSION[$thiss->DETECT_TABLE_COLUMN][RELATIVEDB][TRG_COLUMN][$value] ;
 $GETZAPSQL['WHAT'][$I] =  $_SESSION[$thiss->DETECT_TABLE_COLUMN]['RELATIVEDB']['TRG_COLUMN'][$value] . " as ".$value ;

	// po co sprawdzac czy [SRC_COLUMN][$value] jest tablica?
// if(!(is_array($_SESSION[$thiss->DETECT_TABLE_COLUMN]['RELATIVEDB']['SRC_COLUMN'][$value]) )) {
	// dodaj [RELATIVEDB][JOINQUERY] tylko raz - moze byc kilka pol z jednej tabeli (to samo w [SRC_COLUMN])
	//@2012-10-12 usuwanie warningow sqix
 // if(is_array($GETZAPSQL['FROM_USED'])) $TST121012['FROM_USED']=$GETZAPSQL['FROM_USED']; else $TST121012['FROM_USED'][]="";
 // echo $TST121012['FROM_USED'];
 // echo $GETZAPSQL['FROM_USED'];

  
  if(isset($GETZAPSQL['FROM_USED'])) $TST121012['FROM_USED']=$GETZAPSQL['FROM_USED']; else $TST121012['FROM_USED']=array();
 if(!(in_array($_SESSION[$thiss->DETECT_TABLE_COLUMN]['RELATIVEDB']['SRC_COLUMN'][$value], $TST121012['FROM_USED']) )) {// TODO: in_array() expects parameter 2 to be array, string given
   $GETZAPSQL['FROM'][$IJ] = $_SESSION[$thiss->DETECT_TABLE_COLUMN]['RELATIVEDB']['JOINQUERY'][$_SESSION[$thiss->DETECT_TABLE_COLUMN]['RELATIVEDB']['SRC_COLUMN'][$value]];
   $GETZAPSQL['FROM_USED'][$IJ] = $_SESSION[$thiss->DETECT_TABLE_COLUMN]['RELATIVEDB']['SRC_COLUMN'][$value] ;

//TODO 2006-03 wylaczono z uwagi na inny pomysl - 2 zapytania do bazy danych
//   if($_SESSION[$thiss->DETECT_TABLE_COLUMN][RELATIVEDB][WHERE][$_SESSION[$thiss->DETECT_TABLE_COLUMN][RELATIVEDB][SRC_COLUMN][$value]]) { //DODAJEMY 2006-03 wyjatki grupujaco sortujace
//       echo "<hr>**** ZADREKLAROWANO $VALUE W TRYBIE WHERE **** <hr> ";  
//       $GETZAPSQL[WHERE2].="  ".$_SESSION[$thiss->DETECT_TABLE_COLUMN][RELATIVEDB][WHERE][$_SESSION[$thiss->DETECT_TABLE_COLUMN][RELATIVEDB][SRC_COLUMN][$value]];
//       echo "<br> *** GETZAPSQL[WHERE2]: ". $GETZAPSQL[WHERE2] ."<br>\n " ;
//     }


   $IJ++ ;
  }

 } else {
 $GETZAPSQL['WHAT'][$I] = $thiss->DETECT_TABLE_NAME .".". $value ;
 }

$I++;
	}
} //EOF foreach1

$GETZAPSQL['WHERE2'].=" ".$_SESSION[$thiss->DETECT_TABLE_COLUMN]['SQL_WIEV_CURR_SQL'];
//WHAT_SQL
//foreach ($GETZAPSQL['WHAT'] as $value) {
//if ($GETZAPSQL['WHAT_SQL']) $GETZAPSQL['WHAT_SQL']=$GETZAPSQL['WHAT_SQL']." , $value"; else $GETZAPSQL['WHAT_SQL']=$value ;
//}
$GETZAPSQL['WHAT_SQL'] = implode(" , ", $GETZAPSQL['WHAT']);

//FROM_SQL
$GETZAPSQL['FROM_SQL'] = $thiss->DETECT_TABLE_NAME;
if (is_array($GETZAPSQL['FROM'])) {
	//foreach ($GETZAPSQL['FROM'] as $value) {
	//	$GETZAPSQL['FROM_SQL']=$GETZAPSQL['FROM_SQL']." $value"; 
	//}
	$GETZAPSQL['FROM_SQL'] = $GETZAPSQL['FROM_SQL'] . " " . implode(" ", $GETZAPSQL['FROM']);
}


if ($_SESSION['DEBUG']) {
	echo "<hr>print_r";
	echo "<hr>GETZAPSQL output is WHAT GETZAPSQL['WHAT'] ...<br> From: xxx<hr> ";
	print_r($GETZAPSQL);
	echo "<hr>\n EOF f.GETZAPSQL<hr>";
}

}
