package renderer_html;
require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(create_renderer_html);

#----------------------------------------------------------------------------
# /var/config/www/cgi-bin/renderer_html.pm - render configuration to browser
#
# Creation:     06.01.2004  max
# Last Update:  $Id$
#
# Copyright (c) 2004-2005  eisfair webconf team
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#----------------------------------------------------------------------------

use strict;
use Fcntl ':flock';
use renderer;
use webconf_base;

our $VERBOSE;
our $CONF_LINE_LEN;
our $SERV_LINE_LEN;

no strict "subs";
do "conf.pl";
use strict "subs";


##############################################################################
# sub create_renderer_html()
# - prints html config-form
# IN:  $package    - name of the package
#      $classname  - name for the CSS-class
#      $parameter  - parameter to pass to apply.pl
# OUT: renderer    - reference to hash with rendering functions
##############################################################################
sub create_renderer_html
{
    my ($package, $classname, $parameter) = @_;

    ### build up hashtable for rendering configfiles
    my $renderer = create_renderer($package, $classname);
    $renderer->{"info"}->{"parameter"} = $parameter;

    ### default will be used if no specific function is provided
    $renderer->{"print_default"}   = html_print_edit($renderer);
    $renderer->{"print_typeerror"} = html_print_typeerror($renderer);

    ### empty line
    $renderer->{"print_empty"}     = html_print_empty($renderer);

    ### header and footer
    $renderer->{"print_header"}    = html_print_header($renderer);
    $renderer->{"print_footer"}    = html_print_footer($renderer);

    ### forms
    $renderer->{"begin_form"}      = html_begin_form($renderer);
    $renderer->{"end_form"}        = html_end_form($renderer);

    ### groups
    $renderer->{"begin_group"}     = html_begin_group($renderer);
    $renderer->{"end_group"}       = html_end_group($renderer);

    ### lists
    $renderer->{"begin_list"}      = html_print_empty($renderer);
    $renderer->{"next_list"}       = html_print_empty($renderer);
#    $renderer->{"end_list"}        = html_print_empty($renderer);

    ### config items
    $renderer->{"print_edit"}      = html_print_edit($renderer);
    $renderer->{"print_show"}      = html_print_show($renderer);
    $renderer->{"print_select"}    = html_print_select($renderer);
#    $renderer->{"print_ref"}       = html_print_ref($renderer);
    $renderer->{"print_hidden"}    = html_print_hidden($renderer);

    ### close renderer
    $renderer->{"close"}           = html_close($renderer);

    ### return renderer
    return $renderer
}


#############################################################################
#
# implementation of the renderer
# - structure -
#
#############################################################################

sub html_close
{
my ($renderer) = @_;
return sub
{
}
}


sub html_print_header
{
my ($renderer) = @_;
my $classname = $renderer->{"info"}->{"classname"};
my $package   = $renderer->{"info"}->{"package"};
return sub
{
}
}


sub html_print_footer
{
my ($renderer) = @_;
my $classname = $renderer->{"info"}->{"classname"};
return sub
{
}
}


sub html_begin_form
{
my ($renderer) = @_;
my $classname = $renderer->{"info"}->{"classname"};
my $package   = $renderer->{"info"}->{"package"};
my $parameter = $renderer->{"info"}->{"parameter"};
return sub
{
    print "<form class=\"$classname\" ";
    print "action=\"edit.pl?$parameter\" ";
    print "method=\"post\">\n";
}
}


sub html_end_form
{
my ($renderer) = @_;
my $classname = $renderer->{"info"}->{"classname"};
my $package   = $renderer->{"info"}->{"package"};
return sub
{
    print "<br>\n";
    print "<input class=\"button-$classname\" type=\"submit\" name=\"action\" value=\"   refresh   \">";
    print "&nbsp;&nbsp;&nbsp;";
    print "<input class=\"button-$classname\" type=\"submit\" name=\"action\" value=\" save & apply \">";
    print "<br>\n";
    print "</form>\n";
}
}


sub html_begin_group
{
my ($renderer) = @_;
my $classname = $renderer->{"info"}->{"classname"};
return sub
{
    my ($hformat) = @_;

    print "<BR>\n";
    print "<fieldset class=\"$classname\">";

    if ($hformat->{"name"})
    {
        my $name    = $hformat->{"name"};
        my $comment = $hformat->{"comment"};
        print "<legend class=\"$classname\"";
        print " title=\"$comment\"" if ($comment);
        print ">$name</legend>\n";
    }
    print "<BR><table class=\"$classname\">\n";

    $renderer->{"status"}->{"in_group"} = "true";
}
}


