webgui/lib/WebGUI/FormProcessor.pm
2005-04-20 21:17:44 +00:00

693 lines
13 KiB
Perl

package WebGUI::FormProcessor;
=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 qw(vars subs);
use WebGUI::DateTime;
use WebGUI::HTML;
use WebGUI::Session;
=head1 NAME
Package WebGUI::FormProcessor;
=head1 DESCRIPTION
This package helps in the processing of the form variables that are returned from any WebGUI form.
=head1 SYNOPSIS
use WebGUI::FormProcessor;
$value = WebGUI::FormProcessor::process("favoriteColor","selectList","black");
$value = WebGUI::FormProcessor::asset("assetId");
$value = WebGUI::FormProcessor::checkbox("whichOne");
$value = WebGUI::FormProcessor::checkList("dayOfWeek");
$value = WebGUI::FormProcessor::codearea("snippet");
$value = WebGUI::FormProcessor::color("highlightColor");
$value = WebGUI::FormProcessor::combo("fruit");
$value = WebGUI::FormProcessor::contentType("text");
$value = WebGUI::FormProcessor::date("endDate");
$value = WebGUI::FormProcessor::dateTime("whenToDoIt");
$value = WebGUI::FormProcessor::email("emailAddress");
$value = WebGUI::FormProcessor::fieldType("fieldType");
$value = WebGUI::FormProcessor::filterContent("javascript");
$value = WebGUI::FormProcessor::float("distance");
$value = WebGUI::FormProcessor::group("groupToPost");
$value = WebGUI::FormProcessor::hidden("wid");
$value = WebGUI::FormProcessor::hiddenList("colors");
$value = WebGUI::FormProcessor::HTMLArea("description");
$value = WebGUI::FormProcessor::integer("size");
$value = WebGUI::FormProcessor::interval("timeToLive");
$value = WebGUI::FormProcessor::password("identifier");
$value = WebGUI::FormProcessor::phone("cellPhone");
$value = WebGUI::FormProcessor::radio("whichOne");
$value = WebGUI::FormProcessor::radioList("dayOfWeek");
$value = WebGUI::FormProcessor::selectList("dayOfWeek");
$value = WebGUI::FormProcessor::template("templateId");
$value = WebGUI::FormProcessor::text("firstName");
$value = WebGUI::FormProcessor::textarea("emailMessage");
$value = WebGUI::FormProcessor::timeField("wakeupCall");
$value = WebGUI::FormProcessor::url("homepage");
$value = WebGUI::FormProcessor::yesNo("happy");
$value = WebGUI::FormProcessor::zipcode("workZip");
=head1 METHODS
These functions are available from this package:
=cut
sub _checkEmailAddy {
return ($_[0] =~ /^([A-Z0-9]+[._+-]?){1,}([A-Z0-9]+[_+-]?)+\@(([A-Z0-9]+[._-]?){1,}[A-Z0-9]+\.){1,}[A-Z]{2,4}$/i);
}
#-------------------------------------------------------------------
=head2 asset ( name )
Returns an asset id.
=head3 name
The name of the form variable to retrieve.
=cut
sub asset {
return $session{form}{$_[0]};
}
#-------------------------------------------------------------------
=head2 checkbox ( name )
Returns an array or a carriage return ("\n") separated scalar depending upon whether you're returning the values into an array or a scalar.
=head3 name
The name of the form variable to retrieve.
=cut
sub checkbox {
return selectList($_[0]);
}
#-------------------------------------------------------------------
=head2 checkList ( name )
Returns an array or a carriage return ("\n") separated scalar depending upon whether you're returning the values into an array or a scalar.
=head3 name
The name of the form variable to retrieve.
=cut
sub checkList {
return selectList($_[0]);
}
#-------------------------------------------------------------------
=head2 codearea ( name )
Returns a string.
=head3 name
The name of the form variable to retrieve.
=cut
sub codearea {
return $session{form}{$_[0]};
}
#-------------------------------------------------------------------
=head2 color ( name )
Returns a hex color string like: #000000
=head3 name
The name of the form variable to retrieve.
=cut
sub color {
my $color = $session{form}{$_[0]};
return undef unless $color =~ /\#\w{6}/;
return $color;
}
#-------------------------------------------------------------------
=head2 combo ( name )
Returns either an array of values or a scalar value depending upon what you request.
=head3 name
The name of the form variable to retrieve.
=cut
sub combo {
if ($session{form}{$_[0]."_new"}) {
return $session{form}{$_[0]."_new"};
}
return selectList($_[0]);
}
#-------------------------------------------------------------------
=head2 contentType ( name )
Returns a content type. Defaults to "mixed".
=head3 name
The name of the form variable to retrieve.
=cut
sub contentType {
return ($session{form}{$_[0]} || "mixed");
}
#-------------------------------------------------------------------
=head2 databaseLink ( name )
Returns the ID of a database link.
=head3 name
The name of the form variable to retrieve.
=cut
sub databaseLink {
return selectList($_[0]);
}
#-------------------------------------------------------------------
=head2 date ( name )
Returns an epoch datestamp.
=head3 name
The name of the form variable to retrieve.
=cut
sub date {
return WebGUI::DateTime::setToEpoch($session{form}{$_[0]});
}
#-------------------------------------------------------------------
=head2 dateTime ( name )
Returns an epoch datestamp.
=head3 name
The name of the form variable to retrieve.
=cut
sub dateTime {
return WebGUI::DateTime::setToEpoch($session{form}{$_[0]});
}
#-------------------------------------------------------------------
=head2 email ( name )
Returns an email address.
=head3 name
The name of the form variable to retrieve.
=cut
sub email {
if (_checkEmailAddy($session{form}{$_[0]})) {
return $session{form}{$_[0]};
}
return undef;
}
#-------------------------------------------------------------------
=head2 fieldType ( name )
Returns a field type. Defaults to "text".
=head3 name
The name of the form variable to retrieve.
=cut
sub fieldType {
return ($session{form}{$_[0]} || "text");
}
#-------------------------------------------------------------------
=head2 filterContent ( name )
Returns a scalar filter type. Defaults to "most".
=head3 name
The name of the form variable to retrieve.
=cut
sub filterContent {
return ($session{form}{$_[0]} || "most");
}
#-------------------------------------------------------------------
=head2 float ( name )
Returns a floating point (decimal) number. Defaults to "0.0".
=head3 name
The name of the form variable to retrieve.
=cut
sub float {
if ($session{form}{$_[0]} =~ /^[\d\.\-]+$/) {
return $session{form}{$_[0]};
}
return 0.0;
}
#-------------------------------------------------------------------
=head2 group ( name )
Returns a group Id. Defaults to 2 (registered users).
=head3 name
The name of the form variable to retrieve.
=cut
sub group {
my $value = selectList($_[0]);
if (defined $value) {
return $value;
}
return 2;
}
#-------------------------------------------------------------------
=head2 hidden ( name )
Returns a string.
=head3 name
The name of the form variable to retrieve.
=cut
sub hidden {
return $session{form}{$_[0]};
}
#-------------------------------------------------------------------
=head2 hiddenList ( name )
Returns an array or a carriage return ("\n") separated scalar depending upon whether you're returning the values into an array or a scalar.
=head3 name
The name of the form variable to retrieve.
=cut
sub hiddenList {
return selectList($_[0]);
}
#-------------------------------------------------------------------
=head2 HTMLArea ( name )
Returns a string of HTML that has been cleaned of header information.
=head3 name
The name of the form variable to retrieve.
=cut
sub HTMLArea {
return WebGUI::HTML::cleanSegment($session{form}{$_[0]});
}
#-------------------------------------------------------------------
=head2 integer ( name )
Returns an integer. Defaults to "0".
=head3 name
The name of the form variable to retrieve.
=cut
sub integer {
if ($session{form}{$_[0]} =~ /^[\d\-]+$/) {
return $session{form}{$_[0]};
}
return 0;
}
#-------------------------------------------------------------------
=head2 interval ( name )
Returns an interval in seconds. Defaults to "0".
=head3 name
The name of the form variable to retrieve.
=cut
sub interval {
my $val = WebGUI::DateTime::intervalToSeconds($session{form}{$_[0]."_interval"},$session{form}{$_[0]."_units"}) || 0;
return $val;
}
#-------------------------------------------------------------------
=head2 password ( name )
Returns a string.
=head3 name
The name of the form variable to retrieve.
=cut
sub password {
return $session{form}{$_[0]};
}
#-------------------------------------------------------------------
=head2 phone ( name )
Returns a string filtered to allow only digits, spaces, and these special characters: + - ( )
=head3 name
The name of the form variable to retrieve.
=cut
sub phone {
if ($session{form}{$_[0]} =~ /^[\d\s\-\+\(\)]+$/) {
return $session{form}{$_[0]};
}
return undef;
}
#-------------------------------------------------------------------
=head2 process ( name, type [ , default ] )
Returns whatever would be the expected result of the method type that was specified. This method also checks to make sure that the field is not returning a string filled with nothing but whitespace.
=head3 name
The name of the form variable to retrieve.
=head3 type
The type of form element this variable came from. Defaults to "text" if not specified.
=head3 default
The default value for this variable. If the variable is undefined then the default value will be returned instead.
=cut
sub process {
my ($name, $type, $default) = @_;
my $value;
$type = "text" if ($type eq "");
$value = &$type($name);
unless (defined $value) {
return $default;
}
if ($value =~ /^[\s]+$/) {
return undef;
}
return $value;
}
#-------------------------------------------------------------------
=head2 radio ( name )
Returns a string.
=head3 name
The name of the form variable to retrieve.
=cut
sub radio {
return $session{form}{$_[0]};
}
#-------------------------------------------------------------------
=head2 radioList ( name )
Returns a string.
=head3 name
The name of the form variable to retrieve.
=cut
sub radioList {
return $session{form}{$_[0]};
}
#-------------------------------------------------------------------
=head2 selectList ( name )
Returns an array or a carriage return ("\n") separated scalar depending upon whether you're returning the values into an array or a scalar.
=head3 name
The name of the form variable to retrieve.
=cut
sub selectList {
my @data = $session{cgi}->param($_[0]);
return wantarray ? @data : join("\n",@data);
}
#-------------------------------------------------------------------
=head2 template ( name )
Returns a template id. Defaults to "1".
=head3 name
The name of the form variable to retrieve.
=cut
sub template {
if (exists $session{form}{$_[0]}) {
return $session{form}{$_[0]};
}
return 1;
}
#-------------------------------------------------------------------
=head2 text ( name )
Returns a string of text.
=head3 name
The name of the form variable to retrieve.
=cut
sub text {
return $session{form}{$_[0]};
}
#-------------------------------------------------------------------
=head2 textarea ( name )
Returns a string of text.
=head3 name
The name of the form variable to retrieve.
=cut
sub textarea {
return $session{form}{$_[0]};
}
#-------------------------------------------------------------------
=head2 timeField ( name )
Returns the number of seconds since 00:00:00 on a 24 hour clock. Note, this will adjust for the user's time offset in the reverse manner that the form field
adjusts for it in order to make the times come out appropriately.
=head3 name
The name of the form variable to retrieve.
=cut
sub timeField {
return WebGUI::DateTime::timeToSeconds($session{form}{$_[0]})-($session{user}{timeOffset}*3600);
}
#-------------------------------------------------------------------
=head2 url ( name )
Returns a URL.
=head3 name
The name of the form variable to retrieve.
=cut
sub url {
if ($session{form}{$_[0]} =~ /mailto:/) {
return $session{form}{$_[0]};
} elsif (_checkEmailAddy($session{form}{$_[0]})) {
return "mailto:".$session{form}{$_[0]};
} elsif ($session{form}{$_[0]} =~ /^\// || $session{form}{$_[0]} =~ /:\/\// || $session{form}{$_[0]} =~ /^\^/) {
return $session{form}{$_[0]};
}
return "http://".$session{form}{$_[0]};
}
#-------------------------------------------------------------------
=head2 yesNo ( name )
Returns either a 1 or 0 representing yes and no. Defaults to "0".
=head3 name
The name of the form variable to retrieve.
=cut
sub yesNo {
if ($session{form}{$_[0]} > 0) {
return 1;
}
return 0;
}
#-------------------------------------------------------------------
=head2 zipcode ( name )
Returns a string which allows uppercase alpha characters, digits, spaces, and hypens (dashes).
=head3 name
The name of the form variable to retrieve.
=cut
sub zipcode {
if ($session{form}{$_[0]} =~ /^[A-Z\d\s\-]+$/) {
return $session{form}{$_[0]};
}
return undef;
}
1;