#!/usr/bin/perl


## fora - CGI interface for fora event tracking and process manipulation tool

#Copyright Massachusetts Institute of technology, 1999.
#Written by Eric Prud'hommeaux

BEGIN {unshift@INC,('..');}

use strict;
require Exporter;
require AutoLoader;

use W3C::Util::W3CDebugCGI;
use W3C::Util::Properties;
use W3C::Database::Forum;

$chacl::REVISION = '$Id: fora,v 1.1 2000/03/23 04:01:36 eric Exp $ ';
package forumCGI; # forum-specific common CGI functions
use vars qw($VERSION $DSLI @ISA @EXPORT @EXPORT_OK @TODO);
@ISA = qw(Exporter AutoLoader);
@EXPORT = qw();
@EXPORT_OK = qw();
$VERSION = '0.01';
$DSLI = 'adpO';
@TODO = ();

# Exceptions

$main::EXCEPTION_NEED_REDIRECT = 'Exception: need to redirect to:';

my %scripts = ('issuesList' => 'issuesListCGI', 
	       );

sub new {
    my $proto = shift;
    my $class = ref($proto) || $proto;
    my $self = {};
    $self->{CGI} = new W3C::Util::W3CDebugCGI($0, $ARGV[0] eq 'DEBUG', {-storeIn => '/tmp', -dieNoOpen => 1, -logExt => '.log'});
    bless ($self, $class);
    $self->setForum();
    my $script = $self->{SCRIPT_NAME};
    my $templateVariables = new W3C::Database::TemplateVariables({TITLE => 'List of Issues in '.$self->{FORUM}.' forum'});
    $self->{SCRIPT} = new $script($self, new W3C::Util::Properties('../Conf/forum.prop'), $templateVariables);
    return $self;
}

sub catPath {
    my (@paths) = @_;
    my $ret = '';
    foreach my $path (@paths) {
	$path =~ s:/+$::;
	if ($path =~ m:/(.*):) {
	    $ret .= '/'.$1;
	} else {
	    $ret .= '/'.$path;
	}
    }
    return $ret;
}

sub setForum {
    my ($self) = @_;
    if ($ENV{'PATH_INFO'} =~ m:^/([^/]+)(.*)$:) {
	$self->{SCRIPT_PATH} = $1;
	$self->{SCRIPT_NAME} = $scripts{$1};
	$self->{FORUM} = $2;
#	$self->{FORUM} =~ s:^/::;
    }

    # look for valid script name
    if (!$self->{SCRIPT_NAME}) {
	# make a guess of /issuesList/<path>
	my $location = catPath($ENV{'SCRIPT_NAME'}, 'issuesList', $ENV{'PATH_INFO'}).'/';
	if ($ENV{'QUERY_STRING'}) {
	    $location .= '?'.$ENV{'QUERY_STRING'};
	}
	die $main::EXCEPTION_NEED_REDIRECT.' '.$location.' '.$ENV{SCRIPT_URI}."\n";
    }

    # insure trailing '/'
    if ($self->{FORUM} !~ m:/$:) {
	my $location = catPath($ENV{'SCRIPT_NAME'}, $ENV{'PATH_INFO'}).'/';
	if ($ENV{'QUERY_STRING'}) {
	    $location .= '?'.$ENV{'QUERY_STRING'};
	}
	die $main::EXCEPTION_NEED_REDIRECT.' '.$location.' '.$ENV{SCRIPT_URI}."\n";
    }

#    # look for consecutive '/'s in the path
#    if ($self->{FORUM} =~ s://:/:g) {
#	my $location = catPath($ENV{'SCRIPT_PATH'}, $self->{FORUM}).'/';
#	if ($ENV{'QUERY_STRING'}) {
#	    $location .= '?'.$ENV{'QUERY_STRING'};
#	}
#	die $main::EXCEPTION_NEED_REDIRECT.' '.$location.' '.$ENV{SCRIPT_URI}."\n";
#    }

    # figure out how deep we are and build relative path
    $self->{FORUM} =~ s:/+:/:g;
    my @pieces = split ('/', $self->{FORUM});
    my $depth = @pieces;
    $self->{SCRIPT_BASE} = '../' x ($depth+1);
    return;
}

sub standardHeaders {
    my ($self) = @_;
    $self->{HEADERS} = $self->{CGI}->header(-expires=>'+4h');
}

sub mesg {
    my ($self, $title, $values) = @_;
    my $ret = '';
    $ret .= $self->start_html(-title=>'fora', -BGCOLOR=>"#FFFFFF", -TEXT=>"#000000", -LINK=>"#0000ee", -VLINK=>"#551a8b");
    $ret .= "\n<h1>".$title.'</h1>'."\n";
    foreach my $key (keys %$values) {
	$ret .= '<dt>'.$key.'<dd>'.$values->{$key}."</dd>\n";
    }
    return $ret."</body></html>\n";
}

sub render {
    my ($self) = @_;
    $self->standardHeaders();
    print $self->{HEADERS};
#    print $self->mesg('title', {'attr1' => 'value1'});
    print $self->{SCRIPT}->render;
    return;
}