sub html_end_group
{
my ($renderer) = @_;
return sub
{
    my ($hformat) = @_;

    print "</table>";
    print "</fieldset>" if ($hformat->{"name"});
    print "\n";

    $renderer->{"status"}->{"in_group"} = "false";
}
}


sub html_print_empty
{
my ($renderer) = @_;
return sub
{
    if ($renderer->{"status"}->{"in_group"} =~ /^true$/)
    { 
        print "<tr><td><br></td></tr>\n";
    }
    else
    {
        print "<p><br></p>\n";
    }
}
}


#############################################################################
#
# implementation of the renderer
# - elements -
#
#############################################################################

sub html_showhelp
{
    my ($renderer, $hname, $htext, $classname) = @_;

    if ($hname && $htext)
    {
        $hname =~ s/#/%23/g;
        $htext = make_html($htext);

        print "<span onMouseOver=\"document.getElementById('help_$hname').style.visibility = 'visible';\" ";
        print        "onMouseOut=\"document.getElementById('help_$hname').style.visibility = 'hidden';\" ";
        print        "class=\"helplink\">";
        print "Show help</span><br><div class=\"help\" id=\"help_$hname\">\n";
        print $htext;
        print "</div>\n";
    }
}


sub html_print_typeerror
{
my ($renderer) = @_;
my $classname = $renderer->{"info"}->{"classname"};
my $package   = $renderer->{"info"}->{"package"};
return sub
{
    my $name   = shift;

    print "<tr class=\"$classname\">\n";
    print "<td class=\"optname-$classname\">$name: </td>\n";
    print "<td class=\"opt-$classname\">";
    print "<b>ERROR: invalid type</b></td>\n";
    print "</tr>\n";
}
}


sub html_print_hidden
{
my ($renderer) = @_;
my $classname = $renderer->{"info"}->{"classname"};
my $package   = $renderer->{"info"}->{"package"};
return sub
{
    my ($name, $value, $hstruct, $undefined) = @_;

    print "<tr class=\"$classname\">\n";
    print "<td>&nbsp;</td>\n";
    print "<td class=\"opt-$classname\">";
    print "<input class=\"text-$classname\" name=\"$name\" ";
    print "type=\"hidden\" value=\"$value\">";
    print "<input type=\"hidden\" name=\"UNDEFINED_$name\" value=\"yes\">" if ($undefined eq "yes");
    print "</td></tr>\n";
}
}


sub html_print_show
{
my ($renderer) = @_;
return sub
{
    my ($name, $value, $hstruct, $undefined) = @_;
    $hstruct->{"readonly"} = "yes";
    $renderer->{"print_edit"}($name, $value, $hstruct, $undefined);
}
}


sub html_print_edit
{
my ($renderer) = @_;
my $classname = $renderer->{"info"}->{"classname"};
my $package   = $renderer->{"info"}->{"package"};
return sub
{
    my ($name, $value, $hstruct, $undefined) = @_;
    my ($comment, $readonly) = "";
    my $length = "";

    $comment  = $hstruct->{"comment"}   || "" if ($hstruct);
    $readonly = $hstruct->{"readonly"}  || "" if ($hstruct);
    if ($readonly eq "yes") { $readonly = "readonly"; $length = length($value); }
    else { $readonly = ""; $length = $CONF_LINE_LEN; }

    print "<tr class=\"$classname\">\n";
    print "<td class=\"optname-$classname\">$name = </td>\n";
    print "<td class=\"opt-$classname\">";
    print "<input class=\"text-$classname\" name=\"$name\" ";
    print "type=\"text\" size=\"$length\" ";
    print "title=\"$comment\" "     if ($comment);
    print "maxlength=\"255\" value=\"$value\" $readonly>";

    print "<input type=\"hidden\" name=\"UNDEFINED_$name\" value=\"yes\">" if ($undefined eq "yes");

    ### helptopic found
    html_showhelp($renderer, $hstruct->{"name"}, $hstruct->{"help"}, $classname);
    print "</td></tr>\n";
}
}


