webgui/lib/WebGUI/Form.pm
2005-07-27 23:03:46 +00:00

287 lines
7.3 KiB
Perl

package WebGUI::Form;
=head1 LEGAL
-------------------------------------------------------------------
WebGUI is Copyright 2001-2005 Plain Black Corporation.
-------------------------------------------------------------------
Please read the legal notices (docs/legal.txt) and the license
(docs/license.txt) that came with this distribution before using
this software.
-------------------------------------------------------------------
http://www.plainblack.com info@plainblack.com
-------------------------------------------------------------------
=cut
use strict;
use Tie::IxHash;
use WebGUI::Asset;
use WebGUI::Asset::RichEdit;
use WebGUI::Asset::Template;
use WebGUI::DateTime;
use WebGUI::International;
use WebGUI::Session;
use WebGUI::SQL;
use WebGUI::Style;
use WebGUI::URL;
use WebGUI::Utility;
=head1 NAME
Package WebGUI::Form
=head1 DESCRIPTION
This is a convenience package which provides a simple interface to use all of the form controls without having to load each one seperately, create objects, and call methods.
=head1 SYNOPSIS
use WebGUI::Form;
$html = WebGUI::Form::formFooter();
$html = WebGUI::Form::formHeader();
$html = WebGUI::Form::anyFieldType(%properties);
Example:
$html = WebGUI::Form::text(%properties);
=head1 METHODS
These functions are available from this package:
=cut
#-------------------------------------------------------------------
=head2 AUTOLOAD ()
Dynamically creates functions on the fly for all the different form control types.
=cut
sub AUTOLOAD {
our $AUTOLOAD;
my $name = (split /::/, $AUTOLOAD)[-1];
my @params = @_;
my $cmd = "use WebGUI::Form::".$name;
eval ($cmd);
if ($@) {
WebGUI::ErrorHandler::error("Couldn't compile form control: ".$name.". Root cause: ".$@);
return undef;
}
my $class = "WebGUI::Form::".$name;
return $class->new(@params)->toHtml;
}
#-------------------------------------------------------------------
sub _fixMacros {
my $value = shift;
$value =~ s/\^/\&\#94\;/g;
return $value;
}
#-------------------------------------------------------------------
sub _fixQuotes {
my $value = shift;
$value =~ s/\"/\&quot\;/g;
return $value;
}
#-------------------------------------------------------------------
sub _fixSpecialCharacters {
my $value = shift;
$value =~ s/\&/\&amp\;/g;
return $value;
}
#-------------------------------------------------------------------
sub _fixTags {
my $value = shift;
$value =~ s/\</\&lt\;/g;
$value =~ s/\>/\&gt\;/g;
return $value;
}
#-------------------------------------------------------------------
=head2 dynamicField ( fieldType , hashRef )
Returns a dynamic configurable field.
=head3 fieldType
The field type to use. The field name is the name of the method from this forms package.
=head3 options
The field options. See the documentation for the desired field for more information.
=cut
sub dynamicField {
my $fieldType = shift;
my $param = shift;
# Set options for fields that use a list.
if (isIn($fieldType,qw(selectList checkList radioList))) {
delete $param->{size};
my %options;
tie %options, 'Tie::IxHash';
foreach (split(/\n/, $param->{possibleValues})) {
s/\s+$//; # remove trailing spaces
$options{$_} = $_;
}
if (exists $param->{options} && ref($param->{options}) eq "HASH") {
%options = (%{$param->{options}} , %options);
}
$param->{options} = \%options;
}
# Convert value to list for selectList / checkList
if (isIn($fieldType,qw(selectList checkList)) && ref $param->{value} ne "ARRAY") {
my @defaultValues;
foreach (split(/\n/, $param->{value})) {
s/\s+$//; # remove trailing spaces
push(@defaultValues, $_);
}
$param->{value} = \@defaultValues;
}
# Return the appropriate field.
no strict 'refs';
return &$fieldType($param);
}
#-------------------------------------------------------------------
=head2 file ( hashRef )
Returns a file upload field.
=head3 name
The name field for this form element.
=head3 extras
If you want to add anything special to this form element like javascript actions, or stylesheet information, you'd add it in here as follows:
'onChange="this.form.submit()"'
=head3 size
The number of characters wide this form element should be. There should be no reason for anyone to specify this.
=cut
sub file {
my $params = shift;
my $size = $params->{size} || $session{setting}{textBoxSize} || 30;
return '<input type="file" name="'.$params->{name}.'" size="'.$size.'" '.$params->{extras}.' />';
}
#-------------------------------------------------------------------
=head2 files ( hashRef )
Returns a multiple file upload control.
=head3 name
The name field for this form element.
=cut
sub files {
WebGUI::Style::setScript($session{config}{extrasURL}.'/FileUploadControl.js',{type=>"text/javascript"});
my $uploadControl = '<div id="fileUploadControl"> </div>
<script>
var images = new Array();
';
opendir(DIR,$session{config}{extrasPath}.'/fileIcons');
my @files = readdir(DIR);
closedir(DIR);
foreach my $file (@files) {
unless ($file eq "." || $file eq "..") {
my $ext = $file;
$ext =~ s/(.*?)\.gif/$1/;
$uploadControl .= 'images["'.$ext.'"] = "'.$session{config}{extrasURL}.'/fileIcons/'.$file.'";'."\n";
}
}
$uploadControl .= 'var uploader = new FileUploadControl("fileUploadControl", images, "'.WebGUI::International::get('removeLabel','WebGUI').'");
uploader.addRow();
</script>';
return $uploadControl;
}
#-------------------------------------------------------------------
=head2 formFooter ( )
Returns a form footer.
=cut
sub formFooter {
return "</div></form>\n\n";
}
#-------------------------------------------------------------------
=head2 formHeader ( hashRef )
Returns a form header.
=head3 action
The form action. Defaults to the current page.
=head3 method
The form method. Defaults to "post".
=head3 enctype
The form enctype. Defaults to "multipart/form-data".
=head3 extras
If you want to add anything special to the form header like javascript actions or stylesheet info, then use this.
=cut
sub formHeader {
my $params = shift;
my $action = $params->{action} || WebGUI::URL::page();
my $hidden;
if ($action =~ /\?/) {
my ($path,$query) = split(/\?/,$action);
$action = $path;
my @params = split(/\&/,$query);
foreach my $param (@params) {
$param =~ s/amp;(.*)/$1/;
my ($name,$value) = split(/\=/,$param);
$hidden .= hidden({name=>$name,value=>$value});
}
}
my $method = $params->{method} || "post";
my $enctype = $params->{enctype} || "multipart/form-data";
return '<form action="'.$action.'" enctype="'.$enctype.'" method="'.$method.'" '.$params->{extras}.'><div class="formContents">'.$hidden;
}
1;