package Image::Gallery::Html;
################################################################################
# $Id: Html.pm,v 1.22 2006/09/19 07:18:47 dleonard Exp $
# $Date: 2006/09/19 07:18:47 $
#
# Html output library for Image::Gallery
#
#  GPL v2
#  (c) 2005 dleonard@dleonard.net
################################################################################
use strict;

use Template;

use Image::Gallery::Common;
use vars '@ISA';
@ISA = 'Image::Gallery::Common';

################################################################################
# new
#  Note: 'input' should be a hashref to a hash keyed on filename with each value
#        consisting of a hashref that looks like
#        %Image::Gallery::Caption::Fields
################################################################################
sub new {
 my ($class, $options) = @_;
 if (! ref $options) {
  shift;
  $options = scalar @_ ? {@_} : {};
 }

 my $self = {date => undef,
             debug => undef,
             dir => undef,
             file => 'index.html',
             input => {},
             post_process => undef,  # templates that should be processed afterwards
             pre_process => undef,   # templates that should be processed beforehand
             template_dir => '/usr/local/image_gallery/test/templates',
             template => 'body',
             title => undef};

 foreach (keys %$self) {
  $self->{$_} = $options->{$_} if exists $options->{$_};
 }

 if (!$self->{dir}) {
  $class->fatal('No directory specified.'); 
 }

 if (!$self->{template_dir}) {
  $class->fatal('No template directory specified.');
 }

 # Set title to basename(file) if it isn't set
 if (!$self->{title}) {
  ($self->{title} = $self->{file}) =~ s#.*?([^/]+)$#$1#;
 }

 my %template_hash = (INCLUDE_PATH => $self->{template_dir},
                      OUTPUT_PATH => $self->{dir},
                      POST_PROCESS => $self->{post_process},
                      PRE_PROCESS => $self->{pre_process});

 $template_hash{DEBUG} = $self->{debug} if defined $self->{debug};

 $self->{_template} = Template->new(%template_hash);
 if (!$self->{_template}) {
  $class->fatal('Unable to create template object. ' . $!);
 }

 bless $self, $class;

 # Set date after creating object
 $self->SUPER::date();

 return $self;
} #new

################################################################################
# write_file
# I: $file         # Output file
#    $template     # Template file
#    $input        # Hashref of input data
################################################################################
sub write_file {
 my ($self, $file, $template, $input) = @_;
 if (!defined $file) {
  $self->debug('Using default file [' . $self->{file} . '].');
  $file = $self->{file};
 }
 if (!defined $template) {
  $self->debug('Using default template [' . $self->{template} . '].');
  $template = $self->{template};
 }
 if (!defined $input) {
  $self->debug('Using default input data.');
  $input = $self->{input};
 }

 $input->{date} ||= $self->{date};
 $input->{title} ||= $self->{title};

 if (!$self->{_template}->process($template, $input, $file)) {
  $self->fatal($self->{_template}->error());
 }

 return 1;
} #write_file

1;

__END__

=head1 NAME

Image::Gallery::Html - Web image gallery html output stage

=head1 SYNOPSIS

 use Image::Gallery::Html;

=head1 ABSTRACT

This module provides a mechanism for outputting html pages utilizing Template Toolkit templates, and Image::Gallery data.

=head1 DESCRIPTION

This modules outputs an html page for Image::Gallery.  It utilizes the Template Toolkit to abstract the html from the code.

=head1 METHODS

=head2 new(\%config)

The new() constructor method instantiates a new Image::Gallery:Html object.  A reference to a hash of configuration items may be passed in as a parameter.

=over

=back

=head1 AUTHOR

Douglas Leonard, E<lt>dleonard@dleonard.netE<gt>

L<http://nekoken.org/>

=head1 COPYRIGHT AND LICENSE

Copyright 2004-2006 by Douglas Leonard

This library is free software; you can redistribute it and/or modify it under the terms of the General Public License (GPL) version 2.  For more information, see http://www.fsf.org/licenses/gpl.txt

=cut

