use strict;
use File::Basename;

sub PreCreateForm ()
{

   &MySetVal("State Work", "BtnBase",
             sprintf i18n("Copy (%s)"),
                          uc(&GetAttr("BaseLang")));

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

   foreach ("BtnQry")
   {
      if($_ eq &GetAttr("LastBtn"))
      {
         return;
      }
   }

   if(&GetField("id_doc"))
   {
      my $cDocBaseLang;
      my $cLastChangeBaseLang;

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

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

      &SetField("text_base_lang", $cDocBaseLang);
      &SetField("last_change_base_lang", $cLastChangeBaseLang);

      &ExportFile();
   }
   else
   {
      &SetField("text_base_lang", "");
      &FetchNextDoc();
   }
}


sub MyBtnView()
{
   my $cField = &GetField("document");

   $cField = &ScanDoc($cField);

   &OkForm("",
           (b(i18n("Preview of current entry")) .
            br() x 2 .
            table({-BORDER      => 1,
                   -CELLPADDING => 10},
                  TR(td(p({-ALIGN  => "CENTER"},
                          big(b(&GetField("description")))) .
                        hr({-NOSHADE => 1}) .
                        p({-ALIGN  => "LEFT"},
                          $cField)))) .
            br() x 2 .
            i18n("Please use the back-button of your browser")),
           "-");

   &MyExit();
}



sub MyBtnImp()
{
   my $cExportFileName = &GenExportFileName();
   my $cText = "";

   open HTML_EXP, "<$cExportFileName"
       or &Error(sprintf(i18n("Can't open %s"),
                         $cExportFileName));

   while (<HTML_EXP>)
   {
      $cText .= $_;
   }

   close HTML_EXP;

   &SetField("document", $cText);

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


sub MyBtnBase()
{

   &SetField("document", &GetField("text_base_lang"));

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


sub ExportFile()
{
   my $cExportFileName = &GenExportFileName();

   open HTML_EXP, ">$cExportFileName"
       or die sprintf(i18n("Can't open %s"),
                      $cExportFileName);

   print HTML_EXP html(body(&GetField("document")));

   close HTML_EXP;

   chmod 0666, $cExportFileName;

   &SetField("export_file", "file:$cExportFileName");

   &MySetVal("Layout Field export_file", "Value",
             basename("$cExportFileName"));

}



sub FetchNextDoc ()
{
   my $cNextIdDoc = $oDbSessionGL->SqlSelect
       ("SELECT
            do1.id_doc
         FROM
            wwwdb_doc do1,
            wwwdb_doc do2
         WHERE
            do1.lang   = ? AND
            do2.lang   = ? AND
            do1.id_doc = do2.id_doc AND
            (do2.last_change IS NULL OR
             do2.last_change < do1.last_change)",
        &GetAttr("BaseLang"),
        &GetAttr("Lang"))->[0]->[0];

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

   }
}


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


sub GenExportFileName ()
{
   my $cResult = "";
   my $cBuffer = "";

   foreach ("id_doc", "lang",
            "description")
   {
      $cBuffer .= &GetField($_) . "_";
   }

   $cBuffer =~ s/[^A-Za-z0-9_]//g;
   # $cBuffer =~ s/[aeiouAEIOU]//g;
   $cBuffer =~ s/_$//g;

   $cResult = (&GetAttr("BaseDir") .
               "/tmp/" .
               $cBuffer .
               ".html");

   return $cResult;
}


sub PostFirstColumn ()
{
   my $cResult = "";


   if(&IsAttribOK("ActualLogin") && &IsAttribOK("IsAdmin") &&
       &GetField("id_doc"))
   {
      $cResult .= (br() x 2 .
                   b(i18n("Edit:")) .
                   br().
                   li(&ResolveRefField("wwwdb://WWWdb:System:Doc;id=" .
                                       &GetField("id_doc") .
                                       "," .
                                       &GetAttr("BaseLang"),
                                       i18n("Base document"))));
   }

   return small($cResult);

}

1;

