# -------------------------------------------------------------------------------------
# flo::editor::Link
# -------------------------------------------------------------------------------------
#
#       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:
#
#      Text content component. Should let the user add simple text to pages
#
# -------------------------------------------------------------------------------------
package flo::editor::Text;
use flo::Editor;
use lib::Exception;
use flo::Standard;
use lib::cgi::CGI;
use lib::util::Text2Html;
use flo::XHTMLFilter;
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 (defined $cgi)
    {
	if (defined $cgi->param ($param_name . "_add"))
	{
	    $self->{data} = "";
	}
	else
	{
	    $self->{data} = $cgi->param ($param_name);
	}
    }
    else
    {
	$self->{data} = "";
    }
    $self->{class} = 'text';
    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/text', $lang);
}


sub edit
{
    my $self = shift;
    my $cgi  = $self->{cgi};

    return new flo::Template ( file => $self->template_path,
			       hash => {
				   block_name  => $self->{param_name},
				   block_value => $self->to_html,
			       } )->parse;
}


sub generate_xml
{
    my $self = shift;
    $self->{data} =~ s/\&/&amp;/g;
    $self->{data} =~ s/\</&lt;/g;
    $self->{data} =~ s/\>/&gt;/g;
    return $self->{data};
}


sub parse_xml
{
    my $self = shift;
    $self->{data} = shift;
    $self->{data} =~ s/\&lt\;/</g;
    $self->{data} =~ s/\&gt\;/>/g;
    $self->{data} =~ s/\&amp\;/&/g;
}


sub to_html
{
    my $self = shift;
    my $data = $self->{data};
    return text2html ($data);
}


sub html
{
    my $self = shift;
    my @data = split /\n/, $self->{data};
    my @out  = ('');
    foreach my $line (@data)
    {
	chomp ($line);
	if ($line !~ /^\s*$/x) { $out[$#out] .= $line . "\n" }
	else
	{
	    $out[$#out] eq '' and next;
	    push @out , '';
	}
    }
    
    my $res = join "</p>\n<p>\n", map { text2html ($_) } @out;
    return flo::XHTMLFilter->linkify ($res, flo::Editor->links);
}


1;

