# -----------------------------------------------------------------------------
#
# Author: Klaus Reger <K.Reger@twc.de>
#
# -----------------------------------------------------------------------------
# Program-Data
# -----------------------------------------------------------------------------
# Project  :
# System   :
# Program  :
# Module   : $RCSfile: ShowDoc.pl,v $
# Version  : $Revision: 1.11 $
# Date     : $Date: 2002/02/28 09:01:04 $
# State    : $State: Exp $
#
# Description:
#   Show a WWWdb-Document from the database
#
# -----------------------------------------------------------------------------
# Change-log (See at end of this file)
# $Log: ShowDoc.pl,v $
# Revision 1.11  2002/02/28 09:01:04  k_reger
# - requite ScanDoc removed
#
# Revision 1.10  2001/01/25 12:41:43  k_reger
# - 'text' renamed to 'document'
# - section and subsection can be passed as parameters
#
# Revision 1.9  2000/10/12 13:53:38  k_reger
# - Bugfix: Scan large docs coming from files
#
# Revision 1.8  2000/09/29 12:02:51  k_reger
# - module ScanDoc included
#
#
# --- Known Bugs -------------
# please look at the BUGS-File
# --- TODO -------------------
# please look at the TODO-File
# ----------------------------------------------------------------------------

sub PreDoAction ()
{
   my $cLastBtn   = &GetAttr("LastBtn");
   my $cUrlParams = &GetAttr("UrlParams");
   my @lUrlParams = split ";", $cUrlParams;
   my $iIdDoc       = 0;
   my $plDocInfo;
   my $cDocInfo   = "";
   my $cCategory;
   my $cSubCategory;

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

   foreach (@lUrlParams)
   {
      $iId = $1
          if (/^id=([^;]+)/);
   }

   if ($iId)
   {
      ($iIdDoc, $cCategory, $cSubCategory) = split ",", $iId;

      if($iIdDoc)
      {
         &SetField("id_doc", $iId);
      }
      elsif($cCategory || $cSubCategory)
      {
         my $cQryFields = &MyVal("Data", "QryFields");

         $cQryFields =~ s/\bcategory, //
             if $cCategory;

         $cQryFields =~ s/\bsub_category, //
             if $cSubCategory;

         &SetField("category", $cCategory);
         &SetField("sub_category", $cSubCategory);

         &MySetVal("Data", "QryFields", $cQryFields);

         &BtnQry();
         &MyExit();
      }
   }

   {
      my $cQryFields = &MyVal("Data", "QryFields");

      $cQryFields =~ s/\bcategory, //
          if &GetField("category");

      $cQryFields =~ s/\bsub_category, //
          if &GetField("sub_category");

      &MySetVal("Data", "QryFields", $cQryFields);

   }

   # Get previous document, if possible
   {
      my $iPrevDoc = 0;
      my $iNextDoc = 0;
      my $iMaxSortNr = 0;
      my $iMinSortNr = 0;

      $plDocInfo = $oDbSessionGL->SqlSelect
          ("SELECT
              id_doc, category, sub_category, sort_nr
            FROM
              wwwdb_doc
            WHERE
              id_doc = ? AND
              lang   = ?",
           $iIdDoc,
           &GetAttr("Lang"))->[0];

      $iMaxSortNr = $oDbSessionGL->SqlSelect
          ("SELECT
              MAX(sort_nr)
            FROM
              wwwdb_doc
            WHERE
              category     =  ?      AND
              sub_category =  ?      AND
              sort_nr      <  ?      AND
              lang         =  ?",
           $plDocInfo->[1],
           $plDocInfo->[2],
           $plDocInfo->[3],
           &GetAttr("Lang"))->[0]->[0]
               if $plDocInfo->[3];


      $iPrevDoc = $oDbSessionGL->SqlSelect
          ("SELECT
              id_doc
            FROM
              wwwdb_doc
            WHERE
              category     =  ? AND
              sub_category =  ? AND
              sort_nr      =  ? AND
              lang         =  ?",
           $plDocInfo->[1],
           $plDocInfo->[2],
           $iMaxSortNr,
           &GetAttr("Lang"))->[0]->[0]
               if $iMaxSortNr;

      $iMinSortNr = $oDbSessionGL->SqlSelect
          ("SELECT
              MIN(sort_nr)
            FROM
              wwwdb_doc
            WHERE
              category     =  ? AND
              sub_category =  ? AND
              sort_nr      >  ? AND
              lang         =  ?",
           $plDocInfo->[1],
           $plDocInfo->[2],
           $plDocInfo->[3],
           &GetAttr("Lang"))->[0]->[0]
               if $plDocInfo->[3];


      $iNextDoc = $oDbSessionGL->SqlSelect
          ("SELECT
              id_doc
            FROM
              wwwdb_doc
            WHERE
              category     =  ? AND
              sub_category =  ? AND
              sort_nr      =  ? AND
              lang         =  ?",
           $plDocInfo->[1],
           $plDocInfo->[2],
           $iMinSortNr,
           &GetAttr("Lang"))->[0]->[0]
               if $iMinSortNr;

      if($iPrevDoc)
      {

         &MySetVal("Layout Label nav_top_prev", "Text",
                   "[" . &ResolveRefField(sprintf("wwwdb://%s;id=%d",
                                            &GetAttr("ConfigFile"),
                                            $iPrevDoc),
                                    i18n("<< Prev.")) . "]");
         &MySetVal("Layout Label nav_bot_prev", "Text",
                   "[" . &ResolveRefField(sprintf("wwwdb://%s;id=%d",
                                            &GetAttr("ConfigFile"),
                                            $iPrevDoc),
                                    i18n("<< Prev.")) . "]");
      }

      if($iNextDoc)
      {

         &MySetVal("Layout Label nav_top_next", "Text",
                   "[" . &ResolveRefField(sprintf("wwwdb://%s;id=%d",
                                            &GetAttr("ConfigFile"),
                                            $iNextDoc),
                                    i18n("Next >>")) . "]");
         &MySetVal("Layout Label nav_bot_next", "Text",
                   "[" . &ResolveRefField(sprintf("wwwdb://%s;id=%d",
                                            &GetAttr("ConfigFile"),
                                            $iNextDoc),
                                    i18n("Next >>")) . "]");
      }

      # Disable rulers, if no navigation is shown
      if(!$iPrevDoc && !$iNextDoc)
      {
         &MySetVal("Layout Label hr1", "Text", " ");
         &MySetVal("Layout Label hr2", "Text", " ");
      }

   }

   &MyParam("WWWdbState", "Init");
}


