<?
#
# Prliminaires PHP
#
	Require ( "Common.Inc" );
	Require ( "Com_Odbc.Inc" );

	# Valorisation par dfaut de certains champs  partir de NABAB
	if( ! IsSet( $Style ) ) $Style = 1;
	if( ! IsSet( $Fond ) ) $Fond = "{KEY_HTM_BMPREP}{KEY_HTM_BMPFON}";
	if( ! IsSet( $WinTitle ) ) $WinTitle = "{KEY_GEN_APPNAM} : $REQUEST_URI";
	if( ! IsSet( $Logo ) ) $Logo = "{KEY_HTM_BMPREP}{KEY_HTM_BMPLOG}";
	if( ! IsSet( $Title ) ) $Title = "{KEY_GEN_APPDSC}";
	if( ! IsSet( $FootLine ) ) $FootLine = "<SMALL><SMALL>Application {KEY_GEN_APPNAM}, Vers {KEY_GEN_APPVER}, cr par {KEY_GEN_APPAUT}, dern. maj. le {KEY_GEN_APPDAT}.</SMALL></SMALL>";
	#
	# Page HTML
	#
	Echo_Common_Header( $Style, $Fond, $WinTitle );
	Echo_LogoToMenuV_Cell( $Style, $Logo );

	# Rcuprer infos sur colonnes
	if ( isset( $Section ) ) $arr_cols = SQL_GetColInf( $Section, "", "ALL" );
	# Mise  jour de donnes ?
	if ( $Confirm == "OK" && isset( $arr_cols ) && isset( $Section ) )
	{
		# Construction chaine des paramtres type prm1=val1&prm2=val2...
		$prm_str = $REDIRECT_QUERY_STRING;
		# Ajout des infos postes par le formulaire
		while ( list( $k, $v ) = each( $arr_cols ) )
		{
			$a_str = Split( "{KEY_SQL_FILSEP}", $v, 16 );
			$col_nam = $a_str[{KEY_POS_COLNAM}];
			if ( isset( $HTTP_POST_VARS[$col_nam] ) ) 
				$prm_str = $prm_str . "&" . $col_nam . "=" . $HTTP_POST_VARS[$col_nam];
		}
		# Excution procdure stockes ou sql dynamique
		$sql_string = "";
		unset( $sql_sts );
		if ( $Action == "Ins" ) {
			$sql_string = Build_SQLInsCmd ( $Section, $prm_str, $arr_cols );
			if ( Exec_SqlProc ( "{KEY_SQL_CNXSTR}", "{KEY_SQL_CNXUSR}", "{KEY_SQL_CNXPWD}", $sql_string ) == true ) 
				$sql_sts = "  <I>Dernire cration OK.</I>\n";
			else
				$sql_sts = "  <I>ERREUR lors de la dernire insertion !</I>";
		} elseif ( $Action == "Upd" ) {
			$sql_string = Build_SQLUpdCmd ( $Section, $prm_str, $arr_cols );
			if ( Exec_SqlProc ( "{KEY_SQL_CNXSTR}", "{KEY_SQL_CNXUSR}", "{KEY_SQL_CNXPWD}", $sql_string ) == true ) 
				$sql_sts = "  <I>Dernire mise  jour OK.</I>\n";
			else
				$sql_sts = "  <I>ERREUR lors de la dernire mise  jour !</I>";
		} elseif ( $Action == "Del" ) {			
			$sql_string = Build_SQLDelCmd ( $Section, $prm_str, $arr_cols );
			$sql_string = "Delete From $Section Where rowid IN ( ";
			$rowid_str = "";
			while( list( $k, $v ) = each( $chk_delete ) ) 
			{
				if ( $rowid_str != "" ) $rowid_str .= "', '";
				$rowid_str .= $v;
			}
			$sql_string = $sql_string . ( ( $rowid_str != "" ) ? "'$rowid_str'" : "" ) . " )";
			if ( Exec_SqlProc ( "{KEY_SQL_CNXSTR}", "{KEY_SQL_CNXUSR}", "{KEY_SQL_CNXPWD}", $sql_string ) == true ) {
				$sql_sts = "  <I>Dernire suppression OK.</I>\n";
			} else {
				$sql_sts = "  <I>ERREUR lors de la dernire suppression !</I>";
			}
		}
		# Repasser la page en mode liste
		$Action = "Lst";
	}

	Echo_MenuVToMenuH_Cell( $Style );
	Echo_MenuHToPath_Cell( $Style, $Title );

	# Chemin d'accs et de navigation fonction de l'action et du rfrentiel en cours
	if ( isset( $Section ) ) 
	{
		$ref_str = Sql_GetTabInf( $Section, "" );
		$ref_arr = explode( "{KEY_SQL_FILSEP}", $ref_str[0] );
		$ref_lib = $ref_arr[{KEY_POS_TABDSC}];
		$ref_libplu = $ref_arr[{KEY_POS_TABDSS}];
		unset( $ref_arr );
		$ref_basurl = "<A href=\"Ref{KEY_ACT_FILLST}{KEY_PAG_FILEXT}\">Rfrentiel</A>";
		$ref_lsturl = "<A href=\"Ref{KEY_ACT_FILLST}{KEY_PAG_FILEXT}?Section=$Section&Action=Lst\">&nbsp;". ucfirst( $ref_libplu ) . "</A>";
		if ( $Action == "Lst" )
	                $ref_path = $ref_basurl . " ><B>".ucfirst( $ref_libplu ) ."</B>";
		elseif ( $Action == "Ins" )
	                $ref_path = $ref_basurl . " > " . $ref_lsturl . " > <B>Cration</B>";
		elseif ( $Action == "Upd" )
	                $ref_path = $ref_basurl . " > " . $ref_lsturl . " > <B>Modification</B>";
		elseif ( $Action == "Del" )
	                $ref_path = $ref_basurl . " > " . $ref_lsturl . " > <B>Suppression</B>";
		if ( isset( $sql_sts ) ) $ref_path  = $ref_path . " : <I>" . $sql_sts . "</I>";
		$ref_path .= "<HR>";
	}
	# Message d'erreur ventuel
	echo( "$ref_path\n" );

	Echo_PathToData_Cell( $Style );

	# Contenu de la page de liste du rfrentiel
	if ( ! isset( $Section ) ) 
	{
		echo( "<P>Le rfrentiel permet de paramtrer le comportement de votre application. \n".
			"Il hberge en gnral des paramtres <B>fonctionnels</B> (taux d'un calcul, date d'un inventaire...), \n".
			"des paramtres <B>techniques</B> (nom du navigateur, couleurs de fond...), \n".
			"ou des paramtres <B>publics</B> (civilits, noms des mois de l'anne... ).\n" );
		echo( "<P><B>Activez un des liens du menu pour afficher les donnes du rfrentiel correspondant.</B><DIR>\n" );
		# Ajout d'un lien par objet du rfrentiel en scannant le fichier texte (pour l'exemple, mais on peut aussi utiliser Sql_GetTabInf( "", "REF" )
		$arr_sqltab = File( "{KEY_SQL_FILTAB}" );
		# Pour chaque ligne du fichier des tables
		while ( ( count ( $arr_sqltab ) > 0 ) && ( list( $key, $val ) = each( $arr_sqltab ) ) )
		{
			# Si ce n'est pas une ligne de commentaire
			if ( substr( ltrim( $val ), 1, 1 ) == "{KEY_SQL_FILCMT}" ) continue;
			# Sparer les composants de la chane d'info sur une table
			$arr_str = split( "{KEY_SQL_FILSEP}", $val, 10 );
			# Si il s'agit d'un objet de rfrence
			if ( StrToUpper( $arr_str[{KEY_POS_TABTYP}] ) == "REF" ) echo( "<LI>Vos <A href=\"Ref{KEY_ACT_FILLST}{KEY_PAG_FILEXT}?Section=$arr_str[{KEY_POS_TABNAM}]&Action=Lst\"><B>".ucfirst( $arr_str[{KEY_POS_TABDSS}] )."</B></A>" );
			unset( $arr_str );
		}
		unset ( $arr_sqltab );
		echo( "</UL></DIR><BR>\n" );
	} else {
		# Gestion de la liste
		if ( $Action == "Lst" )
		{
			echo( "<BR>Cette <B>liste</B> contient les $ref_libplu rfrencs dans l'application.<P>\n" );
			echo( "<FORM method=\"POST\" action=\"Ref{KEY_ACT_FILLST}{KEY_PAG_FILEXT}?Section=$Section&Action=Del\">\n" ); 
			# Table des lignes de rfrence
			echo( "<TABLE cellspacing=3 cellpadding=3 width=100%>\n" );
			# Tableau des informations sur les colonnes
			$sql_header = Build_SQLListHeader( $arr_cols );
			# En ttes de colonnes
			echo( "<TR bgcolor=\"{KEY_COL_LSTHDR}\"><TD width=\"5%\" align=\"CENTER\"><B>X</B></TD><TD width=\"5%\" align=\"CENTER\"><B>#</B></TD>$sql_header</TR>\n" );
			# Requte SQL de remplissage
			$SQLString = "Select '<TR><TD align=\"CENTER\"><INPUT type=\"CHECKBOX\" name=\"chk_delete[]\" value=\"' || rowid || '\"></TD>' || ";
			$SQLString .= "'<TD align=\"CENTER\"><A href=\"Ref{KEY_ACT_FILLST}{KEY_PAG_FILEXT}?Section=".$Section."&Action=Upd&rowid=' || rowid || '\">' || rownum || '</A></TD>' || ";
			$arr_cols_names = ColSplit( "{KEY_SQL_FILSEP}", $arr_cols, {KEY_POS_COLNAM} );
			$SQLString = SListEx( $SQLString, "'<TD>' || ", $arr_cols_names, " || '</TD>' || ", "'</TR>' From $Section" );
			unset( $arr_cols_names );
			# Excution de la requte
			Echo_OneColSqlRes ( "{KEY_SQL_CNXSTR}", "{KEY_SQL_CNXUSR}", "{KEY_SQL_CNXPWD}", $SQLString, "" );
			echo( "</TABLE><P>\n" );
			echo( "<A href=\"Ref{KEY_ACT_FILLST}{KEY_PAG_FILEXT}?Section=$Section&Action=Ins\">Nouveau $ref_lib</A>&nbsp;&nbsp;&nbsp;&nbsp;\n" );
			echo( "<INPUT type=\"SUBMIT\" value=\"Supprimer\">\n" );
		# Gestion de la page d'insertion
		} elseif ( $Action == "Ins" ) {
			$sql_formulaire = Build_SQLForm( $arr_cols, $REDIRECT_QUERY_STRING, $Action, "" );
			echo( "<BR><B>Saisissez</B> les caractristiques du $ref_lib  crer et pressez sur Valider.<P>\n" );
			echo( "<FORM method=\"POST\" action=\"Ref{KEY_ACT_FILLST}{KEY_PAG_FILEXT}?Section=$Section&Action=Ins&Confirm=OK\">\n" );
			echo( "<TABLE cellspacing=2 cellpadding=2 border=0 width=\"100%\">\n" );
			echo( "$sql_formulaire" );
			echo( "  <TR><TD><BR></TD></TR><TR><TD>&nbsp;</TD><TD>\n" );
			echo( "<INPUT type=\"SUBMIT\" value=\"Valider\">&nbsp;&nbsp;\n" );
			echo( "<INPUT type=\"RESET\" value=\"Rinitialiser\"></TD></TR>\n" );
			echo( "</TABLE>\n" );
		# Gestion de la page de mise  jour
		} elseif ( $Action == "Upd" ) {
			$sql_det = Exec_SqlSelectOne( "{KEY_SQL_CNXSTR}", "{KEY_SQL_CNXUSR}", "{KEY_SQL_CNXPWD}", "Select * From $Section Where rowid='$rowid' and rownum = 1" );
			$sql_formulaire = Build_SQLForm( $arr_cols, $REDIRECT_QUERY_STRING . $sql_det, $Action, "" );
			echo( "<BR><B>Modifiez</B> les caractristiques du $ref_lib ci-dessous et pressez sur Valider.<P>\n" );
			echo( "<FORM method=\"POST\" action=\"Ref{KEY_ACT_FILLST}{KEY_PAG_FILEXT}?Section=$Section&Action=Upd&Confirm=OK\">\n" );
			echo( "<TABLE cellspacing=2 cellpadding=2 border=0 width=\"100%\">\n" );
			echo( "$sql_formulaire" );
			echo( "<TR><TD><BR></TD></TR><TR><TD>&nbsp;</TD><TD>\n" );
			echo( "<INPUT type=\"SUBMIT\" value=\"Valider\">&nbsp;&nbsp;\n" );
			echo( "<INPUT type=\"RESET\" value=\"Rinitialiser\"></TD></TR>\n" );
			echo( "</TABLE>\n" );
		# Gestion de la page de suppression
		} elseif ( $Action == "Del" ) {
			echo( "<BR>Veuillez <B>confirmer la suppression</B> des $ref_libplu ci-dessous.<P>\n" );
			$FormAction = "Ref{KEY_ACT_FILLST}{KEY_PAG_FILEXT}?Section=$Section&Action=Del&Confirm=OK";
			$FormCodes = "";
			$nb_prm = 0;
			while( list( $k, $v ) = each( $chk_delete ) ) {
				$FormCodes .= ( ( $k == 0 ) ? "$v" : ", $v" );
				$FormAction .= ( "&chk_delete[$nb_prm]=" . $v );
				$nb_prm++;
			}
			echo( "<FORM method=\"POST\" action=\"$FormAction\">\n" );
			echo( "<TABLE cellspacing=2 cellpadding=2 border=0 width=\"100%\">\n" );
			echo( $sql_formulaire );
			echo( "<TR><TD width=\"20%\">Codes :</TD><TD>$FormCodes</TD></TR>" );
			echo( "<TR><TD><BR></TD></TR><TR><TD>&nbsp;</TD><TD><INPUT type=\"SUBMIT\" value=\"Valider\"></TD></TR>\n" );
			echo( "</TABLE>\n" );
		}
	        echo( "</FORM>\n" );
	}
	if ( isset( $arr_cols ) ) unset( $arr_cols );

	Echo_DataToFoot_Cell( $Style );
	Echo_Common_Footer( $Style, $FootLine );
?>