
sub PreDoAction ()
{
   my $cUrlParams = &GetAttr("UrlParams");
   my $cLastBtn   = &GetAttr("LastBtn");
   my @lUrlParams = split ";", $cUrlParams;
   my $iCat       = &GetField("id_category");

   # the url-param overloads the value of the form
   foreach (@lUrlParams)
   {

      $iCat = $1
          if (/^cat=(\d+)/);

      $cSWord = $1
          if (/^sword=([^\s]+)/);
   }

   if ($iCat)
   {
      &SetField("id_category", $iCat);
   }
   else
   {
      &SetField("id_category", 1)
          if !&GetField("id_category");
   }

   # set search-word
   if ($cSWord)
   {
      &SetField("sword", $cSWord);
      &BtnSearch();
   }

   &MyParam('$valuesplit', "[ \t]");
   &MyParam('$valueconj', "or");

   # &SetField("search_cond", &MyParam('$valueconj'));
}



sub PreCreateForm ()
{
   my $cList;
   my @lHistory;
   my $iCategory;
   my $cLastBtn   = &GetAttr("LastBtn");
   my $bNoResult  = 1;

   $iCategory = &GetField("id_category");

   &SetField("search_lang", &GetAttr("Lang"));

   # Generate a path upwards
   $cList = "";

   # get category-info
   $cEntry = $oDbSessionGL->SqlSelect
       ("SELECT
          cat.id_category,
          cat.id_cat_of_cat,
          cat.name,
          cat.id_chain,
          cat.name_chain,
          cat.sort_nr,
          cat.attribs,
          tr1.trans_text   AS name_tra,
          tr2.trans_text   AS name_chain_tra,
          tr3.trans_text   AS tra_description
        FROM
          wwwdb_category    cat,
          wwwdb_translation tr1,
          wwwdb_translation tr2,
          wwwdb_translation tr3
        WHERE
          cat.id_category        = ?           AND
          cat.name_txt_id        = tr1.id_text AND
          cat.name_chain_txt_id  = tr2.id_text AND
          cat.description_txt_id = tr3.id_text AND
          tr1.lang               = ?           AND
          tr2.lang               = tr1.lang    AND
          tr3.lang               = tr1.lang
        ORDER BY
          sort_nr",
        $iCategory,
        &GetAttr("Lang"))->[0];

   @lFields = @$cEntry;

   @lIdChain = split /,/, $lFields[3];
   @lIdNames = split /,/, ($lFields[8] eq "?" ?
                           $lFields[4]:
                           $lFields[8]);

   # generate the access-path with the corresponding links
   for ($iInd = @lIdChain - 1; $iInd >= 0; $iInd--)
   {

      $cResult = &ResolveRefField(sprintf("wwwdb://%s;cat=%s",
                                          &GetAttr("ConfigFile"),
                                          $lIdChain[$iInd]),
                                  $lIdNames[$iInd]);

      $cList = $cResult . " &gt; " . $cList;

   }

   # Add a trailing / and remove the last /
   #   $cList = ("/ " .
   #             &ResolveRefField("wwwdb://" . &GetAttr("ConfigFile"),
   #                              i18n("Top")) .
   #             " / " .
   #             $cList);

   # remove trailing >
   $cList =~ s: &gt; $::;

   $cList .= "&nbsp;" x 5 . "(" . $lFields[9] . ")";

   &MySetVal("Layout Label hier", "Text", "$cList");
   &MySetVal("Header", "Title", i18n("Navigation in Category: ") . $lFields[9]);

   # get sub-categories
   $cList = &HtmlListFromSelect
       ("Cat",
        sprintf("SELECT
                     ca0.id_category,
                     ca0.description,
                     ca0.nr_of_subcats,
                     ca0.nr_of_objs,
                     ca0.sort_nr,
                     ca0.attribs,
                     tra.trans_text as tra_description
                 FROM
                     wwwdb_category     ca0,
                     wwwdb_category     ca1,
                     wwwdb_translation  tra
                 WHERE
                     ca1.id_category        = ca0.id_cat_of_cat AND
                     ca0.id_cat_of_cat      = %d                AND
                     ca0.description_txt_id = tra.id_text       AND
                     tra.lang               = '%s'
                 ORDER BY
                   sort_nr",
                &GetField("id_category"),
                &GetAttr("Lang")));

   # Show list or disable label
   if ($cList)
   {
      &MySetVal("Layout Label subcat1", "Text", "$cList");
      $bNoResult = 0;
   }
   else
   {
     &MySetVal("Layout Label subcat", "Text", " ");
   }


   # get related categories
   $cList = &HtmlListFromSelect
       ("Cat",
        sprintf("SELECT
                     c_c.id_category,
                     cat.description,
                     cat.nr_of_subcats,
                     cat.nr_of_objs,
                     cat.sort_nr,
                     cat.attribs,
                     tra.trans_text as tra_description
                 FROM
                     wwwdb_cat_cat      c_c,
                     wwwdb_category     cat,
                     wwwdb_translation  tra
                 WHERE
                     c_c.id_category        = cat.id_category   AND
                     c_c.id_cat_of_cat      = %d                AND
                     cat.description_txt_id = tra.id_text       AND
                     tra.lang               = '%s'
                 ORDER BY
                   sort_nr",
                &GetField("id_category"),
                &GetAttr("Lang")));

   # Show list or disable label
   if ($cList)
   {
      &MySetVal("Layout Label see_also1", "Text", "$cList");
      $bNoResult = 0;
   }
   else
   {
      &MySetVal("Layout Label see_also", "Text", " ");
   }


   # get the objects of this category
   $cList = &HtmlListFromSelect
       ("Obj",
        sprintf("SELECT
                     obj.ref_link,
                     obj.descript,
                     obj.sort_nr,
                     obj.attribs,
                     tra.trans_text as tra_description
                 FROM
                     wwwdb_category     cat,
                     wwwdb_obj_cat      o_c,
                     wwwdb_object       obj,
                     wwwdb_translation  tra
                 WHERE
                     cat.id_category     = o_c.id_category AND
                     o_c.id_object       = obj.id_object   AND
                     o_c.id_category     = %d              AND
                     obj.descript_txt_id = tra.id_text     AND
                     tra.lang            = '%s'
                 ORDER BY
                     sort_nr",
                &GetField("id_category"),
                &GetAttr("Lang")));

   $cList = b(i18n("Sorry, no result with the actual permissions!"))
       if !$cList && $bNoResult;

   # Show list or disable label
   if ($cList)
   {
      &MySetVal("Layout Label objects1", "Text", "$cList");
   }
   else
   {
      &MySetVal("Layout Label objects", "Text", " ");
   }


}




