<?php


function EDIT_TABLE_RECORD() {
global $GETZAPSQL,$thiss,$SQL_USER,$SQL_DATABASE,$INTERFACE_ADDR,$SQL_USER_PASS,$USERS_FILTER_STATUS,$SQL_WIEV,$USERS_COLUMN,$USERS_COLUMN_DESC,$USERS_HIDECOLUMN;//$USERS_FILTER_PROBLEM

	DETECT_TABLE_COLUMN();

	if (!empty($_REQUEST['MENU_INIT']) && !empty($_REQUEST['ARG1']) && ($_REQUEST['ARG1'] == "EDIT")) {
		if ($_SESSION[$thiss->DETECT_TABLE_COLUMN]['WIEVID'] != intval($_REQUEST['ARG1_VAL'])) {
			$_SESSION[$thiss->DETECT_TABLE_COLUMN]['EDIT_TEMPLATE'] = array();
		}
		$_SESSION[$thiss->DETECT_TABLE_COLUMN]['WIEVID'] = intval($_REQUEST['ARG1_VAL']);
		if ($_SESSION['DEBUG']) echo "*VALUALIZED $ARG1_VAL for $thiss->DETECT_TABLE_COLUMN*";
	}

	if (empty($_SESSION[$thiss->DETECT_TABLE_COLUMN]['WIEVID'])) {
		echo '<p style="color:red">' . "Error: wrong ID!" . '</p>';
		return;
	}

	SEF("MENU");
	MENU();

	if ($_SESSION['DEBUG']) echo "*EDIT_TABLE_RECORD WIEV FUNCTION USED*";
	//if ($USERS_FILTER_PROBLEM) USERS_FILTER_PROBLEM();//TODO FIX THIS

	$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!");

	if ($_SESSION[$thiss->DETECT_TABLE_COLUMN]['TYPE']['A_ADM_COMPANY']) {
		$zapSQL="SELECT * from " . $thiss->DETECT_TABLE_NAME . "  where A_ADM_COMPANY like '" . $_SESSION['ADM_COMPANY'] . "' and ID='" . $_SESSION[$thiss->DETECT_TABLE_COLUMN]['WIEVID'] . "' ;";
	}
	else {
		$zapSQL="SELECT * from " . $thiss->DETECT_TABLE_NAME . "  where  ID='" . $_SESSION[$thiss->DETECT_TABLE_COLUMN]['WIEVID'] . "'   ;";
	}

	GETZAPSQL($_SESSION[$thiss->DETECT_TABLE_COLUMN]['WIEVID']); //@2012-10-06 sqix: warto aby byl taki SQL , gdyz trzeba sprawdzic czy gosc mogl takie ID edytowac w razie czego...
//    $zapSQL="SELECT ".$GETZAPSQL['WHAT_SQL']." from ".$GETZAPSQL['FROM_SQL']."  where ".$GETZAPSQL['WHERE2']." ; ";
    $zapSQL="SELECT ".$GETZAPSQL['WHAT_SQL']." from ".$GETZAPSQL['FROM_SQL']."  where ".$thiss->DETECT_TABLE_NAME.".ID='".$_SESSION[$thiss->DETECT_TABLE_COLUMN]['WIEVID']."' ; ";
  // @2012-10-08 pl: ten sql nie działa przy włączonym sortowaniu!
	// echo "<pre>".$zapSQL."</pre>";


//echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">';print_r($zapSQL);echo'</pre>';
	$result = mysql_query($zapSQL, $conn) or die("Blad zapytania2! L." . __LINE__ . " sql: " . htmlspecialchars($zapSQL));
	$num_rows = mysql_num_rows($result);
	$_SESSION[$thiss->DETECT_TABLE_COLUMN]['MYSQL_NUM_ROWS'] = $num_rows;

	echo'<table width="100%" border="1" cellspacing="0" cellpadding="0" class="tbl-view">';
	echo'<thead>';
	//echo "\n<table width='100%' border='1' cellspacing='0' cellpadding='0'>";
	echo '<TD><font size=-2>';
	echo '</TD><TD>' . "Nazwa_POLA";
	CREATE_MENUFUNC_INIT_2($thiss->DETECT_HIST_FUNCTION, 'HISTORY', $_SESSION[$thiss->DETECT_TABLE_COLUMN]['WIEVID'], '<img src=icon/history.gif alt=HISTORIA border=0>');
	echo '</TD><TD>' . "WARTOSC" . '</TD></TR>';
	echo'</thead>';
	echo'<tbody>';
	echo '<FORM ACTION="' . $_SERVER['PHP_SELF'] . '" method="POST" name="' . $thiss->DETECT_EDIT_FUNCTION . '">';
	echo '<input type="HIDDEN" name="WIEVID" value="' . $_SESSION[$thiss->DETECT_TABLE_COLUMN]['WIEVID'] . '" />';
	echo '<input type="HIDDEN" name="FUNCTION_INIT" value="TABLE_UPDATEDB" />';
//echo'<pre style="max-height:200px;overflow:auto;border:1px solid red;">'.$thiss->DETECT_TABLE_COLUMN.': ';print_r($_SESSION[$thiss->DETECT_TABLE_COLUMN]);echo'</pre>';
//	while ($h = mysql_fetch_row($result)) {

	$ROW121010=1; //@2012-10-10 numer rzedu, moze byc kilka w wyniku JOINowania tabel. Tylko pierwszy rzad pokazujemy ladnie, 
				  //w kolejnych sprawdzamy czy już ta wartość nie wystąpiła wcześniej w danej kolumnie, jak tak, to ignorujemy
				  // jak nie to wyswietlamy ponizej...

	while ($hh = mysql_fetch_array($result)) {

		$t = 0;
		
		foreach ($_SESSION[$thiss->DETECT_TABLE_COLUMN]['DESC'] as $KKEY=>$value_desc) {		

			$value=$KKEY ; 
			

		   if(isset($hh[$value_desc]))	$h[$value]=$hh[$value_desc]; //@2012-10-05 podmiana wskaznikow w arrayu nazwami
		   	
		   	//echo "<br>zzzzz::: ".$value_desc." wykrylem ".$value." , testi: ".$testI." a wartosc to ".$hh[$value] ;
		    //echo "<br>aaalbo::: jest value ".$value.", ktore oznacza ".$_SESSION[$thiss->DETECT_TABLE_COLUMN]['DESC'][$value];
			//modyfikacja aby chodzil po kolumnach do edycji, a nie po wszystkim
			//$_SESSION[$thiss->DETECT_TABLE_COLUMN]['DESC']
			
			
		}
		
		foreach ($_SESSION[$thiss->DETECT_TABLE_COLUMN]['ID'] as $value) {
				//@2012-10-12 warning errors remove by sqix
			  if(isset($ARG1_VAL)) $TST121012['ARG1_VAL']=$ARG1_VAL;
			    else $TST121012['ARG1_VAL']='';
			  
			   if (!(strstr($TST121012['ARG1_VAL'], 'RESTORE'))) {
				if(isset($h[$value])) $_SESSION[$thiss->DETECT_TABLE_COLUMN]['EDIT_TEMPLATE'][$_SESSION[$thiss->DETECT_TABLE_COLUMN]['DESC'][$value]] = $h[$value];
			}
		//@2012-10-12 usuwanie problemow warning sqix	
		if(empty($h[$value])) $h[$value]=""; //trzeba bylo tak zrobic, bo zle porownywalo
		if(isset($_SESSION[$thiss->DETECT_TABLE_COLUMN]['EDITRECORD'][$_SESSION[$thiss->DETECT_TABLE_COLUMN]['DESC'][$value]]))
		    if(($_SESSION[$thiss->DETECT_TABLE_COLUMN]['EDITRECORD'][$_SESSION[$thiss->DETECT_TABLE_COLUMN]['DESC'][$value]]==$h[$value])&&($ROW121010>1)) $ROW121009_IGNORE[$value]=1; else $ROW121009_IGNORE[$value]=0;
			
		if(isset($h[$value])) $_SESSION[$thiss->DETECT_TABLE_COLUMN]['EDITRECORD'][$_SESSION[$thiss->DETECT_TABLE_COLUMN]['DESC'][$value]] = $h[$value];
//			 if($ROW121009_IGNORE[$value]!=1) echo "<br>Ignore:".$value.", czyli.".$h[$value];
		  if(isset($ROW121009_IGNORE[$value]))  //@2012-10-12 naprawa warningow sqix
		      $TST121012['Line102']=$ROW121009_IGNORE[$value];
		        else $TST121012['Line102']="";
		  if($TST121012['Line102']!=1) {
		    //@2012-10-12 naprawa warningow
		    if(isset($_SESSION[$thiss->DETECT_TABLE_COLUMN]['PERMEDIT'][$_SESSION[$thiss->DETECT_TABLE_COLUMN]['DESC'][$value]])) 
		      $TST121012['Line104']=$_SESSION[$thiss->DETECT_TABLE_COLUMN]['PERMEDIT'][$_SESSION[$thiss->DETECT_TABLE_COLUMN]['DESC'][$value]];
		      else $TST121012['Line104']="";
			if (!strstr($TST121012['Line104'], 'R')) {
				continue;
			}
			echo'<tr class="row-' . ($t = 1 - $t) . '">';
			echo '<TD><font size=-2>';//. $_SESSION[$thiss->DETECT_TABLE_COLUMN][PERMEDIT][$_SESSION[$thiss->DETECT_TABLE_COLUMN][DESC][$value]];
			echo '<BR>';
			echo '</TD><TD>';
			if (isset($_SESSION[$thiss->DETECT_TABLE_COLUMN]['DESC_TO_LANG'][$_SESSION[$thiss->DETECT_TABLE_COLUMN]['DESC'][$value]])) { echo "<font size=-3>".$_SESSION[$thiss->DETECT_TABLE_COLUMN]['DESC_TO_LANG'][$_SESSION[$thiss->DETECT_TABLE_COLUMN]['DESC'][$value]] . "</font><br>";
				echo "<font size=-2>".$_SESSION[$thiss->DETECT_TABLE_COLUMN]['DESC'][$value]."</font>";
			} else {
				echo $_SESSION[$thiss->DETECT_TABLE_COLUMN]['DESC'][$value];
			}
			echo '</TD>';
			echo '<TD ';
			if(isset($_SESSION[$thiss->DETECT_TABLE_COLUMN]['EDIT_TEMPLATE'][$_SESSION[$thiss->DETECT_TABLE_COLUMN]['DESC'][$value]]))
			USERS_COLUMN_COLOR($_SESSION[$thiss->DETECT_TABLE_COLUMN]['DESC'][$value],$_SESSION[$thiss->DETECT_TABLE_COLUMN]['EDIT_TEMPLATE'][$_SESSION[$thiss->DETECT_TABLE_COLUMN]['DESC'][$value]], 'bgcolor');
			echo '>';

			if ((strstr($_SESSION[$thiss->DETECT_TABLE_COLUMN]['PERMEDIT'][$_SESSION[$thiss->DETECT_TABLE_COLUMN]['DESC'][$value]],"W")) && (!isset($_SESSION[$thiss->DETECT_TABLE_COLUMN]['TYPESPECIAL'][$_SESSION[$thiss->DETECT_TABLE_COLUMN]['DESC'][$value]]))) {
				if(isset($_SESSION[$thiss->DETECT_TABLE_COLUMN]['EDIT_TEMPLATE'][$_SESSION[$thiss->DETECT_TABLE_COLUMN]['DESC'][$value]])) {
					GETFORMITEM($_SESSION[$thiss->DETECT_TABLE_COLUMN]['EDIT_TEMPLATE'][$_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]);
				} else {
					GETFORMITEM('', $_SESSION[$thiss->DETECT_TABLE_COLUMN]['TYPE'][$_SESSION[$thiss->DETECT_TABLE_COLUMN]['DESC'][$value]], $_SESSION[$thiss->DETECT_TABLE_COLUMN]['DESC'][$value]);
				}

				if (isset($h[$value])) echo "({$h[$value]})";
			} else if (isset($_SESSION[$thiss->DETECT_TABLE_COLUMN]['TYPESPECIAL'][$_SESSION[$thiss->DETECT_TABLE_COLUMN]['DESC'][$value]])) {
				//    echo "special viev of func " . $_SESSION[$thiss->DETECT_TABLE_COLUMN][TYPESPECIAL][$_SESSION[$thiss->DETECT_TABLE_COLUMN][DESC][$value]] . "(" .  $_SESSION[$thiss->DETECT_TABLE_COLUMN]["WIEVID"] .  ") not currently implemented" ;
				if(!function_exists($_SESSION[$thiss->DETECT_TABLE_COLUMN]['TYPESPECIAL'][$_SESSION[$thiss->DETECT_TABLE_COLUMN]['DESC'][$value]])) SEF($_SESSION[$thiss->DETECT_TABLE_COLUMN]['TYPESPECIAL'][$_SESSION[$thiss->DETECT_TABLE_COLUMN]['DESC'][$value]]);
				$_SESSION[$thiss->DETECT_TABLE_COLUMN]['TYPESPECIAL'][$_SESSION[$thiss->DETECT_TABLE_COLUMN]['DESC'][$value]]($_SESSION[$thiss->DETECT_TABLE_COLUMN]['WIEVID'],$_SESSION[$thiss->DETECT_TABLE_COLUMN]['EDIT_TEMPLATE'][$_SESSION[$thiss->DETECT_TABLE_COLUMN]['DESC'][$value]]);
			} else {
			   if(isset($_SESSION[$thiss->DETECT_TABLE_COLUMN]['EDIT_TEMPLATE'][$_SESSION[$thiss->DETECT_TABLE_COLUMN]['DESC'][$value]]))
				echo $_SESSION[$thiss->DETECT_TABLE_COLUMN]['EDIT_TEMPLATE'][$_SESSION[$thiss->DETECT_TABLE_COLUMN]['DESC'][$value]];
			}
			echo '</td></TR>';
		  }
		}
	$ROW121010++;
	}
	echo '<tr><td colspan="3" style="text-align:left"><input type="submit" value="          OK           "></td></tr></form></table>';
}
