# -------------------------------------------------------------------
#  flo::plugin::Cache::Writer
# -------------------------------------------------------------------
#
#  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::Cache::Writer;
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::Object;
use flo::plugin::Cache::Reader;
use strict;
use vars qw /@ISA/;
@ISA = qw /flo::Object/;


##
# __PACKAGE__->main;
# ------------------
#  We had a cache miss! Let's compress data, put it into the cache, and
#  send it to the browser
##
sub main
{
    my $class = shift;
    my $document_table = lib::sql::Table->table ('Document');
    my $document = $document_table->get ( Full_Path => path_info );
    
    # if Cache_Control is set to 0, then we want to pass the request
    # to flo::plugin::explorer::Public
    return unless ($document->{Cache_Control});
    
    my $cgi = lib::cgi::CGI->get;
    my $hash = {};
    $hash->{Document} = $document;
    
    my $cgix = $cgi->new;
    foreach ($cgix->param) { $cgix->delete ($_) }
    $hash->{URI} = { Action => $cgix->self_url };
    if (defined $hash->{Document})
    {
	# compute the data
	my $editor = new flo::Editor;
	$editor->parse_xml ($hash->{Document}->{Body});
	$hash->{Session} = {};
	my $data = new flo::Template ( file => template ('explorer/document/' . $document->Template, $document->Lang),
				       hash => $hash )->parse;
	
	# write the data to HTML page
	my $config = general_config;
	my $dir = $config->get ('cache_dir');
	my $file_name = $document->{Full_Path};
	$file_name = '__root__' if ($file_name eq '/');
	$file_name =~ s/^\///;
	$file_name =~ s/\/$//;
	$file_name =~ tr/\//_/;
	
	my $html_file_name = $dir . '/' . $file_name . '.html';
	my $time_file_name = $dir . '/' . $file_name . '.time';
	
	# print the cache timeout for that page
	open FP, ">$time_file_name" or
	    throw (new lib::Exception ( code => 'CANNOT_OPEN_FILE',
					info => "Could not write-open $time_file_name!" ) );
	print FP time + $document->{Cache_Control} * 60;
	close FP;
	
	# print the html headers and file name for that page
	open FP, ">$html_file_name" or
	    throw (new lib::Exception ( code => 'CANNOT_OPEN_FILE',
					info => "Could not write-open $html_file_name!" ) );
	$flo::HTTP::Header::ACCESSOR->set ('Content-Length: ' . length ($data));
	print FP $flo::HTTP::Header::ACCESSOR->header;
	print FP $data;
	close FP;
	
	# okay, now that everything's been written, let's send the data and terminate
        flo::plugin::Cache::Reader->main;
	return 'TERMINATE';
    }
    else { throw (new lib::Exception (info => path_info)) }
}


1;
