package WebGUI::HTMLForm;
=head1 LEGAL
-------------------------------------------------------------------
WebGUI is Copyright 2001-2002 Plain Black Software.
-------------------------------------------------------------------
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 WebGUI::DateTime;
use WebGUI::International;
use WebGUI::Session;
use WebGUI::SQL;
use WebGUI::URL;
=head1 NAME
Package WebGUI::HTMLForm
=head1 SYNOPSIS
use WebGUI::HTMLForm;
$f = WebGUI::HTMLForm->new;
$f->checkbox("toggleGadget","Turn Gadgets On?");
$f->date("endDate","End Date",$endDate);
$f->email("emailAddress","Email Address");
$f->file("image","Image to Upload");
$f->group("groupToPost","Who can post?");
$f->hidden("wid","55");
$f->HTMLArea("description","Description");
$f->integer("size","Size");
$f->password("identifier","Password");
$f->phone("cellPhone","Cell Phone");
$f->raw("text");
$f->readOnly("34","Page ID");
$f->select("dayOfWeek",\%days,"Which day?");
$f->submit;
$f->text("firstName", "First Name");
$f->textarea("emailMessage","Email Message");
$f->url("homepage","Home Page");
$f->yesNo("happy","Are you happy?");
$f->zipcode("workZip","Office Zip Code");
$f->print;
$f->printRowsOnly;
=head1 DESCRIPTION
Package that makes HTML forms typed data and significantly
reduces the code needed for properties pages in WebGUI.
=head1 METHODS
These methods are available from this class:
=cut
#-------------------------------------------------------------------
sub _fixQuotes {
my $value = shift;
$value =~ s/\"/\"\;/g;
return $value;
}
#-------------------------------------------------------------------
sub _subtext {
my $output;
if ($_[0] ne "") {
$output .= ' '.$_[0].'';
}
return $output;
}
#-------------------------------------------------------------------
sub _tableFormRow {
return '
'.$_[0].'
'.$_[1].'
';
}
#-------------------------------------------------------------------
=head2 checkbox ( name [ label, checked, subtext, value, extras ] )
Adds a checkbox row to this form.
=item name
The name field for this form element.
=item label
The left column label for this form row.
=item checked
If you'd like this box to be defaultly checked, set this to "1".
=item subtext
Extra text to describe this form element or to provide special
instructions.
=item value
The default value for this form element. Defaults to "1".
=item 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()"'
=cut
sub checkbox {
my ($subtext, $class, $output, $name, $label, $extras, $checked, $value);
$class = shift;
$name = shift;
$label = shift;
$checked = shift;
$checked = ' checked="1"' if ($checked);
$subtext = shift;
$value = shift || 1;
$extras = shift;
$output = '';
$output .= _subtext($subtext);
$output = _tableFormRow($label,$output) unless ($class->{_noTable});
$class->{_data} .= $output;
}
#-------------------------------------------------------------------
=head2 date ( name [ label, value, extras, subtext, size ] )
Adds a date row to this form.
=item name
The name field for this form element.
=item label
The left column label for this form row.
=item value
The default date. Pass as an epoch value. Defaults to today.
=item 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()"'
=item subtext
Extra text to describe this form element or to provide special
instructions.
=item size
The number of characters wide this form element should be. There
should be no reason for anyone to specify this.
=cut
sub date {
my ($subtext, $class, $output, $name, $label, $extras, $size, $value);
$class = shift;
$name = shift;
$label = shift;
$value = shift;
$value = epochToSet($value);
$extras = shift;
$subtext = shift;
$size = shift || $session{setting}{textBoxSize} || 30;
$output = '';
$output .= '';
$output .= _subtext($subtext);
$output = _tableFormRow($label,$output) unless ($class->{_noTable});
$class->{_data} .= $output;
}
#-------------------------------------------------------------------
=head2 email ( name [ label, value, maxlength, extras, subtext, size ] )
Adds an email address row to this form.
=item name
The name field for this form element.
=item label
The left column label for this form row.
=item value
The default value for this form element.
=item maxlength
The maximum number of characters to allow in this form element.
=item 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()"'
=item subtext
Extra text to describe this form element or to provide special
instructions.
=item size
The number of characters wide this form element should be. There
should be no reason for anyone to specify this.
=cut
sub email {
my ($subtext, $class, $output, $name, $label, $extras, $size, $maxLength, $value);
$class = shift;
$name = shift;
$label = shift;
$value = shift;
$value = _fixQuotes($value);
$maxLength = shift || 255;
$extras = shift;
$subtext = shift;
$size = shift || $session{setting}{textBoxSize} || 30;
$output = '';
$output .= '';
$output .= _subtext($subtext);
$output = _tableFormRow($label,$output) unless ($class->{_noTable});
$class->{_data} .= $output;
}
#-------------------------------------------------------------------
=head2 file ( name [ label, subtext, extras, size ] )
Adds a file browse row to this form.
=item name
The name field for this form element.
=item label
The left column label for this form row.
=item subtext
Extra text to describe this form element or to provide special
instructions.
=item 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()"'
=item 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 ($subtext, $class, $output, $name, $label, $extras, $size, $value);
$class = shift;
$name = shift;
$label = shift;
$subtext = shift;
$extras = shift;
$size = shift || $session{setting}{textBoxSize} || 30;
$output = '';
$output .= _subtext($subtext);
$output = _tableFormRow($label,$output) unless ($class->{_noTable});
$class->{_data} .= $output;
}
#-------------------------------------------------------------------
=head2 group ( name [ label, value, size, multiple, extras, subtext ] )
Adds a group pull-down to this form. A group pull down
provides a select list that provides name value pairs for all the
groups in the WebGUI system.
=item name
The name field for this form element.
=item label
The left column label for this form row.
=item value
The default value(s) for this form element. This should be passed
as an array reference. Defaults to "7" (Everyone).
=item size
How many rows should be displayed at once?
=item multiple
Set to "1" if multiple groups should be selectable.
=item 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()"'
=item subtext
Extra text to describe this form element or to provide special
instructions.
=cut
sub group {
my ($size, $multiple, %hash, $subtext, $class, $key, $item, $output, $name, $label, $extras, $value);
$class = shift;
$name = shift;
$label = shift;
$value = shift;
if ($$value[0] eq "") { #doing long form otherwise arrayRef didn't work
$value = [7];
}
$size = shift || 1;
$multiple = shift;
$multiple = ' multiple="1" ' if ($multiple);
$extras = shift;
$subtext = shift;
tie %hash, 'Tie::IxHash';
%hash = WebGUI::SQL->buildHash("select groupId,groupName from groups where groupName<>'Reserved' order by groupName");
$output = '