# -------------------------------------------------------------------------------------
# flo::plugin::Sitemap
# -------------------------------------------------------------------------------------
#
#       Author : Jean-Michel Hiver (jhiver@cisedi.com).
#    Copyright : (c) MKDoc Holdings Ltd, 2001
# 
#      Unauthorized modification, use, reuse, distribution or redistribution
#      of this module is stricly forbidden
#
#    Description:
#
#      A plugin that is used to have a user-friendly, dynamic sitemap
#      which can be used to quickly explore the site
#
# -------------------------------------------------------------------------------------
package flo::plugin::Sitemap;
use flo::Object;
use lib::sql::Table;
use lib::cgi::CGI;
use flo::Standard;
use lib::Exception;
use flo::HTTP::Header;
use lib::PSP;
use strict;
use vars qw /@ISA/;
@ISA = qw /flo::Object/;


##
# $class->main;
# -------------
#  This method is invoked when it is found that a user is querying for the
#  sitemap plugin.
##
sub main
{
    my $class = shift;
    my $document_table = table ('Document');
    my $cgi = cgi;

    # 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) );
    
    # otherwise, let's process the request
    $raw_path_info =~ s/$q_http_name$//;
    
    # clean path info so that we know the document from which we want to
    # start site-mapping
    my $path_info = path_info;
    if ($path_info ne '/')
    {
	$path_info =~ s/^\///;
	$path_info =~ s/\/$//;
	my @path_info = split /\//, $path_info;
	pop (@path_info);
	if (@path_info) { $path_info = '/' . (join '/', @path_info) . '/' }
	else { $path_info = '/' }
    }
    
    # get the current document. If it does not exist, let's pass the query to the 404
    my $document = $document_table->get ( Full_Path => $path_info ) or return;
    my $document_lang = $document->{Lang};
    
    # if the CGI objects has no parameters, then we want to compute which nodes
    # have to be displayed from the current node and redirect to this location
    if ($cgi->param ('x'))
    {
	my $cfg = new lib::Config ('flo.Template');
	$lib::PSP::BASE_DIR = $cfg->get ('BASE_DIR');
	print $cgi->header;
	my $psp = new lib::PSP ( template ('sitemap', $document_lang), qw /document root_document/ );
	
	$psp->compile()->( document => $document,
                           root_document => $document_table->get ( ID => 1 ) );
    }
    else
    {
	my $depth  = $config->{MAX_DEPTH};
	my @nodes = $class->grab_nodes ($document->{ID}, $depth);
	my $cgix = $cgi->new;
	$cgix->param ('x', @nodes);
	print $cgix->redirect ( $cgix->self_url );
    }
    
    return 'TERMINATE';
}


sub grab_nodes
{
    my $class = shift;
    my $id = shift;
    my $depth = shift;
    my @res = $class->_grab_nodes ($id, $depth);
    my $document_table = table ('Document');
    my $document = $document_table->get ($id);
    while (defined $document)
    {
	push @res, $document->ID;
	$document = $document->Parent;
    }
    return @res;
}


sub _grab_nodes
{
    my $class = shift;
    my $id = shift;
    my $depth = shift;
    $depth--;
    return () if ($depth == 0);
    
    my $document_table = table ('Document');
    my @res = map { $_->{ID} } $document_table->search ( Parent_ID => $id )->fetch_all;
    return (@res, map { $class->_grab_nodes ($_, $depth) } @res);
}


1;