# --- Generate the form-header ---------------------------------------------
sub MyTableHeader($) # Plugin
{
   my $cTitlePI = shift;

   my $cResult = "";

   return $cResult;

   $cTitlePI = "&nbsp;"
       if !$cTitlePI;

   $cResult .=
       table({-WIDTH   => "100%",
              -BORDER  => 0},
             (TR(td(p({-ALIGN => "LEFT"},
                      h4((&EncodeHtml
                          (i18n(&MyVal("Header", "Title"))))))),
                 td(p({-ALIGN => "RIGHT"},
                      h4(&EncodeHtml(i18n($cTitlePI))))),
                 )));

   $cResult .= hr({-noshade => 1,
                   -size => 1});

   return $cResult;

}




sub PreCreateForm ()
{
   my $cTextField = &GetField("document");

   &MySetVal("Header", "Title", &GetField("description"));

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


   if($cTextField =~ m!^<WWWdb FILE=(.*)/>$!)
   {
      my $cContentOfFile = "";
      my $cFileName;

      $cFileName = (&GetAttr("BaseDir") .
                    "/lib/Data/wwwdb_doc/" .
                    $1);

      open TEXTFILE, "<$cFileName" or
          die sprintf(i18n("Can't open %s"), $cFileName);

      # Read a text file and print it out
      while (<TEXTFILE>)
      {
         $cContentOfFile .= $_;
      }

      $cContentOfFile = &ScanDoc($cContentOfFile);


      &SetField("document", $cContentOfFile);
   }



}


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


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

   return small($cResult);

}


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

   $cField = &ScanDoc($cField);

   &SetField("document", $cField);
}

1;












