#[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 dnheaderconf;
require donoptionformelements; 
require dntemplatemanager;
require dnfeeds;

#this package provides functions for header configuration.
#it works like this:
#if not requested differently, every header line results in an
#<input type = "hidden" name = "dnheaderconf_headerline_[INDEX]" value = "[THE FULL HEADER LINE">.
#there can be config handlers added by add_config_handler.

#the rebuilding function is in dntempeditor.pm
########################################################################################################
sub new{
	my	$classname = $_[0];
	my $self = {};
	bless($self,$classname);
	$self->{'handler_count'} = "0";
	return $self;
};
########################################################################################################
#adds a config-handler for function name $_[1], instance (= the n-th appearance of the same function in the header) $_[2] 
#and param number $_[3].
#$_[4] contains the type of the element which can be "text", "dropdown", "list" ,"templates", "feeds" and "category".
#-if "templates", the param $_[5] and all later ones specifies the templates to list: it can contain a parent template;
#all templates derived from it are included.
#-if "feeds", a list of feeds is included.
#-if "type",  all templates of type $_[5] (and all later params) are included to the list
#if "list" or "dropdown", $_[5] specifies a possible option, $_[6] the human-readable version of it.
#the same for $_[7] and $_[8] and so on.

sub add_handler{
	my $self = $_[0];
	my $settings = {};

	if(($_[4] ne "templates")&&($_[4] ne "feeds")&&($_[4] ne "category")&&($_[4] ne "type")){
		$settings->{'type'} = "$_[4]";
		
		
		for (my $cnt = 5; $cnt < @_; $cnt = $cnt + 2){
			push(@{$settings->{'possible_values'}},$_[$cnt]);
			push(@{$settings->{'possible_human_values'}},$_[$cnt + 1]);
		}; 

	}else{

		#more special types
		$settings->{'type'} = "dropdown";
		if($_[4] eq "templates"){
			my @copy = @_;
			splice(@copy,0,5);

			foreach my $template (dntemplatemanager::get_templates_by_draft(donstdlib::array_to_string(\@copy," "))){
				push(@{$settings->{'possible_values'}},$template);
			};
			
		}elsif($_[4] eq "feeds"){
			my @feeds = dnfeeds->get_list_of_feeds();
			foreach $feed(@feeds){
				push(@{$settings->{'possible_values'}},$feed);				
				push(@{$settings->{'possible_human_values'}},$feed);				

			};
			#add aditional params to list:
			for (my $cnt = 5; $cnt < @_; $cnt = $cnt + 2){
				push(@{$settings->{'possible_values'}},$_[$cnt]);
				push(@{$settings->{'possible_human_values'}},$_[$cnt + 1]);
			}; 


		}elsif($_[4] eq "type"){
			my @copy = @_;
			splice(@copy,0,5);
			$fakein->{'task'} = "user";
			@{$settings->{'possible_values'}} = dntemplatemanager::get_templates_by_type(donstdlib::array_to_string(\@copy," "));
		
		};	
		
		#avoid emty dropdowns as at least one value is necessary
		if(@{$settings->{'possible_values'}} == 0){
				push(@{$settings->{'possible_values'}},donstdlib::get_message("dnheaderconf_empty_value"));							
		};
		

	};
	
	 
	$self->{'confreqs'}->[$self->{'handler_count'}]->{'name'} = $_[1];					#add to names

	$self->{'confreqs'}->[$self->{'handler_count'}]->{'instance'} = $_[2];				#counting from one is considered in create_config_function_elements()
	
	$self->{'confreqs'}->[$self->{'handler_count'}]->{'param'} = $_[3] - 1;			#minus one as we start counting from one
	
	
	$self->{'confreqs'}->[$self->{'handler_count'}]->{'settings_descriptor'} = $settings;
	
	$self->{'handler_count'}++;			#number of handlers

};
########################################################################################################
#creates all Donparser handlers in parser $_[1] for the header of parser $_[2]

