Parent Directory
|
Revision Log
Revision 4256 -
(view)
(download)
(as text)
Original Path: trunk/webwork-modperl/lib/WeBWorK/CGI.pm
| 1 : | gage | 4212 | ################################################################################ |
| 2 : | # WeBWorK Online Homework Delivery System | ||
| 3 : | # Copyright © 2000-2006 The WeBWorK Project, http://openwebwork.sf.net/ | ||
| 4 : | gage | 4232 | |
| 5 : | gage | 4254 | # $CVSHeader: webwork2/lib/WeBWorK/CGI.pm,v 1.20 2006/07/13 19:38:19 gage Exp $ |
| 6 : | gage | 4212 | # |
| 7 : | # This program is free software; you can redistribute it and/or modify it under | ||
| 8 : | # the terms of either: (a) the GNU General Public License as published by the | ||
| 9 : | # Free Software Foundation; either version 2, or (at your option) any later | ||
| 10 : | # version, or (b) the "Artistic License" which comes with this package. | ||
| 11 : | # | ||
| 12 : | # This program is distributed in the hope that it will be useful, but WITHOUT | ||
| 13 : | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 14 : | # FOR A PARTICULAR PURPOSE. See either the GNU General Public License or the | ||
| 15 : | # Artistic License for more details. | ||
| 16 : | ################################################################################ | ||
| 17 : | gage | 4233 | |
| 18 : | |||
| 19 : | |||
| 20 : | |||
| 21 : | gage | 4212 | use HTML::EasyTags; |
| 22 : | use strict; | ||
| 23 : | package CGI; # (override standard CGI namespace!!) | ||
| 24 : | |||
| 25 : | @CGI::ISA = qw(HTML::EasyTags); | ||
| 26 : | our $html2 = HTML::EasyTags->new(); | ||
| 27 : | our $AUTOLOAD; | ||
| 28 : | |||
| 29 : | sub AUTOLOAD { | ||
| 30 : | my $func = $AUTOLOAD; | ||
| 31 : | $func =~ s/^CGI:://; | ||
| 32 : | my $result; | ||
| 33 : | my @inputs = @_; | ||
| 34 : | gage | 4254 | # normalize @inputs; |
| 35 : | if ( ref($inputs[0]) =~/HASH/ ) { #attributes or all parameters have been defined | ||
| 36 : | my $attributes = shift @inputs; | ||
| 37 : | if ( @inputs == 0 ) { | ||
| 38 : | # do nothing -- verything was defined in the attributes | ||
| 39 : | } elsif (ref($inputs[0]) =~/ARRAY/) { # implied group is this case legal? | ||
| 40 : | gage | 4256 | #print "array is here"; |
| 41 : | gage | 4254 | warn "can't follow an ARRAY by other inputs $func @inputs " if @inputs >1; |
| 42 : | } else { #combine remaining inputs for text field | ||
| 43 : | my $text = join("", @inputs); | ||
| 44 : | gage | 4256 | @inputs = ($text); |
| 45 : | gage | 4254 | } |
| 46 : | gage | 4256 | unshift @inputs, $attributes; |
| 47 : | #print "next inputs @inputs"; | ||
| 48 : | gage | 4254 | } elsif (ref($inputs[0]) =~/ARRAY/) { # implied group no other terms allowed |
| 49 : | $func = $func.'_group'; | ||
| 50 : | @inputs = ($inputs[0]); | ||
| 51 : | warn "can't follow an unnamed ARRAY by other inputs $func @inputs " if @inputs >1; | ||
| 52 : | } elsif (@inputs <=1 ) { | ||
| 53 : | # do nothing -- this is something like CGI::p(); | ||
| 54 : | } elsif (@inputs ==2) { # could be two values or a name value pair | ||
| 55 : | if ($inputs[0] =~ /^-?(name|value)$/ ) { # it's a name value pair; | ||
| 56 : | my %inputs = @inputs; | ||
| 57 : | @inputs = (\%inputs); # everything is packaged | ||
| 58 : | } else { | ||
| 59 : | # print "\n#########two values case $func @inputs##########"; # this is for debugging it's actually ok | ||
| 60 : | # this has to be handled individually for each value of $func | ||
| 61 : | } | ||
| 62 : | } elsif (grep /^-?(name|value)$/ , @inputs ) { # -name or -value appears | ||
| 63 : | if (@inputs%2 ==1 ) { | ||
| 64 : | warn "CGI call with named parameters has an odd number of inputs $func, @inputs "; | ||
| 65 : | } else { | ||
| 66 : | my %inputs = @inputs; | ||
| 67 : | @inputs = (\%inputs); | ||
| 68 : | } | ||
| 69 : | } else { | ||
| 70 : | # pass inputs directly to EasyTags | ||
| 71 : | } | ||
| 72 : | # check | ||
| 73 : | # print "\n\n$func inputs:", join(" ",@inputs), " ", (ref($inputs[0]) =~/HASH/)?join(" ", %{$inputs[0]}):'', "\n"; | ||
| 74 : | |||
| 75 : | gage | 4212 | # reverse order to make this compatible with CGI |
| 76 : | $func=~s/^start_?(.*)$/$1_start/; | ||
| 77 : | $func=~s/^end_?(.*)$/$1_end/; | ||
| 78 : | gage | 4214 | my $prolog = ''; |
| 79 : | my $postlog = ''; | ||
| 80 : | gage | 4212 | # handle special cases |
| 81 : | gage | 4251 | CASES:{ |
| 82 : | gage | 4232 | $func =~/^(checkbox)$/ && do { |
| 83 : | gage | 4254 | my $type = $func; |
| 84 : | $func ='input'; | ||
| 85 : | my %inputs=(); | ||
| 86 : | if (ref($inputs[0]) =~/HASH/ ) { # | ||
| 87 : | %inputs = %{$inputs[0]}; | ||
| 88 : | } else { | ||
| 89 : | $inputs{name} = $inputs[0]; | ||
| 90 : | $inputs{value} = $inputs[1]; | ||
| 91 : | } | ||
| 92 : | $inputs{-type} = $type; | ||
| 93 : | my $labels_key = normalizeName('labels?',keys %inputs); | ||
| 94 : | my $label = ($labels_key)?$inputs{$labels_key}:''; | ||
| 95 : | delete($inputs{$labels_key}) if defined $labels_key and exists($inputs{$labels_key}); | ||
| 96 : | @inputs = (\%inputs); | ||
| 97 : | if (defined($label) and $label) { | ||
| 98 : | $prolog = "<label>"; | ||
| 99 : | $postlog = "$label</label>"; | ||
| 100 : | } | ||
| 101 : | last CASES; | ||
| 102 : | }; | ||
| 103 : | $func =~/^(textfield|password_field)$/ && do { | ||
| 104 : | my $type = 'text'; | ||
| 105 : | $func ='input'; | ||
| 106 : | my %inputs=(); | ||
| 107 : | if (ref($inputs[0]) =~/HASH/ ) { # | ||
| 108 : | %inputs = %{$inputs[0]}; | ||
| 109 : | } else { | ||
| 110 : | $inputs{name} = $inputs[0]; | ||
| 111 : | $inputs{value} = $inputs[1]; | ||
| 112 : | } | ||
| 113 : | $inputs{type} = $type; | ||
| 114 : | last CASES; | ||
| 115 : | }; | ||
| 116 : | $func =~/^textarea$/ && do { | ||
| 117 : | my %inputs=(); | ||
| 118 : | if (ref($inputs[0]) =~/HASH/ ) { # | ||
| 119 : | %inputs = %{$inputs[0]}; | ||
| 120 : | } else { | ||
| 121 : | $inputs{name} = $inputs[0]; | ||
| 122 : | $inputs{value} = $inputs[1]; | ||
| 123 : | } | ||
| 124 : | my $default_label = normalizeName('defaults?',keys %inputs); | ||
| 125 : | $inputs{-text} = $inputs{$default_label} if defined $default_label; | ||
| 126 : | %inputs = %{removeParam($default_label, \%inputs)}; | ||
| 127 : | @inputs = (\%inputs ); | ||
| 128 : | last CASES; | ||
| 129 : | }; | ||
| 130 : | $func =~/^submit$/ && do { | ||
| 131 : | my $type = $func; | ||
| 132 : | $func ='input'; | ||
| 133 : | my %inputs=(); | ||
| 134 : | if (ref($inputs[0]) =~/HASH/ ) { # | ||
| 135 : | %inputs = %{$inputs[0]}; | ||
| 136 : | } else { | ||
| 137 : | $inputs{name} = $inputs[0]; | ||
| 138 : | $inputs{value} = $inputs[1]; | ||
| 139 : | } | ||
| 140 : | $inputs{-type} = $type; | ||
| 141 : | my ($labels_key) = normalizeName('labels?',keys %inputs); | ||
| 142 : | $inputs{-value}= $inputs{$labels_key} if defined $labels_key and exists $inputs{$labels_key}; # use value for name | ||
| 143 : | delete($inputs{$labels_key}) if defined $labels_key and exists $inputs{$labels_key}; | ||
| 144 : | @inputs = (\%inputs); | ||
| 145 : | last CASES; | ||
| 146 : | }; | ||
| 147 : | $func =~/^radio$/ && do { | ||
| 148 : | my $type = $func; | ||
| 149 : | $func ='input'; | ||
| 150 : | my %inputs=(); | ||
| 151 : | if (ref($inputs[0]) =~/HASH/ ) { # | ||
| 152 : | %inputs = %{$inputs[0]}; | ||
| 153 : | } else { | ||
| 154 : | $inputs{name} = $inputs[0]; | ||
| 155 : | $inputs{value} = $inputs[1]; | ||
| 156 : | } | ||
| 157 : | $inputs{-type} = $type; | ||
| 158 : | my ($values_key) = normalizeName('values?',keys %inputs); | ||
| 159 : | $inputs{-value}= $inputs{$values_key}; # use value for name | ||
| 160 : | delete($inputs{$values_key}) if defined $values_key and exists $inputs{$values_key}; | ||
| 161 : | @inputs = (\%inputs); | ||
| 162 : | last CASES; | ||
| 163 : | }; | ||
| 164 : | $func =~/^(p|Tr|td|li|table|div|th)$/ && do { # concatenate inputs | ||
| 165 : | my %inputs=(); | ||
| 166 : | gage | 4256 | #print "previous inputs @inputs"; |
| 167 : | gage | 4254 | if (ref($inputs[0]) =~/HASH/ ) { # |
| 168 : | gage | 4256 | my $attributes = shift @inputs; |
| 169 : | if (ref($inputs[0]) =~/ARRAY/) { | ||
| 170 : | my @values = @{$inputs[0]}; | ||
| 171 : | foreach my $attribute (keys %{$attributes} ){ | ||
| 172 : | $inputs{$attribute} = [ map { $attributes->{$attribute} } @values ]; | ||
| 173 : | #print "$attribute is ", @{$inputs{$attribute}},"\n"; | ||
| 174 : | } | ||
| 175 : | @inputs = (%inputs,text=>\@values); | ||
| 176 : | $func = $func."_group"; | ||
| 177 : | } else { | ||
| 178 : | #print "\nfirst inputs @inputs\n"; | ||
| 179 : | my $text = join(" ",@inputs); | ||
| 180 : | @inputs = ($attributes, $text); | ||
| 181 : | #print "\ninputs @inputs\n"; | ||
| 182 : | } | ||
| 183 : | } elsif (ref($inputs[0]) =~/ARRAY/ ) { | ||
| 184 : | # do nothing | ||
| 185 : | warn "inputs which start with an array reference should have only on element" if @inputs >1; | ||
| 186 : | gage | 4254 | } else { |
| 187 : | @inputs = (join("",@inputs)); | ||
| 188 : | gage | 4256 | } |
| 189 : | gage | 4254 | last CASES; |
| 190 : | }; | ||
| 191 : | $func =~ /^hidden/ && do { # handles name value pairs | ||
| 192 : | my $type = $func; | ||
| 193 : | $func ='input'; | ||
| 194 : | my %inputs=(); | ||
| 195 : | if (ref($inputs[0]) =~/HASH/ ) { # | ||
| 196 : | %inputs = %{$inputs[0]}; | ||
| 197 : | } else { | ||
| 198 : | $inputs{name} = $inputs[0]; | ||
| 199 : | $inputs{value} = $inputs[1]; | ||
| 200 : | } | ||
| 201 : | $inputs{-type} = $type; | ||
| 202 : | my $default_label = normalizeName('defaults?',keys %inputs); | ||
| 203 : | $inputs{-text} = $inputs{$default_label} if defined $default_label; | ||
| 204 : | %inputs = %{removeParam($default_label, \%inputs)}; | ||
| 205 : | @inputs = (\%inputs ); | ||
| 206 : | }; | ||
| 207 : | |||
| 208 : | $func =~/^radio_group$/ &&do { | ||
| 209 : | my $type = $func; | ||
| 210 : | $func ='input_group', | ||
| 211 : | my %inputs=(); | ||
| 212 : | if (ref($inputs[0]) =~/HASH/ ) { # | ||
| 213 : | %inputs = %{$inputs[0]}; | ||
| 214 : | } else { | ||
| 215 : | warn "probable error $func @inputs "; | ||
| 216 : | } | ||
| 217 : | $inputs{type}=$type; | ||
| 218 : | %inputs = %{removeParam('override',\%inputs)}; | ||
| 219 : | my $labels_key = normalizeName('labels?',keys %inputs); | ||
| 220 : | my $values_key = normalizeName('values?',keys %inputs); | ||
| 221 : | my $name_key = normalizeName('name', keys %inputs); | ||
| 222 : | my $default_key = normalizeName('defaults?', keys %inputs); | ||
| 223 : | my $linebreak_key = normalizeName('linebreaks?', keys %inputs); | ||
| 224 : | my $ra_value = $inputs{$values_key}; | ||
| 225 : | my $rh_labels = $inputs{labels_key}; | ||
| 226 : | my @values = @{$inputs{$values_key}}; | ||
| 227 : | my $ret = (defined($linebreak_key) and defined($inputs{$linebreak_key}) and $inputs{$linebreak_key} )?"<br>":''; | ||
| 228 : | # deal with the default option | ||
| 229 : | my $selected_button = ''; | ||
| 230 : | my $text = ''; | ||
| 231 : | my $selected_value = $values[0]; | ||
| 232 : | if (defined($default_key) and $default_key and defined($inputs{$default_key})) { | ||
| 233 : | # grab the selected options | ||
| 234 : | $selected_value = $inputs{$default_key}; | ||
| 235 : | } | ||
| 236 : | ## match labels to values | ||
| 237 : | my @text=(); | ||
| 238 : | if (defined($labels_key) and $labels_key) { | ||
| 239 : | my %labels= %{$inputs{$labels_key}}; | ||
| 240 : | delete($inputs{$labels_key}) if exists $inputs{$labels_key}; | ||
| 241 : | @text = map {( exists($labels{$_}) )? $labels{$_}.$ret: $_.$ret } @values; | ||
| 242 : | } else { # no labels | ||
| 243 : | @text = map {$_ .$ret} @values; | ||
| 244 : | } | ||
| 245 : | my @checked = map { $selected_value eq $_ } @values; | ||
| 246 : | $inputs{-text} = \@text; | ||
| 247 : | %inputs = %{removeParam($linebreak_key,\%inputs)}; | ||
| 248 : | %inputs = %{removeParam($default_key,\%inputs)}; | ||
| 249 : | |||
| 250 : | @inputs = (\%inputs ); | ||
| 251 : | last CASES; | ||
| 252 : | }; | ||
| 253 : | $func =~/^(popup_menu|scrolling_list)$/ &&do{ | ||
| 254 : | my %inputs=(); | ||
| 255 : | if (ref($inputs[0]) =~/HASH/ ) { # | ||
| 256 : | %inputs = %{$inputs[0]}; | ||
| 257 : | } else { | ||
| 258 : | warn "probable error $func @inputs "; | ||
| 259 : | } | ||
| 260 : | %inputs = %{removeParam('override',\%inputs)}; | ||
| 261 : | my $values_key = normalizeName('values?',keys %inputs); #get keys | ||
| 262 : | my $labels_key = normalizeName('labels?',keys %inputs); | ||
| 263 : | my $default_key = normalizeName('defaults?', keys %inputs); | ||
| 264 : | my $ra_value = $inputs{$values_key}; | ||
| 265 : | my $rh_labels = $inputs{labels_key}; | ||
| 266 : | my @values = eval{ @{$inputs{$values_key}} }; | ||
| 267 : | @values = grep {defined($_) and $_} @values; | ||
| 268 : | warn "error in $values_key $inputs{$values_key}",join(' ', @inputs), caller(), $@ if $@; | ||
| 269 : | |||
| 270 : | # deal with the default option | ||
| 271 : | |||
| 272 : | my $selected_option = ''; | ||
| 273 : | my $text = ''; | ||
| 274 : | my %selected_values = ($values[0] => 1); # select the first value by default | ||
| 275 : | if (defined($default_key) and $default_key and defined($inputs{$default_key}) and $inputs{$default_key}) { | ||
| 276 : | # grab the selected options | ||
| 277 : | if (ref($inputs{$default_key})=~/ARRAY/ ) { | ||
| 278 : | %selected_values = map {$_ => 1 } @{$inputs{$default_key}}; | ||
| 279 : | } elsif ($inputs{$default_key}) { | ||
| 280 : | %selected_values = ($inputs{$default_key} => 1); | ||
| 281 : | } | ||
| 282 : | } | ||
| 283 : | my @selected = map {(exists($selected_values{$_}) )?1 : 0 } @values; | ||
| 284 : | %inputs = %{removeParam($default_key,\%inputs)}; | ||
| 285 : | ## match labels to values | ||
| 286 : | my @text=(); | ||
| 287 : | if (defined($labels_key) and $labels_key) { | ||
| 288 : | my %labels= %{$inputs{$labels_key}}; | ||
| 289 : | delete($inputs{$labels_key}) if exists $inputs{$labels_key}; | ||
| 290 : | @text = map {( exists($labels{$_}) )? $labels{$_}: $_ } @values; | ||
| 291 : | } else { # no labels | ||
| 292 : | @text = @values; | ||
| 293 : | } | ||
| 294 : | %inputs = %{removeParam($labels_key,\%inputs)}; | ||
| 295 : | %inputs = %{removeParam($values_key,\%inputs)}; | ||
| 296 : | # end match labels to values | ||
| 297 : | $prolog = $html2->select_start(\%inputs)."\n".$selected_option; | ||
| 298 : | $postlog = $html2->select_end(); | ||
| 299 : | $func = 'option_group'; | ||
| 300 : | @inputs =({-value=>\@values, -text=>\@text, -selected =>\@selected }); | ||
| 301 : | last CASES; | ||
| 302 : | }; | ||
| 303 : | } # end CASES block | ||
| 304 : | |||
| 305 : | gage | 4256 | # print "\n\nto EasyTags $func @inputs "; |
| 306 : | gage | 4254 | $result = eval { $html2->$func(@inputs) }; |
| 307 : | gage | 4212 | #$result = eval { use WeBWorK::CGI; $html2->$func(@_) }; |
| 308 : | #handle special cases | ||
| 309 : | gage | 4214 | if ( $prolog or $postlog ) { |
| 310 : | gage | 4254 | $result =~ s/\n/\n /g; |
| 311 : | $result =~s/^\n//; # get rid of extra return?? | ||
| 312 : | gage | 4214 | $result = "$prolog$result$postlog" ; |
| 313 : | gage | 4212 | } |
| 314 : | return $result; | ||
| 315 : | } | ||
| 316 : | gage | 4214 | sub normalizeName { |
| 317 : | my $name = shift; #name to find | ||
| 318 : | my @inputs = @_; #inputs | ||
| 319 : | gage | 4226 | my ($key) = grep /^-?$name$/, @inputs; |
| 320 : | gage | 4214 | return $key; |
| 321 : | } | ||
| 322 : | gage | 4212 | |
| 323 : | gage | 4214 | # possible utility subroutines. |
| 324 : | sub removeParam { | ||
| 325 : | my $name = shift; | ||
| 326 : | my $rh_inputs = shift; | ||
| 327 : | delete($rh_inputs->{$name}) if defined $name and exists $rh_inputs->{$name}; | ||
| 328 : | delete($rh_inputs->{-$name}) if defined $name and exists $rh_inputs->{-$name}; | ||
| 329 : | $rh_inputs; | ||
| 330 : | } | ||
| 331 : | gage | 4220 | sub labelsToText { #takes labels attached to values and distributes them into a text variable |
| 332 : | my $rh_labels = shift; | ||
| 333 : | my $rh_values = shift; | ||
| 334 : | } | ||
| 335 : | gage | 4212 | 1; |
| aubreyja at gmail dot com | ViewVC Help |
| Powered by ViewVC 1.0.9 |