sub HtmlListFromSelect($$)
{
   my $cType    = shift;
   my $cSqlStmt = shift;

   my $plRecord;
   my $iNrOfEntries      = 0;
   my @lFoundRecord;

   # &nbsp; prevents <UL> to be seen as "< UL" from DBIx::Recordset
   my $cResult = "<UL>";

   $plRecord = $oDbSessionGL->SqlSelect($cSqlStmt);

   foreach (@$plRecord)
   {
      my $lFields;

      @lFields = @$_;

      if ($cType eq "Cat")
      {
         next
             if(!&IsAttribOK($lFields[5],
                             "IsHidden" => 0));

         $lFields[1] = $lFields[6]
             unless ($lFields[6] eq "?");

         # categories and/or objects
         if($lFields[2] + $lFields[3])
         {
            my $cNrOfChilds;

            # categories, but no objects
            if ($lFields[2] and !$lFields[3])
            {
               $cNrOfChilds = sprintf(" (<B>%d</B>)",
                                      $lFields[2]);
            }
            # no categories, but objects
            elsif (!$lFields[2] and $lFields[3])
            {
               $cNrOfChilds = sprintf(" (%d)",
                                      $lFields[3]);
            }
            # both
            else
            {
               $cNrOfChilds = sprintf(" (<B>%d</B>/%d)",
                                      $lFields[2],
                                      $lFields[3]);
            }

            $cResult .= li(&ResolveRefField(sprintf("wwwdb://%s;cat=%s",
                                                    &GetAttr("ConfigFile"),
                                                    $lFields[0]),
                                            $lFields[1]) . $cNrOfChilds);

         }
      }
      elsif($cType eq "Obj")
      {

         next
             if(!&IsAttribOK($lFields[3],
                             "IsHidden" => 0));

         $lFields[1] = $lFields[4]
             unless ($lFields[4] eq "?");

         $cResult .= li(&ResolveRefField($lFields[0], $lFields[1]));

      }
      elsif($cType eq "ObjEdit")
      {

         next
             if(!&IsAttribOK($lFields[3],
                             "IsHidden" => 0));

         $lFields[1] = $lFields[4]
             unless ($lFields[4] eq "?");

         $cResult .= li(&ResolveRefField
                        (sprintf("wwwdb://WWWdb:NavObj;id=%d",
                                 $lFields[0]),
                         $lFields[1]));
      }
      else
      {
         $cResult .= li($_);
      }

      $cResult .= "\n";

      @lFoundRecord = @lFields;
      $iNrOfEntries ++;

   } # foreach (@$plRecord)


   if(!$iNrOfEntries)
   {
      $cResult = "";
   }
   else
   {
      $cResult .= "</UL>";

      if($iNrOfEntries == 1 && $cType eq "Obj")
      {
         # if there's only one wwwdb-object, jump directly to it
         if ($lFoundRecord[0] =~ "^wwwdb://(.*)\$" &&
             &IsAttribOK("!IsAdmin"))
         {
            &Redirect(&GetAttr("SessionId"), $1);
         }
      }
   }

   return $cResult;

}