sub renderTop {
    my ($self, $templateVariables) = @_;
    my $ret = $self->{CGI}->start_html(-title=>$templateVariables->{TITLE}, -BGCOLOR=>"#FFFFFF", -TEXT=>"#000000", -LINK=>"#0000ee", -VLINK=>"#551a8b");
    $ret .= <<EOTOP
<style type="text/css">
<!-- 
   .navbar { text-align: center }
-->   
</style>
</head>
<body>
<p>
<a href="http://www.w3.org/"><img border="0" alt="W3C" src="http://www.w3.org/Icons/w3c_home" WIDTH="72" HEIGHT="48"></A> |
<a href="http://www.w3.org/Tools/ETA/">Event Tracking Agent</a>
</p>

<p align="center" class="navbar">
[<a href="../issuesList.php3/">All Issues</a>] 
[<a href="../issuesList.php3/?filter1=issues.owner%3D8&amp;pass_contacts_id=8">Unassigned Issues</a>]
[<a href="../issuesList.php3/?filter1=issues.state%3D1">New Issues</a>]
[<a href="../issuesList.php3/?filter1=%28issues.state%3D400+OR+issues.state%3D500%29">Closed Issues</a>]<br>
[<a href="../linksList.php3/">List Links</a>] 
[<a href="../eventsList.php3/">List Events</a>] 
[<a href="../contactsList.php3/">List Contacts</a>] 
[<a href="../skillsList.php3/">List Skills</a>] 
[<a href="../forumsList.php3/">List Fora</a>] 
[<a href="../schemasList.php3/">List Schemas</a>] 
[<a href="../help.html">Help</a>]<br>
[<a href="../issues.php3/?editing=1&amp;nextAction=add">Add Issue</a>]
[<a href="../contacts.php3/?editing=1&amp;nextAction=add">Add Contact</a>]
[<a href="../links.php3/?editing=1&amp;nextAction=add">Add Link</a>]</p><h2>List of Issues in Top forum</h2>
<p align="left">
An <a href="../help.html#Issues">issue</a> is the document, bug, request, annotation, discussion item,
or anything else that you want to track using ETA.</p>

<h3 align="center">Issues in other Fora: [<a href="../issuesList.php3/">Top</a>] [<a href="../issuesList.php3/news/">news/</a>]  [<a href="../issuesList.php3/tools/">tools/</a>]  [<a href="../issuesList.php3/wca/">wca/</a>] </h3>
EOTOP
    ;
    return $ret;
}

sub renderBottom {
    my ($self, $templateVariables) = @_;
    my $ret = <<EOBOTTOM
<p align="left"></p>
<p>
<hr title="separator">
<address>
forum\@w3.org,<br>
\$Date: 2000/03/23 04:01:36 $</address>
</body>
</html>
EOBOTTOM
    ;
    return $ret;
}

package issuesListCGI; # CGI interface for a forum issuesList
use vars qw(@ISA);
@ISA = qw(Exporter AutoLoader W3C::Database::issuesList);
sub new {
    my ($proto, $cgi, $properties, $templateVariables) = @_;
    my $class = ref($proto) || $proto;
    my $self = $class->SUPER::new($properties, $cgi->{FORUM}, $templateVariables);
    $self->{CGI} = $cgi;
    $self->{DATABASE} = $properties->getI('database');
    bless ($self, $class);
    return $self;
}

sub render {
    my ($self) = @_;
    my $ret = $self->{CGI}->renderTop($self->{TEMPLATE_VARIABLES});
    $ret .= <<EOTABLE;
<table border="1" cellspacing="0" width="100%">
<caption></caption>
<tbody>
<tr>
<th><a href="../issuesList.php3/?order=issues.id">Number</a></th>
<th><a href="../issuesList.php3/?order=issues.summary">Summary</a></th>
<th><a href="../issuesList.php3/?order=issues.category">Category</a></th>
<th><a href="../issuesList.php3/?order=issues.priority">Priority</a></th>
<th><a href="../issuesList.php3/?order=issues.state">Current State</a></th>
<th><a href="../issuesList.php3/?order=issues.created">Created on</a></th>
<th><a href="../issuesList.php3/?order=creatorContact.realname">Created by</a></th>
<th><a href="../issuesList.php3/?order=ownerContact.realname">Assigned to</a></th>
<th><a href="../issuesList.php3/?order=issues.o_result">Resolution</a></th>
</tr>
</tbody>
</table>

<p align="center">[<a href="../issues.php3/?editing=1&amp;nextAction=add">Add New Issue</a>] [<a href="../issuesList.php3/?filter1=issues.owner%3D8&amp;pass_contacts_id=8">Unassigned Issues</a>] [<a href="../issuesList.php3/?filter1=issues.state%3D1">New Issues</a>] </p>
EOTABLE
    ;
    $ret .= $self->{CGI}->renderBottom($self->{TEMPLATE_VARIABLES});
    return $ret;
}

package forumCGI; # the "main" code
eval {
    my $CGIInterface = new forumCGI;
    $CGIInterface->render;
}; if ($@) {
    print &redirect($1, $2) if ($@ =~ m/\A \Q$main::EXCEPTION_NEED_REDIRECT\E \s ([^ ]*) \s ([^ ]*) \Z/x);
}
exit 0;

sub redirect {
    my ($where, $error) = @_;
    my $redirResponse = <<EOF
Status: 302 Moved Temporarily
Location: $where
Connection: close
Content-Type: text/html

<!DOCTYPE HTML PUBLIC \"-//IETF//DTD HTML 2.0//EN\">
<HTML><HEAD>
<TITLE>302 Moved Temporarily</TITLE>
</HEAD><BODY>
<H1>Moved Temporarily</H1>
URL \"$error\" can be found at <A HREF=\"$where\">$where</A>.<BR>
Squirrely little bastard got away this time.<P></BODY></HTML>
EOF
    ;
    return $redirResponse;
}

