use strict;


# --- Things to do, before the HTML-form will be generated
sub PreCreateForm()
{
   my $plSelect;

   if (&GetField("id_category") and
       &GetField("id_category") < 10000)
   {
      &SetField("id_system",  1);
   }

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

   $plSelect = $oDbSessionGL->SqlSelect
          ("SELECT DISTINCT
                key_value, label
            FROM
                wwwdb_lookup
            WHERE
                lang      = ?         AND
                (key_name = 'AdmAtt' OR
                 key_name = 'UserAtt')
            ORDER BY
                 key_value",
           &GetAttr("Lang"));

   &MySetVal("Layout Field poss_attribs", "Values",
             join "\n", map
             {
                $_->[0] . "\\," . $_->[1] . " (" . $_->[0] . ")";
             } @$plSelect);

   if(&GetField("id_category"))
   {
      my $cResult;

      $cResult = &GetSortNrList();

      &MySetVal("Layout Field sort_nr", "Type", "Select");
      &MySetVal("Layout Field sort_nr", "Values", $cResult);

   }
}



sub PreBtnNew()
{
   my $iMaxCatId;

   # generate a new object-id
   $iMaxCatId = ($oDbSessionGL->SqlSelect
                 (sprintf ("SELECT MAX(id_category)
                            FROM   wwwdb_category
                            WHERE  %s",
                           &GetField("id_system")?
                           "id_category < 10000":
                           "id_category >= 10000")))->[0]->[0];

   &SetField("id_category", ($iMaxCatId?
                             $iMaxCatId + 1:
                             (&GetField("id_system")?
                              1 : 10000)));


   &SetField("id_chain", sprintf(i18n("%s - Please reorganize!"),
                                 &GetField("name")));
   &SetField("name_chain", sprintf(i18n("%s - Please reorganize!"),
                                   &GetField("description")));

}


sub PostBtnNew()
{
   if (&GetAttr("RecordOk"))
   {
      print &Redirect(&GetAttr("SessionId"),
                      sprintf("WWWdb:NavCat;id=%s",
                              &GetField("id_category")));

   }
}


sub PreBtnUpd()
{
   my $cField;

   # texts for wwwdb_category
   foreach $cField ("description", "name", "name_chain")
   {
      if(&GetField($cField . "_txt_id") > 0)
      {
         # mark all texts, so they have to be translated
         $oDbSessionGL->SqlDo
             ("UPDATE wwwdb_translation
                 SET last_change = NULL
                 WHERE id_text = ?",
              &GetField($cField . "_txt_id"));

         # make a base-language translation-record
         $oDbSessionGL->SqlDo
             ("DELETE FROM wwwdb_translation
               WHERE
                   id_text = ? AND
                   lang    = ?",
              &GetField($cField . "_txt_id"),
              &GetAttr("BaseLang"));

         $oDbSessionGL->SqlDo
             ("INSERT INTO wwwdb_translation
                 (id_text, lang, trans_text, last_change)
               VALUES
                 (?, ?, ?, ?)",
              &GetField($cField . "_txt_id"),
              &GetAttr("BaseLang"),
              &GetField($cField),
              $oDbSessionGL->DateToDb(scalar(localtime)));

      }   }
}


sub PreBtnDel()
{
   my $cField;

   # texts for wwwdb_category
   foreach $cField ("description", "name", "name_chain")
   {
      if(&GetField($cField . "_txt_id") > 0)
      {
         # until this text is translated, the original text will be used
         $oDbSessionGL->SqlDo
             ("DELETE FROM wwwdb_translation
                  WHERE id_text = ?",
              &GetField($cField . "_txt_id"));
      }
   }
}


sub Check_wwwdb_category ()
{
   # Set time-stamp
   &SetField("last_change",
             $oDbSessionGL->DateToDb(scalar(localtime())));

}


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

   $cResult .= (br() x 2 .
                b(i18n("Don't forget:")) .
                li(&ResolveRefField("wwwdb://WWWdb:System:TransGenRecords",
                                    i18n("Transl.-Generation"))) .
                li(&ResolveRefField("wwwdb://WWWdb:NavCatReorg",
                                    i18n("Reorganisation"))));

   $cResult .= (br() x 2 .
                b(i18n("See also:")) .
                li(&ResolveRefField("wwwdb://WWWdb:NavObj",
                                    i18n("Edit Objects"))));

   return small($cResult);

}


sub BtnCopyAttribs ()
{
   my $cPossAttribs = &GetField("poss_attribs");

   $cPossAttribs =~ s/\\,/ && /g;
   &SetField("attribs", $cPossAttribs);

   if (&GetField("id_category"))
   {
      &MyParam("WWWdbState", "Work");
   }
   else
   {
      &MyParam("WWWdbState", "Init");
   }

   &GenPage();

}




sub GetSortNrList($)
{
   my %hSortNr  = ();
   my $plSelect;
   my $cResult = "";

#          $plSelect = $oDbSessionGL->SqlSelect
#              ("SELECT
#                    t0.id_object,
#                    t4.id_object,
#                    t4.descript,
#                    t4.sort_nr,
#                    t5.lang,
#                    t5.trans_text
#                FROM
#                    wwwdb_object      t0,
#                    wwwdb_obj_cat     t1,
#                    wwwdb_category    t2,
#                    wwwdb_obj_cat     t3,
#                    wwwdb_object      t4,
#                    wwwdb_translation t5
#                WHERE
#                    (t0.id_object    = t1.id_object)       AND
#                    (t1.id_category  = t2.id_category)     AND
#                    (t2.id_category  = t3.id_category)     AND
#                    (t3.id_object    = t4.id_object)       AND
#                    (t5.id_text      = t4.descript_txt_id) AND
#                    (t0.id_object    = ?)                  AND
#                    (t5.lang         = ?)
#                ORDER BY
#                    t4.sort_nr asc",
#               &GetField("id_object"),
#               &GetAttr("Lang"));

   $oDbSessionGL->setDebugLvl(10);
   $plSelect = $oDbSessionGL->SqlSelect
         ("SELECT
               t2.id_category,
               t2.description,
               t2.sort_nr,
               t3.trans_text
           FROM
               wwwdb_category t0,
               wwwdb_category t2,
               wwwdb_translation t3
           WHERE
               (t3.id_text       = t2.description_txt_id) AND
               (t0.id_cat_of_cat = t2.id_cat_of_cat)      AND
               (t0.id_category   = ?)                     AND
               (t3.lang          = ?)",
          &GetField("id_category"),
          &GetAttr("Lang"));

   {
      my $iLastSortNr = 0;

      foreach (@$plSelect)
      {
         if($_->[2] eq "" || $_->[2] == 0 || defined $hSortNr{$_->[2]})
         {
            # currently do nothing
         }
         else
         {
            $hSortNr{$_->[2]} = (sprintf("%05d: ", $_->[2]) .
                                 ($_->[3] eq "?"?
                                  $_->[1]:
                                  $_->[3]));
         }
      }

      foreach (sort {$a <=> $b}
               keys %hSortNr)
      {
         my $iNewKey = int(($iLastSortNr + $_) / 2);

         $iLastSortNr = $_;

         $cResult .= sprintf ("%s\\,%s\n" x 2,
                              $iNewKey,
                              sprintf("%05d: ...", $iNewKey),
                              $_,
                              $hSortNr{$_});
      }

      $cResult .= sprintf ("%s\\,%s\n",
                              ($iLastSortNr + 10),
                              sprintf("%05d: ...", ($iLastSortNr + 10)));

   }

   return $cResult;
}

1;

