blib/lib/CGI/FormBuilder/Template/CGI_SSI.pm untested

filter: Statementstmt 0.0% 0 / 70 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 10 MC/DCmcdc n/a Subroutinesub 0.0% 0 / 10 Podpod 0.0% 0 / 3 totaltotal 0.0% 0 / 99 SCAR 30.0
CC
4
Cov
0%
render
20.0
new
2.0
engine
2.0
CRAP 20.0
1  
2 ###########################################################################
3 # Copyright (c) Nate Wiger http://nateware.com. All Rights Reserved.
4 # Please visit http://formbuilder.org for tutorials, support, and examples.
5 ###########################################################################
6  
7 package CGI::FormBuilder::Template::CGI_SSI;
8  
9 =head1 NAME
10  
11 CGI::FormBuilder::Template::CGI_SSI - FormBuilder interface to CGI::SSI
12  
13 =head1 SYNOPSIS
14  
15 my $form = CGI::FormBuilder->new(
16 fields => \@fields,
17 template => {
18 type => 'CGI_SSI',
19 file => "template.html",
20 },
21 );
22  
23 =cut
24  
25 use Carp;
26 use strict;
27 use warnings;
28 no warnings 'uninitialized';
29  
30 use CGI::FormBuilder::Util;
31 use CGI::SSI;
32 use base 'CGI::SSI';
33  
34  
35 our $VERSION = '3.10';
36  
37 #
38 # For legacy reasons, and due to its somewhat odd interface,
39 # CGI::SSI vars use a completely different naming scheme.
40 #
41 our %FORM_VARS = (
42 'js-head' => 'jshead',
43 'form-title' => 'title',
44 'form-start' => 'start',
45 'form-submit' => 'submit',
46 'form-reset' => 'reset',
47 'form-end' => 'end',
48 'form-invalid' => 'invalid',
49 'form-required' => 'required',
50 );
51  
52 our %FIELD_VARS = map { $_ => "$_-%s" } qw(
53 field
54 value
55 label
56 type
57 comment
58 required
59 error
60 invalid
61 missing
62 nameopts
63 cleanopts
64 );
65  
66 sub new {
67 my $self = shift;
68 my $class = ref($self) || $self;
69 my $opt = arghash(@_);
70  
71 $opt->{die_on_bad_params} = 0; # force to avoid blow-ups
72  
73 my %opt2 = %$opt;
74 delete $opt2{virtual};
75 delete $opt2{file};
76 delete $opt2{string};
77 $opt->{engine} = CGI::SSI->new(%opt2);
78  
79 return bless $opt, $class; # rebless
80 }
81  
82 sub engine {
83 return shift()->{engine};
84 }
85  
86 sub render {
87 my $self = shift;
88 my $tvar = shift || puke "Missing template expansion hashref (\$form->prepare failed?)";
89  
90 while(my($to, $from) = each %FORM_VARS) {
91 debug 1, "renaming attr $from to: <!--#echo var=\"$to\">";
92 $tvar->{$to} = "$tvar->{$from}";
93 }
94  
95 #
96 # For CGI::SSI, each data struct is manually assigned
97 # to a separate <!--#echo var=... -->"
98 #
99 my @fieldlist;
100 for my $field (@{$tvar->{fields}}) {
101  
102 # Field name is usually a good idea
103 my $name = $field->{name};
104 debug 1, "expanding field: $name";
105  
106 # Get all values
107 my @value = @{$tvar->{field}{$name}{values} || []};
108 my @options = @{$tvar->{field}{$name}{options} || []};
109  
110 #
111 # Auto-expand all of our field tags, such as field, label, value
112 # comment, error, etc, etc
113 #
114 my %all_loop;
115 while(my($key, $str) = each %FIELD_VARS) {
116 my $var = sprintf $str, $name;
117 $all_loop{$key} = $tvar->{field}{$name}{$key};
118 $tvar->{$var} = "$tvar->{field}{$name}{$key}"; # fuck Perl
119 debug 2, "<!--#echo var=\"$var\"> = " . $all_loop{$str};
120 }
121 }
122 # kill our previous fields list
123 $tvar->{fields} = \@fieldlist;
124  
125 # loop thru each field we have and set the tmpl_param
126 while(my($param, $tag) = each %$tvar) {
127 $self->{engine}->set($param => $tag);
128 }
129  
130 # template output
131 SWITCH: {
132 if($self->{virtual}) {
133 return $self->engine->include(virtual=>$self->{virtual});
134 }
135 if($self->{file}) {
136 return $self->engine->include(file=>$self->{file});
137 }
138 if($self->{string}) {
139 return $self->engine->process($self->{string});
140 }
141 }
142 }
143  
144 1;
145 __END__