#!/usr/local/bin/perl -w
###############################################################################
# story.pl - this code displays the pda story page
#
# Copyright (C) 2001 Norbert "Momo_102" Kuemin
# momo_102@bluemail.ch
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
###############################################################################
use strict;
use lib '/home/slash';
use vars '%I';
use Slash;
use AvantGo;

##### Configuration #############################################
my $AVG = new AvantGo("/home/slash/avantgo.user");

#################################################################
sub main {
   *I = getSlashConf();
   getSlash();
   my $SECT = getSection($I{F}{section});
   $SECT->{mainsize} = int($SECT->{artcount} / 3);
   my $title = $SECT->{title};
   $title = "$title" unless $SECT->{isolate};

   $AVG->env_load();
   $AVG->user_load();
	
   pda_header($I{sitename});
   pda_story_display(CGI::param("sid"),CGI::param("nr"));
   pda_footer();
}

#################################################################
sub pda_header {
   my $title = shift;
   print CGI::header();
   print <<EOT if $title;
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
<HTML><HEAD><TITLE>$title</TITLE></HEAD><BODY>
EOT
}  
   
#################################################################
sub pda_footer {
   print <<EOT; 
</BODY></HTML>  
EOT
}  

#################################################################
sub pda_story_nav {
   my ($sid, $psid, $nsid, $nr, $cnr) = @_;
   if ($psid) { print "<A HREF=\"story.pl?sid=$psid&nr=".($nr-1)."\">&lt;-</A> "; }
   print "<A HREF=\"index.pl";
   print "#$nr\">Home</A> ";
   if ($cnr > 0) {
      print "<A HREF=\"comment.pl?sid=$sid&nr=$nr\">Comment($cnr)</A> ";
   }
   my ($story, $comment, $poll, $hof, $uid) = $AVG->user_get("-");
   if ($AVG->env_get("USERID") ne "") { ($story, $comment, $poll, $hof, $uid) = $AVG->user_get(); }
   if ($nr < $story) {
      if ($nsid) { print "<A HREF=\"story.pl?sid=$nsid&nr=".($nr+1)."\">-&gt;</A> "; }
   }
}

#################################################################
sub pda_story_display {
   my ($sid,$nr) = @_;
   my ($title,$introtext,$bodytext,$date,$time,$author) = sqlSelect("title,introtext,bodytext,date_format(time,\"%W, %d. %M %H:%i\"),time,aid", "newstories", "sid='$sid'");
   my ($psid) = sqlSelect("sid", "newstories", "time>'$time' AND displaystatus = 0 AND time < now() ORDER BY time ASC LIMIT 1");
   my ($nsid) = sqlSelect("sid", "newstories", "time<'$time' AND displaystatus = 0 AND time < now() ORDER BY time DESC LIMIT 1");
   my $cnr = 0;
   my ($story, $comment, $poll, $hof, $uid) = $AVG->user_get("-");
   if ($AVG->env_get("USERID") ne "") { ($story, $comment, $poll, $hof, $uid) = $AVG->user_get(); }
   if ($comment ne "none") {
      ($cnr) = sqlSelect("count(*)", "comments", "sid='$sid'");
   }
   pda_story_nav($sid, $psid, $nsid, $nr, $cnr);
   print "<HR>";
   print "<B>$title</B> by $author\n";
   print "<P>\n";
   print $introtext;
   if ($bodytext) { print "<P>".$bodytext; }
   print "<HR>";
   pda_story_nav($sid, $psid, $nsid, $nr, $cnr);
   print "<HR>$date\n";
}

#################################################################
main();
#################################################################
