package WebGUI::Wobject::MailForm; #------------------------------------------------------------------- # WebGUI is Copyright 2001-2003 Plain Black LLC. #------------------------------------------------------------------- # 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 #------------------------------------------------------------------- use strict; use Tie::CPHash; use Tie::IxHash; use WebGUI::Form; use WebGUI::HTMLForm; use WebGUI::Icon; use WebGUI::International; use WebGUI::MessageLog; use WebGUI::Privilege; use WebGUI::Session; use WebGUI::SQL; use WebGUI::URL; use WebGUI::Wobject; our @ISA = qw(WebGUI::Wobject); our $namespace = "MailForm"; our $name = WebGUI::International::get(1,$namespace); our @fields = qw(width fromField fromStatus toField toStatus ccField ccStatus bccField bccStatus subjectField subjectStatus acknowledgement storeEntries); #------------------------------------------------------------------- sub duplicate { my ($w, %data, $sth); tie %data, 'Tie::CPHash'; $w = $_[0]->SUPER::duplicate($_[1]); $w = WebGUI::Wobject::MailForm->new({wobjectId=>$w,namespace=>$namespace}); $w->set({ width=>$_[0]->get("width"), fromField=>$_[0]->get("fromField"), fromStatus=>$_[0]->get("fromStatus"), toField=>$_[0]->get("toField"), toStatus=>$_[0]->get("toStatus"), ccField=>$_[0]->get("ccField"), ccStatus=>$_[0]->get("ccStatus"), bccField=>$_[0]->get("bccField"), bccStatus=>$_[0]->get("bccStatus"), subjectField=>$_[0]->get("subjectField"), subjectStatus=>$_[0]->get("subjectStatus"), acknowledgement=>$_[0]->get("acknowledgement"), storeEntries=>$_[0]->get("storeEntries"), }); $sth = WebGUI::SQL->read("select * from MailForm_field where wobjectId=".$_[0]->get("wobjectId")); while (%data = $sth->hash) { $data{MailForm_fieldId} = "new"; $w->setCollateral("MailForm_field","MailForm_fieldId",\%data); } $sth->finish; } #------------------------------------------------------------------- sub purge { WebGUI::SQL->write("delete from MailForm_field where wobjectId=".$_[0]->get("wobjectId")); WebGUI::SQL->write("delete from MailForm_entry where wobjectId=".$_[0]->get("wobjectId")); WebGUI::SQL->write("delete from MailForm_entryData where wobjectId=".$_[0]->get("wobjectId")); $_[0]->SUPER::purge(); } #------------------------------------------------------------------- sub set { $_[0]->SUPER::set($_[1],[@fields]); } #------------------------------------------------------------------- sub uiLevel { return 5; } #------------------------------------------------------------------- sub www_deleteField { return WebGUI::Privilege::insufficient() unless (WebGUI::Privilege::canEditPage()); return $_[0]->confirm(WebGUI::International::get(19,$namespace), WebGUI::URL::page('func=deleteFieldConfirm&wid='.$_[0]->get("wobjectId").'&fid='.$session{form}{fid})); } #------------------------------------------------------------------- sub www_deleteFieldConfirm { return WebGUI::Privilege::insufficient() unless (WebGUI::Privilege::canEditPage()); $_[0]->deleteCollateral("MailForm_field","MailForm_fieldId",$session{form}{fid}); $_[0]->reorderCollateral("MailForm_field","MailForm_fieldId"); return ""; } #------------------------------------------------------------------- sub www_edit { return WebGUI::Privilege::insufficient() unless (WebGUI::Privilege::canEditPage()); my ($output, $f, $proceed); my %fieldStatus = ( 1 => WebGUI::International::get(4, $namespace), 2 => WebGUI::International::get(5, $namespace), 3 => WebGUI::International::get(6, $namespace) ); # field defaults my %data = ( width => 45, fromField => '', fromStatus => 3, toField => $session{setting}{companyEmail}, toStatus => 1, ccField => '', ccStatus => 1, bccField => '', bccStatus => 1, subjectField => WebGUI::International::get(2, $namespace), subjectStatus => 3, acknowledgement => WebGUI::International::get(3, $namespace), storeEntries => 1, ); # initialize fields from existing data, if any foreach my $field (@fields) { $data{$field} = $_[0]->get($field) if ($_[0]->get($field)); } if ($_[0]->get("wobjectId") eq "new") { $proceed = 1; } $output = helpIcon(1,$_[0]->get("namespace")); $output .= '