sub html_print_select
{
my ($renderer) = @_;
my $classname = $renderer->{"info"}->{"classname"};
my $package   = $renderer->{"info"}->{"package"};
return sub
{
    my ($name, $value, $hstruct, $undefined) = @_;
    my ($comment, $readonly, $style) = "";
    my $size = 1;
    my $valref = "";

    $valref   = $hstruct->{"values"}    || "";
    $comment  = $hstruct->{"comment"}   || "" if ($hstruct);
    $readonly = $hstruct->{"readonly"}  || "" if ($hstruct);
    $style    = $hstruct->{"style"}     || "" if ($hstruct);

    if ($style =~ /list/ && $hstruct->{"size"} > 1)
    {
        $size = $hstruct->{"size"};
    }

    print "<tr class=\"$classname\">\n";
    print "<td class=\"optname-$classname\">$name = </td>\n";
    print "<td class=\"opt-$classname\">";

    if ($readonly eq "yes")
    {
        my $length = length($value);
        print "<input class=\"text-$classname\" name=\"$name\" ";
        print "type=\"text\" size=\"$length\" ";
        print "title=\"$comment\" "             if ($comment);
        print "maxlength=\"$length\" value=\"$value\" readonly>";
    }
    else
    {
        print "<select class=\"$classname\" name=\"$name\" size=\"$size\"";
        print " multiple"                    if ($style =~ /multiselect/);
        print " title=\"$comment\" "         if ($comment);
        print ">\n";

        foreach (@$valref)
        {
            print "<option class=\"$classname\"";
            print " selected"                 if ($value =~ /$_/);
            print ">$_</option>\n";
        }
        print "</select>\n";
    }

    print "<input type=\"hidden\" name=\"UNDEFINED_$name\" value=\"yes\">" if ($undefined eq "yes");

    ### helptopic found
    html_showhelp($renderer, $hstruct->{"name"}, $hstruct->{"help"}, $classname);
    print "</td></tr>\n";
}
}


sub html_print_ref
{
my ($renderer) = @_;
my $classname = $renderer->{"info"}->{"classname"};
my $package   = $renderer->{"info"}->{"package"};
return sub
{
    my ($name, $value, $hstruct, $undefined) = @_;

    my @valref;

    my $oname   = $hstruct->{"name"}    || "";
    my $length  = length($value) + 1;

    my $comment = $hstruct->{"comment"} || "";
    my $ro      = $hstruct->{"ro"}      || 0;


    my $sname = "/etc/config.d/".$hstruct->{"file"} || "";
    $sname = ""          if (!-f $sname);
    my $m     = $hstruct->{"ref"}                   || "";
    if ($sname && $m)
    {
        my $count = 0;
        open(SOURCE, "<$sname");
        while (<SOURCE>) { $count = $1 if (/^$m='([^']+)'/); }
        close(SOURCE);

        for (1 .. $count) { push(@valref, $_); }
    }
    else
    {
        $value = "No reference specified!"      if (!$m);
        $value = "Reference is zero!"           if ($m == 0);
        $value = "No configfile specified!"     if (!$sname);
        $value = "No configfile and no reference specified!" if (!$sname && !$m);
        $ro    = "yes";
    }

    print "<tr class=\"$classname\">\n";
    print "<td class=\"optname-$classname\">$name = </td>\n";
    print "<td class=\"opt-$classname\">";

    if ($ro eq "yes")
    {
        $length = length($value);
        print "<input class=\"text-$classname\" name=\"$name\" ";
        print "title=\"$comment\" "     if ($comment);
        print "type=\"text\" size=\"$length\"";
        print "maxlength=\"$length\" value=\"$value\" readonly>";
    }
    else
    {
        print "<select class=\"$classname\" name=\"$name\" size=\"1\"";
        print " title=\"$comment\" "     if ($comment);
        print " multiple";
        print ">\n";

        foreach (@valref)
        {
            print "<option class=\"$classname\"";
            print " selected"                       if ( $value =~ /$_/ );
            print ">$_</option>\n";
        }
        print "</select>\n";
    }

    print "<input type=\"hidden\" name=\"UNDEFINED_$name\" value=\"yes\">" if ($undefined eq "yes");

    ### helptopic found
    html_showhelp($renderer, $hstruct->{"name"}, $hstruct->{"help"}, $classname);
    print "</td></tr>\n";

}
}

1;

