#[BOFN]###############################################################################
#
#
#Pagenews - a free script to publish news on websites
#Copyright (C) 2004,2005,2006,2007,2008 Philipp Kindt
#
#This file is part of Pagenews.
#
# 	 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 3 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.
#[EOFN]###############################################################################


package dntempeditor;

use dntemplatemanager;

#this package provides functions for template editing

########################################################################################################
#this function needs a reference on %in in $_[0] and returns a header built from the form
#elements in the headerconf form.
sub rebuild_header{
	my $in = $_[0];
	
	my @header;
	my $functions;
	foreach my $key (keys(%$in)){
		my @parts = split(/\./,$key);
		my $registered = 0;
		if(($parts[0] eq "DNADMIN")&&($parts[1] eq "HEADERCONF")){
		
			if($parts[2] eq "UMODLINE"){
				chomp($in->{$key});
				$header[$parts[3] - 1] = $in->{$key};
			}elsif($parts[4] eq "FUNCTION"){
			
				
				#check if function in its instance is allready registered. if not, do it.
				for(my $cnt = 0; $cnt < @{$functions->{'registry'}->{'names'}}; $cnt++){
					if($parts[5] eq $functions->{'registry'}->{'names'}->[$cnt]){					#name is registered
						if($parts[7] eq $functions->{'registry'}->{'instances'}->[$cnt]){			#name is registered
							$registered = 1;
						};						
					};
				};
				
				if($registered == 0){
					push(@{$functions->{'registry'}->{'names'}},$parts[5]);
					push(@{$functions->{'registry'}->{'instances'}},$parts[7]);				
					push(@{$functions->{'registry'}->{'lines'}},$parts[3]);
				
					push(@{$functions->{'registry'}->{'name_by_instance'}->{$parts[7]}},$parts[5]);

				};
					push(@{$functions->{'data'}->{'paramregistry'}->{$parts[5]}->{$parts[7]}},$parts[9]);
					$functions->{'data'}->{'params'}->{$parts[5]}->{$parts[7]}->[$parts[9]] = $in->{$key};
					$functions->{'data'}->{'lines'}->{$parts[5]}->{$parts[7]} = $parts[3];
					
			};
		};
	};
	@{$functions->{'registry'}->{'instances'}} = sort {$a <=> $b} @{$functions->{'registry'}->{'instances'}};
	for(my $cnt = $functions->{'registry'}->{'instances'}->[$#{functions->{'registry'}->{'instances'}}]; $cnt > 0; $cnt--){	
		foreach $function (@{$functions->{'registry'}->{'name_by_instance'}->{$cnt}}){
			if($function ne ""){
				my $headerline = "[FUNCTION $function ";
				@pregistry = sort {$a <=> $b} @{$functions->{'data'}->{'paramregistry'}->{$function}->{$cnt}};			
				for(my $cnt2 = 0; $cnt2 < @pregistry;$cnt2++){
					$headerline .= "\"".$functions->{'data'}->{'params'}->{$function}->{$cnt}->[$pregistry[$cnt2]]."\" "; 
				};
				chop($headerline);
				$headerline .= "]";
				$header[$functions->{'data'}->{'lines'}->{$function}->{$cnt} - 1] = $headerline;
			};
		};
	};
	
	my $headertxt = donstdlib::array_to_string(\@header,"\n");
	return $headertxt;
};



1;

