<?php


/**
 * TODO: @param $EXTERNAL_UNION_FUNCTION nie jest obsługiwany!
 */
function WIEVTABLE_UNION($EXTERNAL_UNION_FUNCTION) {
global $thiss,$result,$SQL_USER,$SQL_DATABASE,$INTERFACE_ADDR,$SQL_USER_PASS,$USERS_FILTER_PROBLEM,$USERS_FILTER_STATUS,$SQL_WIEV;
if($_SESSION[DEBUG]) { echo "f.USERS"; }
SEF("MENU");
MENU();
DETECT_TABLE_COLUMN();
if($_SESSION[DEBUG]) echo "USERS WIEV FUNCTION USED";
if($USERS_FILTER_PROBLEM) USERS_FILTER_PROBLEM();
//SQL_WIEV();
//$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!");
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] ." ";
L_APPOITMENT_RESULT($zapSQL);
if($_SESSION[DEBUG]) echo "<hr>*ZAPSQL $zapSQL ***<hr> ".$_SESSION[$thiss->DETECT_TABLE_COLUMN][FINDSQL]."***";
//$result = mysql_query($zapSQL,$conn) or die("Blad zapytania1!");
$_SESSION[$thiss->DETECT_TABLE_COLUMN][MYSQL_NUM_ROWS_TOTAL] = mysql_num_rows($result);
$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][FINDSQL] . " ". $_SESSION[$thiss->DETECT_TABLE_COLUMN][USERS_SORTER_SQL] ."  ".$_SESSION[$thiss->DETECT_TABLE_COLUMN][SQL_WIEV_CURR_SQL]."";
//echo "$zapSQL";
//$result = mysql_query($zapSQL,$conn) or die("Blad zapytania2!");

L_APPOITMENT_RESULT($zapSQL);

$num_rows = mysql_num_rows($result);

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($_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($_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,TODAY,APP_DZISIAJ);
 if($_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(strstr($_SESSION[$thiss->DETECT_TABLE_COLUMN][FUNCPERMEDIT][P_NEW_DEAL],"RW")) { 
  CREATE_MENUFUNC_INIT_2(USERS_NEWDEAL,NEWDEAL,'','NOWA_UMOWA') ;
 }
 if(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>') ;
 }

echo'<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."*";

    foreach ($_SESSION[$thiss->DETECT_TABLE_COLUMN][ID] as $value) {
    if(!$_SESSION[$thiss->DETECT_TABLE_COLUMN][HIDE][$value]==HIDE) { echo "<td><font size='-2'>" ; 
if($_SESSION[$thiss->DETECT_TABLE_COLUMN][SORTED][$_SESSION[$thiss->DETECT_TABLE_COLUMN][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,$_SESSION[$thiss->DETECT_TABLE_COLUMN][DESC][$value],'<img src=icon/sortred.gif border='.$border.' alt=SORTUJ><img src=icon/sortred.gif border='.$border.' alt=SORTUJ>') ; 

if($_SESSION[$thiss->DETECT_TABLE_COLUMN][USERS_SORTER_SQL]) { 
 if($_SESSION[$thiss->DETECT_TABLE_COLUMN][SORTED][$_SESSION[$thiss->DETECT_TABLE_COLUMN][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,$_SESSION[$thiss->DETECT_TABLE_COLUMN][DESC][$value],'<img src=icon/sort.gif border='.$border.' alt=sortuj>') ; 
      }

      CREATE_MENU_NOTD_INIT_3(USERS_COLUMN,$value,HIDE,'<img src=icon/shutdown.gif border=0 alt=sortuj>') ;

      echo "<br><b>" . $_SESSION[$thiss->DETECT_TABLE_COLUMN][DESC][$value] . "</font></b></td>"; 

					 }

     }
     echo "</tr> \n";
//SECOND_ROW
   if($_SESSION[$thiss->DETECT_TABLE_COLUMN][MENU_FIND]==ENABLE) {
    echo "<tr><TD><font size=-2><FORM action='$PHP_SELF' method='POST' name='FILTER_SEARCH'>";
    CREATE_MENU_NOTD_INIT_3(FILTER_SEARCH,MENU_FIND,'','X');
    echo "<input type=hidden name=FUNCTION_INIT value=FILTER_SEARCH><input type=submit value=OK>";
    echo "</TD>";
    foreach ($_SESSION[$thiss->DETECT_TABLE_COLUMN][ID] as $value) {
    if(!$_SESSION[$thiss->DETECT_TABLE_COLUMN][HIDE][$value]==HIDE) { echo "<td><font size='-2'>" ;
	GETFORMITEM($_SESSION[$thiss->DETECT_TABLE_COLUMN][FIND][$_SESSION[$thiss->DETECT_TABLE_COLUMN][DESC][$value]],$_SESSION[$thiss->DETECT_TABLE_COLUMN][TYPE][$_SESSION[$thiss->DETECT_TABLE_COLUMN][DESC][$value]],$_SESSION[$thiss->DETECT_TABLE_COLUMN][DESC][$value]);
    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>';
if( $num_rows == 0 ) echo "</tr></table>Brak Danych" ; else {


$t = 0;
echo'<tbody>';
    while ($h=mysql_fetch_row($result)) {
		echo '<tr class="row-' . ($t = 1 - $t) . '">';
		echo '<td align="center">';
//TODO funkcje wykryte specyfuiczne dla tego menu
//    echo "<a href=$PHP_SELF?MENU_INIT=VIEWHISTORY
if($thiss->DETECT_HIST_FUNCTION)  CREATE_MENUFUNC_INIT_2($thiss->DETECT_HIST_FUNCTION,HISTORY,$h[$_SESSION[$thiss->DETECT_TABLE_COLUMN][DESC_TO_KEY][ID]],'<img src=icon/history.gif alt=HISTORIA border=0 width=18>') ;
if($thiss->DETECT_EDIT_FUNCTION)    CREATE_MENUFUNC_INIT_2($thiss->DETECT_EDIT_FUNCTION,EDIT,$h[$_SESSION[$thiss->DETECT_TABLE_COLUMN][DESC_TO_KEY][ID]],'<img src=icon/edit.gif alt=EDYCJA border=0 width=20>') ;
    echo "</TD>";
    foreach ($_SESSION[$thiss->DETECT_TABLE_COLUMN][ID] as $value) {
    if(!$_SESSION[$thiss->DETECT_TABLE_COLUMN][HIDE][$value]==HIDE) { 
     echo "<TD ";
      USERS_COLUMN_COLOR($_SESSION[$thiss->DETECT_TABLE_COLUMN][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>";
return;
}
