<?php

//w CONF_INIT nalezy podac parametry do wyzwalania glownej funkcji przegladania WIEVTABLE
function USERS_COLUMN_INIT2( $INIT_TABLE, $EXTRACOLUMN, $PERM_MASK,$GENCONF='',$CONF_INIT='' ) {
global $result,$USERS_COLUMN,$USERSHIST_COLUMN,$special_tables_ignore_fields;

//@2012-10-15 sqix : sprawdzamy czy jest taka funkcja w systemie, jezeli nie ma, to sprawdzamy czy mozemy ja zaladowac, 
//jezeli dalej jej nie ma, to tworzymy taka funkcje w systemie domyslnie
		SEF($INIT_TABLE);
		if(!function_exists($INIT_TABLE)) {
		

	/*	  $filename="column_init/superedit-".$INIT_TABLE.".php";
		  	if (!$handle = fopen($localpath.$filename, 'x+')) {
		         echo " Cannot open/w file (".$filename.")";
		         exit;
		    }
	*/	    
		    $string_funkcji="   
				function ".$INIT_TABLE."() {
					// init columns if not set
				  	if (empty(\$_SESSION['".$INIT_TABLE."_COLUMN']['ID'])) {
						SEF('USERS_COLUMN_INIT2');
						USERS_COLUMN_INIT2('".$INIT_TABLE."', '".$EXTRACOLUMN."', '".$PERM_MASK."');
					}
					".$CONF_INIT."
					SEF('WIEVTABLE');
					if(isset(\$params)) WIEVTABLE(\$params);
				    else WIEVTABLE();
				}
		     ";

		$_SESSION['INIT_TABLE_FILE'][$INIT_TABLE]=$string_funkcji;
/*
		    if (fwrite($handle, $string_funkcji) === FALSE) {
		    //echo "<br>Blad w tworzeniu funkcji widoku/edycji (".$filename.").";
		        DEBUG_S(-2, "Blad w tworzeniu funkcji widoku/edycji (".$filename.")",'',__FILE__, __FUNCTION__, __LINE__);
		        exit;
		    }
*/
//		    echo "<br>Stworzono funkcje do edycji/widoku tabeli ".$filename.".";
		    DEBUG_S(2, "Stworzono funkcje do edycji/widoku tabeli: " ,$string_funkcji,__FILE__, __FUNCTION__, __LINE__);
		}  //eof if function_exists

	   SEF($INIT_TABLE."_HIST");
		if(!function_exists($INIT_TABLE."_HIST")) {
/*		  $filename="column_init/superedit-".$INIT_TABLE."_HIST.php";
		  	if (!$handle = fopen($filename, 'x+')) {
		         echo "Cannot open file (".$filename.")";
		         exit;
		    }
*/		    
		    $string_funkcji="   
				function ".$INIT_TABLE."_HIST() {
				// init columns if not set
				  	if (empty(\$_SESSION['".$INIT_TABLE."_COLUMN']['ID'])) {
						SEF('USERS_COLUMN_INIT2');
						USERS_COLUMN_INIT2('".$INIT_TABLE."', '".$EXTRACOLUMN."', '".$PERM_MASK."');
					}
				SEF('WIEVTABLE_HIST');
				WIEVTABLE_HIST();
				}
		     ";
		     $_SESSION['INIT_TABLE_FILE'][$INIT_TABLE."_HIST"]=$string_funkcji;
/*		    if (fwrite($handle, $string_funkcji) === FALSE) {
		        echo "<br>Blad w tworzeniu funkcji widoku/edycji (".$filename.").";
		        exit;
		    }

		    echo "<br>Stworzono funkcje do edycji/widoku tabeli ".$filename.".";
*/
		}  //eof if function_exists

		SEF($INIT_TABLE."_EDIT");
		if(!function_exists($INIT_TABLE."_EDIT")) {
/*		  $filename="column_init/superedit-".$INIT_TABLE."_EDIT.php";
		  	if (!$handle = fopen($filename, 'x+')) {
		         echo "Cannot open file (".$filename.")";
		         exit;
		    }
*/		    
		    $string_funkcji="  
				function ".$INIT_TABLE."_EDIT() {
				// init columns if not set
				  	if (empty(\$_SESSION['".$INIT_TABLE."_COLUMN']['ID'])) {
						SEF('USERS_COLUMN_INIT2');
						USERS_COLUMN_INIT2('".$INIT_TABLE."', '".$EXTRACOLUMN."', '".$PERM_MASK."');
					}
				SEF('EDIT_TABLE_RECORD');
				EDIT_TABLE_RECORD();
				}
		     ";
		 $_SESSION['INIT_TABLE_FILE'][$INIT_TABLE."_EDIT"]=$string_funkcji;
/*
		    if (fwrite($handle, $string_funkcji) === FALSE) {
		        echo "<br>Blad w tworzeniu funkcji widoku/edycji (".$filename.").";
		        exit;
		    }
		    echo "<br>Stworzono funkcje do edycji/widoku tabeli ".$filename.".";
*/
		}  //eof if function_exists


if(empty($GENCONF)) {  //jezeli tylko byla opcja zrobienia konfiga, to wychodzimy
	
		echo "<font size=-3>.".$INIT_TABLE."</font> ";
		
		$COLUMN = $INIT_TABLE;
		
		// clean session data
		$_SESSION[$COLUMN."_COLUMN"]['DESC'] = array();
		$_SESSION[$COLUMN."_COLUMN"]['TYPE'] = array();
		$_SESSION[$COLUMN."_COLUMN"]['ID'] = array();
		$_SESSION[$COLUMN."_COLUMN"]['DESC_TO_KEY'] = array();
		$_SESSION[$COLUMN."_COLUMN"]['HIDE'] = array();
		
		//$EXTRACOLUMN=" ";
		$EXTRACOLUMN = (!empty($EXTRACOLUMN))? explode(" ", $EXTRACOLUMN) : array();
		
		//TODO2006-03 zapytanie w foreach
		//inicjalizacja podrecznikowych zapytan do tabel dla jednej sesji
		//jezeli nie zostalo to anulowane przy logowaniu:
		if (!isset($_REQUEST['NOPREINIT'])) {
			if (isset($_SESSION[$COLUMN."_COLUMN"]['RELATIVEDB']) && isset($_SESSION[$COLUMN."_COLUMN"]['RELATIVEDB']['SQL_QUERY'][0])) {
				foreach ($_SESSION[$COLUMN."_COLUMN"]['RELATIVEDB']['SQL_QUERY'] as $value_key => $value) {
					// echo "<br> Zainicjalizowano EXTRACOLUMN zapytanie value $value key $value_key <br> ";
					ZAP_SQL($value);
				}
			}
		}
		
		ZAP_SQL("show fields from $COLUMN");
		$KEY = 0;
		
		$_SESSION[$COLUMN."_COLUMN"]['PERMEDIT']['ID'] = "R";
		$_SESSION[$COLUMN."_COLUMN"]['PERMEDIT']['A_RECORD_CREATE_DATE'] = "R";
		$_SESSION[$COLUMN."_COLUMN"]['PERMEDIT']['A_RECORD_UPDATE_DATE'] = "R";
		$_SESSION[$COLUMN."_COLUMN"]['PERMEDIT']['A_RECORD_UPDATE_AUTHOR'] = "R";
		$_SESSION[$COLUMN."_COLUMN"]['PERMEDIT']['A_RECORD_CREATE_AUTHOR'] = "R";
		
		while ($h = mysql_fetch_array($result)) {
			$_SESSION[$COLUMN."_COLUMN"]['DESC']["$KEY"] = $h[0];
			$_SESSION[$COLUMN."_COLUMN"]['TYPE'][$_SESSION[$COLUMN."_COLUMN"]['DESC']["$KEY"]] = $h[1];
			if (!isset($_SESSION[$COLUMN."_COLUMN"]['PERMEDIT'][$h[0]])) {
				$_SESSION[$COLUMN."_COLUMN"]['PERMEDIT'][$h[0]] = $PERM_MASK;
			}
			$_SESSION[$COLUMN."_COLUMN"]['DESC_TO_KEY'][$h[0]] = $KEY;
			$_SESSION[$COLUMN."_COLUMN"]['Default'][$h[0]] = $h['Default'];
			$USERS_COLUMN_INIT2['DESC_TO_KEY'][$h[0]] = $KEY; // prawdziwe kolumny
			$_SESSION[$COLUMN."_COLUMN"]['ID']["$KEY"] = $KEY;
			if(isset($_SESSION['USER_PROFILE'][$COLUMN."_COLUMN"]["$KEY"]))
			if (isset($_SESSION['USER_PROFILE'][$COLUMN."_COLUMN"]) && $_SESSION['USER_PROFILE'][$COLUMN."_COLUMN"]["$KEY"] == "HIDE") {
				$_SESSION[$COLUMN."_COLUMN"]['HIDE']["$KEY"] = 'HIDE';
			}
			$KEY++;
		}
		
		foreach ($EXTRACOLUMN as $h) {
		    if (in_array($h,$_SESSION[$COLUMN."_COLUMN"]['DESC'])) { 
		    	continue; //@2013-01-21 blad podwojnej kolumny
		    	}
			$_SESSION[$COLUMN."_COLUMN"]['DESC']["$KEY"] = $h;
			$_SESSION[$COLUMN."_COLUMN"]['DESC_TO_KEY'][$h] = $KEY;
			if (!isset($_SESSION[$COLUMN."_COLUMN"]['PERMEDIT'][$h])) {
				$_SESSION[$COLUMN."_COLUMN"]['PERMEDIT'][$h] = "N";
			}
			$_SESSION[$COLUMN."_COLUMN"]['ID']["$KEY"] = $KEY;
			$TEMP1=$_SESSION[$COLUMN."_COLUMN"]['DESC']["$KEY"];
			$_SESSION[$COLUMN."_COLUMN"]['TYPE'][$_SESSION[$COLUMN."_COLUMN"]['DESC']["$KEY"]] = "varchar(245)";
			if(isset($_SESSION['USER_PROFILE'][$COLUMN."_COLUMN"]["$KEY"]))
			if (isset($_SESSION['USER_PROFILE'][$COLUMN."_COLUMN"]) && $_SESSION['USER_PROFILE'][$COLUMN."_COLUMN"]["$KEY"] == "HIDE") {
				$_SESSION[$COLUMN."_COLUMN"]['HIDE']["$KEY"] = 'HIDE';
			}
			$KEY++;
		}

		//@2012-10-17 jezeli tabela jest SES_XXX_A to T_WHEN_CONNECTED oraz inne powinno byc TYPESPECIAL
		
		if(substr($COLUMN,0,4)=='SES_' && substr($COLUMN,-2)=='_A') {
				if(isset($_SESSION[$COLUMN."_COLUMN"]['DESC_TO_KEY']['T_WHEN_CONNECTED']))
					$_SESSION[$COLUMN."_COLUMN"]['TYPESPECIAL']['T_WHEN_CONNECTED'] = 'T_WHEN_CONNECTED';
					
				if(isset($_SESSION[$COLUMN."_COLUMN"]['DESC_TO_KEY']['A_STATUS']))
					$_SESSION[$COLUMN."_COLUMN"]['TYPESPECIAL']['A_STATUS']="A_STATUS";
					
				if(isset($_SESSION[$COLUMN."_COLUMN"]['DESC_TO_KEY']['A_STATUS_CURRENT']))
					$_SESSION[$COLUMN."_COLUMN"]['TYPESPECIAL']['A_STATUS_CURRENT']="A_STATUS_CURRENT";
					
				if(isset($_SESSION[$COLUMN."_COLUMN"]['DESC_TO_KEY']['A_ADM_COMPANY']))
					$_SESSION[$COLUMN."_COLUMN"]['TYPESPECIAL']['A_ADM_COMPANY']="A_ADM_COMPANY";
					
				if(isset($_SESSION[$COLUMN."_COLUMN"]['DESC_TO_KEY']['P_SERVICE_QUOTA_PROFILE']))
					$_SESSION[$COLUMN."_COLUMN"]['TYPESPECIAL']['P_SERVICE_QUOTA_PROFILE']="P_SERVICE_QUOTA_PROFILE";
					
				if(isset($_SESSION[$COLUMN."_COLUMN"]['DESC_TO_KEY']['P_SERVICE_QUOTA']))
					$_SESSION[$COLUMN."_COLUMN"]['TYPESPECIAL']['P_SERVICE_QUOTA']="P_SERVICE_QUOTA";
					
				if(isset($_SESSION[$COLUMN."_COLUMN"]['DESC_TO_KEY']['T_NETWORK_SERVER']))
					$_SESSION[$COLUMN."_COLUMN"]['TYPESPECIAL']['T_NETWORK_SERVER']="T_NETWORK_SERVER";
					
				if(isset($_SESSION[$COLUMN."_COLUMN"]['DESC_TO_KEY']['T_TELBOX_ADDRESS']))
					$_SESSION[$COLUMN."_COLUMN"]['TYPESPECIAL']['T_TELBOX_ADDRESS']="T_TELBOX_ADDRESS";
					
				if(isset($_SESSION[$COLUMN."_COLUMN"]['DESC_TO_KEY']['P_SERVICE']))
					$_SESSION[$COLUMN."_COLUMN"]['TYPESPECIAL']['P_SERVICE']="P_SERVICE";
					
				if(isset($_SESSION[$COLUMN."_COLUMN"]['DESC_TO_KEY']['N_AVG_TRAFFIC']))
					$_SESSION[$COLUMN."_COLUMN"]['TYPESPECIAL']['N_AVG_TRAFFIC']="N_AVG_TRAFFIC";
					
				if(isset($_SESSION[$COLUMN."_COLUMN"]['DESC_TO_KEY']['A_PROBLEM']))
					$_SESSION[$COLUMN."_COLUMN"]['TYPESPECIAL']['A_PROBLEM']="A_PROBLEM";
					
				if(isset($_SESSION[$COLUMN."_COLUMN"]['DESC_TO_KEY']['T_WHO_CONNECTED']))
					$_SESSION[$COLUMN."_COLUMN"]['TYPESPECIAL']['T_WHO_CONNECTED']="T_WHO_CONNECTED";
					
				if(isset($_SESSION[$COLUMN."_COLUMN"]['DESC_TO_KEY']['L_APPOITMENT_USER']))
					$_SESSION[$COLUMN."_COLUMN"]['TYPESPECIAL']['L_APPOITMENT_USER']="L_APPOITMENT_USER";
					
				if(isset($_SESSION[$COLUMN."_COLUMN"]['DESC_TO_KEY']['L_APPOITMENT_DATE']))
					$_SESSION[$COLUMN."_COLUMN"]['TYPESPECIAL']['L_APPOITMENT_DATE']="L_APPOITMENT_DATE";
					
				if(isset($_SESSION[$COLUMN."_COLUMN"]['DESC_TO_KEY']['A_STATUS_CURRENT']))
					$_SESSION[$COLUMN."_COLUMN"]['TYPESPECIAL']['L_APPOITMENT_PERIOD']="L_APPOITMENT_PERIOD";
					
				if(isset($_SESSION[$COLUMN."_COLUMN"]['DESC_TO_KEY']['P_DEALNUMBER']))
					$_SESSION[$COLUMN."_COLUMN"]['TYPESPECIAL']['P_DEALNUMBER']="P_DEALNUMBER";
					
				if(isset($_SESSION[$COLUMN."_COLUMN"]['DESC_TO_KEY']['S_HW']))
					$_SESSION[$COLUMN."_COLUMN"]['TYPESPECIAL']['S_HW']="S_HW";
		}
		
		
		
		
		//@2012-10-15 test czy sa odpowiednie kolumny w tabeli : 
		$USERS_COLUMN_INIT2['REQUEST_COLUMN']=array(
						'A_RECORD_CREATE_DATE'=>'varchar(30)','A_RECORD_CREATE_AUTHOR'=>'varchar(100) not null',
						'A_RECORD_UPDATE_DATE'=>'varchar(30)','A_RECORD_UPDATE_AUTHOR'=>'varchar(100) not null',
						'L_APPOITMENT_DATE'=>'varchar(30) not null',
						'L_APPOITMENT_USER'=>"varchar(100) not null",
						'L_APPOITMENT_PERIOD'=>"varchar(4) not null",
						'L_APPOITMENT_INFO'=>"varchar(255) not null",
						'L_APPOITMENT_TYPE'=>"enum('', 'ARRANGED', 'TO_ARRANGE', 'CANCELLED', 'TO_CANCEL', 'RELATE', 'WAIT')",
						'A_PROBLEM'=>"enum('', 'WARNING', 'PROBLEM', 'SERIOUS', 'UNVERIFIED')",
						'A_PROBLEM_DESC'=>'varchar(255) not null',
						'A_PROBLEM_DATE'=>'varchar(30) not null',
						'A_STATUS'=>"enum('WAITING', 'NORMAL', 'MONITOR', 'WARNING', 'OFF_SOFT', 'OFF_HARD', 'DELETED')",
						'A_STATUS_INFO'=>"varchar(255) not null",
						'A_ADM_COMPANY'=>'varchar(100) not null',
						'T_WORKPOINTS'=>"varchar(100) not null",
						'T_WORKPOINTS_VALUE'=>"varchar(100) not null",
						'T_WORKPOINTS_TYPE'=>"varchar(100) not null",
						'T_WORKPOINTS_USER'=>"varchar(100) not null",
						'T_WORKPOINTS_DATE'=>"varchar(100) not null",
						'P_ACCOUNT'=>"varchar(40) not null"
						);
						
			if(!in_array($COLUMN,$special_tables_ignore_fields))	{	//ignorowanie specjalnych kolumn	
		  foreach($USERS_COLUMN_INIT2['REQUEST_COLUMN'] as $tst_column=>$tst_type) {
		  	if(!isset($_SESSION[$COLUMN."_COLUMN"]['DESC_TO_KEY'][$tst_column])) {
			  if(!isset($ZAP_SQL_upd_struct)) settype($ZAP_SQL_upd_struct,"string");
		  	  $ZAP_SQL_upd_struct.="\n alter table ".$INIT_TABLE." add ".$tst_column." ".$tst_type."  ; " ;
		  	    	  echo "<br><font color=red>Brak Kolumny: ".$tst_column." w tabeli: ".$COLUMN." .</font>" ;
		
		  	  }
		  
		  }
		
		$COLUMN = $INIT_TABLE."_HIST";
		
		//sprawdzam czy jest tabela historyczna
		ZAP_SQL("show tables like '".$COLUMN."'");
		  if(mysql_num_rows($result)!=1) { echo "<br><font color=red>Brak tabeli historycznej!: ".$COLUMN." </font>";
			  if(!isset($ZAP_SQL_upd_struct)) settype($ZAP_SQL_upd_struct,"string");
			   $ZAP_SQL_upd_struct.="\n CREATE TABLE  `".$COLUMN."` (
					 `ID` INT( 11 ) NOT NULL AUTO_INCREMENT ,
					 `ID_USERS2` INT( 11 ) NOT NULL ,
						PRIMARY KEY (  `ID` ) ,
						INDEX (  `ID_USERS2` )
					) TYPE = MYISAM  ;";
		  } else { //jest tabela historyczna
		//   echo "<br> Jest tabela ".$COLUMN;
		
					
					ZAP_SQL("show fields from $COLUMN");
					$KEY = 0;
					while ($h = mysql_fetch_row($result)) {
						$_SESSION[$COLUMN."_COLUMN"]['DESC']["$KEY"] = $h[0];
						$_SESSION[$COLUMN."_COLUMN"]['TYPE'][$_SESSION[$COLUMN."_COLUMN"]['DESC']["$KEY"]] = $h[1];
						if (!isset($_SESSION[$COLUMN."_COLUMN"]['PERMEDIT'][$h[0]])) {
							$_SESSION[$COLUMN."_COLUMN"]['PERMEDIT'][$h[0]] = "RW";
						}
						$_SESSION[$COLUMN."_COLUMN"]['DESC_TO_KEY'][$h[0]] = $KEY;
						$_SESSION[$COLUMN."_COLUMN"]['ID']["$KEY"] = $KEY;
						if (isset($_SESSION['USER_PROFILE'][$COLUMN."_COLUMN"]) && $_SESSION['USER_PROFILE'][$COLUMN."_COLUMN"]["$KEY"] == "HIDE") {
							$_SESSION[$COLUMN."_COLUMN"]['HIDE']["$KEY"] = 'HIDE';
						}
						$KEY++;
					}
					
					
					
					
		  }
		
		  //uzupelnienie tabeli Historycznej!	
		  foreach($USERS_COLUMN_INIT2['DESC_TO_KEY'] as $tst_column=>$tst_index) {
		  	if(!isset($_SESSION[$COLUMN."_COLUMN"]['DESC_TO_KEY'][$tst_column])) {
			  if(!isset($ZAP_SQL_upd_struct)) settype($ZAP_SQL_upd_struct,"string");
		  	  $ZAP_SQL_upd_struct.="\n alter table ".$COLUMN." add ".$tst_column." varchar(255) default 'N/S;' ; " ;
		  	    	  echo "<br><font color=red>Brak Kolumny: ".$tst_column." w tabeli: ".$COLUMN." .</font>" ;
		
		  	  }
		  
		  }
		
			//czy jest odniesienie do ID_USERS2
			if(!isset($_SESSION[$COLUMN."_COLUMN"]['DESC_TO_KEY']['ID_USERS2'])) {
			   	  if(!isset($ZAP_SQL_upd_struct)) settype($ZAP_SQL_upd_struct,"string");
				  $ZAP_SQL_upd_struct.="\n alter table ".$COLUMN." add ID_USERS2 int(11) ; " ;
				  $ZAP_SQL_upd_struct.="\n alter table ".$COLUMN." add INDEX(ID_USERS2) ; " ;
					 }

					  
			} //eof if $special_tables_ignore_fields
		
		
		
			  if(isset($ZAP_SQL_upd_struct))   echo "<br><font color=red>W celu aktualizacji struktury wyzwol zapytanie:<pre> ".$ZAP_SQL_upd_struct ."</pre></font>";
		
	}	

}
