# MKDoc RSS 0.90 Headline Plugin
# Copyright Chris Croome, <chris@mkdoc.com>, 13 April 2001
# This script is released under the Perl Artistic Licence
#
# Create an RSS 0.9 file
# (http://my.netscape.com/publish/help/quickstart.html)
# using XML::RSS (http://search.cpan.org/search?dist=XML-RSS)
#
# ./rss090headline.rdf
#   max 8 children, listed by date modified, non-nested, infinite depth
#     documents fields; uri, title
package flo::plugin::XML::RDF::RSS::090::Headline;
use strict;
use lib::sql::Table;
use lib::cgi::CGI;
use flo::Standard;
use lib::Exception;
use XML::RSS;
use lib::util::Text2Html;
use lib::cgi::CGI;
use flo::Object;
use flo::CGI;
use vars qw /@ISA/;
@ISA = qw /flo::Object/;


sub main
{
    my $class = shift;
    my $cgi = lib::cgi::CGI->get;
    
    # if this module is not concerned, let's return ASAP
    my $config = $class->config;
    my $http_name = $config->{http_name};
    my $q_http_name = quotemeta ($http_name);
    my $raw_path_info = raw_path_info;
    return unless ( ($raw_path_info =~ /.*\/$q_http_name$/o) );

    # makes all uri's absolute
    $flo::CGI::FULL_URI = 1;
    
    # otherwise, let's process the request
    $raw_path_info =~ s/$q_http_name$//;
    my $path_info = $raw_path_info;
    
    # retrieve the 'Document' table object
    my $document_table = table ('Document');
    
    # retrieves the current document
    my $current_document = $document_table->get (Full_Path => $path_info);
    return unless (defined $current_document);
    
    # what is the ID of this document ?
    my $current_document_id = $current_document->{ID};
    
    # this stuff is to get the URI of the current document
    my $cgiz = $cgi->new;
    $cgiz->path_info ($current_document->{Full_Path});
    $current_document->{URI} = $cgiz->self_url;
    
    # Create a RSS 0.9 output
    my $rss = new XML::RSS (version => '0.9');
    $rss->strict (1);
    
    $rss->channel (
		   title       => text2xml (full_title ($current_document)),
		   link        => $current_document->{URI},
		   description => text2xml ($current_document->{Description})
		   );
    
    $rss->textinput(
		    title        => "Search",
		    description  => "Use this form to search " . text2xml ($current_document->{Title}), 
		    name         => "q",
		    link         => "$current_document->{URI}" ."search.html",
		    );
    
    # This can be used for a RSS image
    #       $rss->image(
    #                          title  => text2xml ($current_document->{Title}),
    #                          url    => "http://mkdoc.com/images/logo.gif",
    #                          link   => $current_document->{URI},
    #       );    
    # get all documents that are decendants of the current document
    use lib::sql::Condition;
    my $cond = new lib::sql::Condition;
    $cond->add ('Full_Path', 'LIKE', $path_info . '_%');
    my $children_query = $document_table->select ( 
						   cols   => [ qw /Date_Created Full_Path Title/ ],
						   sort   => [ qw/Date_Created/ ],
						   desc   => 1,
						   page   => [ 1, 8 ], # max 8 documents
						   where  => $cond 
						   );
    
    # retrieve the results in an array
    # Now, each element of @children_document is a hash reference which has
    # table attribute name as keys and record field values as values
    my @children_document = $children_query->fetch_all;
    my $cgix = $cgi->new;
    
    foreach my $headline (@children_document) {
	$cgix->path_info ($headline->{Full_Path});
        $headline->{URI} = $cgix->self_url;
	
        $rss->add_item
	    (
	     title => text2xml ($headline->{Title}),
	     link  => text2xml ($headline->{URI})
	     );
    }
    
    # print "Content-Type: text/plain; charset=us-ascii\n\n";
    print "Content-Type: application/xml; charset=us-ascii\n\n";
    print $rss->as_string;
    
    # print Dumper ($current_document) . "\n";
    return 'TERMINATE';
}


sub full_title
{
    my $document = shift;
    my $document_table = table ('Document');
    
    my $title = $document->{Title};
    while ($document->{Parent_ID})
    {
	$document = $document_table->get ( ID => $document->{Parent_ID} );
	$title = $document->{Title} . ' : ' . $title;
    }
    return $title;
}

1;


