<?php


function WIEVTABLE( $params = array() ) {
global $GETZAPSQL,$thiss,$SQL_USER,$SQL_DATABASE,$INTERFACE_ADDR,$SQL_USER_PASS,$SQL_WIEV;
if($_SESSION['DEBUG']) echo "f.USERS";

SEF("MENU");
MENU();

DETECT_TABLE_COLUMN();

if ($_SESSION['DEBUG']) echo "USERS WIEV FUNCTION USED";
if (!isset($_SESSION[$thiss->DETECT_TABLE_COLUMN]['SQL_WIEV_CURR_SQL'])) SQL_WIEV();

// connect to DB
$conn=mysql_pconnect($INTERFACE_ADDR,$SQL_USER,$SQL_USER_PASS);
if (!$conn) die("ERROR podczas tworzenia polaczenia do bazy $SQL_DATABASE !\n");
mysql_select_db($SQL_DATABASE) or die("ERROR podczas wyboru bazy R50!");

// generate $GETZAPSQL
GETZAPSQL();
// if($_SESSION[$thiss->DETECT_TABLE_COLUMN][DESC_TO_KEY][L_APPOITMENT_DATE]) $_SESSION[$thiss->DETECT_TABLE_COLUMN][USERS_FILTER_APPOINTMENT_SQL]=$_SESSION[USERS_FILTER_APPOINTMENT_SQL];
// if($_SESSION[$thiss->DETECT_TABLE_COLUMN][DESC_TO_KEY][A_STATUS]) $_SESSION[$thiss->DETECT_TABLE_COLUMN][USERS_FILTER_STATUS_SQL]=$_SESSION[USERS_FILTER_STATUS_SQL];
//$_SESSION[USERS_FILTER_STATUS_SQL]


//$zapSQL="SELECT * from $thiss->DETECT_TABLE_NAME where A_ADM_COMPANY like '$_SESSION[ADM_COMPANY]' $_SESSION[USERS_FILTER_PROBLEM_SQL] ".$_SESSION[$thiss->DETECT_TABLE_COLUMN][USERS_FILTER_STATUS_SQL]."  ".$_SESSION[$thiss->DETECT_TABLE_COLUMN][USERS_FILTER_APPOINTMENT_SQL]."  ".$_SESSION[$thiss->DETECT_TABLE_COLUMN][USERS_FILTER_STATUS_SQL]." " . $_SESSION[$thiss->DETECT_TABLE_COLUMN][FINDSQL] ." ". $_SESSION[$thiss->DETECT_TABLE_COLUMN][USERS_SORTER_SQL] ." ;";
$zapSQL="SELECT ".$GETZAPSQL['WHAT_SQL']." from ".$GETZAPSQL['FROM_SQL']."  where ".$GETZAPSQL['WHERE']."  ;";
if ($_SESSION['DEBUG']) echo "*ZAPSQL_NIECORRECT $zapSQL ***<hr> ".$_SESSION[$thiss->DETECT_TABLE_COLUMN]['FINDSQL']."***";
$result = mysql_query($zapSQL,$conn) or die("Blad zapytania1!:.".htmlspecialchars($zapSQL));
$num_rows = mysql_num_rows($result);
$_SESSION[$thiss->DETECT_TABLE_COLUMN]['MYSQL_NUM_ROWS_TOTAL'] = $num_rows;

$zapSQL="SELECT ".$GETZAPSQL['WHAT_SQL']." from ".$GETZAPSQL['FROM_SQL']."  where ".$GETZAPSQL['WHERE2']." ; ";
if ($_SESSION['DEBUG']) echo "<hr>*ZAPSQL_CORRECT $zapSQL ***<hr> ".$_SESSION[$thiss->DETECT_TABLE_COLUMN]['FINDSQL']."***";
$result = mysql_query($zapSQL,$conn) or die("Blad zapytania2!");
$_SESSION['MYSQL_NUM_ROWS'] = $num_rows;

echo "<table border='1' cellspacing='0' cellpadding='0' width='100%'><tr>";
CREATE_MENU_INIT('USERS_FILTER_PROBLEM', 'PROBLEM', '<font color=#CC0000>PROBLEMY</b></font>');
CREATE_MENU_INIT('USERS_FILTER_PROBLEM', 'WARNING', '<font color=#CC0000>OSTRZEZENIA</b></font>');
CREATE_MENU_INIT('USERS_FILTER_PROBLEM', 'NORMAL', '<font color=#CC0000>BEZ_PROBLEM.</b></font>');
if(!empty($_SESSION['USERS_FILTER_PROBLEM_SQL'])) $FILTER_BOLD="<b>*"; else $FILTER_BOLD="";
CREATE_MENU_INIT('USERS_FILTER_PROBLEM', '', "<font color=#CC0000>".$FILTER_BOLD."KASUJ-FILTR</b></font>");
 if($_SESSION[$thiss->DETECT_TABLE_COLUMN]['DESC_TO_KEY']['A_STATUS']) {
 CREATE_MENU_INIT('USERS_FILTER_STATUS', 'WAITING', '<font color=#000055><i>OCZEKUJACY</i></font>');
 CREATE_MENU_INIT('USERS_FILTER_STATUS', 'AKTYWNI', '<font color=#000055><i>AKTYWNI</i></font>');
 if(!empty($_SESSION['USERS_FILTER_STATUS_SQL'])) $FILTER_BOLD="<b>*"; else $FILTER_BOLD="";
 CREATE_MENU_INIT('USERS_FILTER_STATUS', '', "<font color=#000055><i>".$FILTER_BOLD."KASUJ-FILTR</b></i></font>");
}
if($_SESSION[$thiss->DETECT_TABLE_COLUMN]['DESC_TO_KEY']['L_APPOITMENT_DATE']) {
 CREATE_MENU_INIT('USERS_FILTER_APPOINTMENT', 'OLD', 'A_STARE');
 CREATE_MENU_INIT('USERS_FILTER_APPOINTMENT', 'NOW', 'A_ZARAZ');
 CREATE_MENU_INIT('USERS_FILTER_APPOINTMENT', 'TODAY', 'A_DZISIAJ');
 if(!empty($_SESSION['USERS_FILTER_APPOINTMENT_SQL'])) $FILTER_BOLD="<b>*"; else $FILTER_BOLD="";
 CREATE_MENU_INIT('USERS_FILTER_APPOINTMENT', '', $FILTER_BOLD."APP_ALL</b>");
}




CREATE_MENUFUNC_INIT('USERS_COLUMN_MENU', '<img src=icon/excel.gif alt=WYBIERZ_KOLUMNY border=0 width=18>');
echo "</tr></table>";
NAVIGATOR();
 if(isset($_SESSION[$thiss->DETECT_TABLE_COLUMN]['FUNCPERMEDIT']['P_NEW_DEAL']) && $_SESSION[$thiss->DETECT_TABLE_COLUMN]['FUNCPERMEDIT']['P_NEW_DEAL'] == "RW") {
  CREATE_MENUFUNC_INIT_2('USERS_NEWDEAL', 'NEWDEAL', '', 'NOWA_UMOWA') ;
 }
 if(isset($_SESSION[$thiss->DETECT_TABLE_COLUMN]['FUNCPERMEDIT']['NEW_RECORD']) && strstr($_SESSION[$thiss->DETECT_TABLE_COLUMN]['FUNCPERMEDIT']['NEW_RECORD'], "X")) {
  CREATE_MENUFUNC_INIT_2('NEW_RECORD', 'NEW_RECORD', '', '<font size=-1>( NOWY_REKORD<img src=icon/new.gif border=0 alt=NOWY_REKORD> )</font>') ;
 }

if (!empty($params['toolbar'])) {
	$sep_out = "&nbsp;&nbsp;|&nbsp;&nbsp;";
	echo $sep_out . implode($sep_out, $params['toolbar']);
}

echo "\n" . '<table width="100%" border="1" cellspacing="0" cellpadding="0" class="tbl-view">';
echo'<thead>';
//echo "\n<table width='100%' border='1' cellspacing='1' cellpadding='0'>";
    echo "<tr><TD><font size=-1>";
    //CREATE_MENU_NOTD_INIT_3(USERS_COLUMN,$value,HIDE,'X') ;
    CREATE_MENU_NOTD_INIT_3('FILTER_SEARCH', 'MENU_FIND', 'ENABLE' ,'<img src=icon/search.gif alt=SZUKAJ border=0>');
    echo "</TD>";
//echo "<hr>HERE".$_SESSION[$thiss->DETECT_TABLE_COLUMN][ID]."*".$thiss->DETECT_TABLE_COLUMN."*";
if($_SESSION['DEBUG']) print_r($GETZAPSQL);
    foreach ($GETZAPSQL['ID'] as $value) {

    if(true) { echo "<td><font size='-2'>";
	    $ALT = (isset($_SESSION[$thiss->DETECT_TABLE_COLUMN]['DESC_TO_LANG'][$GETZAPSQL['WHAT_DESC'][$value]]))? $_SESSION[$thiss->DETECT_TABLE_COLUMN]['DESC_TO_LANG'][$GETZAPSQL['WHAT_DESC'][$value]] : '';
if(isset($_SESSION[$thiss->DETECT_TABLE_COLUMN]['SORTED'][$GETZAPSQL['WHAT_DESC'][$value]]) && $_SESSION[$thiss->DETECT_TABLE_COLUMN]['SORTED'][$GETZAPSQL['WHAT_DESC'][$value]] == 'PRI')  $border=1; else $border=0; // echo "<font color='red'><img src=icon/sortred.gif width=10 border=0 alt=sortuj></font>";
    CREATE_MENU_NOTD_INIT_2('USERS_SORTER', 'USERS_SORTER', 'USERS_SORTER1', $GETZAPSQL['WHAT_DESC'][$value], '<img src=icon/sortred.gif border='.$border.' alt='.$ALT.'><img src=icon/sortred.gif border='.$border.' alt=SORTUJ>');

if(isset($_SESSION[$thiss->DETECT_TABLE_COLUMN]['USERS_SORTER_SQL']) && $_SESSION[$thiss->DETECT_TABLE_COLUMN]['USERS_SORTER_SQL']) {
  if(isset($_SESSION[$thiss->DETECT_TABLE_COLUMN]['SORTED'][$GETZAPSQL['WHAT_DESC'][$value]]))if($_SESSION[$thiss->DETECT_TABLE_COLUMN]['SORTED'][$GETZAPSQL['WHAT_DESC'][$value]] == 'SEC')  $border=1; else $border=0; // echo "<font color='green'><img src=icon/sort.gif width=10 border=0 alt=sortuj></font>";
    CREATE_MENU_NOTD_INIT_2('USERS_SORTER', 'USERS_SORTER', 'USERS_SORTER2', $GETZAPSQL['WHAT_DESC'][$value], '<img src="icon/sort.gif" border="'.$border.'" alt="'.$ALT.'">');
      }

    CREATE_MENU_NOTD_INIT_3('USERS_COLUMN', $_SESSION[$thiss->DETECT_TABLE_COLUMN]['DESC_TO_KEY'][$GETZAPSQL['WHAT_DESC'][$value]], 'HIDE', '<img src=icon/shutdown.gif border=0 alt="'.$ALT.' WYLACZ_KOLUMNE">');


//      echo "<br><b>" .substr($GETZAPSQL[WHAT_DESC][$value],0,8) . "</font></b></td>"; 
echo "<br><b>" .str_replace('_',' ',$GETZAPSQL['WHAT_DESC'][$value]) . "</font></b></td>";
					 }

     }
     echo "</tr> \n";
//SECOND_ROW
		//@2012-10-12 warning clearing by sqix
  if(isset($_SESSION[$thiss->DETECT_TABLE_COLUMN]['MENU_FIND'])) if($_SESSION[$thiss->DETECT_TABLE_COLUMN]['MENU_FIND'] == 'ENABLE') {
    echo "<tr><TD><font size=-2><FORM action='".$_SERVER['PHP_SELF']."' method='POST' name='FILTER_SEARCH'>";
    echo '<a href="' . "?FUNCTION_INIT=FILTER_SEARCH&amp;ARG1=MENU_FIND&amp;ARG1_VAL=" . '">' . '<img src="./icon/del.png" alt="' . "X" . '" />' . '</a>';
    echo "<input type=hidden name=FUNCTION_INIT value=FILTER_SEARCH><input type=submit value=OK>";
    echo '<input type="button" onclick="return resetForm(this);" value="Czyść" />';
    echo "</TD>";
    foreach ($GETZAPSQL['ID'] as $value) {
    if(true) { echo "<td><font size='-2'>" ; 
	GETFORMITEM($_SESSION[$thiss->DETECT_TABLE_COLUMN]['FIND'][$GETZAPSQL['WHAT_DESC'][$value]], $_SESSION[$thiss->DETECT_TABLE_COLUMN]['TYPE'][$GETZAPSQL['WHAT_DESC'][$value]], $GETZAPSQL['WHAT_DESC'][$value], array('multiple'=>true));
    echo "</font></b></td>"; 
					 } //EOF if(!$thiss->DETECT_TABLE_COLUMN[HIDE][$value]==HIDE)
     } //EOF foreach ($thiss->DETECT_TABLE_COLUMN[ID] as $value)
     echo "</tr></form> \n";
  } //EOF  if($_SESSION[$thiss->DETECT_TABLE_COLUMN][MENU_FIND]==ENABLE)
echo'</thead>';
echo'<tbody id="wyniki-body">';
	if ($num_rows == 0) {
		echo '</tbody></table>'."Brak Danych";
		return;
	}

	$t = 0;
	while ($h = mysql_fetch_row( $result )) {
		echo '<tr class="wynik row-' . ($t = 1 - $t) . '">';
		echo '<td align="center">';
//TODO funkcje wykryte specyfuiczne dla tego menu
//    echo "<a href=$PHP_SELF?MENU_INIT=VIEWHISTORY
		if (isset($_SESSION[$thiss->DETECT_TABLE_COLUMN]['FUNCPERMEDIT']['NEW_RECORD']) && strstr($_SESSION[$thiss->DETECT_TABLE_COLUMN]['FUNCPERMEDIT']['NEW_RECORD'], "X")) {
			$js = "return confirm('Czy na pewno chcesz utworzyć nowy rekord na podstawie danych z rekordu ID = ".$h[$GETZAPSQL['DESC_TO_KEY']['ID']]."?');";
			echo '<a href="'.$_SERVER['PHP_SELF']."?FUNCTION_INIT=COPY_RECORD&ARG1=NR_POZ&ARG1_VAL=".$h[$GETZAPSQL['DESC_TO_KEY']['ID']].'" onclick="'.$js.'">'."CP".'</a>'."&nbsp;&nbsp;";
		}
    CREATE_MENUFUNC_INIT_2($thiss->DETECT_HIST_FUNCTION, 'HISTORY', $h[$GETZAPSQL['DESC_TO_KEY']['ID']], '<img src=icon/history.gif alt=HISTORIA border=0 width=18>') ;
    CREATE_MENUFUNC_INIT_2($thiss->DETECT_EDIT_FUNCTION, 'EDIT', $h[$GETZAPSQL['DESC_TO_KEY']['ID']], '<img src=icon/edit.gif alt=EDYCJA border=0 width=20>') ;
    echo '</td>';
		if ($_SESSION['DEBUG']) print_r($h);
		foreach ($GETZAPSQL['ID'] as $value) {
			echo '<td ';
				USERS_COLUMN_COLOR($GETZAPSQL['WHAT_DESC'][$value], $h[$value] , 'bgcolor');
			echo '>';
			echo $h[$value];// . $_SESSION[$thiss->DETECT_TABLE_COLUMN][DESC][$value];
			echo '</td>';
		}
		echo '</tr>'."\n";
	}
	echo'</tbody>';
	echo '</table>';
}