'.WebGUI::International::get(7, $namespace).'

'; $f = WebGUI::HTMLForm->new; $f->integer("width",WebGUI::International::get(8, $namespace),$_[0]->get("width") || 45); $f->raw( $_[0]->_textSelectRow("fromField",WebGUI::International::get(10, $namespace),$data{fromField},128, "fromStatus",\%fieldStatus,$data{fromStatus}) ); $f->raw( $_[0]->_textSelectRow("toField",WebGUI::International::get(11, $namespace),$data{toField},128, "toStatus",\%fieldStatus,$data{toStatus}) ); $f->raw( $_[0]->_textSelectRow("ccField",WebGUI::International::get(12, $namespace),$data{ccField},128, "ccStatus",\%fieldStatus,$data{ccStatus}) ); $f->raw( $_[0]->_textSelectRow("bccField",WebGUI::International::get(13, $namespace),$data{bccField},128, "bccStatus",\%fieldStatus,$data{bccStatus}) ); $f->raw( $_[0]->_textSelectRow("subjectField",WebGUI::International::get(14, $namespace),$data{subjectField},128, "subjectStatus",\%fieldStatus,$data{subjectStatus}) ); $f->HTMLArea("acknowledgement",WebGUI::International::get(16, $namespace),$_[0]->get("acknowledgement") || WebGUI::International::get(3, $namespace)); $f->yesNo("storeEntries",WebGUI::International::get(26,$namespace),[ $data{storeEntries} ]); $f->yesNo("proceed",WebGUI::International::get(15,$namespace),$proceed); $output .= $_[0]->SUPER::www_edit($f->printRowsOnly); return $output; } #------------------------------------------------------------------- sub www_editSave { return WebGUI::Privilege::insufficient() unless (WebGUI::Privilege::canEditPage()); my ($property); foreach my $field (@fields) { $property->{$field} = $session{form}{$field}; } $_[0]->SUPER::www_editSave($property); if ($session{form}{proceed}) { return $_[0]->www_editField(); } else { return ""; } } #------------------------------------------------------------------- sub www_editField { return WebGUI::Privilege::insufficient() unless (WebGUI::Privilege::canEditPage()); my ($output, %field, $f, %fieldTypes, %fieldStatus, %validation); tie %field, 'Tie::CPHash'; tie %fieldTypes, 'Tie::IxHash'; tie %validation, 'Tie::IxHash'; %fieldStatus = ( 1 => WebGUI::International::get(4, $namespace), 2 => WebGUI::International::get(5, $namespace), 3 => WebGUI::International::get(6, $namespace) ); %fieldTypes = ( text => "Textbox", checkbox => "Checkbox", checkList => "Checkbox List", radioList => "Radio Buttons List", textarea => "Textarea", date => "Date", email => "Email Address", url => "URL", yesNo => "Yes/No", select => "Drop-Down Box", ); %validation = ( none => "None", notnull => "Not empty", number => "Number", word => "Word char [a-zA-Z0-9_]", email => "Valid E-mail address", ); %field = WebGUI::SQL->quickHash("select * from MailForm_field where MailForm_fieldId='$session{form}{fid}'"); $output = helpIcon(2,$_[0]->get("namespace")); $output .= '

'.WebGUI::International::get(20,$namespace).'

