# -------------------------------------------------------------------------------------
# flo::editor::Link
# -------------------------------------------------------------------------------------
#
#       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:
#
#      Link content component. Should let the user add hyperlinks to pages
#
# -------------------------------------------------------------------------------------
package flo::editor::Link;
use flo::Editor;
use flo::Standard;
use lib::Exception;
use lib::cgi::CGI;
use lib::util::Text2Html;
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->{url} = "http://";
	    $self->{title} = "";
	    $self->{description} = "";
	}
	else
	{
	    $self->{url} = join ' ', split /(?:\n|\r)/, $cgi->param ($param_name . "_url");
	    $self->{url} =~ s/^\s+//;
	    $self->{url} =~ s/\s+$//;
	    $self->{title} = join ' ', split /(?:\n|\r)/, $cgi->param ($param_name . "_title");
	    $self->{title} =~ s/^\s+//;
	    $self->{title} =~ s/\s+$//;
	    $self->{description} = join ' ', split /(?:\n|\r)/, $cgi->param ($param_name . "_description");
	    $self->{description} =~ s/^\s+//;
	    $self->{description} =~ s/\s+$//;	    
	}
    }
    else
    {
	$self->{url} = "";
	$self->{title} = "";
	$self->{description} = "";
    }
    $self->{class} = 'link';
    return $self;
}


sub full_url
{
    my $self = shift;
    my $url  = shift;
    return $url if ($url =~ /^(http|https|mailto|news|gopher):/);
    
    if ($url =~ /^\//)
    {
	return $ENV{SERVER_NAME} . $url;
    }
    else
    {
	return $ENV{SERVER_NAME} . $ENV{PATH_INFO} . $url;
    }
}


sub url
{
    my $self = shift;
    $self->{url} = shift || return $self->{url};
}


sub title
{
    my $self = shift;
    $self->{title} = shift || return $self->{title};
}


sub description
{
    my $self = shift;
    $self->{description} = shift || return $self->{description};
}


##
# 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/link', $lang);
}


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

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


sub generate_xml
{
    my $self = shift;
    $self->{title} =~ s/\&/&amp;/g;
    $self->{title} =~ s/\</&lt;/g;
    $self->{title} =~ s/\>/&gt;/g;
    $self->{description} =~ s/\&/&amp;/g;
    $self->{description} =~ s/\</&lt;/g;
    $self->{description} =~ s/\>/&gt;/g;
    my $url = $self->{url};
    my $description = $self->{description};
    my $title = $self->{title};
    return "<a href=\"$url\" title=\"$description\">$title</a>";
}


sub parse_xml
{
    my $self = shift;
    my $line = shift;
    if ($line)
    {
	my ($url, $description, $title) = $line =~ /\<a href=\"(.*?)\" title=\"(.*?)\"\>(.*)\<\/a\>/;
	$self->{url} = $url;
	$self->{description} = $description;
	$self->{title} = $title;
    }
    else
    {
	$self->{url} = "";
	$self->{description} = "";
	$self->{title} = "";
    }
}


sub html
{
    my $self  = shift;
    my $url   = $self->{url};
    my $title = text2html ($self->{title});
    my $desc  = text2html ($self->{description});
    return "<a href=\"$url\" title=\"$title\">$desc</a>";
}


sub to_html { return shift->html }


1;

