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 lookup-record in the base-language from db
      if (&GetField("id_lookup"))
      {
         my $cLabelBaseLang;
         my $cLastChangeBaseLang;

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

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

         &SetField("label_base_lang", "[$cLabelBaseLang]");
         &SetField("last_change_base_lang", $cLastChangeBaseLang);

         &SetField("label", "")
             if (&GetAttr("Lang") ne &GetAttr("BaseLang") &&
                 !&GetField("last_change"));

      }
      else
      {
         &SetField("label_base_lang", "");
         &FetchNextLookup();
      }
   }
}



sub FetchNextLookup ()
{

   # select every record without timestamp or where
   # the timestamp of the base-language is newer
   # than in the current language
   my $cNextIdLookup = $oDbSessionGL->SqlSelect
       ("SELECT
            lo1.id_lookup
         FROM
            wwwdb_lookup lo1,
            wwwdb_lookup lo2
         WHERE
            lo1.lang      = ?             AND
            lo2.lang      = ?             AND
            lo1.id_lookup = lo2.id_lookup AND
            (lo2.last_change IS NULL OR
             lo2.last_change < lo1.last_change)",
        &GetAttr("BaseLang"),
        &GetAttr("Lang"))->[0]->[0];

   if(defined $cNextIdLookup)
   {
      print &Redirect(&GetAttr("SessionId"),
                      (&GetAttr("ConfigFile") .
                       ";id=" .
                       $cNextIdLookup .
                       "," .
                       (&GetAttr("Lang"))));
      &MyExit();
   }
   else
   {
      &SetField("label_base_lang",
                i18n("<No Lookup-entry to translate>"));
   }
}


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


1;