'; $f = WebGUI::HTMLForm->new; $f->hidden("wid",$_[0]->get("wobjectId")); $session{form}{fid} = "new" if ($session{form}{fid} eq ""); $f->hidden("fid",$session{form}{fid}); $f->hidden("func","editFieldSave"); $f->text("name",WebGUI::International::get(21,$namespace),$field{name}); $f->text( -name=>"subtext", -value=>$field{subtext}, -label=>"Subtext", -subtext=>"Optional extra text" ); my $status = [ $field{status} ||= 3 ]; # make it modifiable by default $f->select("status",\%fieldStatus,WebGUI::International::get(22,$namespace),$status); my $type = [ $field{type} ||= "text" ]; $f->select("type",\%fieldTypes,WebGUI::International::get(23,$namespace),$type); $f->select("validation",\%validation,"Input validation", [$field{validation} || "none"]); $f->integer("width",WebGUI::International::get(8, $namespace),$field{width} || $_[0]->get("width") || 45); $f->integer( -name=>"rows", -value=>$field{rows} || "", -label=>WebGUI::International::get(27, $namespace), -subtext=>WebGUI::International::get(28, $namespace), ); $f->textarea("possibleValues",WebGUI::International::get(24,$namespace),$field{possibleValues}); $f->textarea("defaultValue",WebGUI::International::get(25,$namespace),$field{defaultValue}); $f->yesNo("proceed",WebGUI::International::get(15,$namespace)); $f->submit; $output .= $f->print; return $output; } #------------------------------------------------------------------- sub www_editFieldSave { return WebGUI::Privilege::insufficient() unless (WebGUI::Privilege::canEditPage()); my ($seq); if ($session{form}{fid} eq "new") { ($seq) = WebGUI::SQL->quickArray("select max(sequenceNumber) from MailForm_field where wobjectId=".$_[0]->get("wobjectId")); $session{form}{fid} = getNextId("MailForm_fieldId"); WebGUI::SQL->write("insert into MailForm_field (wobjectId,MailForm_fieldId,sequenceNumber, width) values (".$_[0]->get("wobjectId").",$session{form}{fid},".($seq+1).", $session{form}{width} )"); } WebGUI::SQL->write("update MailForm_field set name=".quote($session{form}{name}). ", status=".quote($session{form}{status}). ", type=".quote($session{form}{type}). ", possibleValues=".quote($session{form}{possibleValues}). ", defaultValue=".quote($session{form}{defaultValue}). ", width=".quote($session{form}{width}). ", rows=".quote($session{form}{rows}). ", validation=".quote($session{form}{validation}). ", subtext=".quote($session{form}{subtext}). " where MailForm_fieldId=$session{form}{fid}"); if ($session{form}{proceed}) { $session{form}{fid} = "new"; return $_[0]->www_editField(); } else { return ""; } } #------------------------------------------------------------------- sub www_moveFieldDown { return WebGUI::Privilege::insufficient() unless (WebGUI::Privilege::canEditPage()); $_[0]->moveCollateralDown("MailForm_field","MailForm_fieldId",$session{form}{fid}); return ""; } #------------------------------------------------------------------- sub www_moveFieldUp { return WebGUI::Privilege::insufficient() unless (WebGUI::Privilege::canEditPage()); $_[0]->moveCollateralUp("MailForm_field","MailForm_fieldId",$session{form}{fid}); return ""; } #------------------------------------------------------------------- sub www_view { my ($output, $sth, $f, $row, %data); tie %data, 'Tie::CPHash'; $output = $_[0]->displayTitle; $output .= $_[0]->description.'

