# Create a list of headlines that can be inlcuded on other web sites
# with some code like this:
#
# <script language="JavaScript" src="http://mkdoc.com/headline.js"></script> 
#
# ./headline.js
#   max 15 children, listed by date modified, non-nested, infinite depth
#     documents fields; uri, title
package flo::plugin::JavaScript::Headline;
use strict;
use lib::sql::Table;
use lib::cgi::CGI;
use flo::Standard;
use lib::Exception;
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);
    
    # 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;
    
    my $jstitle       = text2html ($current_document->{Title});
    my $jslink        = ($current_document->{URI});
    my $jsdescription = text2html ($current_document->{Description});
    
    # 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;
    
    print "Content-Type: application/x-javascript; charset=us-ascii\n\n";

    print <<EOF;
document.writeln("<p><a href=\\"$jslink\\">$jstitle</a></p>");
document.writeln("<ul>");
EOF

    foreach my $headline (@children_document) {
	
        $cgix->path_info ($headline->{Full_Path});
        $headline->{URI} = $cgix->self_url;

        my $escaped_headline = $headline->{Title};
        $escaped_headline  =~ s/\"/&quot;/g;

	print <<EOF;
document.writeln("<li><a href=\\"$headline->{URI}\\">$escaped_headline</a></li>");
EOF

    }

print <<EOF;
document.writeln("</ul>");
EOF

    return 'TERMINATE';
}


1;


