| 1 | ############################################################################### | ||
| 2 | # XML::Template::Element::Form | ||
| 3 | # | ||
| 4 | # Copyright (c) 2002-2003 Jonathan A. Waxman <jowaxman@law.upenn.edu> | ||
| 5 | # All rights reserved. | ||
| 6 | # | ||
| 7 | # This program is free software; you can redistribute it and/or | ||
| 8 | # modify it under the same terms as Perl itself. | ||
| 9 | ############################################################################### | ||
| 10 | package XML::Template::Element::Form; | ||
| 11 | use base qw(XML::Template::Element); | ||
| 12 | |||
| 13 | use strict; | ||
| 14 | use CGI; | ||
| 15 | use File::Spec; | ||
| 16 | use XML::Template::Element; | ||
| 17 | |||
| 18 | |||
| 19 | =pod | ||
| 20 | |||
| 21 | =head1 NAME | ||
| 22 | |||
| 23 | XML::Template::Element::Form - XML::Template plugin module for the | ||
| 24 | form namespace. | ||
| 25 | |||
| 26 | =head1 SYNOPSIS | ||
| 27 | |||
| 28 | This XML::Template plugin module implements the form namespace tagset. | ||
| 29 | The block namespace includes tags that simplify the creation of HTML | ||
| 30 | forms. | ||
| 31 | |||
| 32 | =head1 CONSTRUCTOR | ||
| 33 | |||
| 34 | XML::Template::Element::Form inherits its constructor method, C<new>, | ||
| 35 | from L<XML::Template::Element>. | ||
| 36 | |||
| 37 | =head1 FORM TAGSET METHODS | ||
| 38 | |||
| 39 | =head2 select | ||
| 40 | |||
| 41 | This method implements the beginning of an HTML select form element. The | ||
| 42 | following attributes are used: | ||
| 43 | |||
| 44 | =over 4 | ||
| 45 | |||
| 46 | =item name | ||
| 47 | |||
| 48 | The name of the select element. | ||
| 49 | |||
| 50 | =item default | ||
| 51 | |||
| 52 | The value of the option child to select by default. | ||
| 53 | |||
| 54 | =back | ||
| 55 | |||
| 56 | =cut | ||
| 57 | |||
| 58 | sub select { | ||
| 59 | my $self = shift; | ||
| 60 | my ($code, $attribs) = @_; | ||
| 61 | |||
| 62 | my $name = $self->get_attrib ($attribs, 'name') || 'undef'; | ||
| 63 | my $default = $self->get_attrib ($attribs, 'default') || 'undef'; | ||
| 64 | |||
| 65 | my $outcode = qq! | ||
| 66 | do { | ||
| 67 | \$vars->create_context (); | ||
| 68 | |||
| 69 | print "<select name=\\"" . $name . "\\">\n"; | ||
| 70 | |||
| 71 | my \$__default = $default; | ||
| 72 | $code | ||
| 73 | |||
| 74 | print "</select>\n"; | ||
| 75 | |||
| 76 | \$vars->delete_context (); | ||
| 77 | }; | ||
| 78 | !; | ||
| 79 | |||
| 80 | return $outcode; | ||
| 81 | } | ||
| 82 | |||
| 83 | =pod | ||
| 84 | |||
| 85 | =head2 option | ||
| 86 | |||
| 87 | This method implements the option section of an HTML select form element. | ||
| 88 | The label of the option is contained in the content of this element The | ||
| 89 | following attributes are used: | ||
| 90 | |||
| 91 | =over 4 | ||
| 92 | |||
| 93 | =item value | ||
| 94 | |||
| 95 | The value of the option. | ||
| 96 | |||
| 97 | =back | ||
| 98 | |||
| 99 | =cut | ||
| 100 | |||
| 101 | sub option { | ||
| 102 | my $self = shift; | ||
| 103 | my ($code, $attribs) = @_; | ||
| 104 | |||
| 105 | my $value = $self->get_attrib ($attribs, 'value') || 'undef'; | ||
| 106 | |||
| 107 | return '' if $value eq 'undef'; | ||
| 108 | |||
| 109 | my $outcode = qq! | ||
| 110 | do { | ||
| 111 | \$vars->create_context (); | ||
| 112 | |||
| 113 | print "<option value=\\"" . $value . "\\""; | ||
| 114 | print " selected" if $value eq \$__default; | ||
| 115 | print ">"; | ||
| 116 | $code | ||
| 117 | print "</option>\n"; | ||
| 118 | |||
| 119 | \$vars->delete_context (); | ||
| 120 | }; | ||
| 121 | !; | ||
| 122 | |||
| 123 | return $outcode; | ||
| 124 | } | ||
| 125 | |||
| 126 | =pod | ||
| 127 | |||
| 128 | =head2 upload | ||
| 129 | |||
| 130 | This method implements the upload tag, which is used to handle files | ||
| 131 | uploaded via CGI. The following attributes are used: | ||
| 132 | |||
| 133 | =over 4 | ||
| 134 | |||
| 135 | =item name | ||
| 136 | |||
| 137 | The name of the upload form element used to upload the file. | ||
| 138 | |||
| 139 | =item dest | ||
| 140 | |||
| 141 | The file spec to which the uploaded file will be saved. | ||
| 142 | |||
| 143 | =back | ||
| 144 | |||
| 145 | =cut | ||
| 146 | |||
| 147 | sub upload { | ||
| 148 | my $self = shift; | ||
| 149 | my ($code, $attribs) = @_; | ||
| 150 | |||
| 151 | my $name = $self->get_attrib ($attribs, 'name') || 'undef'; | ||
| 152 | my $dest = $self->get_attrib ($attribs, 'dest') || 'undef'; | ||
| 153 | |||
| 154 | my $outcode = qq! | ||
| 155 | do { | ||
| 156 | \$vars->create_context (); | ||
| 157 | |||
| 158 | my \$cgi = CGI->new (); | ||
| 159 | my \$fh = \$cgi->param ($name); | ||
| 160 | open (OUTFILE, ">" . $dest) | ||
| 161 | || die XML::Template::Exception->new ('Upload', \$\!); | ||
| 162 | my \$buffer = ''; | ||
| 163 | while (my \$bytesread = read (\$fh, \$buffer, 1024)) { | ||
| 164 | print OUTFILE \$buffer; | ||
| 165 | } | ||
| 166 | close OUTFILE; | ||
| 167 | |||
| 168 | \$vars->delete_context (); | ||
| 169 | }; | ||
| 170 | !; | ||
| 171 | |||
| 172 | return $outcode; | ||
| 173 | } | ||
| 174 | |||
| 175 | =pod | ||
| 176 | |||
| 177 | =head1 AUTHOR | ||
| 178 | |||
| 179 | Jonathan Waxman | ||
| 180 | <jowaxman@bbl.med.upenn.edu> | ||
| 181 | |||
| 182 | =head1 COPYRIGHT | ||
| 183 | |||
| 184 | Copyright (c) 2002-2003 Jonathan A. Waxman | ||
| 185 | All rights reserved. | ||
| 186 | |||
| 187 | This program is free software; you can redistribute it and/or | ||
| 188 | modify it under the same terms as Perl itself. | ||
| 189 | |||
| 190 | =cut | ||
| 191 | |||
| 192 | |||
| 193 | 1; |