package Template::Parser::Form;
$Template::Parser::Form::VERSION = 2.07;
# use strict coding pragma
use strict;
# make expt traces for diagnostic debug
use expt_handler;
# --------------------------------------------------------
# Constructor
# --------------------------------------------------------
sub new {
my($class) = shift;
my(%in) = @_;
my($this) = {};
my($name) = __PACKAGE__;
# -------------------------------------------
# bless object ref into the class' namespace
# -------------------------------------------
bless($this, $class);
# -------------------------------------------
# begin setting up class attributes
# -------------------------------------------
$this->{'name'} = $name;
$this->{'pcom'} = $in{'main'} || undef;
$this->{'parser'} = $in{'parser'} || undef;
$this->{'cgi'} = $in{'cgi'} || undef;
$this->{'expt'} = expt_handler->new();
# -------------------------------------------
# finish by verifying class attributes
# -------------------------------------------
$this->verify_attributes();
# return object reference
return($this);
}
# --------------------------------------------------------
# Template::Parser::Form::verify_attributes()
# --------------------------------------------------------
sub verify_attributes {
my($this) = shift;
my($name) = $this->{'name'};
my($expt) = $this->{'expt'};
$expt->
belly_up
(
qq[$name needs a PCOM object]
)
if not ($this->{'pcom'});
$expt->
belly_up
(
qq[$name needs a CGI object]
)
if (not $this->{'cgi'});
$expt->
belly_up
(
qq[$name needs a Template::Parser object]
)
if (not $this->{'parser'});
return(1);
}
# --------------------------------------------------------
# Template::Parser::Form::make_form()
# --------------------------------------------------------
sub make_form {
my($this) = shift;
my(%in) = @_;
my($pcom) = $this->{'pcom'};
my($parser) = $this->{'parser'};
my($cgi) = $this->{'cgi'};
my($def) = $in{'def preloaded'};
my($object) = $pcom->{'DIR'}{'objects'}.'/'.$in{'object'};
my($layout) = $parser->load_object( $object );
my($form) = $in{'form'};
my($option) = '';
my($field) = '';
my($i) = 0;
$parser->
set_object
(
'name' => 'live_cmpts',
'value' => $layout,
);
if (length($def) == 0) {
$def = $pcom->{'DIR'}{'defs'}.'/minidefs/'.$in{'def'}.'.mdef';
$def = $parser->load_file( $def );
}
$parser->
set_object
(
'name' => 'form',
'value' => $form,
);
$def = $parser->parse( $def );
$this->
{'expt'}->
fwarn
(
$def
)
if ($this->{'verbose'});
if (CORE::eval($def)) {
$def = CORE::eval( $def );
}
else {
warn($@);
}
foreach $field (@{ $def->{'fields'} }) {
++$i;
$field->{'iterator'} = $i;
$parser->
set_object
(
'name' => 'formfield',
'value' => $field,
);
if (($field->{'type'} ne 'hidden') && ($field->{'type'} ne 'select')) {
$layout->{'tmp'} .= $parser->parse( $layout->{'body'} );
next;
}
if ($field->{'type'} eq 'hidden') {
$layout->{'hiddenfields'} .= $parser->parse( $layout->{'hidden'} );
}
elsif ($field->{'type'} eq 'select') {
my($sel) = '';
foreach $option (@{ $field->{'options'} }) {
$parser->
set_object
(
'name' => 'option',
'value' => $option,
);
$sel .= $parser->parse( $layout->{'option'} );
}
$parser->
set_object
( 'name' => 'select',
'param' => 'options',
'value' => $sel,
);
$layout->{'tmp'} .= $parser->parse( $layout->{'body'} );
}
}
$parser->
set_object
(
'name' => 'form',
'param' => 'hidden',
'value' => $layout->{'hiddenfields'},
);
if ($layout->{'tmp'}) {
# put cap and toe on the just-conglommerated layout body
$layout->
{'parsed'} =
$parser->parse($layout->{'header'}).
$layout->{'tmp'}.
$parser->parse( $layout->{'footer'});
}
else {
# or put cap and toe on the empty return
$layout->
{'parsed'} =
$parser->
parse
(
$layout->{'header'}.
$layout->{'empty'}.
$layout->{'footer'}
);
}
# return the parsed object now
return($layout->{'parsed'});
}
# --------------------------------------------------------
# Template::Parser::Form::get_form()
# --------------------------------------------------------
sub get_form {
my($this) = shift;
my(%in) = @_;
my($def) = $in{'def'};
my($object) = $in{'object'};
my($form) =
{
'name' => $in{'object'},
'action' => $in{'action'},
'method' => $in{'method'} || 'post',
};
return
(
$this->
make_form
(
'def' => $def,
'object' => $object,
'form' => $form,
)
);
}
# --------------------------------------------------------
# Template::Parser::Form::DESTROY()
# --------------------------------------------------------
sub DESTROY {}
1;
$Template::Parser::Form::cmpos = {};
$Template::Parser::Form::cmpos =
{
'body' => <<'__cmpo__',
=
# take care of formfields that have null widths
$formfield->{'input-width'} ||= '250';
$formfield->{'label-width'} ||= '150';
# take care of formfields that have null styles
if (!$formfield->{'label-tdstyle'}) {
if ($formfield->{'iterator'} % 2) {
$formfield->{'label-tdstyle'} = 'greybak';
}
else {
$formfield->{'label-tdstyle'} = 'whitebak';
}
}
if (!$formfield->{'input-tdstyle'}) {
if ($formfield->{'iterator'} % 2) {
$formfield->{'input-tdstyle'} = 'greybak';
}
else {
$formfield->{'input-tdstyle'} = 'whitebak';
}
}
# now that this is done, make sure that special
# formfields are parsed in their own files so
# that colspans and all that don't get messed up
# in the table holding our form...
if ( $formfield->{'type'} eq 'title' ) {
$formfield->{'title table cell'} = qq[
].
q[
];
return
(
$formfield->{'title table cell'}.
$parser->parse( $live_cmpts->{'title'} ).
'
'
);
}
elsif ( $formfield->{'type'} eq 'subtitle' ) {
# !! we override subtitle td styles for now !!
$formfield->{'tdstyle'} = 'lightgreybak';
$formfield->{'subtitle table cell'} = qq[
];
return
(
$formfield->{'subtitle table cell'}.
$parser->parse( $live_cmpts->{'subtitle'} ).' '
);
}
elsif ( $formfield->{'type'} eq 'submit' ) {
$formfield->{'submit table cell'} = qq[
]
.q[
];
return
(
$formfield->{'submit table cell'}.
$parser->parse( $live_cmpts->{'sumbit'} ).
q[
]
);
}
# once we have sent any special formfield types
# to their respective files, treat the current
# formfield normally...
$formfield->{'left table cell'} = qq[
];
if (length($formfield->{'desc'}) > 0) {
$formfield->{'leftside'} = qq[
$formfield->{'label'}
$formfield->{'desc'}
]
}
else {
$formfield->{'leftside'} = $formfield->{'label'};
}
if ($formfield->{'type'} eq 'readonly') {
$formfield->{'leftside'} =
''.
$formfield->{'leftside'}.
' ';
}
$formfield->{'left table cell'} .= $formfield->{'leftside'}.' ';
$formfield->{'right table cell'} = qq[
];
$formfield->{'rightside'} =
$parser->parse( $live_cmpts->{ $formfield->{'type'} } );
$formfield->{'right table cell'} .= $formfield->{'rightside'}.qq[\n
];
return($formfield->{'left table cell'} .$formfield->{'right table cell'});
?>
__cmpo__
'value-as-text' => <<'__cmpo__',
=
($formfield->{'css-class'})
? qq[ class="$formfield->{'css-class'}" : ''
?>>= $formfield->{'value'} ?>
__cmpo__
'text' => <<'__cmpo__',
=
($formfield->{'comments'})
? qq[ $formfield->{'comments'} ] : ''
?>
{'css-class'})
? qq[
class="$formfield->{'css-class'}"
: q[ class="240px"]
?>=
($formfield->{'js'}) ? qq[$formfield->{'js'}] : '' ?> />=
($formfield->{'required'})
? qq[ * ] : ''
?>=
($formfield->{'required'})
? qq[
]
: '' ?>
__cmpo__
'select-header' => <<'__cmpo__',
=
($formfield->{'comments'})
? qq[ $formfield->{'comments'} ] : ''
?>
{'multiple'}) ? qq[multiple="multiple"] : '' ?>=
$formfield->{'css'}
?>=
($formfield->{'css-class'}) ? qq[ class="$formfield->{'css-class'}"]
: qq[ class="240px"]
?>>
__cmpo__
'button' => <<'__cmpo__',
=
($formfield->{'comments'})
? qq[ $formfield->{'comments'} ] : ''
?> {'css-class'})
? qq[ class="$formfield->{'css-class'}"]
: q[ class="button"]
?>=
$formfield->{'css'} ?>=
$formfield->{'js'} ?> />=
($formfield->{'required'}) ? qq[
]
: '' ?>
__cmpo__
'subtitle' => <<'__cmpo__',
= $formfield->{'title'} ?>
__cmpo__
'readonly' => <<'__cmpo__',
=
($formfield->{'css-class'})
? qq[ class="$formfield->{'css-class'}"] : ''
?>>= ($formfield->{'vmask'} || $formfield->{'value'}) ?>
=
($formfield->{'required'}) ? qq[
]
: '' ?>
__cmpo__
'header' => <<'__cmpo__',
__cmpo__
'checkbox' => <<'__cmpo__',
=
($formfield->{'comments'})
? qq[ $formfield->{'comments'} ] : ''
?> {'checked'} ?>=
$formfield->{'css'} ?>=
($formfield->{'css-class'})
? qq[ class="$formfield->{'css-class'}" : ''
?>=
$formfield->{'js'} ?> />=
($formfield->{'required'})
? qq[ * ] : ''
?>= $formfield->{'label'} ?>=
($formfield->{'required'})
? qq[
]
: '' ?>
__cmpo__
'password' => <<'__cmpo__',
=
($formfield->{'comments'})
? qq[ $formfield->{'comments'} ] : ''
?>
{'css-class'})
? qq[ class="$formfield->{'css-class'}"
: q[ class="240px"]
?>=
($formfield->{'js'}) ? qq[\n$formfield->{'js'}] : '' ?> />=
($formfield->{'required'})
? qq[ * ] : ''
?>=
($formfield->{'required'})
? qq[
]
: '' ?>
__cmpo__
'option.html' => <<'__cmpo__',
{'selected'}) ? q[ selected="selected"] : ''
?>>= $formfield->{'prefix'} ?>=
$option->{'name'} || $option->{'value'}
?>
__cmpo__
'sep' => <<'__cmpo__',
__cmpo__
'radio' => <<'__cmpo__',
=
($formfield->{'comments'})
? qq[ $formfield->{'comments'} ]
: '' ?>
{'checked'} ?>=
($formfield->{'css-class'}) ? qq[ class="$formfield->{'css-class'}" : ''
?>= $formfield->{'css'} ?>= $formfield->{'js'} ?> />=
($formfield->{'required'})
? qq[
]
: ''?>
__cmpo__
'submit' => <<'__cmpo__',
= $formfield->{'comments'} ?>
{'css-class'})
? qq[ class="$formfield->{'css-class'}"]
: qq[ class="button"]
?>= $formfield->{'css'} ?>=
$formfield->{'js'} ?> />
__cmpo__
};
1;