# -----------------------------------------------------------------------------
#
#   Copyright (c) 1999-2002 by Klaus Reger <K.Reger@wwwdb.org>
#
#   You may distribute under the terms of either the GNU General Public
#   License or the Artistic License, as specified in the Perl README file.
#
#   THIS IS BETA SOFTWARE!
#
#   THIS PACKAGE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
#   IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
#   WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
#
#   $Id: CreateAppRc_1.pl,v 1.7 2002/11/18 14:58:02 k_reger Exp $
#
# -----------------------------------------------------------------------------
# Program-Data
# -----------------------------------------------------------------------------
# Project  :
# System   :
# Program  :
# Module   : $RCSfile: CreateAppRc_1.pl,v $
# Version  : $Revision: 1.7 $
# Date     : $Date: 2002/11/18 14:58:02 $
# State    : $State: Exp $
#
# Description:
#   Select a table from the selected database
#
# -----------------------------------------------------------------------------
# Change-log (See at end of this file)
# $Log: CreateAppRc_1.pl,v $
# Revision 1.7  2002/11/18 14:58:02  k_reger
# - Target-Db added
#
#
# --- Known Bugs -------------
# 
# --- TODO -------------------
# 
# ----------------------------------------------------------------------------
use vars qw($oDbTargetMGL $oSessionGL);
use strict;

$oDbTargetMGL = undef;

sub PreCreateForm ()
{
   my $cTable;
   my $oConfigFile;
   my @lTables;
   my @lUrlParams = split ";", &GetAttr("UrlParams");
   my @lDatabases;
   my $bUseSessionParam = 0;

   foreach (@lUrlParams)
   {
	   # after generation of the config-file it will be called 
	   # immediately
	   if (/^use_session$/)
	   {
		   $bUseSessionParam = 1;
	   } 
   }

   unless ($bUseSessionParam)
   {
	   $oSessionGL->setState("CreateAppRc,Database", undef);
	   &MySetVal("State Init", "BtnPrev", "-");
   }

   my $cDatabase = ($oSessionGL->getState("CreateAppRc,Database") ||
					&MyVal('DB', 'Database'));

   $oDbTargetMGL = DbSession->new
	   (&MyVal('DB', 'Driver', 'Default'),
		$cDatabase,
		$oSessionGL->getState("CreateAppRc,User"),
		$oSessionGL->getState("CreateAppRc,Password"),
		"DbHost"          => &MyVal('TargetDB', 'Host', "localhost"),
		);

   &SetField("Database", $cDatabase);

   @lTables = $oDbTargetMGL->getDbHandle()->tables();

   push @lTables, "UserFormat\\," . i18n("UserFormat");

   foreach (@lTables)
   {
	   $_ = "".$_ . "\\," . "".$_;
   }

   &MySetVal("Layout Field DbTable", "Values",
             join "\n", @lTables);

   $cTable = $oSessionGL->getState("CreateAppRc,DbTable");

   if(&GetAttr("LastBtn") ne "BtnNext")
   {
      &SetField("DbTable", $cTable);
   }

}


sub Check_AppRc_Database()
{
   &AddError("Database", "No database, please select one!!")
       if(!&GetField("Database"));
}


sub Check_AppRc_DbTable()
{
   &AddError("DbTable", "No database-table, please select one!!")
       if(!&GetField("DbTable"));
}



sub MyBtnPrev ()
{
   print &Redirect(&GetAttr("SessionId"), "WWWdb:Wiz:CreateAppRc_0");
}


sub MyBtnNext ()
{
   &CheckRecord();

   if(!&GetField("DbTable") || !&GetField("Database"))
   {
      &GenPage();
   }
   else
   {
      if (&GetField("ForceCreation"))
      {
         foreach (".rc", ".pl")
         {
            # remove the existing config-file
            unlink(&GetAttr("BaseDir") .
                   "/lib/WWWdb/tmp/" .
                   GetField("DbTable") . $_);
         }
      }

      $oSessionGL->setState("CreateAppRc,DbTable",
                            &GetField("DbTable"));
      $oSessionGL->setState("CreateAppRc,ConfigFile",
                            "/tmp/" . &GetField("DbTable") . "$$.rc");

      print &Redirect(&GetAttr("SessionId"),
                      "WWWdb:Wiz:CreateAppRc_2");
   }
}

1;
