#!/usr/local/bin/perl -w
###############################################################################
# comment.pl - this code displays the pda comment 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");
my $COMMENT_MAX_THREADLINES = 10;

#################################################################
sub main {
   *I = getSlashConf();
   getSlash();

   my $SECT = getSection($I{F}{section});
   $SECT->{mainsize} = int($SECT->{artcount} / 3);

   $AVG->env_load();    
   $AVG->user_load(); 

   my $nr = 0;
   if (CGI::param("nr")) { $nr = CGI::param("nr"); }
   if (CGI::param("cid")) {
      # Comment
     pda_comment_disp(CGI::param("sid"),$nr,CGI::param("cid"));
   } else {
      # Index
      pda_comment_list(CGI::param("sid"),$nr,$COMMENT_MAX_THREADLINES);
   }
   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_comment_list {
   my ($sid,$nr,$limit) = @_;
   my $title = "";
   if ($nr>0) {
      ($title) = sqlSelect("title", "newstories", "sid='$sid'");
   } else {
      ($title) = sqlSelect("question", "pollquestions", "qid='$sid'");
   }
   pda_header($title);
   pda_comment_list_nav($sid,$nr);
   print "<HR>\n";
   print "<B>Comments:</B>\n";
   print "<UL>\n";
   my ($amount) = sqlSelect("count(cid)", "comments", "sid='$sid'");
   my ($story, $comment, $poll, $hof, $uid) = $AVG->user_get("-");
   if ($AVG->env_get("USERID") ne "") { ($story, $comment, $poll, $hof, $uid) = $AVG->user_get(); }
   if (($amount > $limit) && ($comment ne "all")) {
      pda_comment_list_high($sid,$nr,$limit);
   } else {
      pda_comment_list_low($sid,$nr,0);
   }
   print "</UL>\n";
   print "<HR>\n";
   pda_comment_list_nav($sid,$nr);
}

#################################################################
sub pda_comment_list_nav {
   my ($sid,$nr) = @_;
   print "<A HREF=\"index.pl";
   if ($nr>0) {   
      print "#$nr";
   }
   print "\">Home</A> ";
   if ($nr>0) {
      print "<A HREF=\"story.pl?sid=$sid&nr=$nr\">Story</A>\n";
   } else {
      print "<A HREF=\"vote.pl\">Poll</A>\n";
   }
}

#################################################################
sub pda_comment_list_low {
   my ($sid,$nr,$parent_id) = @_;
   my $cursor = sqlSelectMany('cid,subject', 'comments',"sid='$sid' AND pid=$parent_id", "ORDER BY date");
   while (my($cid,$suject) = $cursor->fetchrow()) {
      print "<LI> <A HREF=\"comment.pl?sid=$sid&nr=$nr&cid=$cid\">$suject</A>\n";
      print "<UL>\n";
      pda_comment_list_low($sid,$nr,$cid);
      print "</UL>\n";
   }
   $cursor->finish;
}

#################################################################
sub pda_comment_list_high {
   my ($sid,$nr,$limit) = @_;
   my $cursor = sqlSelectMany('cid,subject', 'comments',"sid='$sid' ORDER BY points DESC,date DESC LIMIT $limit");
   while (my($cid,$suject) = $cursor->fetchrow()) {
      print "<LI> <A HREF=\"comment.pl?sid=$sid&nr=$nr&cid=$cid\">$suject</A>\n";
   }
   $cursor->finish;
}

#################################################################
sub pda_comment_disp {
   my ($sid,$nr,$cid) = @_;
   my ($title) = sqlSelect("title", "newstories", "sid='$sid'");
   my ($subject,$comment,$uid,$date) = sqlSelect("subject,comment,uid,date_format(date,\"%W, %d. %M %H:%i\")","comments","sid='$sid' AND cid=$cid");
   my ($nickname,$sig) = sqlSelect("nickname,sig","users","uid=$uid");
   pda_header($title);
   pda_comment_disp_nav($sid,$nr);
   print "<HR>\n";
   print "Subject: $subject<BR>\n";
   print "Author: $nickname<BR>\n";
   print "Date: $date<HR>\n";
   print $comment;
   if ($sig) {
      print "<BR>$sig\n";
   }
   print "<HR>\n";
   pda_comment_disp_nav($sid,$nr);
}

#################################################################
sub pda_comment_disp_nav {
   my ($sid,$nr) = @_;
   print "<A HREF=\"index.pl";
   if ($nr>0) {
      print "#$nr";
   }
   print "\">Home</A> ";
   if ($nr>0) {
      print "<A HREF=\"story.pl?sid=$sid&nr=$nr\">Story</A>\n";
   } else {
      print "<A HREF=\"vote.pl\">Poll</A>\n";
   }
   print "<A HREF=\"comment.pl?sid=$sid&nr=$nr\">Comments</A>\n";
}

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