# -------------------------------------------------------------------------------------
# flo::editor::File
# -------------------------------------------------------------------------------------
#
#       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 class that can be used to attach any kind of file to an MKDoc document
#
# -------------------------------------------------------------------------------------
package flo::editor::File;
use lib::util::Text2Html;
use flo::Standard;
use flo::Editor;
use strict;

sub new
{
    my $class = shift;
    $class    = ref $class || $class;
    my $self  = bless { @_ }, $class;
    
    my $cgi = $self->{cgi};
    my $param_name = $self->{param_name};
    
    # if CGI is defined, then we probably want to do some stuff
    if (defined $cgi)
    {
	# If the component has just been added
	if (defined $cgi->param ( $param_name . "_add" ) )
	{
	    # No file has been uploaded yet
	    $self->{file} = "";
	    
	    # And thus, same for the title tag
	    $self->{title} = "";
	}
	
	# Else some modifications may have been made
	# to the file
	else
	{
	    # update title field
	    if (defined $cgi->param ($param_name . "_title"))
	    {
		$self->{title} = $cgi->param ($param_name . "_title");
	    }
	    else { $self->{title} = "" }
	    
	    # if the upload field is not empty, then the user
	    # just uploaded a new file
	    if (defined $cgi->param ($param_name . "_upload"))
	    {
		my $new_file = $cgi->param ($param_name);
		if ($new_file)
		{
		    # if the user previously uploaded another file,
		    # then it should be removed
		    my $old_file = $cgi->param ($param_name . "_file");
		    if ($old_file)
		    {
			unlink general_config->get ('file_disk_path') . "/" . $old_file;
		    }
		    
		    # then, we should upload the new file and set the
		    # current object
		    my ($fil_name, $fil_ext) = $new_file =~ /(.*)\.(.*)/;
		    $fil_ext = lc ($fil_ext);
		    if ($old_file) { $old_file =~ s/\..*?$// }
		    $fil_name = $old_file || $self->compute_name;
		    unless ($fil_ext) { $fil_ext = 'unknown' }
		    my $filename = general_config->get ('file_disk_path') . "/" . $fil_name . "." . $fil_ext;    
		    
		    no strict;
		    open (FP, ">$filename") or die "Cannot open $filename";
		    my $buffer;
		    $filename = $cgi->param($param_name);
		    while ( read ($filename, $buffer, 1024)) {
			print FP $buffer;
		    }
		    close FP;
		    use strict;
		    
		    my $info_filename = general_config->get ('file_disk_path') . "/." . $fil_name . "." . $fil_ext . ".nfo";
		    open (FP, ">$info_filename") or die "Cannot open $info_filename";
		    print FP $new_file;
		    close FP;
		    
		    $self->{file} = $fil_name . "." . $fil_ext;
		}
	    }
	    else
	    {
		$self->{file} = $cgi->param ($param_name . "_file");
	    }
	}
    }

    $self->{class} = 'file';
    return $self;
}


##
# Computes the path of the template which has to be used for
# that component given the current document language.
##
sub template_path
{
    my $self = shift;
    
    my $document_table = table ('Document');
    my $document = $document_table->get ( Full_Path => path_info );
    my $lang = $document->{Lang};
    
    return template ('editor/file', $lang);
}


##
# Computes an 8 character name for old MSDOS systems
# (this is much nicer under MICROS~1 Windows)
##
sub compute_name
{
    my $self = shift;
    my @char = ('a'..'z', 'A'..'Z', 0..9);
    my $size = scalar (@char);
    
    return join '', map { $char[int rand $size] } 1..8;
}


sub edit
{
    my $self = shift;
    my $cgi  = $self->{cgi};
    
    return new flo::Template ( file => $self->template_path,
			       hash => {
				   block_name  => $self->{param_name},
				   file        => $self->{file},
				   title       => $self->{title},
				   file_url    => general_config->get ("file_http_path") . "/" . $self->{file}
			       } )->parse;
}


sub generate_xml
{
    my $self = shift;
    if ($self->{file})
    {
	my $src = general_config->get ("file_http_path") . "/" . $self->{file};
	my $title = text2html ($self->{title});
	return qq |<a href="$src">$title</a> />|;
    }
    else { return "" }
}


sub parse_xml
{
    my $self = shift;
    my $line = shift;
    if ($line)
    {
	my ($file, $title) = $line =~ /\<a href=\"(.*?)\"\>(.*)\<\/a\> \/\>/;
	my $q_file_http_path = quotemeta (general_config->get ("file_http_path"));
	$file =~ s/^$q_file_http_path//o;
	$file =~ s/^\///o;
	$self->{file}  = $file;
	$self->{title} = $title || $self->{file};
    }
    else
    {
	$self->{file}  = "";
	$self->{title} = "";
    }
}


sub html
{
    my $self = shift;
    return $self->generate_xml;
}


1;
