# -----------------------------------------------------------------------------
# TWC
# Treuwert-Computer GmbH
# Schloßbergring 9
# 79098 Freiburg
#
# Author: Klaus Reger <K.Reger@twc.de>
#
# -----------------------------------------------------------------------------
# Program-Data
# -----------------------------------------------------------------------------
# Project  :
# System   :
# Program  :
# Module   : $RCSfile: News.pl,v $
# Version  : $Revision: 1.4 $
# Date     : $Date: 2002/02/28 14:48:20 $
# State    : $State: Exp $
#
# Description:
#   Celledur Neuigkeiten
# -----------------------------------------------------------------------------
# $Log: News.pl,v $
# Revision 1.4  2002/02/28 14:48:20  k_reger
# - Scan-Doc removed
#
# Revision 1.3  2001/01/25 10:13:17  k_reger
# - renamed 'text' to 'document'
#
# Revision 1.2  2000/10/12 13:08:56  k_reger
# - Handle large texts in files now
#
# Revision 1.1  2000/09/29 11:23:03  k_reger
# -view documents, that contain news
#
# Revision 1.3  2000/09/05 09:59:51  klaus
# - CVS-Header eingefügt
#
# -----------------------------------------------------------------------------

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

   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 .= $_;
      }

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

   # get newest docs
   $plBegriffe = $oDbSessionGL->SqlSelect
       ("SELECT
           id_doc, description, last_change
         FROM
           wwwdb_doc
         WHERE
           lang      = ?  AND
           category  = 'news'
         ORDER BY
           last_change desc",
        &GetAttr("Lang"));

   foreach $cBegriff (@{$plBegriffe})
   {
      my $cText = $cBegriff->[2];

      $iInd --;

      last
          unless $iInd;

      $cNews .= li({-TYPE => "disc"},
                   &ResolveRefField("wwwdb://WWWdb:News;id=" .
                                    $cBegriff->[0] .
                                    "," .
                                    &GetAttr("Lang"),
                                   ($cBegriff->[1]))) . br();

      # $cNews .= br() x 1;

   }

   $cNews = h4(i18n("Actual news:")) . small(ul($cNews));

   &SetField("news", $cNews);

   if(&GetField("id_doc"))
   {
      # Get previous document, if possible
      {

         $plDocInfo = $oDbSessionGL->SqlSelect
             ("SELECT
                 id_doc, last_change
               FROM
                 wwwdb_doc
               WHERE
                 category = 'news' AND
                 id_doc = ?        AND
                 lang   = ?",
              &GetField("id_doc"),
              &GetAttr("Lang"))->[0];

         if ($plDocInfo->[1])
         {

            $plPrevDoc = $oDbSessionGL->SqlSelect
                ("SELECT
                     id_doc, last_change
                   FROM
                     wwwdb_doc
                   WHERE
                     category     =  'news' AND
                     last_change  <  ?      AND
                     lang         =  ?
                   ORDER BY last_change DESC",
                  $plDocInfo->[1],
                 &GetAttr("Lang"))->[0];

            $plNextDoc = $oDbSessionGL->SqlSelect
                ("SELECT
                     id_doc, last_change
                   FROM
                     wwwdb_doc
                   WHERE
                     category     =  'news' AND
                     last_change  >  ?      AND
                     lang         =  ?
                   ORDER BY last_change ASC",
                 $plDocInfo->[1],
                 &GetAttr("Lang"))->[0];

            if($plPrevDoc->[1])
            {

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

            if($plNextDoc->[1])
            {

               &MySetVal("Layout Label nav_top_next", "Text",
                         "[" . &ResolveRefField(sprintf("wwwdb://%s;id=%d,%s",
                                                        &GetAttr("ConfigFile"),
                                                        $plNextDoc->[0],
                                                        &GetAttr("Lang")),
                                                i18n("Next >>")) . "]");
            }
         }
      }
   }
   else
   {
      &MySetVal("Layout Label col", "Background", "");
   }

}

sub PreDoAction()
{
   &SetField("lang", &GetAttr("Lang"));
}


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

   $cField = &ScanDoc($cField);

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



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("Lang"),
                                       i18n("This document"))));
   }

   return small($cResult);

}


1;

