#[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 donoptionformelements;

#this package provides the function create_settings_form_element
#########################################################################################################
#you can give a settings descriptor ($settings) to this function and it returns a html form element that enables choosing this setting
#this hash the folowing fileds:
#$settings->{'name'}							:	string				: name of the element
#$settings->{'value'}						:	string				: default value
#$settings->{'type'}							:	string				: type of config element: can be "text" for text filed, "dropdown" for dropdown menu or list for "list"  
#$settings->{'possible_values'}			:	array reference	: contains all possible values to be returned, only when the type is not "text"
#$settings->{'possible_human_values'}	:	array reference	: (optional) contains all possible values to be showed to the user,must have the same order as the possible values. Only when the type is not "text"
#$settings->{'size'}							:	integer				: (optional) size of the field  


sub create_settings_form_element{
	my $settings = $_[0];
	my $code;
	
	
	#set defaults of optional params which were left blank
	if($settings->{'size'} eq ""){
		if($settings->{'type'} eq "list"){
			$settings->{'size'} = 5;
		}else{
			$settings->{'size'} = 20;
		};
	};

	#make sure the parentheses aren't destroyed:
	$settings->{'name'} =~ s/\"/\\\"/g;
	$settings->{'name'} =~ s/\n//g;
	
	$settings->{'value'} =~ s/\"/\\\"/g;
	$settings->{'value'} =~ s/\n//g;
	
	$settings->{'size'} =~ s/[a-zA-Z]/ /g;
	$settings->{'list_rows'} =~ s/[a-zA-Z]/ /g;
	

#----------------------------------------------------------------------------------------	
#text
	if($settings->{'type'} eq "text"){	
		$code = "<input type = \"text\" name = \"$settings->{'name'}\" value = \"$settings->{'value'}\" size = \"$settings->{'size'}\">\n";


#----------------------------------------------------------------------------------------	
#dropdown and lists
	}elsif(($settings->{'type'} eq "dropdown")||($settings->{'type'} eq "list")){
		if($settings->{'type'} eq "dropdown"){
			$code .= "<select name = \"$settings->{'name'}\">\n";
		}elsif($settings->{'type'} eq "list"){
			$code .= "<select name = \"$settings->{'name'}\" size = \"$settings->{'size'}\">\n";		
		};
		
		for (my $cnt = 0; $cnt < @{$settings->{'possible_values'}}; $cnt++){
			$settings->{'possible_values'}->[$cnt] =~ s/\"/\\\"/g;
			$settings->{'possible_values'}->[$cnt] =~ s/\n//g;
			
			$settings->{'possible_human_values'}->[$cnt] =~ s/\n//g;
			$settings->{'possible_human_values'}->[$cnt] = doncgitools::txt_to_html($settings->{'possible_human_values'}->[$cnt]);
												

			if($settings->{'possible_human_values'}->[$cnt] eq ""){
				$settings->{'possible_human_values'}->[$cnt] = $settings->{'possible_values'}->[$cnt];
			};
			my $amandmend = "";
			if($settings->{'possible_values'}->[$cnt] eq $settings->{'value'}){
				$amandmend = " selected";
			};
			$code .= "<option value = \"$settings->{'possible_values'}->[$cnt]\"$amandmend>$settings->{'possible_human_values'}->[$cnt]</option>\n";
		};
		$code .= "</select>\n";		
	};
	return $code;
};



1;

