# use strict;

# ----------------------------------------------------------------
# Plugin for PreCreateForm()
sub PreCreateForm()
{
   # ensure, that non admins get no access
   if(!$oSessionGL->getState("ActualLogin") ||
      !$oSessionGL->getState("IsAdmin"))
   {
      &OkForm("ERROR",
              sprintf(i18n("Sorry, please %s first as WWWdb-admin!"),
                      "<A HREF=" .
                      &CreateReference(&GetAttr("SessionId"),
                                       "Login") .
                      ">" .
                      i18n("login") .
                      "</A>"),
              "BtnExit");
      &MyExit();
   }

   &SetField("CmdChecked", 0);
   return;
}



# ----------------------------------------------------------------
# Plugin for Check_UserFormat_SqlCommand()
sub Check_UserFormat_SqlCommand()
{

   return;
}



# ----------------------------------------------------------------
# Plugin for MyBtnNew()
sub MyBtnNew()
{
   my $cResult = "";
   my $lRecord;
   my $pStmtHdl;
   my @lFields;
   my $cField;
   my $cSqlCmd = &GetField("SqlCommand");
   my $cSqlError = "";
   my $iInd = 0;

   unless(&GetAttr("RecordOk"))
   {
      &GenPage();
      &MyExit();
   }
   $lRecord = eval
   {
      my $pDbHdl = $oDbSessionGL->{"DbHandle"};

      local $SIG{'__DIE__'};
      local $SIG{'__WARN__'};

      $pStmtHdl = $pDbHdl->prepare($cSqlCmd) or
          die(sprintf(i18n("Prepare: [%s] %s"),
                      $pDbHdl->state(),
                      $pDbHdl->errstr()));

      print STDERR "SSSSS 1.0\n";
      unless ($cResult)
      {
         $pStmtHdl->execute() or
             die(sprintf(i18n("Execute: [%s] %s"),
                         $pDbHdl->state(),
                         $pDbHdl->errstr()));

         $cResult = $pStmtHdl->fetchall_arrayref();
      }
      print STDERR "SSSSS 2.0\n";

      if ($cResult eq "0E0")
      {
         $cResult = [[undef]];
      }
      print STDERR "SSSSS 3.0\n";

      $cResult;

   };

   if ($DBI::errstr)
   {
      &Error(b(i18n("Invalid SQL-statement, please re-enter.")) . p() .
             ($DBI::errstr?
              (b(i18n("The SQL-error was: ")) . p() .
               sprintf(i18n("[%s] %s"),
                       $DBI::state,
                       i($DBI::errstr)) . p()):
              ""));
      &MyExit();
   }

   if ($oDbSessionGL->{"DbDriver"} ne "mysql")
   {
      $cResult = "<TR>";

      # append field-names
      foreach (@{$pStmtHdl->{NAME}})
      {
         $cResult .= th({BGCOLOR =>  "#cccccc"}, $_);
      }
      $cResult .= "</TR>";
   }

   # generate them
   foreach (@$lRecord)
   {
      my $lFields;

      @lFields = @{$_};

      $cResult .= "<TR>";

      foreach $cField (@lFields)
      {
         $cResult .= td({BGCOLOR => ($iInd % 2)? "#dddddd": "#eeeeee"},
                        $cField? $cField: "NULL");

      }

      $cResult .= "</TR>";
      $iInd ++;
   }

   $cResult = table({BORDER => 0},
                    $cResult);

   &OkForm("",
           b(i18n("The result of your SQL-command is:")) . p() .
           $cResult);
   &MyExit();

   return;
}



1;
