################################################################################
#
# File name: StickyForms.pm
# Project: HTML::StickyForms
#
# Author: Peter Haworth
# Date created: 06/06/2000
#
# $Id: StickyForms.pm,v 1.3 2005/10/19 16:24:20 pmh Exp $
#
# Copyright Peter Haworth 2001
# You may use and distribute this module according to the same terms
# that Perl is distributed under.
#
################################################################################
package HTML::StickyForms;
use strict;
use vars qw(
$VERSION
);
# Use the same version as HTML::StickyForm
$VERSION=0.07;
################################################################################
# Class method: new($request)
# Description: Return a new HTML::StickyForms object
# $request may be an instance of CGI (new or old) or Apache::Request
# Author: Peter Haworth
sub new{
my($class,$req)=@_;
my $type;
if(!$req){
$type='empty';
}elsif(UNIVERSAL::isa($req,'Apache::Request')){
$type='apreq';
}elsif(UNIVERSAL::isa($req,'CGI') || UNIVERSAL::isa($req,'CGI::State')){
$type='CGI';
}else{
# XXX Maybe this should die?
return undef;
}
my $self=bless {
req => $req,
type => $type,
values_as_labels => 0,
well_formed => '',
},$class;
# Count submitted fields
$self->set_sticky;
$self;
}
################################################################################
# Method: set_sticky([BOOL])
# Description: Count the number of parameters set in the request
# Author: Peter Haworth
sub set_sticky{
my $self=shift;
return $self->{params}=!!$_[0] if @_;
$self->{params}=()=$self->{type} eq 'empty' ? () : $self->{req}->param;
}
################################################################################
# Method: values_as_labels([BOOL])
# Description: Set/Get the values_as_labels attribute
# Author: Peter Haworth. Idea from Thomas Klausner (domm@zsi.at)
sub values_as_labels{
my $self=shift;
return $self->{values_as_labels}=$_[0] if @_;
$self->{values_as_labels};
}
################################################################################
# Method: well_formed([BOOL])
# Description: Set/Get the well_formed attribute
# Author: Peter Haworth
sub well_formed{
my $self=shift;
return !!($self->{well_formed}=$_[0] ? '/' : '') if @_;
!!$self->{well_formed};
}
################################################################################
# Method: trim_params()
# Description: Trim leading and trailing whitespace from all submitted values
# Author: Peter Haworth
sub trim_params{
my($self)=@_;
my $req=$self->{req};
my $type=$self->{type};
return if $type eq 'empty';
foreach my $k($req->param){
my @v=$req->param($k);
my $changed;
foreach(@v){
$changed+= s/^\s+//s + s/\s+$//s;
}
if($changed){
if($type eq 'apreq'){
# XXX This should work, but doesn't
# $req->param($k,\@v);
# This does work, though
if(@v==1){
$req->param($k,$v[0]);
}else{
my $tab=$req->parms;
$tab->unset($k);
foreach(@v){
$tab->add($k,$_);
}
}
}else{
$req->param($k,@v)
}
}
}
}
################################################################################
# Subroutine: _escape($string)
# Description: Escape HTML-special characters in $string
# Author: Peter Haworth
sub _escape($){
$_[0]=~s/([<>&"\177-\377])/sprintf "%d;",ord $1/ge;
}
################################################################################
# Method: text(%args)
# Description: Return an HTML field
# Special %args elements:
# type => type attribute value, defaults to "text"
# default => value attribute value, if sticky values not present
# Author: Peter Haworth
sub text{
my($self,%args)=@_;
my $type=delete $args{type} || 'text';
my $name=delete $args{name};
my $value=delete $args{default};
$value=$self->{req}->param($name) if $self->{params};
_escape($name);
_escape($value);
my $field=qq({well_formed}>";
}
################################################################################
# Method: password(%args)
# Description: Return an HTML field
# As text()
# Author: Peter Haworth
sub password{
my $self=shift;
$self->text(@_,type => 'password');
}
################################################################################
# Method: textarea(%args)
# Description: Return an HTML