# -------------------------------------------------------------------------------------
# flo::Template::Path
# -------------------------------------------------------------------------------------
#
#         Author : Jean-Michel Hiver (jhiver@cisedi.com)        (c) MKDoc Holdings Ltd.
#    Description : Provides the structure for the bread
#                  crumb trail loop.
#
#    <% path set="Path" %>
#    <% loop name="Path" %>
#      <a href="<% var name="URI" %>"><% var name="Title" encode_html="1" %></a> &gt;
#    <% end-loop>
#    <% var name="Title" encode_html="1" %>
# -------------------------------------------------------------------------------------
package flo::Template::Path;
use lib::Template::AbstractTag;
use lib::Exception;
use lib::Template;
use flo::Standard;
use strict;
use vars qw /@ISA/;
@ISA = qw /lib::Template::AbstractTag/;


sub main
{
    my $self = shift;
    my $result = $self->result;
    my $tokens = $self->tokens;
    my ($token_name, $token_hash) = $self->parse_tag (shift @{$tokens});
    
    my $symbol = $token_hash->{set} or
        throw (new lib::Exception ( code => 'VARIABLE_NOT_SET',
                                    info => "$token_name should have a 'symbol' attribute" ) );
    
    my $document_table = table ('Document');

    my $path_info = path_info;
    $path_info =~ s/^\///;
    $path_info =~ s/\/$//;
    my @path   = split /\//, $path_info;
    
    # builds the array for the bread crumb trail structure
    my @result = ();
    my $cgix = cgi->new;
    for ($cgix->param) { $cgix->delete ($_) }
    
    my $not_root = @path;
    while (@path)
    {
	pop (@path);
	@path or last;
        my $full_path = '/' . (join '/', @path) . '/';
        my $document = $document_table->select ( cols => [ qw /Full_Path Title Description/ ],
						 where => { Full_Path => $full_path } )->next;
	(defined $document) or
	    throw (new lib::Exception ( code => 'CANNOT_GET_DOCUMENT',
					info => $full_path ) );
	$cgix->path_info ($full_path);
	$document->{URI} = $cgix->self_url;
	unshift @result, $document;
    }
    
    if ($not_root)
    {
	my $full_path = '/';
	my $document = $document_table->select ( cols => [ qw /Full_Path Title Description/ ],
						 where => { Full_Path => $full_path } )->next;
	(defined $document) or
	    throw (new lib::Exception ( code => 'CANNOT_GET_DOCUMENT',
					info => $self ) );
	$cgix->path_info ($full_path);
	$document->{URI} = $cgix->self_url;
	unshift @result, $document;
    }
    
    $lib::Template::STASH->{$symbol} = \@result;
}


1;