#it provides handlers to the parser $_[1] to get header data:
#
#for each function that has to be configured:
#[DNADMIN.HEADERCONF.FUNCTION.$function.INSTANCE.$instcnt{$function}.NROFPARAMS] :number of params of a function (instance)
#..and for each param:
#[DNADMIN.HEADERCONF.FUNCTION.$function.INSTANCE.$instcnt{$function}.PARAM.$userparam.CONFIGURATION]	:	the html code for configuration of a function param
#[DNADMIN.HEADERCONF.FUNCTION.$function.INSTANCE.$instcnt{$function}.PARAM.$userparam.VALUE]				:	the value of an unconfigured param
#[DNADMIN.HEADERCONF.FUNCTION.$function.INSTANCE.$instcnt{$function}.PARAM.$userparam.HIDDENELEMENT]	:	html code for hidden form element that contains the param				
#
#for each function:
#[DNADMIN.HEADERCONF.FUNCTION.$function.INSTANCE.$instcnt{$function}.NONCONFPARAMS.HIDDENELEMENTS]		: 	html form elements for all params of a function which are not to be configured


#once:
#[DNADMIN.HEADERCONF.ALL.NONCONF.PARAMS.HIDDENELEMENTS]	:	html code for hidden form elements for params that weren't to configure of all functions which something was to configure
#[DNADMIN.HEADERCONF.NONCONFLINES.HIDDENELEMENTS]			: 	html code for hidden form elements for all lines that did not contain anything to configure
#[DNADMIN.HEADERCONF.RESTOFHEADER.HIDDENELEMENTS]			:	html code for hidden form element that contains everything in the header that is not to be configured by the user; this replaces the two handlers abouve


#counting starts allways at 1, NOT AT ZERO as we are humans (hopefully)
#don't look at this function if you do not want to get bored

#please bear in mind that one line in the header must contain only one element. (only when using configlets)


#a bit off-topic:
#normaly, you write configlets like this
#1. create configuration requests
#2. set the [DNADMIN.HEADERCONF.FUNCTION.$function.INSTANCE.$instcnt{$function}.PARAM.$userparam.CONFIGURATION]
#as needed
#3. set [DNADMIN.HEADERCONF.RESTOFHEADER.HIDDENELEMENT]	

