725
|
|
|
|
|
|
|
#$attr->{class} ||= $self->class($self->{fieldname}); |
726
|
|
|
|
|
|
|
|
727
|
383
|
|
|
|
|
1198
|
return htmltag('td', $attr); |
728
|
|
|
|
|
|
|
} |
729
|
|
|
|
|
|
|
|
730
|
|
|
|
|
|
|
sub div { |
731
|
90
|
|
|
90
|
0
|
222
|
my $self = shift; |
732
|
|
|
|
|
|
|
|
733
|
|
|
|
|
|
|
# single hashref kills everything; a list is temporary |
734
|
90
|
50
|
33
|
|
|
266
|
$self->{div} = shift if @_ == 1 && UNIVERSAL::isa($_[0], 'HASH'); |
735
|
|
|
|
|
|
|
|
736
|
90
|
|
|
|
|
211
|
my $attr = $self->{div}; |
737
|
90
|
50
|
|
|
|
225
|
if (@_) { |
738
|
|
|
|
|
|
|
# if still have args, create a temp hash |
739
|
90
|
|
|
|
|
241
|
my %temp = %$attr; |
740
|
90
|
|
|
|
|
262
|
while (my $k = shift) { |
741
|
172
|
|
|
|
|
525
|
$temp{$k} = shift; |
742
|
|
|
|
|
|
|
} |
743
|
90
|
|
|
|
|
198
|
$attr = \%temp; |
744
|
|
|
|
|
|
|
} |
745
|
|
|
|
|
|
|
|
746
|
90
|
|
|
|
|
309
|
return htmltag('div', $attr); |
747
|
|
|
|
|
|
|
} |
748
|
|
|
|
|
|
|
|
749
|
|
|
|
|
|
|
sub submitted { |
750
|
13
|
|
|
13
|
1
|
88
|
my $self = shift; |
751
|
13
|
|
33
|
|
|
123
|
my $smnam = shift || $self->submittedname; # temp smnam |
752
|
13
|
100
|
|
|
|
45
|
my $smtag = $self->{name} ? "${smnam}_$self->{name}" : $smnam; |
753
|
|
|
|
|
|
|
|
754
|
13
|
100
|
|
|
|
58
|
if ($self->{params}->param($smtag)) { |
755
|
|
|
|
|
|
|
# If we've been submitted, then we return the value of |
756
|
|
|
|
|
|
|
# the submit tag (which allows multiple submission buttons). |
757
|
|
|
|
|
|
|
# Must use an "|| 0E0" or else hitting "Enter" won't cause |
758
|
|
|
|
|
|
|
# $form->submitted to be true (as the button is only sent |
759
|
|
|
|
|
|
|
# across CGI when clicked). |
760
|
11
|
|
50
|
|
|
286
|
my $sr = $self->{params}->param($self->submitname) || '0E0'; |
761
|
11
|
|
|
|
|
216
|
debug 2, "\$form->submitted() is true, returning $sr"; |
762
|
11
|
|
|
|
|
31
|
return $sr; |
763
|
|
|
|
|
|
|
} |
764
|
2
|
|
|
|
|
37
|
return 0; |
765
|
|
|
|
|
|
|
} |
766
|
|
|
|
|
|
|
|
767
|
|
|
|
|
|
|
# This creates a modified self_url, just including fields (no sessionid, etc) |
768
|
|
|
|
|
|
|
sub query_string { |
769
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
770
|
0
|
|
|
|
|
0
|
my @qstr = (); |
771
|
0
|
|
|
|
|
0
|
for my $f ($self->fields, $self->keepextras) { |
772
|
|
|
|
|
|
|
# get all values, but ONLY from CGI |
773
|
0
|
|
|
|
|
0
|
push @qstr, join('=', escapeurl($f), escapeurl($_)) for $self->cgi_param($f); |
774
|
|
|
|
|
|
|
} |
775
|
0
|
|
|
|
|
0
|
return join '&', @qstr; |
776
|
|
|
|
|
|
|
} |
777
|
|
|
|
|
|
|
|
778
|
|
|
|
|
|
|
sub self_url { |
779
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
780
|
0
|
|
|
|
|
0
|
return join '?', $self->action, $self->query_string; |
781
|
|
|
|
|
|
|
} |
782
|
|
|
|
|
|
|
|
783
|
|
|
|
|
|
|
# must forcibly return scalar undef for CGI::Session easiness |
784
|
|
|
|
|
|
|
sub sessionid { |
785
|
159
|
|
|
159
|
1
|
8829
|
my $self = shift; |
786
|
159
|
100
|
|
|
|
454
|
$self->{sessionid} = shift if @_; |
787
|
159
|
100
|
|
|
|
1025
|
return $self->{sessionid} if $self->{sessionid}; |
788
|
153
|
50
|
|
|
|
391
|
return undef unless $self->{sessionidname}; |
789
|
153
|
|
|
|
|
179
|
my %cookies; |
790
|
153
|
50
|
|
|
|
513
|
if ($self->{cookies}) { |
791
|
153
|
|
|
|
|
12592
|
require CGI::Cookie; |
792
|
153
|
|
|
|
|
29530
|
%cookies = CGI::Cookie->fetch; |
793
|
|
|
|
|
|
|
} |
794
|
153
|
50
|
|
|
|
2645
|
if (my $cook = $cookies{"$self->{sessionidname}"}) { |
795
|
0
|
|
|
|
|
0
|
return $cook->value; |
796
|
|
|
|
|
|
|
} else { |
797
|
153
|
|
50
|
|
|
690
|
return $self->{params}->param($self->{sessionidname}) || undef; |
798
|
|
|
|
|
|
|
} |
799
|
|
|
|
|
|
|
} |
800
|
|
|
|
|
|
|
|
801
|
|
|
|
|
|
|
sub statetags { |
802
|
129
|
|
|
129
|
0
|
220
|
my $self = shift; |
803
|
129
|
|
|
|
|
339
|
my @html = (); |
804
|
|
|
|
|
|
|
|
805
|
|
|
|
|
|
|
# get _submitted |
806
|
129
|
|
|
|
|
675
|
my $smnam = $self->submittedname; |
807
|
129
|
100
|
|
|
|
469
|
my $smtag = $self->{name} ? "${smnam}_$self->{name}" : $smnam; |
808
|
129
|
|
|
|
|
735
|
my $smval = $self->{params}->param($smnam) + 1; |
809
|
129
|
|
|
|
|
3153
|
push @html, htmltag('input', name => $smtag, value => $smval, type => 'hidden'); |
810
|
|
|
|
|
|
|
|
811
|
|
|
|
|
|
|
# and how about _sessionid |
812
|
129
|
100
|
|
|
|
530
|
if (defined(my $sid = $self->sessionid)) { |
813
|
2
|
|
|
|
|
8
|
push @html, htmltag('input', name => $self->{sessionidname}, |
814
|
|
|
|
|
|
|
type => 'hidden', value => $sid); |
815
|
|
|
|
|
|
|
} |
816
|
|
|
|
|
|
|
|
817
|
|
|
|
|
|
|
# and what page (hooks for ::Multi) |
818
|
129
|
100
|
|
|
|
2991
|
if (defined $self->{page}) { |
819
|
2
|
|
|
|
|
16
|
push @html, htmltag('input', name => $self->pagename, |
820
|
|
|
|
|
|
|
type => 'hidden', value => $self->{page}); |
821
|
|
|
|
|
|
|
} |
822
|
|
|
|
|
|
|
|
823
|
129
|
50
|
|
|
|
1040
|
return wantarray ? @html : join "\n", @html; |
824
|
|
|
|
|
|
|
} |
825
|
|
|
|
|
|
|
|
826
|
|
|
|
|
|
|
*keepextra = \&keepextras; |
827
|
|
|
|
|
|
|
sub keepextras { |
828
|
129
|
|
|
129
|
1
|
434
|
local $^W = 0; # -w sucks |
829
|
129
|
|
|
|
|
217
|
my $self = shift; |
830
|
129
|
|
|
|
|
236
|
my @keep = (); |
831
|
129
|
|
|
|
|
218
|
my @html = (); |
832
|
|
|
|
|
|
|
|
833
|
|
|
|
|
|
|
# which ones do they want? |
834
|
129
|
50
|
|
|
|
397
|
$self->{keepextras} = shift if @_; |
835
|
129
|
100
|
|
|
|
782
|
return '' unless $self->{keepextras}; |
836
|
|
|
|
|
|
|
|
837
|
|
|
|
|
|
|
# If we set keepextras, then this means that any extra fields that |
838
|
|
|
|
|
|
|
# we've set that are *not* in our fields() will be added to the form |
839
|
10
|
|
100
|
|
|
53
|
my $ref = ref $self->{keepextras} || ''; |
840
|
10
|
100
|
|
|
|
36
|
if ($ref eq 'ARRAY') { |
|
|
50
|
|
|
|
|
|
841
|
4
|
|
|
|
|
7
|
@keep = @{$self->{keepextras}}; |
|
4
|
|
|
|
|
18
|
|
842
|
|
|
|
|
|
|
} elsif ($ref) { |
843
|
0
|
|
|
|
|
0
|
puke "Unsupported data structure type '$ref' passed to 'keepextras' option"; |
844
|
|
|
|
|
|
|
} else { |
845
|
|
|
|
|
|
|
# Set to "1", so must go thru all params, skipping |
846
|
|
|
|
|
|
|
# leading underscore fields and form fields |
847
|
6
|
|
|
|
|
26
|
for my $p ($self->{params}->param) { |
848
|
50
|
100
|
100
|
|
|
466
|
next if $p =~ /^_/ || $self->{fieldrefs}{$p}; |
849
|
32
|
|
|
|
|
51
|
push @keep, $p; |
850
|
|
|
|
|
|
|
} |
851
|
|
|
|
|
|
|
} |
852
|
|
|
|
|
|
|
|
853
|
|
|
|
|
|
|
# In array context, we just return names we've resolved |
854
|
10
|
50
|
|
|
|
34
|
return @keep if wantarray; |
855
|
|
|
|
|
|
|
|
856
|
|
|
|
|
|
|
# Make sure to get all values |
857
|
10
|
|
|
|
|
22
|
for my $p (@keep) { |
858
|
40
|
|
|
|
|
144
|
for my $v ($self->{params}->param($p)) { |
859
|
40
|
|
|
|
|
853
|
debug 1, "keepextras: saving hidden param $p = $v"; |
860
|
40
|
|
|
|
|
110
|
push @html, htmltag('input', name => $p, type => 'hidden', value => $v); |
861
|
|
|
|
|
|
|
} |
862
|
|
|
|
|
|
|
} |
863
|
10
|
|
|
|
|
82
|
return join "\n", @html; # wantarray above |
864
|
|
|
|
|
|
|
} |
865
|
|
|
|
|
|
|
|
866
|
|
|
|
|
|
|
sub javascript { |
867
|
165
|
|
|
165
|
1
|
298
|
my $self = shift; |
868
|
165
|
100
|
|
|
|
441
|
$self->{javascript} = shift if @_; |
869
|
|
|
|
|
|
|
|
870
|
|
|
|
|
|
|
# auto-determine javascript setting based on user agent |
871
|
165
|
100
|
|
|
|
611
|
if (lc($self->{javascript}) eq 'auto') { |
872
|
150
|
50
|
33
|
|
|
631
|
if (exists $ENV{HTTP_USER_AGENT} |
873
|
|
|
|
|
|
|
&& $ENV{HTTP_USER_AGENT} =~ /lynx|mosaic/i) |
874
|
|
|
|
|
|
|
{ |
875
|
|
|
|
|
|
|
# Turn off for old/non-graphical browsers |
876
|
0
|
|
|
|
|
0
|
return 0; |
877
|
|
|
|
|
|
|
} |
878
|
150
|
|
|
|
|
510
|
return 1; |
879
|
|
|
|
|
|
|
} |
880
|
15
|
50
|
|
|
|
110
|
return $self->{javascript} if exists $self->{javascript}; |
881
|
|
|
|
|
|
|
|
882
|
|
|
|
|
|
|
# Turn on for all other browsers by default. |
883
|
|
|
|
|
|
|
# I suspect this process should be reversed, only |
884
|
|
|
|
|
|
|
# showing JavaScript on those browsers we know accept |
885
|
|
|
|
|
|
|
# it, but maintaining a full list will result in this |
886
|
|
|
|
|
|
|
# module going out of date and having to be updated. |
887
|
0
|
|
|
|
|
0
|
return 1; |
888
|
|
|
|
|
|
|
} |
889
|
|
|
|
|
|
|
|
890
|
|
|
|
|
|
|
sub jsname { |
891
|
132
|
|
|
132
|
0
|
225
|
my $self = shift; |
892
|
132
|
100
|
|
|
|
903
|
return $self->{name} |
893
|
|
|
|
|
|
|
? (join '_', $self->{jsname}, tovar($self->{name})) |
894
|
|
|
|
|
|
|
: $self->{jsname}; |
895
|
|
|
|
|
|
|
} |
896
|
|
|
|
|
|
|
|
897
|
|
|
|
|
|
|
sub script { |
898
|
132
|
|
|
132
|
0
|
248
|
my $self = shift; |
899
|
|
|
|
|
|
|
|
900
|
|
|
|
|
|
|
# get validate() function name |
901
|
132
|
|
33
|
|
|
468
|
my $jsname = $self->jsname || puke "Must have 'jsname' if 'javascript' is on"; |
902
|
132
|
|
50
|
|
|
772
|
my $jspre = $self->jsprefix || ''; |
903
|
|
|
|
|
|
|
|
904
|
|
|
|
|
|
|
# "counter" |
905
|
132
|
|
|
|
|
366
|
$self->{_didscript} = 1; |
906
|
132
|
100
|
|
|
|
444
|
return '' unless $self->javascript; |
907
|
|
|
|
|
|
|
|
908
|
|
|
|
|
|
|
# code for misc non-validate functions |
909
|
120
|
|
|
|
|
417
|
my $jsmisc = $self->script_growable # code to grow growable fields, if any |
910
|
|
|
|
|
|
|
. $self->script_otherbox; # code to enable/disable the "other" box |
911
|
|
|
|
|
|
|
|
912
|
|
|
|
|
|
|
# custom user jsfunc option for w/i validate() |
913
|
120
|
|
100
|
|
|
672
|
my $jsfunc = $self->jsfunc || ''; |
914
|
120
|
|
50
|
|
|
696
|
my $jshead = $self->jshead || ''; |
915
|
|
|
|
|
|
|
|
916
|
|
|
|
|
|
|
# expand per-field validation functions, but |
917
|
|
|
|
|
|
|
# only if we are not using Data::FormValidator |
918
|
120
|
50
|
|
|
|
703
|
unless (UNIVERSAL::isa($self->{validate}, 'Data::FormValidator')) { |
919
|
120
|
|
|
|
|
313
|
for ($self->field) { |
920
|
353
|
|
|
|
|
1370
|
$jsfunc .= $_->script; |
921
|
|
|
|
|
|
|
} |
922
|
|
|
|
|
|
|
} |
923
|
|
|
|
|
|
|
|
924
|
|
|
|
|
|
|
# skip out if we have nothing useful |
925
|
120
|
100
|
100
|
|
|
1814
|
return '' unless $jsfunc || $jsmisc || $jshead; |
|
|
|
66
|
|
|
|
|
926
|
|
|
|
|
|
|
|
927
|
|
|
|
|
|
|
# prefix with opening code |
928
|
38
|
100
|
|
|
|
97
|
if ($jsfunc) { |
929
|
33
|
|
|
|
|
181
|
$jsfunc = <
|
930
|
|
|
|
|
|
|
function $jsname (form) { |
931
|
|
|
|
|
|
|
var alertstr = ''; |
932
|
|
|
|
|
|
|
var invalid = 0; |
933
|
|
|
|
|
|
|
var invalid_fields = new Array(); |
934
|
|
|
|
|
|
|
|
935
|
|
|
|
|
|
|
EOJ1 |
936
|
|
|
|
|
|
|
if (invalid > 0 || alertstr != '') { |
937
|
|
|
|
|
|
|
EOJ2 |
938
|
|
|
|
|
|
|
|
939
|
|
|
|
|
|
|
# Check to see if we have our own jserror callback on form failure |
940
|
|
|
|
|
|
|
# if not, then use the builtin one. Aka jsalert |
941
|
33
|
50
|
|
|
|
183
|
if (my $jse = $self->jserror) { |
942
|
0
|
|
|
|
|
0
|
$jsfunc .= " return $jse(form, invalid, alertstr, invalid_fields);\n"; |
943
|
|
|
|
|
|
|
} else { |
944
|
|
|
|
|
|
|
# Finally, close our JavaScript if it was opened, wrapping in "; |
978
|
|
|
|
|
|
|
} |
979
|
|
|
|
|
|
|
|
980
|
|
|
|
|
|
|
sub script_growable { |
981
|
120
|
|
|
120
|
0
|
217
|
my $self = shift; |
982
|
120
|
100
|
|
|
|
526
|
return '' unless my @growable = grep { $_->growable } $self->field; |
|
353
|
|
|
|
|
1133
|
|
983
|
|
|
|
|
|
|
|
984
|
2
|
|
50
|
|
|
9
|
my $jspre = $self->jsprefix || ''; |
985
|
2
|
|
|
|
|
5
|
my $jsmisc = ''; |
986
|
|
|
|
|
|
|
|
987
|
2
|
|
|
|
|
6
|
my $grow = $self->growname; |
988
|
2
|
|
|
|
|
21
|
$jsmisc .= <
|
989
|
|
|
|
|
|
|
var ${jspre}counter = new Object; // for assigning unique ids; keyed by field name |
990
|
|
|
|
|
|
|
var ${jspre}limit = new Object; // for limiting the size of growable fields |
991
|
|
|
|
|
|
|
function ${jspre}grow (baseID) { |
992
|
|
|
|
|
|
|
// inititalize the counter for this ID |
993
|
|
|
|
|
|
|
if (isNaN(${jspre}counter[baseID])) ${jspre}counter[baseID] = 1; |
994
|
|
|
|
|
|
|
|
995
|
|
|
|
|
|
|
// don't go past the growth limit for this field |
996
|
|
|
|
|
|
|
if (${jspre}counter[baseID] >= ${jspre}limit[baseID]) return; |
997
|
|
|
|
|
|
|
|
998
|
|
|
|
|
|
|
var base = document.getElementById(baseID + '_' + (${jspre}counter[baseID] - 1)); |
999
|
|
|
|
|
|
|
|
1000
|
|
|
|
|
|
|
// we are inserting after the last field |
1001
|
|
|
|
|
|
|
insertPoint = base.nextSibling; |
1002
|
|
|
|
|
|
|
|
1003
|
|
|
|
|
|
|
// line break |
1004
|
|
|
|
|
|
|
base.parentNode.insertBefore(document.createElement('br'), insertPoint); |
1005
|
|
|
|
|
|
|
|
1006
|
|
|
|
|
|
|
var dup = base.cloneNode(true); |
1007
|
|
|
|
|
|
|
|
1008
|
|
|
|
|
|
|
dup.setAttribute('id', baseID + '_' + ${jspre}counter[baseID]); |
1009
|
|
|
|
|
|
|
base.parentNode.insertBefore(dup, insertPoint); |
1010
|
|
|
|
|
|
|
|
1011
|
|
|
|
|
|
|
// add some padding space between the field and the "add field" button |
1012
|
|
|
|
|
|
|
base.parentNode.insertBefore(document.createTextNode(' '), insertPoint); |
1013
|
|
|
|
|
|
|
|
1014
|
|
|
|
|
|
|
${jspre}counter[baseID]++; |
1015
|
|
|
|
|
|
|
|
1016
|
|
|
|
|
|
|
// disable the "add field" button if we are at the limit |
1017
|
|
|
|
|
|
|
if (${jspre}counter[baseID] >= ${jspre}limit[baseID]) { |
1018
|
|
|
|
|
|
|
var addButton = document.getElementById('$grow' + '_' + baseID); |
1019
|
|
|
|
|
|
|
addButton.setAttribute('disabled', 'disabled'); |
1020
|
|
|
|
|
|
|
} |
1021
|
|
|
|
|
|
|
} |
1022
|
|
|
|
|
|
|
|
1023
|
|
|
|
|
|
|
EOJS |
1024
|
|
|
|
|
|
|
|
1025
|
|
|
|
|
|
|
# initialize growable counters |
1026
|
2
|
|
|
|
|
5
|
for (@growable) { |
1027
|
2
|
|
|
|
|
12
|
my $count = scalar(my @v = $_->values); |
1028
|
2
|
50
|
|
|
|
8
|
$jsmisc .= "${jspre}counter['$_'] = $count;\n" if $count > 0; |
1029
|
|
|
|
|
|
|
# assume that values of growable > 1 provide limits |
1030
|
2
|
|
|
|
|
6
|
my $limit = $_->growable; |
1031
|
2
|
50
|
33
|
|
|
16
|
if ($limit && $limit ne 1) { |
1032
|
0
|
|
|
|
|
0
|
$jsmisc .= "${jspre}limit['$_'] = $limit;\n"; |
1033
|
|
|
|
|
|
|
} |
1034
|
|
|
|
|
|
|
} |
1035
|
2
|
|
|
|
|
10
|
return $jsmisc; |
1036
|
|
|
|
|
|
|
} |
1037
|
|
|
|
|
|
|
|
1038
|
|
|
|
|
|
|
sub script_otherbox { |
1039
|
120
|
|
|
120
|
0
|
232
|
my $self = shift; |
1040
|
120
|
100
|
|
|
|
337
|
return '' unless my @otherable = grep { $_->other } $self->field; |
|
353
|
|
|
|
|
1004
|
|
1041
|
|
|
|
|
|
|
|
1042
|
5
|
|
50
|
|
|
24
|
my $jspre = $self->jsprefix || ''; |
1043
|
5
|
|
|
|
|
12
|
my $jsmisc = ''; |
1044
|
|
|
|
|
|
|
|
1045
|
5
|
|
|
|
|
24
|
$jsmisc .= <
|
1046
|
|
|
|
|
|
|
// turn on/off any "other"fields |
1047
|
|
|
|
|
|
|
function ${jspre}other_on (othername) { |
1048
|
|
|
|
|
|
|
var box = document.getElementById(othername); |
1049
|
|
|
|
|
|
|
box.removeAttribute('disabled'); |
1050
|
|
|
|
|
|
|
} |
1051
|
|
|
|
|
|
|
|
1052
|
|
|
|
|
|
|
function ${jspre}other_off (othername) { |
1053
|
|
|
|
|
|
|
var box = document.getElementById(othername); |
1054
|
|
|
|
|
|
|
box.setAttribute('disabled', 'disabled'); |
1055
|
|
|
|
|
|
|
} |
1056
|
|
|
|
|
|
|
|
1057
|
|
|
|
|
|
|
EOJS |
1058
|
|
|
|
|
|
|
|
1059
|
5
|
|
|
|
|
24
|
return $jsmisc; |
1060
|
|
|
|
|
|
|
} |
1061
|
|
|
|
|
|
|
|
1062
|
|
|
|
|
|
|
sub noscript { |
1063
|
15
|
|
|
15
|
0
|
32
|
my $self = shift; |
1064
|
|
|
|
|
|
|
# no state is kept and no args are allowed |
1065
|
15
|
50
|
|
|
|
65
|
puke "No args allowed for \$form->noscript" if @_; |
1066
|
15
|
50
|
|
|
|
89
|
return '' unless $self->javascript; |
1067
|
15
|
|
|
|
|
117
|
return ''; |
1068
|
|
|
|
|
|
|
} |
1069
|
|
|
|
|
|
|
|
1070
|
|
|
|
|
|
|
sub submits { |
1071
|
123
|
|
|
123
|
0
|
324
|
local $^W = 0; # -w sucks |
1072
|
123
|
|
|
|
|
203
|
my $self = shift; |
1073
|
|
|
|
|
|
|
|
1074
|
|
|
|
|
|
|
# handle the submit button(s) |
1075
|
|
|
|
|
|
|
# logic is a little complicated - if set but to a false value, |
1076
|
|
|
|
|
|
|
# then leave off. otherwise use as the value for the tags. |
1077
|
123
|
|
|
|
|
270
|
my @submit = (); |
1078
|
123
|
|
|
|
|
257
|
my $sn = $self->{submitname}; |
1079
|
123
|
|
|
|
|
484
|
my $sc = $self->class($self->{buttonname}); |
1080
|
123
|
100
|
|
|
|
425
|
if (ref $self->{submit} eq 'ARRAY') { |
1081
|
|
|
|
|
|
|
# multiple buttons + JavaScript - dynamically set the _submit value |
1082
|
17
|
50
|
|
|
|
69
|
my @oncl = $self->javascript |
1083
|
|
|
|
|
|
|
? (onclick => "this.form.$sn.value = this.value;") : (); |
1084
|
17
|
|
|
|
|
39
|
my $i=1; |
1085
|
17
|
|
|
|
|
79
|
for my $subval (autodata $self->{submit}) { |
1086
|
40
|
100
|
|
|
|
123
|
my $si = $i > 1 ? "_$i" : ''; # number with second one |
1087
|
40
|
|
|
|
|
244
|
push @submit, { type => 'submit', |
1088
|
|
|
|
|
|
|
id => "$self->{name}$sn$si", |
1089
|
|
|
|
|
|
|
class => $sc, |
1090
|
|
|
|
|
|
|
name => $sn, |
1091
|
|
|
|
|
|
|
value => $subval, @oncl }; |
1092
|
40
|
|
|
|
|
106
|
$i++; |
1093
|
|
|
|
|
|
|
} |
1094
|
|
|
|
|
|
|
} else { |
1095
|
|
|
|
|
|
|
# show the text on the button |
1096
|
106
|
100
|
|
|
|
1021
|
my $subval = $self->{submit} eq 1 ? $self->{messages}->form_submit_default |
1097
|
|
|
|
|
|
|
: $self->{submit}; |
1098
|
106
|
|
|
|
|
925
|
push @submit, { type => 'submit', |
1099
|
|
|
|
|
|
|
id => "$self->{name}$sn", |
1100
|
|
|
|
|
|
|
class => $sc, |
1101
|
|
|
|
|
|
|
name => $sn, |
1102
|
|
|
|
|
|
|
value => $subval }; |
1103
|
|
|
|
|
|
|
} |
1104
|
123
|
100
|
|
|
|
1009
|
return wantarray ? @submit : [ map { htmltag('input', $_) } @submit ]; |
|
3
|
|
|
|
|
7
|
|
1105
|
|
|
|
|
|
|
} |
1106
|
|
|
|
|
|
|
|
1107
|
|
|
|
|
|
|
sub submit { |
1108
|
128
|
|
|
128
|
1
|
247
|
my $self = shift; |
1109
|
128
|
50
|
|
|
|
365
|
$self->{submit} = shift if @_; |
1110
|
128
|
100
|
66
|
|
|
938
|
return '' if ! $self->{submit} || $self->static || $self->disabled; |
|
|
|
100
|
|
|
|
|
1111
|
|
|
|
|
|
|
|
1112
|
|
|
|
|
|
|
# no newline on buttons regardless of setting |
1113
|
122
|
|
|
|
|
526
|
return join '', map { htmltag('input', $_) } $self->submits(@_); |
|
143
|
|
|
|
|
633
|
|
1114
|
|
|
|
|
|
|
} |
1115
|
|
|
|
|
|
|
|
1116
|
|
|
|
|
|
|
sub reset { |
1117
|
129
|
|
|
129
|
1
|
413
|
local $^W = 0; # -w sucks |
1118
|
129
|
|
|
|
|
255
|
my $self = shift; |
1119
|
129
|
50
|
|
|
|
374
|
$self->{reset} = shift if @_; |
1120
|
129
|
50
|
66
|
|
|
914
|
return '' if ! $self->{reset} || $self->static || $self->disabled; |
|
|
|
66
|
|
|
|
|
1121
|
5
|
|
|
|
|
24
|
my $sc = $self->class($self->{buttonname}); |
1122
|
|
|
|
|
|
|
|
1123
|
|
|
|
|
|
|
# similar to submit(), but a little simpler ;-) |
1124
|
5
|
100
|
|
|
|
34
|
my $reset = $self->{reset} eq 1 ? $self->{messages}->form_reset_default |
1125
|
|
|
|
|
|
|
: $self->{reset}; |
1126
|
5
|
|
|
|
|
36
|
my $rn = $self->resetname; |
1127
|
5
|
|
|
|
|
32
|
return htmltag('input', type => 'reset', |
1128
|
|
|
|
|
|
|
id => "$self->{name}$rn", |
1129
|
|
|
|
|
|
|
class => $sc, |
1130
|
|
|
|
|
|
|
name => $rn, |
1131
|
|
|
|
|
|
|
value => $reset); |
1132
|
|
|
|
|
|
|
} |
1133
|
|
|
|
|
|
|
|
1134
|
|
|
|
|
|
|
sub text { |
1135
|
59
|
|
|
59
|
1
|
94
|
my $self = shift; |
1136
|
59
|
50
|
|
|
|
194
|
$self->{text} = shift if @_; |
1137
|
|
|
|
|
|
|
|
1138
|
|
|
|
|
|
|
# having any required fields changes the leading text |
1139
|
59
|
|
|
|
|
86
|
my $req = 0; |
1140
|
59
|
|
|
|
|
99
|
my $inv = 0; |
1141
|
59
|
|
|
|
|
160
|
for ($self->fields) { |
1142
|
173
|
100
|
|
|
|
806
|
$req++ if $_->required; |
1143
|
173
|
100
|
|
|
|
991
|
$inv++ if $_->invalid; # failed validate() |
1144
|
|
|
|
|
|
|
} |
1145
|
|
|
|
|
|
|
|
1146
|
59
|
100
|
100
|
|
|
305
|
unless ($self->static || $self->disabled) { |
1147
|
|
|
|
|
|
|
# only show either invalid or required text |
1148
|
56
|
100
|
|
|
|
149
|
return $self->{text} .' '. sprintf($self->{messages}->form_invalid_text, |
1149
|
|
|
|
|
|
|
$inv, |
1150
|
|
|
|
|
|
|
$self->invalid_tag).'' if $inv; |
1151
|
|
|
|
|
|
|
|
1152
|
54
|
100
|
|
|
|
217
|
return $self->{text} .' '. sprintf($self->{messages}->form_required_text, |
1153
|
|
|
|
|
|
|
$self->required_tag).'' if $req; |
1154
|
|
|
|
|
|
|
} |
1155
|
44
|
|
|
|
|
186
|
return $self->{text}; |
1156
|
|
|
|
|
|
|
} |
1157
|
|
|
|
|
|
|
|
1158
|
|
|
|
|
|
|
sub invalid_tag { |
1159
|
19
|
|
|
19
|
0
|
91
|
my $self = shift; |
1160
|
19
|
|
100
|
|
|
58
|
my $label = shift || ''; |
1161
|
19
|
100
|
|
|
|
99
|
my @tags = $self->{stylesheet} |
1162
|
|
|
|
|
|
|
? (qq(), '') |
1163
|
|
|
|
|
|
|
: ('', ''); |
1164
|
19
|
100
|
|
|
|
121
|
return wantarray ? @tags : join $label, @tags; |
1165
|
|
|
|
|
|
|
} |
1166
|
|
|
|
|
|
|
|
1167
|
|
|
|
|
|
|
sub required_tag { |
1168
|
48
|
|
|
48
|
0
|
72
|
my $self = shift; |
1169
|
48
|
|
100
|
|
|
154
|
my $label = shift || ''; |
1170
|
48
|
100
|
|
|
|
206
|
my @tags = $self->{stylesheet} |
1171
|
|
|
|
|
|
|
? (qq(), '') |
1172
|
|
|
|
|
|
|
: ('', ''); |
1173
|
48
|
100
|
|
|
|
268
|
return wantarray ? @tags : join $label, @tags; |
1174
|
|
|
|
|
|
|
} |
1175
|
|
|
|
|
|
|
|
1176
|
|
|
|
|
|
|
sub cgi_param { |
1177
|
9
|
|
|
9
|
1
|
26
|
my $self = shift; |
1178
|
9
|
|
|
|
|
51
|
$self->{params}->param(@_); |
1179
|
|
|
|
|
|
|
} |
1180
|
|
|
|
|
|
|
|
1181
|
|
|
|
|
|
|
sub tmpl_param { |
1182
|
74
|
|
|
74
|
1
|
141
|
my $self = shift; |
1183
|
74
|
100
|
|
|
|
204
|
if (my $key = shift) { |
1184
|
4
|
50
|
|
|
|
27
|
return @_ ? $self->{tmplvar}{$key} = shift |
1185
|
|
|
|
|
|
|
: $self->{tmplvar}{$key}; |
1186
|
|
|
|
|
|
|
} else { |
1187
|
|
|
|
|
|
|
# return hash or key/value pairs |
1188
|
70
|
|
100
|
|
|
369
|
my $hr = $self->{tmplvar} || {}; |
1189
|
70
|
50
|
|
|
|
388
|
return wantarray ? %$hr : $hr; |
1190
|
|
|
|
|
|
|
} |
1191
|
|
|
|
|
|
|
} |
1192
|
|
|
|
|
|
|
|
1193
|
|
|
|
|
|
|
sub version { |
1194
|
|
|
|
|
|
|
# Hidden trailer. If you perceive this as annoying, let me know and I |
1195
|
|
|
|
|
|
|
# may remove it. It's supposed to help. |
1196
|
129
|
50
|
|
129
|
0
|
893
|
return '' if $::TESTING; |
1197
|
0
|
0
|
|
|
|
0
|
if (ref $_[0]) { |
1198
|
0
|
|
|
|
|
0
|
return "\n\n"; |
1199
|
|
|
|
|
|
|
} else { |
1200
|
0
|
|
|
|
|
0
|
return "CGI::FormBuilder v$VERSION by Nate Wiger. All Rights Reserved.\n"; |
1201
|
|
|
|
|
|
|
} |
1202
|
|
|
|
|
|
|
} |
1203
|
|
|
|
|
|
|
|
1204
|
|
|
|
|
|
|
sub values { |
1205
|
49
|
|
|
49
|
1
|
94
|
my $self = shift; |
1206
|
|
|
|
|
|
|
|
1207
|
49
|
50
|
|
|
|
159
|
if (@_) { |
1208
|
49
|
|
|
|
|
162
|
$self->{values} = arghash(@_); |
1209
|
49
|
|
|
|
|
123
|
my %val = (); |
1210
|
49
|
|
|
|
|
104
|
my @val = (); |
1211
|
|
|
|
|
|
|
|
1212
|
|
|
|
|
|
|
# We currently make two passes, first getting the values |
1213
|
|
|
|
|
|
|
# and storing them into a temp hash, and then going thru |
1214
|
|
|
|
|
|
|
# the fields and picking up the values and attributes. |
1215
|
49
|
|
|
|
|
109
|
local $" = ','; |
1216
|
49
|
|
|
|
|
259
|
debug 1, "\$form->{values} = ($self->{values})"; |
1217
|
|
|
|
|
|
|
|
1218
|
|
|
|
|
|
|
# Using isa() allows objects to transparently fit in here |
1219
|
49
|
50
|
|
|
|
423
|
if (UNIVERSAL::isa($self->{values}, 'CODE')) { |
|
|
100
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
1220
|
|
|
|
|
|
|
# it's a sub; lookup each value in turn |
1221
|
0
|
|
|
|
|
0
|
for my $key (&{$self->{values}}) { |
|
0
|
|
|
|
|
0
|
|
1222
|
|
|
|
|
|
|
# always assume an arrayref of values... |
1223
|
0
|
|
|
|
|
0
|
$val{$key} = [ &{$self->{values}}($key) ]; |
|
0
|
|
|
|
|
0
|
|
1224
|
0
|
|
|
|
|
0
|
debug 2, "setting values from \\&code(): $key = (@{$val{$key}})"; |
|
0
|
|
|
|
|
0
|
|
1225
|
|
|
|
|
|
|
} |
1226
|
|
|
|
|
|
|
} elsif (UNIVERSAL::isa($self->{values}, 'HASH')) { |
1227
|
|
|
|
|
|
|
# must lc all the keys since we're case-insensitive, then |
1228
|
|
|
|
|
|
|
# we turn our values hashref into an arrayref on the fly |
1229
|
47
|
|
|
|
|
161
|
my @v = autodata $self->{values}; |
1230
|
47
|
|
|
|
|
229
|
while (@v) { |
1231
|
91
|
|
|
|
|
215
|
my $key = lc shift @v; |
1232
|
91
|
|
|
|
|
247
|
$val{$key} = [ autodata shift @v ]; |
1233
|
91
|
|
|
|
|
277
|
debug 2, "setting values from HASH: $key = (@{$val{$key}})"; |
|
91
|
|
|
|
|
395
|
|
1234
|
|
|
|
|
|
|
} |
1235
|
|
|
|
|
|
|
} elsif (UNIVERSAL::isa($self->{values}, 'ARRAY')) { |
1236
|
|
|
|
|
|
|
# also accept an arrayref which is walked sequentially below |
1237
|
2
|
|
|
|
|
10
|
debug 2, "setting values from ARRAY: (walked below)"; |
1238
|
2
|
|
|
|
|
11
|
@val = autodata $self->{values}; |
1239
|
|
|
|
|
|
|
} else { |
1240
|
0
|
|
|
|
|
0
|
puke "Unsupported operand to 'values' option - must be \\%hash, \\&sub, or \$object"; |
1241
|
|
|
|
|
|
|
} |
1242
|
|
|
|
|
|
|
|
1243
|
|
|
|
|
|
|
# redistribute values across all existing fields |
1244
|
49
|
|
|
|
|
199
|
for ($self->fields) { |
1245
|
118
|
|
100
|
|
|
557
|
my $v = $val{lc($_)} || shift @val; # use array if no value |
1246
|
118
|
100
|
|
|
|
535
|
$_->field(value => $v) if defined $v; |
1247
|
|
|
|
|
|
|
} |
1248
|
|
|
|
|
|
|
} |
1249
|
|
|
|
|
|
|
|
1250
|
|
|
|
|
|
|
} |
1251
|
|
|
|
|
|
|
|
1252
|
|
|
|
|
|
|
sub name { |
1253
|
60
|
|
|
60
|
1
|
91
|
my $self = shift; |
1254
|
60
|
50
|
|
|
|
331
|
@_ ? $self->{name} = shift : $self->{name}; |
1255
|
|
|
|
|
|
|
} |
1256
|
|
|
|
|
|
|
|
1257
|
|
|
|
|
|
|
sub nameopts { |
1258
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
1259
|
0
|
0
|
|
|
|
0
|
if (@_) { |
1260
|
0
|
|
|
|
|
0
|
$self->{nameopts} = shift; |
1261
|
0
|
|
|
|
|
0
|
for ($self->fields) { |
1262
|
0
|
|
|
|
|
0
|
$_->field(nameopts => $self->{nameopts}); |
1263
|
|
|
|
|
|
|
} |
1264
|
|
|
|
|
|
|
} |
1265
|
0
|
|
|
|
|
0
|
return $self->{nameopts}; |
1266
|
|
|
|
|
|
|
} |
1267
|
|
|
|
|
|
|
|
1268
|
|
|
|
|
|
|
sub sortopts { |
1269
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
1270
|
0
|
0
|
|
|
|
0
|
if (@_) { |
1271
|
0
|
|
|
|
|
0
|
$self->{sortopts} = shift; |
1272
|
0
|
|
|
|
|
0
|
for ($self->fields) { |
1273
|
0
|
|
|
|
|
0
|
$_->field(sortopts => $self->{sortopts}); |
1274
|
|
|
|
|
|
|
} |
1275
|
|
|
|
|
|
|
} |
1276
|
0
|
|
|
|
|
0
|
return $self->{sortopts}; |
1277
|
|
|
|
|
|
|
} |
1278
|
|
|
|
|
|
|
|
1279
|
|
|
|
|
|
|
sub selectnum { |
1280
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
1281
|
0
|
0
|
|
|
|
0
|
if (@_) { |
1282
|
0
|
|
|
|
|
0
|
$self->{selectnum} = shift; |
1283
|
0
|
|
|
|
|
0
|
for ($self->fields) { |
1284
|
0
|
|
|
|
|
0
|
$_->field(selectnum => $self->{selectnum}); |
1285
|
|
|
|
|
|
|
} |
1286
|
|
|
|
|
|
|
} |
1287
|
0
|
|
|
|
|
0
|
return $self->{selectnum}; |
1288
|
|
|
|
|
|
|
} |
1289
|
|
|
|
|
|
|
|
1290
|
|
|
|
|
|
|
sub options { |
1291
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
1292
|
0
|
0
|
|
|
|
0
|
if (@_) { |
1293
|
0
|
|
|
|
|
0
|
$self->{options} = arghash(@_); |
1294
|
0
|
|
|
|
|
0
|
my %val = (); |
1295
|
|
|
|
|
|
|
|
1296
|
|
|
|
|
|
|
# same case-insensitization as $form->values |
1297
|
0
|
|
|
|
|
0
|
my @v = autodata $self->{options}; |
1298
|
0
|
|
|
|
|
0
|
while (@v) { |
1299
|
0
|
|
|
|
|
0
|
my $key = lc shift @v; |
1300
|
0
|
|
|
|
|
0
|
$val{$key} = [ autodata shift @v ]; |
1301
|
|
|
|
|
|
|
} |
1302
|
|
|
|
|
|
|
|
1303
|
0
|
|
|
|
|
0
|
for ($self->fields) { |
1304
|
0
|
|
|
|
|
0
|
my $v = $val{lc($_)}; |
1305
|
0
|
0
|
|
|
|
0
|
$_->field(options => $v) if defined $v; |
1306
|
|
|
|
|
|
|
} |
1307
|
|
|
|
|
|
|
} |
1308
|
0
|
|
|
|
|
0
|
return $self->{options}; |
1309
|
|
|
|
|
|
|
} |
1310
|
|
|
|
|
|
|
|
1311
|
|
|
|
|
|
|
sub labels { |
1312
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
1313
|
0
|
0
|
|
|
|
0
|
if (@_) { |
1314
|
0
|
|
|
|
|
0
|
$self->{labels} = arghash(@_); |
1315
|
0
|
|
|
|
|
0
|
my %val = (); |
1316
|
|
|
|
|
|
|
|
1317
|
|
|
|
|
|
|
# same case-insensitization as $form->values |
1318
|
0
|
|
|
|
|
0
|
my @v = autodata $self->{labels}; |
1319
|
0
|
|
|
|
|
0
|
while (@v) { |
1320
|
0
|
|
|
|
|
0
|
my $key = lc shift @v; |
1321
|
0
|
|
|
|
|
0
|
$val{$key} = [ autodata shift @v ]; |
1322
|
|
|
|
|
|
|
} |
1323
|
|
|
|
|
|
|
|
1324
|
0
|
|
|
|
|
0
|
for ($self->fields) { |
1325
|
0
|
|
|
|
|
0
|
my $v = $val{lc($_)}; |
1326
|
0
|
0
|
|
|
|
0
|
$_->field(label => $v) if defined $v; |
1327
|
|
|
|
|
|
|
} |
1328
|
|
|
|
|
|
|
} |
1329
|
0
|
|
|
|
|
0
|
return $self->{labels}; |
1330
|
|
|
|
|
|
|
} |
1331
|
|
|
|
|
|
|
|
1332
|
|
|
|
|
|
|
# Note that validate does not work like a true accessor |
1333
|
|
|
|
|
|
|
sub validate { |
1334
|
19
|
|
|
19
|
1
|
154
|
my $self = shift; |
1335
|
|
|
|
|
|
|
|
1336
|
19
|
50
|
|
|
|
56
|
if (@_) { |
1337
|
0
|
0
|
|
|
|
0
|
if (ref $_[0]) { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
1338
|
|
|
|
|
|
|
# this'll either be a hashref or a DFV object |
1339
|
0
|
|
|
|
|
0
|
$self->{validate} = shift; |
1340
|
|
|
|
|
|
|
} elsif (@_ % 2 == 0) { |
1341
|
|
|
|
|
|
|
# someone passed a hash-as-list |
1342
|
0
|
|
|
|
|
0
|
$self->{validate} = { @_ }; |
1343
|
|
|
|
|
|
|
} elsif (@_ > 1) { |
1344
|
|
|
|
|
|
|
# just one argument we'll interpret as a DFV profile name; |
1345
|
|
|
|
|
|
|
# an odd number > 1 is probably a typo... |
1346
|
0
|
|
|
|
|
0
|
puke "Odd number of elements passed to validate"; |
1347
|
|
|
|
|
|
|
} |
1348
|
|
|
|
|
|
|
} |
1349
|
|
|
|
|
|
|
|
1350
|
19
|
|
|
|
|
24
|
my $ok = 1; |
1351
|
|
|
|
|
|
|
|
1352
|
19
|
50
|
|
|
|
80
|
if (UNIVERSAL::isa($self->{validate}, 'Data::FormValidator')) { |
1353
|
0
|
|
0
|
|
|
0
|
my $profile_name = shift || 'fb'; |
1354
|
0
|
|
|
|
|
0
|
debug 1, "validating fields via the '$profile_name' profile"; |
1355
|
|
|
|
|
|
|
# hang on to the DFV results, for things like DBIx::Class::WebForm |
1356
|
0
|
|
|
|
|
0
|
$self->{dfv_results} = $self->{validate}->check($self, $profile_name); |
1357
|
|
|
|
|
|
|
|
1358
|
|
|
|
|
|
|
# mark the invalid fields |
1359
|
0
|
|
|
|
|
0
|
my @invalid_fields = ( |
1360
|
|
|
|
|
|
|
$self->{dfv_results}->invalid, |
1361
|
|
|
|
|
|
|
$self->{dfv_results}->missing, |
1362
|
|
|
|
|
|
|
); |
1363
|
0
|
|
|
|
|
0
|
for my $field_name (@invalid_fields) { |
1364
|
0
|
|
|
|
|
0
|
$self->field( |
1365
|
|
|
|
|
|
|
name => $field_name, |
1366
|
|
|
|
|
|
|
invalid => 1, |
1367
|
|
|
|
|
|
|
); |
1368
|
|
|
|
|
|
|
} |
1369
|
|
|
|
|
|
|
# validation failed |
1370
|
0
|
0
|
|
|
|
0
|
$ok = 0 if @invalid_fields > 0; |
1371
|
|
|
|
|
|
|
} else { |
1372
|
19
|
|
|
|
|
48
|
debug 1, "validating all fields via \$form->validate"; |
1373
|
19
|
|
|
|
|
49
|
for ($self->fields) { |
1374
|
41
|
100
|
|
|
|
132
|
$ok = 0 unless $_->validate; |
1375
|
|
|
|
|
|
|
} |
1376
|
|
|
|
|
|
|
} |
1377
|
19
|
|
|
|
|
92
|
debug 1, "validation done, ok = $ok (should be 1)"; |
1378
|
19
|
|
|
|
|
64
|
return $ok; |
1379
|
|
|
|
|
|
|
} |
1380
|
|
|
|
|
|
|
|
1381
|
|
|
|
|
|
|
sub confirm { |
1382
|
|
|
|
|
|
|
# This is nothing more than a special wrapper around render() |
1383
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
1384
|
0
|
0
|
|
|
|
0
|
my $date = $::TESTING ? 'LOCALTIME' : localtime(); |
1385
|
0
|
|
0
|
|
|
0
|
$self->{text} ||= sprintf $self->{messages}->form_confirm_text, $date; |
1386
|
0
|
|
|
|
|
0
|
$self->{static} = 1; |
1387
|
0
|
|
|
|
|
0
|
return $self->render(@_); |
1388
|
|
|
|
|
|
|
} |
1389
|
|
|
|
|
|
|
|
1390
|
|
|
|
|
|
|
# Prepare a template |
1391
|
|
|
|
|
|
|
sub prepare { |
1392
|
70
|
|
|
70
|
1
|
119
|
my $self = shift; |
1393
|
70
|
|
|
|
|
319
|
debug 1, "Calling \$form->prepare(@_)"; |
1394
|
|
|
|
|
|
|
|
1395
|
|
|
|
|
|
|
# Build a big hashref of data that can be used by the template |
1396
|
|
|
|
|
|
|
# engine. Templates then have the ability to expand this however |
1397
|
|
|
|
|
|
|
# they see fit. |
1398
|
70
|
|
|
|
|
271
|
my %tmplvar = $self->tmpl_param; |
1399
|
|
|
|
|
|
|
|
1400
|
|
|
|
|
|
|
# This is based on the original Template Toolkit render() |
1401
|
70
|
|
|
|
|
244
|
for my $field ($self->field) { |
1402
|
|
|
|
|
|
|
|
1403
|
|
|
|
|
|
|
# Extract value since used often |
1404
|
201
|
|
|
|
|
827
|
my @value = $field->tag_value; |
1405
|
|
|
|
|
|
|
|
1406
|
|
|
|
|
|
|
# Create a struct for each field |
1407
|
201
|
|
|
|
|
1237
|
$tmplvar{field}{"$field"} = { |
1408
|
|
|
|
|
|
|
%$field, # gets invalid/missing/required |
1409
|
|
|
|
|
|
|
field => $field->tag, |
1410
|
|
|
|
|
|
|
value => $value[0], |
1411
|
|
|
|
|
|
|
values => \@value, |
1412
|
|
|
|
|
|
|
options => [$field->options], |
1413
|
|
|
|
|
|
|
label => $field->label, |
1414
|
|
|
|
|
|
|
type => $field->type, |
1415
|
|
|
|
|
|
|
comment => $field->comment, |
1416
|
|
|
|
|
|
|
nameopts => $field->nameopts, |
1417
|
|
|
|
|
|
|
cleanopts => $field->cleanopts, |
1418
|
|
|
|
|
|
|
}; |
1419
|
|
|
|
|
|
|
# Force-stringify "$field" to get name() under buggy Perls |
1420
|
201
|
|
|
|
|
1422
|
$tmplvar{field}{"$field"}{error} = $field->error; |
1421
|
|
|
|
|
|
|
} |
1422
|
|
|
|
|
|
|
|
1423
|
|
|
|
|
|
|
# Must generate JS first because it affects the others. |
1424
|
|
|
|
|
|
|
# This is a bit action-at-a-distance, but I just can't |
1425
|
|
|
|
|
|
|
# figure out a way around it. |
1426
|
70
|
|
|
|
|
343
|
debug 2, "\$tmplvar{jshead} = \$self->script"; |
1427
|
70
|
|
|
|
|
363
|
$tmplvar{jshead} = $self->script; |
1428
|
70
|
|
|
|
|
273
|
debug 2, "\$tmplvar{title} = \$self->title"; |
1429
|
70
|
|
|
|
|
319
|
$tmplvar{title} = $self->title; |
1430
|
70
|
|
|
|
|
226
|
debug 2, "\$tmplvar{start} = \$self->start . \$self->statetags . \$self->keepextras"; |
1431
|
70
|
|
|
|
|
358
|
$tmplvar{start} = $self->start . $self->statetags . $self->keepextras; |
1432
|
70
|
|
|
|
|
309
|
debug 2, "\$tmplvar{submit} = \$self->submit"; |
1433
|
70
|
|
|
|
|
280
|
$tmplvar{submit} = $self->submit; |
1434
|
70
|
|
|
|
|
372
|
debug 2, "\$tmplvar{reset} = \$self->reset"; |
1435
|
70
|
|
|
|
|
281
|
$tmplvar{reset} = $self->reset; |
1436
|
70
|
|
|
|
|
240
|
debug 2, "\$tmplvar{end} = \$self->end"; |
1437
|
70
|
|
|
|
|
234
|
$tmplvar{end} = $self->end; |
1438
|
70
|
|
|
|
|
315
|
debug 2, "\$tmplvar{invalid} = \$self->invalid"; |
1439
|
70
|
|
|
|
|
374
|
$tmplvar{invalid} = $self->invalid; |
1440
|
70
|
|
|
|
|
263
|
debug 2, "\$tmplvar{required} = \$self->required"; |
1441
|
70
|
|
|
|
|
330
|
$tmplvar{required} = $self->required; |
1442
|
|
|
|
|
|
|
|
1443
|
70
|
|
|
|
|
317
|
my $fieldsets = $self->fieldsets; |
1444
|
70
|
|
|
|
|
246
|
for my $key (keys %$fieldsets) { |
1445
|
74
|
|
|
|
|
691
|
$tmplvar{fieldset}{$key} = { |
1446
|
|
|
|
|
|
|
name => $key, |
1447
|
|
|
|
|
|
|
label => $fieldsets->{$key}, |
1448
|
|
|
|
|
|
|
} |
1449
|
|
|
|
|
|
|
} |
1450
|
70
|
|
|
|
|
240
|
$tmplvar{fieldsets} = [ map $tmplvar{fieldset}{$_}, $self->fieldsets ]; |
1451
|
|
|
|
|
|
|
|
1452
|
70
|
|
|
|
|
257
|
debug 2, "\$tmplvar{fields} = [ map \$tmplvar{field}{\$_}, \$self->field ]"; |
1453
|
70
|
|
|
|
|
215
|
$tmplvar{fields} = [ map $tmplvar{field}{$_}, $self->field ]; |
1454
|
|
|
|
|
|
|
|
1455
|
70
|
50
|
|
|
|
470
|
return wantarray ? %tmplvar : \%tmplvar; |
1456
|
|
|
|
|
|
|
} |
1457
|
|
|
|
|
|
|
|
1458
|
|
|
|
|
|
|
sub render { |
1459
|
70
|
|
|
70
|
1
|
1419
|
local $^W = 0; # -w sucks |
1460
|
70
|
|
|
|
|
231
|
my $self = shift; |
1461
|
70
|
|
|
|
|
404
|
debug 1, "starting \$form->render(@_)"; |
1462
|
|
|
|
|
|
|
|
1463
|
|
|
|
|
|
|
# any arguments are used to make permanent changes to the $form |
1464
|
70
|
100
|
|
|
|
224
|
if (@_) { |
1465
|
1
|
50
|
|
|
|
7
|
puke "Odd number of arguments passed into \$form->render()" |
1466
|
|
|
|
|
|
|
unless @_ % 2 == 0; |
1467
|
1
|
|
|
|
|
4
|
while (@_) { |
1468
|
3
|
|
|
|
|
5
|
my $k = shift; |
1469
|
3
|
|
|
|
|
18
|
$self->$k(shift); |
1470
|
|
|
|
|
|
|
} |
1471
|
|
|
|
|
|
|
} |
1472
|
|
|
|
|
|
|
|
1473
|
|
|
|
|
|
|
# check for engine type |
1474
|
70
|
|
|
|
|
108
|
my $mod; |
1475
|
70
|
|
|
|
|
185
|
my $ref = ref $self->{template}; |
1476
|
70
|
50
|
66
|
|
|
399
|
if (! $ref && $self->{template}) { |
1477
|
|
|
|
|
|
|
# "legacy" string filename for HTML::Template; redo format |
1478
|
|
|
|
|
|
|
# modifying $self object is ok because it's compatible |
1479
|
0
|
|
|
|
|
0
|
$self->{template} = { |
1480
|
|
|
|
|
|
|
type => 'HTML', |
1481
|
|
|
|
|
|
|
filename => $self->{template}, |
1482
|
|
|
|
|
|
|
}; |
1483
|
0
|
|
|
|
|
0
|
$ref = 'HASH'; # tricky |
1484
|
0
|
|
|
|
|
0
|
debug 2, "rewrote 'template' option since found filename"; |
1485
|
|
|
|
|
|
|
} |
1486
|
|
|
|
|
|
|
# Get ourselves ready |
1487
|
70
|
|
|
|
|
283
|
$self->{prepare} = $self->prepare; |
1488
|
|
|
|
|
|
|
# weaken($self->{prepare}); |
1489
|
|
|
|
|
|
|
|
1490
|
70
|
|
|
|
|
117
|
my $opt; |
1491
|
70
|
100
|
|
|
|
552
|
if ($ref eq 'HASH') { |
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
|
|
50
|
|
|
|
|
|
1492
|
|
|
|
|
|
|
# must copy to avoid destroying |
1493
|
12
|
|
|
|
|
20
|
$opt = { %{ $self->{template} } }; |
|
12
|
|
|
|
|
71
|
|
1494
|
12
|
|
100
|
|
|
87
|
$mod = ucfirst(delete $opt->{type} || 'HTML'); |
1495
|
|
|
|
|
|
|
} elsif ($ref eq 'CODE') { |
1496
|
|
|
|
|
|
|
# subroutine wrapper |
1497
|
0
|
|
|
|
|
0
|
return &{$self->{template}}($self); |
|
0
|
|
|
|
|
0
|
|
1498
|
|
|
|
|
|
|
} elsif (UNIVERSAL::can($self->{template}, 'render')) { |
1499
|
|
|
|
|
|
|
# instantiated object |
1500
|
0
|
|
|
|
|
0
|
return $self->{template}->render($self); |
1501
|
|
|
|
|
|
|
} elsif ($ref) { |
1502
|
0
|
|
|
|
|
0
|
puke "Unsupported operand to 'template' option - must be \\%hash, \\&sub, or \$object w/ render()"; |
1503
|
|
|
|
|
|
|
} |
1504
|
|
|
|
|
|
|
|
1505
|
|
|
|
|
|
|
# load user-specified rendering module, or builtin rendering |
1506
|
70
|
|
100
|
|
|
346
|
$mod ||= 'Builtin'; |
1507
|
|
|
|
|
|
|
|
1508
|
|
|
|
|
|
|
# user can give 'Their::Complete::Module' or an 'IncludedAdapter' |
1509
|
70
|
50
|
|
|
|
480
|
$mod = join '::', __PACKAGE__, 'Template', $mod unless $mod =~ /::/; |
1510
|
70
|
|
|
|
|
339
|
debug 1, "loading $mod for 'template' option"; |
1511
|
|
|
|
|
|
|
|
1512
|
|
|
|
|
|
|
# load module |
1513
|
70
|
|
|
|
|
6565
|
eval "require $mod"; |
1514
|
70
|
100
|
|
|
|
482
|
puke "Bad template engine $mod: $@" if $@; |
1515
|
|
|
|
|
|
|
|
1516
|
|
|
|
|
|
|
# create new object |
1517
|
|
|
|
|
|
|
#CGI::FormBuilder::Template::Builtin |
1518
|
|
|
|
|
|
|
|
1519
|
63
|
|
|
|
|
455
|
my $tmpl = $mod->new($opt); |
1520
|
|
|
|
|
|
|
# Experiemental: Alter tag names as we're rendering, to support |
1521
|
|
|
|
|
|
|
# Ajaxian markup schemes that use their own tags (Backbase, Dojo, etc) |
1522
|
63
|
|
|
|
|
139
|
local %CGI::FormBuilder::Util::TAGNAMES; |
1523
|
63
|
|
|
|
|
176
|
while (my($k,$v) = each %{$self->{tagnames}}) { |
|
72
|
|
|
|
|
469
|
|
1524
|
9
|
|
|
|
|
19
|
$CGI::FormBuilder::Util::TAGNAMES{$k} = $v; |
1525
|
|
|
|
|
|
|
} |
1526
|
|
|
|
|
|
|
|
1527
|
|
|
|
|
|
|
|
1528
|
|
|
|
|
|
|
# Call the engine's prepare too, if it exists |
1529
|
|
|
|
|
|
|
# Give it the form object so it can do what it wants |
1530
|
|
|
|
|
|
|
# This will have all of the prepared data in {prepare} anyways |
1531
|
63
|
100
|
66
|
|
|
573
|
if ($tmpl && UNIVERSAL::can($tmpl, 'prepare')) { |
1532
|
59
|
|
|
|
|
248
|
$tmpl->prepare($self); |
1533
|
|
|
|
|
|
|
} |
1534
|
|
|
|
|
|
|
|
1535
|
|
|
|
|
|
|
|
1536
|
|
|
|
|
|
|
|
1537
|
|
|
|
|
|
|
# dispatch to engine, prepend header |
1538
|
63
|
|
|
|
|
494
|
debug 1, "returning $tmpl->render($self->{prepare})"; |
1539
|
|
|
|
|
|
|
|
1540
|
63
|
|
|
|
|
290
|
my $ret = $self->header . $tmpl->render($self->{prepare}); |
1541
|
|
|
|
|
|
|
|
1542
|
|
|
|
|
|
|
#we have a circular reference but we need to kill it after setting up return |
1543
|
63
|
|
|
|
|
2002
|
weaken($self->{prepare}); |
1544
|
63
|
|
|
|
|
728
|
return $ret; |
1545
|
|
|
|
|
|
|
} |
1546
|
|
|
|
|
|
|
|
1547
|
|
|
|
|
|
|
# These routines should be moved to ::Mail or something since they're rarely used |
1548
|
|
|
|
|
|
|
sub mail () { |
1549
|
|
|
|
|
|
|
# This is a very generic mail handler |
1550
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
1551
|
0
|
|
|
|
|
0
|
my $args = arghash(@_); |
1552
|
|
|
|
|
|
|
|
1553
|
|
|
|
|
|
|
# Where does the mailer live? Must be sendmail-compatible |
1554
|
0
|
|
|
|
|
0
|
my $mailer = undef; |
1555
|
0
|
0
|
0
|
|
|
0
|
unless ($mailer = $args->{mailer} && -x $mailer) { |
1556
|
0
|
|
|
|
|
0
|
for my $sendmail (qw(/usr/lib/sendmail /usr/sbin/sendmail /usr/bin/sendmail)) { |
1557
|
0
|
0
|
|
|
|
0
|
if (-x $sendmail) { |
1558
|
0
|
|
|
|
|
0
|
$mailer = "$sendmail -t"; |
1559
|
0
|
|
|
|
|
0
|
last; |
1560
|
|
|
|
|
|
|
} |
1561
|
|
|
|
|
|
|
} |
1562
|
|
|
|
|
|
|
} |
1563
|
0
|
0
|
|
|
|
0
|
unless ($mailer) { |
1564
|
0
|
|
|
|
|
0
|
belch "Cannot find a sendmail-compatible mailer; use mailer => '/path/to/mailer'"; |
1565
|
0
|
|
|
|
|
0
|
return; |
1566
|
|
|
|
|
|
|
} |
1567
|
0
|
0
|
|
|
|
0
|
unless ($args->{to}) { |
1568
|
0
|
|
|
|
|
0
|
belch "Missing required 'to' argument; cannot continue without recipient"; |
1569
|
0
|
|
|
|
|
0
|
return; |
1570
|
|
|
|
|
|
|
} |
1571
|
0
|
0
|
|
|
|
0
|
if ($args->{from}) { |
1572
|
0
|
|
|
|
|
0
|
(my $from = $args->{from}) =~ s/"/\\"/g; |
1573
|
0
|
|
|
|
|
0
|
$mailer .= qq( -f "$from"); |
1574
|
|
|
|
|
|
|
} |
1575
|
|
|
|
|
|
|
|
1576
|
0
|
|
|
|
|
0
|
debug 1, "opening new mail to $args->{to}"; |
1577
|
|
|
|
|
|
|
|
1578
|
|
|
|
|
|
|
# untaint |
1579
|
0
|
|
|
|
|
0
|
my $oldpath = $ENV{PATH}; |
1580
|
0
|
|
|
|
|
0
|
$ENV{PATH} = '/usr/bin:/usr/sbin'; |
1581
|
|
|
|
|
|
|
|
1582
|
0
|
0
|
|
|
|
0
|
open(MAIL, "|$mailer >/dev/null 2>&1") || next; |
1583
|
0
|
|
|
|
|
0
|
print MAIL "From: $args->{from}\n"; |
1584
|
0
|
|
|
|
|
0
|
print MAIL "To: $args->{to}\n"; |
1585
|
0
|
0
|
|
|
|
0
|
print MAIL "Cc: $args->{cc}\n" if $args->{cc}; |
1586
|
0
|
0
|
|
|
|
0
|
print MAIL "Content-Type: text/plain; charset=\"" |
1587
|
|
|
|
|
|
|
. $self->charset . "\"\n" if $self->charset; |
1588
|
0
|
|
|
|
|
0
|
print MAIL "Subject: $args->{subject}\n\n"; |
1589
|
0
|
|
|
|
|
0
|
print MAIL "$args->{text}\n"; |
1590
|
|
|
|
|
|
|
|
1591
|
|
|
|
|
|
|
# retaint |
1592
|
0
|
|
|
|
|
0
|
$ENV{PATH} = $oldpath; |
1593
|
|
|
|
|
|
|
|
1594
|
0
|
|
|
|
|
0
|
return close(MAIL); |
1595
|
|
|
|
|
|
|
} |
1596
|
|
|
|
|
|
|
|
1597
|
|
|
|
|
|
|
sub mailconfirm () { |
1598
|
|
|
|
|
|
|
|
1599
|
|
|
|
|
|
|
# This prints out a very generic message. This should probably |
1600
|
|
|
|
|
|
|
# be much better, but I suspect very few if any people will use |
1601
|
|
|
|
|
|
|
# this method. If you do, let me know and maybe I'll work on it. |
1602
|
|
|
|
|
|
|
|
1603
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
1604
|
0
|
0
|
|
|
|
0
|
my $to = shift unless (@_ > 1); |
1605
|
0
|
|
|
|
|
0
|
my $args = arghash(@_); |
1606
|
|
|
|
|
|
|
|
1607
|
|
|
|
|
|
|
# must have a "to" |
1608
|
0
|
0
|
0
|
|
|
0
|
return unless $args->{to} ||= $to; |
1609
|
|
|
|
|
|
|
|
1610
|
|
|
|
|
|
|
# defaults |
1611
|
0
|
|
0
|
|
|
0
|
$args->{from} ||= 'auto-reply'; |
1612
|
0
|
|
0
|
|
|
0
|
$args->{subject} ||= sprintf $self->{messages}->mail_confirm_subject, $self->title; |
1613
|
0
|
|
0
|
|
|
0
|
$args->{text} ||= sprintf $self->{messages}->mail_confirm_text, scalar localtime(); |
1614
|
|
|
|
|
|
|
|
1615
|
0
|
|
|
|
|
0
|
debug 1, "mailconfirm() called, subject = '$args->{subject}'"; |
1616
|
|
|
|
|
|
|
|
1617
|
0
|
|
|
|
|
0
|
$self->mail($args); |
1618
|
|
|
|
|
|
|
} |
1619
|
|
|
|
|
|
|
|
1620
|
|
|
|
|
|
|
sub mailresults () { |
1621
|
|
|
|
|
|
|
# This is a wrapper around mail() that sends the form results |
1622
|
0
|
|
|
0
|
1
|
0
|
my $self = shift; |
1623
|
0
|
|
|
|
|
0
|
my $args = arghash(@_); |
1624
|
|
|
|
|
|
|
|
1625
|
0
|
0
|
|
|
|
0
|
if (exists $args->{plugin}) { |
1626
|
0
|
|
|
|
|
0
|
my $lib = "CGI::FormBuilder::Mail::$args->{plugin}"; |
1627
|
0
|
|
|
|
|
0
|
eval "use $lib"; |
1628
|
0
|
0
|
|
|
|
0
|
puke "Cannot use mailresults() plugin '$lib': $@" if $@; |
1629
|
0
|
|
|
|
|
0
|
eval { |
1630
|
0
|
|
|
|
|
0
|
my $plugin = $lib->new( form => $self, %$args ); |
1631
|
0
|
|
|
|
|
0
|
$plugin->mailresults(); |
1632
|
|
|
|
|
|
|
}; |
1633
|
0
|
0
|
|
|
|
0
|
puke "Could not mailresults() with plugin '$lib': $@" if $@; |
1634
|
0
|
|
|
|
|
0
|
return; |
1635
|
|
|
|
|
|
|
} |
1636
|
|
|
|
|
|
|
|
1637
|
|
|
|
|
|
|
# Get the field separator to use |
1638
|
0
|
|
0
|
|
|
0
|
my $delim = $args->{delimiter} || ': '; |
1639
|
0
|
|
0
|
|
|
0
|
my $join = $args->{joiner} || $"; |
1640
|
0
|
|
0
|
|
|
0
|
my $sep = $args->{separator} || "\n"; |
1641
|
|
|
|
|
|
|
|
1642
|
|
|
|
|
|
|
# subject default |
1643
|
0
|
|
0
|
|
|
0
|
$args->{subject} ||= sprintf $self->{messages}->mail_results_subject, $self->title; |
1644
|
0
|
|
|
|
|
0
|
debug 1, "mailresults() called, subject = '$args->{subject}'"; |
1645
|
|
|
|
|
|
|
|
1646
|
0
|
0
|
|
|
|
0
|
if ($args->{skip}) { |
1647
|
0
|
0
|
|
|
|
0
|
if ($args->{skip} =~ m#^m?(\S)(.*)\1$#) { |
1648
|
0
|
|
|
|
|
0
|
($args->{skip} = $2) =~ s/\\\//\//g; |
1649
|
0
|
|
|
|
|
0
|
$args->{skip} =~ s/\//\\\//g; |
1650
|
|
|
|
|
|
|
} |
1651
|
|
|
|
|
|
|
} |
1652
|
|
|
|
|
|
|
|
1653
|
0
|
|
|
|
|
0
|
my @form = (); |
1654
|
0
|
|
|
|
|
0
|
for my $field ($self->fields) { |
1655
|
0
|
0
|
0
|
|
|
0
|
if ($args->{skip} && $field =~ /$args->{skip}/) { |
1656
|
0
|
|
|
|
|
0
|
next; |
1657
|
|
|
|
|
|
|
} |
1658
|
0
|
|
|
|
|
0
|
my $v = join $join, $field->value; |
1659
|
0
|
0
|
|
|
|
0
|
$field = $field->label if $args->{labels}; |
1660
|
0
|
|
|
|
|
0
|
push @form, "$field$delim$v"; |
1661
|
|
|
|
|
|
|
} |
1662
|
0
|
|
|
|
|
0
|
my $text = join $sep, @form; |
1663
|
|
|
|
|
|
|
|
1664
|
0
|
|
|
|
|
0
|
$self->mail(%$args, text => $text); |
1665
|
|
|
|
|
|
|
} |
1666
|
|
|
|
|
|
|
|
1667
|
131
|
|
|
131
|
|
49178
|
sub DESTROY { 1 } |
1668
|
|
|
|
|
|
|
|
1669
|
|
|
|
|
|
|
# This is used to access all options after new(), by name |
1670
|
|
|
|
|
|
|
sub AUTOLOAD { |
1671
|
|
|
|
|
|
|
# This allows direct addressing by name |
1672
|
3229
|
|
|
3229
|
|
13259
|
local $^W = 0; |
1673
|
3229
|
|
|
|
|
4433
|
my $self = shift; |
1674
|
3229
|
|
|
|
|
14636
|
my($name) = $AUTOLOAD =~ /.*::(.+)/; |
1675
|
|
|
|
|
|
|
|
1676
|
|
|
|
|
|
|
# If fieldsubs => 1 set, then allow grabbing fields directly |
1677
|
3229
|
50
|
33
|
|
|
9251
|
if ($self->{fieldsubs} && $self->{fieldrefs}{$name}) { |
1678
|
0
|
|
|
|
|
0
|
return $self->field(name => $name, @_); |
1679
|
|
|
|
|
|
|
} |
1680
|
|
|
|
|
|
|
|
1681
|
3229
|
|
|
|
|
12368
|
debug 3, "-> dispatch to \$form->{$name} = @_"; |
1682
|
3229
|
100
|
|
|
|
7743
|
if (@_ % 2 == 1) { |
1683
|
5
|
|
|
|
|
14
|
$self->{$name} = shift; |
1684
|
|
|
|
|
|
|
|
1685
|
5
|
100
|
|
|
|
15
|
if ($REARRANGE{$name}) { |
1686
|
|
|
|
|
|
|
# needs to be splatted into every field |
1687
|
2
|
|
|
|
|
6
|
for ($self->fields) { |
1688
|
6
|
|
|
|
|
19
|
my $tval = rearrange($self->{$name}, "$_"); |
1689
|
6
|
|
|
|
|
32
|
$_->$name($tval); |
1690
|
|
|
|
|
|
|
} |
1691
|
|
|
|
|
|
|
} |
1692
|
|
|
|
|
|
|
} |
1693
|
|
|
|
|
|
|
|
1694
|
|
|
|
|
|
|
# Try to catch $form->$fieldname usage |
1695
|
3229
|
50
|
66
|
|
|
20730
|
if ((! exists($self->{$name}) || @_) && ! $CGI::FormBuilder::Util::OURATTR{$name}) { |
|
|
|
66
|
|
|
|
|
1696
|
0
|
0
|
|
|
|
0
|
if ($self->{fieldsubs}) { |
1697
|
0
|
|
|
|
|
0
|
return $self->field(name => $name, @_); |
1698
|
|
|
|
|
|
|
} else { |
1699
|
0
|
|
|
|
|
0
|
belch "Possible field access via \$form->$name() - see 'fieldsubs' option" |
1700
|
|
|
|
|
|
|
} |
1701
|
|
|
|
|
|
|
} |
1702
|
|
|
|
|
|
|
|
1703
|
3229
|
|
|
|
|
16444
|
return $self->{$name}; |
1704
|
|
|
|
|
|
|
} |
1705
|
|
|
|
|
|
|
|
1706
|
|
|
|
|
|
|
1; |
1707
|
|
|
|
|
|
|
__END__ |