sub BtnSearch ()
{
   my $cSword;

   $cSword = lc(&GetField("sword"));

   $cSword =~ s/\*/%/g;

   # get the objects of this category
   $cList = &HtmlListFromSelect
       ("Obj",
        sprintf("SELECT DISTINCT
                     obj.ref_link,
                     obj.descript,
                     obj.sort_nr,
                     obj.attribs,
                     tra.trans_text as tra_description
                 FROM
                     wwwdb_sword    swd,
                     wwwdb_obj_swd  o_s,
                     wwwdb_object   obj,
                     wwwdb_translation  tra
                 WHERE
                     swd.id_sword  =    o_s.id_sword   AND
                     o_s.id_object =    obj.id_object  AND
                     %s
                     swd.sword     LIKE '%s'           AND
                     obj.descript_txt_id = tra.id_text AND
                     tra.lang            = '%s'
                 ORDER BY
                     obj.descript",
                (&GetField("search_lang")?
                 "swd.lang = '" . &GetField("search_lang") . "' AND":
                 ""),
                $cSword,
                &GetAttr("Lang")));


   # Show list or disable label
   if ($cList)
   {
      &MySetVal("Layout Label search_result", "Text",
                i18n("Search-Results:") . "<BR><HR>");
      &MySetVal("Layout Label search_result1", "Text", "$cList");
   }
   else
   {
      &MySetVal("Layout Label search_result", "Text", " ");
      &MySetVal("Layout Label search_result1", "Text",
                i18n("Sorry, no entries found!"));
   }
}

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

   if(&IsAttribOK("ActualLogin") && &IsAttribOK("IsAdmin"))
   {
      $cResult .= (br() x 2 .
                   b(i18n("Edit:")) .
                   br().
                   li(&ResolveRefField("wwwdb://WWWdb:NavCat;id=" .
                                       &GetField("id_category"),
                                       i18n("This category"))));

   # get the objects of this category
   $cList = &HtmlListFromSelect
       ("ObjEdit",
        sprintf("SELECT
                     obj.id_object,
                     obj.descript,
                     obj.sort_nr,
                     obj.attribs,
                     tra.trans_text as tra_description
                 FROM
                     wwwdb_category     cat,
                     wwwdb_obj_cat      o_c,
                     wwwdb_object       obj,
                     wwwdb_translation  tra
                 WHERE
                     cat.id_category     = o_c.id_category AND
                     o_c.id_object       = obj.id_object   AND
                     o_c.id_category     = %d              AND
                     obj.descript_txt_id = tra.id_text     AND
                     tra.lang            = '%s'
                 ORDER BY
                     sort_nr",
                &GetField("id_category"),
                &GetAttr("Lang")));

      if ($cList)
      {

         $cList =~ s(</?UL>)()g;

         $cResult .= (br() .
                      li(b(i18n("Objects:"))) .
                      $cList);

      }

      $cResult = small($cResult);
   }

   return $cResult;

}


1;
