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 = ''; $class->{_data} .= $output; } #------------------------------------------------------------------- =head2 HTMLArea ( name [ label, value, subtext, extras, wrap, rows, columns ] ) Adds an HTML area row to this form. An HTML area is different than a standard text area in that it provides rich edit functionality and some special error trapping for HTML and other special characters. =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 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 wrap The method for wrapping text in the text area. Defaults to "virtual". There should be almost no reason to specify this. =item rows The number of characters tall this form element should be. There should be no reason for anyone to specify this. =item columns The number of characters wide this form element should be. There should be no reason for anyone to specify this. =cut sub HTMLArea { my ($subtext, $class, $output, $name, $value, $columns, $rows, $wrap, $extras, $label); $class = shift; $name = shift; $label = shift; $value = shift; $subtext = shift; $extras = shift; $wrap = shift || "virtual"; $rows = shift || $session{setting}{textAreaRows} || 5; $columns = shift || $session{setting}{textAreaCols} || 50; $output = ''; $value =~ s/\/\>\;/g; if ($session{setting}{richEditor} eq "edit-on-pro") { $output .= ''; } else { $output .= ''; } $output .= '
'; $output .= ''; $output .= _subtext($subtext); $output = _tableFormRow($label,$output) unless ($class->{_noTable}); $class->{_data} .= $output; } #------------------------------------------------------------------- =head2 integer ( name [ label, value, maxlength, extras, subtext, size ] ) Adds an integer 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. Defaults to 11. =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 integer { my ($subtext, $class, $output, $name, $label, $extras, $size, $maxLength, $value); $class = shift; $name = shift; $label = shift; $value = shift || 0; $value = _fixQuotes($value); $maxLength = shift || 11; $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 new ( [ noTable, action, extras, method, enctype ] ) Constructor. =item noTable If this is set to "1" then no table elements will be wrapped around each form element. Defaults to "0". =item action The Action URL for the form information to be submitted to. This defaults to the current page. =item extras If you want to add anything special to your form like javascript actions, or stylesheet information, you'd add it in here as follows: 'name="myForm" onChange="myForm.submit()"' =item method The form's submission method. This defaults to "POST" and probably shouldn't be changed. =item enctype The ecapsulation type for this form. This defaults to "multipart/form-data" and should probably never be changed. =cut sub new { my ($header, $footer, $noTable, $enctype, $class, $method, $action, $extras); $class = shift; $noTable = shift || 0; $action = shift || WebGUI::URL::page(); $method = shift || "POST"; $extras = shift; $enctype = shift || "multipart/form-data"; $header = '
'; $header .= '' unless ($noTable); $footer = '
' unless ($noTable); $footer .= '
'; bless {_noTable => $noTable, _header => $header, _footer => $footer, _data => ''}, $class; } #------------------------------------------------------------------- =head2 password ( name [ label, value, subtext, maxlength, extras, size ] ) Adds a password 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 subtext Extra text to describe this form element or to provide special instructions. =item maxlength The maximum number of characters to allow in this form element. Defaults to "35". =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. Defaults to "30" unless overridden in the settings. =cut sub password { my ($subtext, $class, $output, $name, $label, $extras, $size, $maxLength, $value); $class = shift; $name = shift; $label = shift; $value = shift; $value = _fixQuotes($value); $subtext = shift; $maxLength = shift || 35; $extras = shift; $size = shift || $session{setting}{textBoxSize} || 30; $output = ''; $output .= _subtext($subtext); $output = _tableFormRow($label,$output) unless ($class->{_noTable}); $class->{_data} .= $output; } #------------------------------------------------------------------- =head2 phone ( name [ label, value, maxlength, extras, subtext, size ] ) Adds a text 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 phone { my ($subtext, $class, $output, $name, $label, $extras, $size, $maxLength, $value); $class = shift; $name = shift; $label = shift; $value = shift; $value = _fixQuotes($value); $maxLength = shift || 30; $extras = shift; $subtext = shift; $size = shift || $session{setting}{textBoxSize} || 30; $output .= ''; $output .= _subtext($subtext); $output = _tableFormRow($label,$output) unless ($class->{_noTable}); $class->{_data} .= $output; } #------------------------------------------------------------------- =head2 print ( ) Returns the HTML for this form object. =cut sub print { my ($class); $class = shift; return $class->{_header}.$class->{_data}.$class->{_footer}; } #------------------------------------------------------------------- =head2 printRowsOnly ( ) Returns the HTML for this form object except for the form header and footer. =cut sub printRowsOnly { my ($class); $class = shift; return $class->{_data}; } #------------------------------------------------------------------- =head2 raw ( value ) Adds raw data to the form. This is primarily useful with the printRowsOnly method and if you generate your own form elements. =cut sub raw { my ($class, $output, $value); $class = shift; $value = shift; $class->{_data} .= $value; } #------------------------------------------------------------------- =head2 readOnly ( value [ label, subtext ] ) Adds a read only row to this form. This is mainly used for displaying not editable properties, but it can also be used to quickly insert custom form elements. =item value The read only value. =item label The left column label for this form row. =item subtext Extra text to describe this form element or to provide special instructions. =cut sub readOnly { my ($output, $subtext, $class, $label, $value); $class = shift; $value = shift; $label = shift; $subtext = shift; $output = $value; $output .= _subtext($subtext); $output = _tableFormRow($label,$output) unless ($class->{_noTable}); $class->{_data} .= $output; } #------------------------------------------------------------------- =head2 select ( name, options [ label, value, size, multiple, extras, subtext ] ) Adds a select list row to this form. =item name The name field for this form element. =item options The list of options for this select list. Should be passed as a hash reference. =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. =item size The number of characters tall this form element should be. Defaults to "1". =item multiple A boolean value for whether this select list should allow multiple selections. Defaults to "0". =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 select { my ($label, $subtext, $class, $output, $value, $key, $item, $name, $options, $size, $multiple, $extras); $class = shift; $name = shift; $options = shift; $label = shift; $value = shift; $size = shift || 1; $multiple = shift; $multiple = ' multiple="1"' if ($multiple); $extras = shift; $subtext = shift; $output = ''; $output .= _subtext($subtext); $output = _tableFormRow($label,$output) unless ($class->{_noTable}); $class->{_data} .= $output; } #------------------------------------------------------------------- =head2 text ( name [ label, value, maxlength, extras, subtext, size ] ) Adds a text 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 text { 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 .= _subtext($subtext); $output = _tableFormRow($label,$output) unless ($class->{_noTable}); $class->{_data} .= $output; } #------------------------------------------------------------------- =head2 textarea ( name [ label, value, subtext, extras, wrap, rows, columns ] ) Adds a text area 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 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 wrap The method for wrapping text in the text area. Defaults to "virtual". There should be almost no reason to specify this. =item rows The number of characters tall this form element should be. There should be no reason for anyone to specify this. =item columns The number of characters wide this form element should be. There should be no reason for anyone to specify this. =cut sub textarea { my ($subtext, $class, $output, $name, $value, $columns, $rows, $wrap, $extras, $label); $class = shift; $name = shift; $label = shift; $value = shift; $subtext = shift; $extras = shift; $wrap = shift || "virtual"; $rows = shift || $session{setting}{textAreaRows} || 5; $columns = shift || $session{setting}{textAreaCols} || 50; $output .= ''; $output .= _subtext($subtext); $output = _tableFormRow($label,$output) unless ($class->{_noTable}); $class->{_data} .= $output; } #------------------------------------------------------------------- =head2 url ( name [ label, value, maxlength, extras, subtext, size ] ) Adds a URL 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. Defaults to 2048. =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 url { my ($subtext, $class, $output, $name, $label, $extras, $size, $maxLength, $value); $class = shift; $name = shift; $label = shift; $value = shift; $value = _fixQuotes($value); $maxLength = shift || 2048; $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 yesNo ( name [ label, value, extras, subtext ] ) Adds a yes/no radio menu 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(s) for this form element. Valid values are "1" and "0". 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()"' =item subtext Extra text to describe this form element or to provide special instructions. =cut sub yesNo { my ($subtext, $class, $output, $name, $label, $extras, $value); $class = shift; $name = shift; $label = shift; $value = shift || 0; $extras = shift; $subtext = shift; $output = ''.WebGUI::International::get(138); $output .= '   '; $output .= ''.WebGUI::International::get(139); $output .= _subtext($subtext); $output = _tableFormRow($label,$output) unless ($class->{_noTable}); $class->{_data} .= $output; } #------------------------------------------------------------------- =head2 zipcode ( name [ label, value, maxlength, extras, subtext, size ] ) Adds a zip code 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 zipcode { 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 .= _subtext($subtext); $output = _tableFormRow($label,$output) unless ($class->{_noTable}); $class->{_data} .= $output; } 1;