use strict;

sub PreCreateForm ()
{
   my $cLastBtn   = &GetAttr("LastBtn");

   &SetField("lang", &GetAttr("Lang"));
   &SetField("base_lang", &GetAttr("BaseLang"));


   # in QRY-mode leave everything untouched
   unless ($cLastBtn eq "BtnQry")
   {
      # read the search-word from the db
      if (&GetField("id_sword"))
      {
         my $cSwordBaseLang;
         my $cLastChangeBaseLang;

         my $plResult = $oDbSessionGL->SqlSelect
             ("SELECT
                   sword,
                   last_change
               FROM
                   wwwdb_sword
               WHERE
                   id_sword  = ? AND
                   lang      = ?",
              &GetField("id_sword"),
              &GetAttr("BaseLang"))->[0];

         ($cSwordBaseLang,
          $cLastChangeBaseLang) = @$plResult;

         &SetField("sword_base_lang", $cSwordBaseLang);
         &SetField("last_change_base_lang", $cLastChangeBaseLang);

         &SetField("sword", "")
             if (((&GetAttr("Lang") ne &GetAttr("BaseLang")) &&
                  (&GetField("sword") eq &SetField("sword_base_lang"))) ||
                 (!&GetField("last_change")));
      }
      else
      {
         &SetField("sword_base_lang", "");
         &FetchNextSword();
      }
   }
}


# find out, which is the next search-word, we have to translate
sub FetchNextSword ()
{
   # select every record without timestamp or where
   # the timestamp of the base-language is newer
   # than in the current language
   my $cNextIdSword = $oDbSessionGL->SqlSelect
       ("SELECT
            sw1.id_sword
         FROM
            wwwdb_sword sw1,
            wwwdb_sword sw2
         WHERE
            sw1.lang     = ? AND
            sw2.lang     = ? AND
            sw1.id_sword = sw2.id_sword AND
            (sw2.last_change IS NULL OR
             sw2.last_change < sw1.last_change)",
        &GetAttr("BaseLang"),
        &GetAttr("Lang"))->[0]->[0];

   if(defined $cNextIdSword)
   {
      print &Redirect(&GetAttr("SessionId"),
                      (&GetAttr("ConfigFile") .
                       ";id=" .
                       $cNextIdSword .
                       "," .
                       (&GetAttr("Lang"))));
      &MyExit();
   }
   else
   {
      &SetField("sword_base_lang",
                i18n("<No Search-word to translate>"));

   }
}

sub Check_wwwdb_sword()
{
   &SetField("sword",
             lc(&GetField("sword")));
}


sub MyBtnCopy()
{
   my $cSWord = &GetField("sword_base_lang");

   &SetField("sword", $cSWord);

   &MyParam("WWWdbState", "Work");
   &GenPage();
}


sub PreBtnUpd()
{
   # Set time-stamp
   &SetField("last_change",
             (&GetField("last_change_base_lang")?
              &GetField("last_change_base_lang"):
              $oDbSessionGL->DateToDb(scalar(localtime()))));
}



1;

