# defined Plugins:
#
# --- Things, to do, before WWWdb does any Action
# PreDoAction()
#
# --- Things to do, before the HTML-form will be generated
# PreCreateForm()
#
# --- Check-Routine for every field of the Table <Table>
# Check_<Table>_EveryField()
#
# --- Check-Routine for field <Field> of the Table <Table>
# Check_<Table>_<Field>()
#
# --- Check-Routine for the Table <Table> after all single-checks
# Check_<Table>()
#
# --- Conversion for fieldtype <FieldType> of db <Db> coming from form
# <Db>_<FieldType>_Form2Db()
#
# --- Conversion for fieldtype <FieldType> of db <Db> coming from db
# <Db>_<FieldType>_Db2Form()
#
# --- Do action for Button <Btn>
# (Pre|Post|My)Btn<Btn>()
#
# --- Set up the navigation-column on the left side
# (Pre|Post|My)FirstColumn()
#
# --- Generate the header of the form
# (Pre|Post|My)FormHeader()
#
# --- Generate the header of the table
# (Pre|Post|My)TableHeader($)
#
# --- Generate the footer of the table
# (Pre|Post|My)TableFooter()
#
# --- Generate the footer of the page
# (Pre|Post|My)FormFooter()


sub MyBtnNew()
{
   &ReorgCategories();

   &OkForm("",
           i18n("The categories in WWWdb were successfully reorganized"));

   &MyExit();

}


sub ReorgCategories()
{
   my $iCat;
   my $cCat;
   my $plCat;
   my $plRecord;
   my  @lLang;

   $plRecord = $oDbSessionGL->SqlSelect
       ("SELECT
            key_value
         FROM
            wwwdb_lookup
         WHERE
            lang     = ? AND
            key_name = 'lang_select'",
        &GetAttr("BaseLang"));

   # remember only the language (in key_value)
   @lLang = map {$_->[0]} @$plRecord;

   $plCat = $oDbSessionGL->SqlSelect
       ("SELECT
             id_category,
             name_chain_txt_id
         FROM
             wwwdb_category
         WHERE
             id_category > 1");

   foreach $iCat (@{$plCat})
   {
      my $cIdChain        = "";
      my $cLang;
      my $cNameChain      = "";
      my $iNameChainTxtId = $iCat->[1];
      my $iWorkCat        = $iCat->[0];
      my %hNameChain      = ();

      # select the parent of the actual category and generate path
      while (1)
      {
         my $lFields;
         my $cEntry;

         # get parent-category
         $lFields = $oDbSessionGL->SqlSelect
             ("SELECT
                   ca0.id_category,
                   ca0.id_cat_of_cat,
                   ca0.name,
                   ca0.name_txt_id
               FROM
                   wwwdb_category ca0,
                   wwwdb_category ca1
               WHERE
                   ca1.id_category = ca0.id_cat_of_cat AND
                   ca0.id_category = ?",
              $iWorkCat)->[0];

         last
             unless $lFields;

         # Point to parent
         $iWorkCat   = $lFields->[1];

         # concat the path
         $cIdChain   = $lFields->[0] . "," . $cIdChain;
         $cNameChain = $lFields->[2] . "," . $cNameChain;

         foreach $cLang (@lLang)
         {
            my $cTransText;

            $cTransText = $oDbSessionGL->SqlSelect
                ("SELECT trans_text
                  FROM   wwwdb_translation
                  WHERE
                      id_text = ? AND
                      lang    = ?",
                 $lFields->[3],
                 $cLang)->[0]->[0];

            $hNameChain{$cLang} = ($cTransText . "," .
                                   $hNameChain{$cLang});
         }

      } # while (1)

      # remove trailing ,
      $cIdChain =~ s:,$::;
      $cNameChain =~ s:,$::;
      $iWorkCat = $iCat->[0];

      $oDbSessionGL->SqlDo
          ("UPDATE wwwdb_category
            SET
              id_chain   = ?,
              name_chain = ?
            WHERE
              id_category = ?",
           $cIdChain,
           $cNameChain,
           $iWorkCat);

      foreach $cLang (@lLang)
      {
         my $cTransText;

         # remove trailing ,
         $hNameChain{$cLang} =~ s:,$::;
         $oDbSessionGL->SqlDo
             ("UPDATE wwwdb_translation
               SET trans_text  = ?,
                   last_change = ?
               WHERE
                   id_text     = ? AND
                   lang        = ?",
              $hNameChain{$cLang},
              $oDbSessionGL->DateToDb(scalar(localtime())),
              $iNameChainTxtId,
              $cLang)
                 if $iNameChainTxtId && $hNameChain{$cLang};
      }
   } # foreach $iCat (@lCat)


   $plCat = $oDbSessionGL->SqlSelect
       ("SELECT id_category, name_chain
         FROM wwwdb_category");

   foreach $cCat (@$plCat)
   {
      my $iNrOfSubcats;
      my $iNrOfObjects;

      @lFields = @$cCat;

      $iNrOfSubcats = ($oDbSessionGL->SqlSelect
                       ("SELECT COUNT(*) AS NR_OF_SUBCAT
                         FROM   wwwdb_category
                         WHERE  name_chain LIKE ".
                        $oDbSessionGL->getDbHandle()->quote
                        ($lFields[1] . "%" . "")))->[0]->[0];

      $iNrOfObjects = ($oDbSessionGL->SqlSelect
                       ("SELECT COUNT(*) AS NR_OF_OBJ
                         FROM
                           wwwdb_category c,
                           wwwdb_obj_cat  oc
                         WHERE
                           c.id_category = oc.id_category AND
                           c.name_chain LIKE ?",
                        $lFields[1] . "%"))->[0]->[0];

      $oDbSessionGL->SqlDo
          ("UPDATE wwwdb_category
            SET
              nr_of_subcats = ?,
              nr_of_objs    = ?
            WHERE
              id_category = ?",
           $iNrOfSubcats - 1,
           $iNrOfObjects,
           $lFields[0]);
   }
}



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

   $cResult .= (br() x 2 .
                b(i18n("Don't forget:")) .
                li(&ResolveRefField("wwwdb://WWWdb:System:TransGenRecords",
                                    i18n("Transl.-generation"))));
   $cResult .= (br() x 2 .
                b(i18n("See also:")) .
                li(&ResolveRefField("wwwdb://WWWdb:NavCat",
                                    i18n("Edit categories"))) .
                li(&ResolveRefField("wwwdb://WWWdb:NavObj",
                                    i18n("Edit objects"))));

   return small($cResult);

}



1;