'; # get all international text for each field caption my %text = ( from => WebGUI::International::get(10, $namespace), to => WebGUI::International::get(11, $namespace), cc => WebGUI::International::get(12, $namespace), bcc => WebGUI::International::get(13, $namespace), subject => WebGUI::International::get(14, $namespace), ); if ($session{var}{adminOn}) { $output .= '' .WebGUI::International::get(9,$namespace).''; } $f = WebGUI::HTMLForm->new(); $f->hidden("wid",$_[0]->get("wobjectId")); $f->hidden('func','send'); foreach my $field (qw(from to cc bcc subject)) { if ($_[0]->get("${field}Status") == 1) { # Hidden field, don't show on form for security reasons } else { my $row = '\u$field: ".$_[0]->get("${field}Field"); } else { # Modifiable Field if ($field eq 'content') { my $taWidth = $_[0]->get("width") - 9; $row .= "top\">".$text{$field}.": "; } else { my $caption = $text{$field}; $row .= "top\">$caption: "; } } $row .= ""; $f->raw($row); } } $sth = WebGUI::SQL->read("select * from MailForm_field where wobjectId=".$_[0]->get("wobjectId")." order by sequenceNumber"); while (%data = $sth->hash) { # process macros on default values $data{defaultValue} = $_[0]->processMacros($data{defaultValue}); if ($data{status} == 1) { # hidden field, don't show on form for security reasons $row = ""; # but show for admins if ($session{var}{adminOn}) { $row = "\u".$data{name}." (hidden) ".$data{defaultValue}; $row .= $_[0]->_fieldAdminIcons($data{MailForm_fieldId}); } } elsif ($data{status} == 2) { # read-only field $row = "\u".$data{name}." ".$data{defaultValue}; if ($session{var}{adminOn}) { $row .= $_[0]->_fieldAdminIcons($data{MailForm_fieldId}); } $row .= ""; } else { # modifiable field $row = $_[0]->_createField(\%data); } $f->raw($row); } $f->submit(WebGUI::International::get(73, $namespace)); $output .= $f->print; return $_[0]->processMacros($output); } #------------------------------------------------------------------- sub _createField { my ($self, $data) = @_; my $name = WebGUI::URL::urlize($data->{name}); my $f = WebGUI::HTMLForm->new( 'noTable' ); SWITCH: for ($data->{type}) { /^text$/ && do { $f->text( -name=>$name, -label=>$data->{name}, -value=>$session{form}{$name} || $data->{defaultValue}, -maxlength=>255, -size=>$data->{width} || $self->get("width"), -subtext=>$data->{subtext}, ); last SWITCH; }; /^email$/ && do { $f->email( -name=>$name, -label=>$data->{name}, -value=>$session{form}{$name} || $data->{defaultValue}, -maxlength=>255, -size=>$data->{width} || $self->get("width"), -subtext=>$data->{subtext}, ); last SWITCH; }; /^url$/ && do { $f->url( -name=>$name, -label=>$data->{name}, -value=>$session{form}{$name} || $data->{defaultValue}, -maxlength=>255, -size=>$data->{width} || $self->get("width"), -subtext=>$data->{subtext}, ); last SWITCH; }; /^textarea$/ && do { $f->textarea( -name=>$name, -label=>$data->{name}, -value=>$session{form}{$name} || $data->{defaultValue}, -maxlength=>255, -size=>$data->{width} || $self->get("width"), -subtext=>$data->{subtext}, -columns=>$data->{width} || $self->get("width") - 9, -rows=>$data->{rows} || 9, ); last SWITCH; }; /^date$/ && do { $f->date( -name=>$name, -label=>$data->{name}, -value=>$session{form}{$name} || $data->{defaultValue}, -size=>$data->{width} || $self->get("width"), -subtext=>$data->{subtext}, ); last SWITCH; }; /^yesNo$/ && do { my $value; if ($data->{defaultValue} =~ /yes/i) { $value = 1; } elsif ($data->{defaultValue} =~ /no/i) { $value = 0; } else { $value = 2; } $f->yesNo( -name=>$name, -label=>$data->{name}, -value=>$session{form}{$name} || $data->{defaultValue}, -subtext=>$data->{subtext}, ); last SWITCH; }; /^checkbox$/ && do { my $value = ($data->{defaultValue} =~ /checked/i) ? 1 : ""; $f->checkbox( -name=>$name, -label=>$data->{name}, -value=>$session{form}{$name} || $data->{defaultValue}, -subtext=>$data->{subtext}, ); last SWITCH; }; /^select$/ && do { # size, multiple, extras, subtext my %selectOptions; tie %selectOptions, 'Tie::IxHash'; # add an empty option if no default value is provided foreach (split(/\n/, $data->{possibleValues})) { s/\s+$//; # remove trailing spaces $selectOptions{$_} = $_[0]->processMacros($_); } $f->selectList( -name=>$name, -options=>\%selectOptions, -label=>$data->{name}, -value=>[$session{form}{$name}] || [$data->{defaultValue}], -subtext=>$data->{subtext}, ); last SWITCH; }; /^checkList$/ && do { my (%selectOptions, @defaultValues); my $vertical = 1; # TODO: Make this configurable $name = "field_".$data->{sequenceNumber} if ($name eq ""); # Empty fieldname not allowed tie %selectOptions, 'Tie::IxHash'; foreach (split(/\n/, $data->{possibleValues})) { s/\s+$//; # remove trailing spaces $selectOptions{$_} = $_[0]->processMacros($_); } if ($session{form}{$name}) { @defaultValues = $session{cgi}->param($name); } else { # put default values in array foreach (split(/\n/, $data->{defaultValue})) { s/\s+$//; # remove trailing spaces push(@defaultValues, $_); } } $f->checkList( -name=>$name, -options=>\%selectOptions, -label=>$data->{name}, -value=>\@defaultValues, -subtext=>$data->{subtext}, -vertical=>$vertical ); last SWITCH; }; /^radioList$/ && do { my (%selectOptions, @defaultValues); my $vertical = 1; # TODO: Make this configurable $name = "field_".$data->{sequenceNumber} if ($name eq ""); # Empty fieldname not allowed tie %selectOptions, 'Tie::IxHash'; foreach (split(/\n/, $data->{possibleValues})) { s/\s+$//; # remove trailing spaces $selectOptions{$_} = $_[0]->processMacros($_); } if ($session{form}{$name}) { @defaultValues = $session{cgi}->param($name); } else { # put default values in array foreach (split(/\n/, $data->{defaultValue})) { s/\s+$//; # remove trailing spaces push(@defaultValues, $_); } } $f->radioList( -name=>$name, -options=>\%selectOptions, -label=>$data->{name}, -value=>\@defaultValues, -subtext=>$data->{subtext}, -vertical=>$vertical ); last SWITCH; }; } my $row = ''.$data->{name}.''.$f->printRowsOnly(); if ($session{var}{adminOn}) { $row .= $self->_fieldAdminIcons($data->{MailForm_fieldId}); } $row .= ''; return $row; } #------------------------------------------------------------------- sub _fieldAdminIcons { my $fid = $_[1]; return ' '.deleteIcon('func=deleteField&wid='.$_[0]->get("wobjectId").'&fid='.$fid) .editIcon('func=editField&wid='.$_[0]->get("wobjectId").'&fid='.$fid) .moveUpIcon('func=moveFieldUp&wid='.$_[0]->get("wobjectId").'&fid='.$fid) .moveDownIcon('func=moveFieldDown&wid='.$_[0]->get("wobjectId").'&fid='.$fid); } # Other methods #------------------------------------------------------------------- # textSelectRow basically combines HTMLForm::text with HTMLForm::select # to put a text box and select box on the same table row sub _textSelectRow { my ($self, $textName, $textLabel, $textValue, $textMaxLength, $selectName, $selectOptions, $selectValue) = @_; my $output; $textValue = WebGUI::Form::_fixQuotes($textValue); my $textSize = $session{setting}{textBoxSize}; $output = ''; $output .= ' '; my $selectSize = 1; $output .= '