package render_config;
require Exporter;
@ISA = qw(Exporter);
@EXPORT = qw(render_config);

#----------------------------------------------------------------------------
# /var/config/www/cgi-bin/renderer_config.pm - render configuration
#
# 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;

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

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



#############################################################################
# sub render_config($aref, $href, $cf, $ishelp)
# - writes a new config-file
# IN:  $renderer  - reference to hash with rendering functions
#      $astruct   - reference on the form-structure (see parse_confform)
#      $hvalues   - reference on a hash with the options from the config-file
# OUT: -
##############################################################################
sub render_config
{
   my ($renderer, $astruct, $hvalues) = @_;
   my ($form, $aform, $group, $agroup, $element, $helement);

   #### print page header
   # format would be $astruct->[0]
   $renderer->{"call"}->("print_header");

   ### loop over all forms
   foreach $form (1 .. $#$astruct )
   {
      $aform = $astruct->[$form];

      ### print form header
      #  format would be $aform[0]
      $renderer->{"call"}->("begin_form");

      ### loop over all groups
      foreach $group (1 .. $#$aform )
      {
         ### get current group
         $agroup = $astruct->[$form][$group];

         ### print group header
         my $name = $astruct->[$form][$group][0]{"name"};
         $renderer->{"call"}->("begin_group", $astruct->[$form][$group][0]);

         ### render all elements
         render_elements($renderer, $astruct->[$form][$group], $hvalues, "", "", "");

         ### print group footer
         $renderer->{"call"}->("end_group", $astruct->[$form][$group][0]);
      }

      if ($astruct->[$form][0]{"user-vars"} =~ /^(hidden|show|edit)$/)
      {
          my %xgroup = ("name", "User set variables");
          my @agroup = "";

          ### print goup header
          if ($astruct->[$form][0]{"user-vars"} =~ /^(show|edit)$/)
          {
              $renderer->{"call"}->("begin_group", \%xgroup);
          }

          foreach (keys %$hvalues)
          {
              my %helement = "";

              %helement->{"type"} = $astruct->[$form][0]{"user-vars"};
              %helement->{"name"} = $_;

              push(@agroup, \%helement);
          }

          ### render all elements
          render_elements($renderer, \@agroup, $hvalues, "", "", "");

          ### print group footer
          if ($astruct->[$form][0]{"user-vars"} =~ /^(show|edit)$/)
          {
              $renderer->{"call"}->("end_group", \%xgroup);
          }
      }

      ### print form footer
      $renderer->{"call"}->("end_form");
   }

   ### print page footer
   $renderer->{"call"}->("print_footer");

   ### finish
   $renderer->{"call"}->("close");
}




#############################################################################
# sub render_elements($renderer, $helements, $hvalues, $mymaster, $prefix, $replace)
# - writes a new config-file
# IN:  $renderer  - reference to hash with rendering functions
#      $helements - reference on the structure of the current group
#      $hvalues   - reference on a hash with the options from the config-file
#      $mymaster  - name of the master, if slaves are to be rendered
#      $prefix    - prefix of the variable name, which has to be replaced
#      $replace   - replace with this new prefix
# OUT: -
##############################################################################
sub render_elements
{
   my ($renderer, $helements, $hvalues, $mymaster, $prefix, $replace) = @_;
   my ($element, $type, $name, $val, $master, $slave, $undefined);

   ### loop over all elements
   foreach $element (1 .. $#$helements )
   {
      $master = $helements->[$element]{"master"} || "";
      next if ("$mymaster" ne "$master");

      $type = $helements->[$element]{"type"};
      $name = $helements->[$element]{"name"};
      $name =~ s/^$prefix/$replace/  if ($prefix && $name && $replace);
      $val  = delete($hvalues->{"$name"});

      $type = "default"   if(!defined($type));
      $name = "unknown"   if(!defined($name));

      $undefined = $hvalues->{"UNDEFINED_$name"};

      if (!defined($val))
      {
         if ($helements->[$element]{"match"} =~ m/^numeric$/i) { $val = "0"; }
         else                                                  { $val = "";  }

         # handling of optional variables
         $undefined = $helements->[$element]{"optional"}; 
      }

      $renderer->{"print"}->($type, $name, $val, $helements->[$element], $undefined);

      if ($helements->[$element]{"ismaster"} && $val)
      {
         #print dependent values
         $renderer->{"call"}->("begin_list") unless ($mymaster);

         foreach $slave (1 .. $val)
         {
            my $newreplace = $helements->[$element]{"ismaster"};
            $newreplace =~ s/^$prefix/$replace/ if ($prefix);
            $newreplace =~ s/%+/$slave/;

            render_elements($renderer, $helements, $hvalues,
                            $helements->[$element]{"name"},
                            $helements->[$element]{"ismaster"},
                            $newreplace);

            $renderer->{"call"}->("next_list") unless ($mymaster);
         }

         $renderer->{"call"}->("end_list") unless ($mymaster);
      }
   }
}

1;

