# -------------------------------------------------------------------------------------
# flo::editor::Image
# -------------------------------------------------------------------------------------
#
#       Author : Jean-Michel Hiver (jhiver@mkdoc.com).
#    Copyright : (c) MKDoc Holdings Ltd, 2001
# 
#      Unauthorized modification, use, reuse, distribution or redistribution
#      of this module is stricly forbidden.
#
#    Description:
#
#      Image content component. Should let the user upload images
#
# -------------------------------------------------------------------------------------
package flo::editor::Image;
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 image has been uploaded yet
	    $self->{image} = "";

	    # And thus, same for the alt tag
	    $self->{alt} = "";
	}
	
	# else some modifications may have been made
	# to the image
	else
	{
	    # update alt field
	    if (defined $cgi->param ($param_name . "_alt"))
	    {
		$self->{alt} = $cgi->param ($param_name . "_alt");
	    }
	    else { $self->{alt} = "" }
	    
	    # if the upload field is not empty, then the user
	    # just uploaded a new image
	    if (defined $cgi->param ($param_name . "_upload"))
	    {
		my $new_image = $cgi->param ($param_name);
		if ($new_image)
		{
		    # if the user previously uploaded another image,
		    # then it should be removed
		    my $old_image = $cgi->param ($param_name . "_img");
		    if ($old_image)
		    {
			unlink general_config->get ('image_disk_path') . "/" . $old_image;
		    }
		    
		    # then, we should upload the new image and set the
		    # current object
		    my ($img_name, $img_ext) = $new_image =~ /(.*)\.(.*)/;
		    $img_ext = lc ($img_ext);
		    if ($old_image) { $old_image =~ s/\..*?$// }
		    $img_name = $old_image || $self->compute_name;
		    
		    if ($img_ext !~ /^(?:jpg|gif|png)$/i) { $img_ext = 'unknown' }
		    my $filename = general_config->get ('image_disk_path') . "/" . $img_name . "." . $img_ext;
		    
		    open (FP, ">$filename") or die "Cannot open $filename";
		    my $buffer;
		    
		    no strict;
		    $filename = $cgi->param($param_name);
		    while ( read ($filename, $buffer, 1024)) {
			print FP $buffer;
		    }
		    use strict;
		    close FP;
		    
		    $self->{image} = $img_name . "." . $img_ext;
		}
	    }
	    else
	    {
		$self->{image} = $cgi->param ($param_name . "_img");
	    }
	}
    }

    $self->{class} = 'image';
    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/image', $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},
				   image       => $self->{image},
				   alt         => $self->{alt},
				   image_url   => general_config->get ("image_http_path") . "/" . $self->{image}
			       } )->parse;
}


sub generate_xml
{
    my $self = shift;
    if ($self->{image})
    {
	my $src = general_config->get ("image_http_path") . "/" . $self->{image};
	my $alt = text2html ($self->{alt});
	return qq |<img src="$src" alt="$alt" />|;
    }
    else { return "" }
}


sub parse_xml
{
    my $self = shift;
    my $line = shift;
    if ($line)
    {
	my ($path, $img, $alt) = $line =~ /<img src=\"(.*)\/(.*?)\" alt=\"(.*)\" \/>/;
	$self->{image} = $img;
	$self->{alt}   = $alt || $self->{image};
	$self->{alt}   = html2text ($alt);
    }
    else
    {
	$self->{image} = "";
	$self->{alt}   = "";
    }
}


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


1;
