# -------------------------------------------------------------------
#  flo::plugin::explorer::Print
# -------------------------------------------------------------------
#
#  flo::plugin::Explorer is the default plugin. The public interface 
#  should let visitors explore the database categories and articles, 
#  whilst the admin interface for editors provides an integrated menu
#  for modifying these categories and articles.
#
#  Author: Jean-Michel Hiver (jhiver@cisedi.com)
#  Copyright MKDoc Holding Ltd.
#
# -------------------------------------------------------------------
package flo::plugin::explorer::Print;
use flo::Standard;
use flo::Template;
use flo::Editor;
use lib::sql::Table;
use lib::cgi::CGI;
use lib::Exception;
use lib::Config;
use flo::HTTP::Header;
use flo::Record::Contributor;
use flo::Record::Document;
use flo::Record::Redirect;
use flo::Object;
use flo::CGI;
use vars qw /@ISA/;
@ISA = qw /flo::Object/;
use strict;


##
# __PACKAGE__->main;
# ------------------
#  Displays a given document
##
sub main
{
    my $class = shift;

    # 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) );

    # this should make 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;
    
    my $document_table = lib::sql::Table->table ('Document');
    my $document = $document_table->get ( Full_Path => $path_info );
    my $cgi = lib::cgi::CGI->get;
    my $hash = {};
    $hash->{Document} = $document;
    
    my $cgix = $cgi->new;
    foreach ($cgix->param) { $cgix->delete ($_) }
    $cgix->path_info ($path_info);
    $hash->{URI} = { Action => $cgix->self_url };
    
    if (defined $hash->{Document})
    {
	my $editor = new flo::Editor;
	$editor->parse_xml ($hash->{Document}->{Body});
	$hash->{Session} = {};
	
      flo::HTTP::Header->out
	  ( new flo::Template ( file => template ('explorer/print', $document->Lang),
				hash => $hash ) );
	return 'TERMINATE';
    }
    else { throw (new lib::Exception (info => path_info)) }
}


1;