sub create_config_function_elements{
	my $self = $_[0];
	my $parser_page = $_[1];
	my $parser_config = $_[2];
	my $confline = 0;
	my $nonconftxt;					#a text containg hidden form tags for header lines that do not contain functions that have to be configured or do not contain functions at all
	 
	my $line_nonconf_elements;		#contains hidden form tags for function prameters that aren't to configure by the user
	my $all_nonconf_params;			#hidden form tags for parametes of all functions that aren't to configure by the user
	my $lincnt =	0;
	foreach $headerline (split(/\n/,$parser_config->{'header'})){							#go through each line
		$linecnt++;
		$confline = 0;																						#if this keeps beeing zero, the line is not to be configured
		
	#	$headerline = doncgitools::txt_to_html($headerline,"soft");
		$headerline =~ m/\[FUNCTION .*\]/g;															#find possible function handler		
		my $funchandler = $&;
		my $barebone = substr($funchandler,1,length($funchandler)-2);						#cut away the brackets
		(my $waste, my $function, my @temp_funcparams) = split(/\ /,$barebone);
		my $funcparamstr = donstdlib::array_to_string(\@temp_funcparams," "); 
		
		#retrive params - considering quotes!
		my @funcparams = $parser_page->create_func_param_array($funcparamstr,\@temp_funcparams,"sdf");
		
		if($function ne ""){
			$instcnt{$function}++;																			#increment the instance counter for the current function. this means the next function call is the next instance of it. as we are human - friendly, we start counting at one 				
	
	
			$line_nonconf_elements = "";
			for(my $paramcnt = 0; $paramcnt < @funcparams; $paramcnt++){						#go through each param of the function 	
				$funcparams[$paramcnt] = doncgitools::txt_to_html($funcparams[$paramcnt],"soft");
				for(my $confreqcnt = 0; $confreqcnt < $self->{'handler_count'}; $confreqcnt++){	#go through each config request
					if(($self->{'confreqs'}->[$confreqcnt]->{'name'} eq $function)&&($self->{'confreqs'}->[$confreqcnt]->{'instance'} eq $instcnt{$function})){								#check if function name and instantein confrequest and handler is the same
						
						#from this moment on, it's sure that this line contains something that has to be configured
						$confline = 1;

						$parser_page->add_handler("DNADMIN.HEADERCONF.FUNCTION.$function.INSTANCE.$instcnt{$function}.NROFPARAMS",@funcparams);
						
						#we begin counting at one...
						my $userparam = $paramcnt + 1;

					
						if($self->{'confreqs'}->[$confreqcnt]->{'param'} eq $paramcnt){
							#param is to be configured

							#complete settings descriptor
							$self->{'confreqs'}->[$confreqcnt]->{'settings_descriptor'}->{'value'} = $funcparams[$paramcnt];
							$self->{'confreqs'}->[$confreqcnt]->{'settings_descriptor'}->{'name'}="DNADMIN.HEADERCONF.LINE.$linecnt.FUNCTION.$function.INSTANCE.$instcnt{$function}.PARAM.$userparam.VALUE";
							$parser_page->add_handler("DNADMIN.HEADERCONF.FUNCTION.$function.INSTANCE.$instcnt{$function}.PARAM.$userparam.CONFIGURATION",donoptionformelements::create_settings_form_element($self->{'confreqs'}->[$confreqcnt]->{'settings_descriptor'}));
						}else{					
							#param is not to be configured
				
							#add handler for value 
							$parser_page->add_handler("DNADMIN.HEADERCONF.FUNCTION.$function.INSTANCE.$instcnt{$function}.PARAM.$userparam.VALUE",$funcparams[$paramcnt]);
							
							#add handler for hidden form element
							my $element = "<input type = \"hidden\" name = \"DNADMIN.HEADERCONF.LINE.$linecnt.FUNCTION.$function.INSTANCE.$instcnt{$function}.PARAM.$userparam.VALUE\" value = \"$funcparams[$paramcnt]\">";
							$parser_page->add_handler("DNADMIN.HEADERCONF.FUNCTION.$function.INSTANCE.$instcnt{$function}.PARAM.$userparam.HIDDENELEMENT",$element);		
							#add it to a html text that contains all params not to be configured in line
							$line_nonconf_elements .= $element."\n";
							
							#add it to all params (not just of this function) that do not have to be configured
							$all_nonconf_params .= $element."\n";
						};
					};
				};
			};
		};

		if($confline == 0){
			#add header line as an unmodified line 	
			$headerline = doncgitools::txt_to_html($headerline,"soft");
			$nonconftxt .= "<input type = \"hidden\" name = \"DNADMIN.HEADERCONF.UMODLINE.$linecnt.VALUE\" value = \"$headerline\">\n";
		
		}else{
			$parser_page->add_handler("DNADMIN.HEADERCONF.FUNCTION.$function.INSTANCE.$instcnt{$function}.NONCONFPARAMS.HIDDENELEMENTS",$line_nonconf_elements);								
		};
		
	};
	
	
	$parser_page->add_handler("DNADMIN.HEADERCONF.ALL.NONCONF.PARAMS.HIDDENELEMENTS",$all_nonconf_params);
	
	$parser_page->add_handler("DNADMIN.HEADERCONF.NONCONFLINES.HIDDENELEMENTS",$nonconftxt);
	
	$parser_page->add_handler("DNADMIN.HEADERCONF.RESTOFHEADER.HIDDENELEMENTS",$nonconftxt.$all_nonconf_params);
	
}; 

