added sol100 and chado cvterm pages to validate_all.t
[sgn.git] / lib / CXGN / Page / Form / ConfirmStore.pm
blob842b941143dd9244b77ad9de571797d3aff75d25
2 =head1 NAME
4 CXGN::Page::Form::ConfirmStore.pm -- classes to deal with user-modifiable web forms
6 =head1 DESCRIPTION
8 This is a subclass of L<CXGN::Page::Form::Static>, and overrides the functions therein to generate editable components in the form. For more information, see L<CXGN::Page::Form::Static>.
10 =head1 AUTHOR(S)
12 Lukas Mueller (lam87@cornell.edu)
14 =cut
16 use strict;
17 use CXGN::Page;
18 use CXGN::Page::Form::Static;
19 use CXGN::Page::Form::Editable;
21 package CXGN::Page::Form::ConfirmStore;
23 use base qw / CXGN::Page::Form::Editable /;
25 sub new {
26 my $class = shift;
27 my $self = $class->SUPER::new(@_);
28 $self->set_static_form(CXGN::Page::Form::Static->new()) ;
29 $self->set_reset_button_text("Reset");
30 $self->set_submit_button_text("Store");
31 $self->set_submit_method(); #use hardcoded default
32 return $self;
35 =head2 is_editable
37 Whether the form can be filled in.
39 =cut
41 sub is_editable {
42 return 0;
48 =head2 store
50 Usage:
51 Desc:
52 Ret:
53 Args:
54 Side Effects:
55 Example:
57 =cut
59 sub store {
60 my $self = shift;
61 my %args = @_;
63 #print STDERR "args hash before in the store function: \n";
64 # while (my ($key, $value) = each(%args)) { print STDERR "$key $value\n"; }
65 #print STDERR "STORING FORM DATA...\n";
67 $self->propagate_input(%args);
71 # commit the changes to the database using the store method on
72 # each object
74 #print STDERR "STORING OBJECT...\n";
76 my %distinct_objects = $self->get_distinct_objects();
79 # check the uniqueness constraints of the object
81 foreach my $k (keys(%distinct_objects)) {
82 my $obj = $distinct_objects{$k};
83 if ($obj->can("exists_in_database")) {
84 my $message = $obj->exists_in_database();
85 if ($message) {
86 my $text = "This object (" . ref($obj) . ") already seems to exist in the database and violates constraints. Please correct your input.";
87 $message = "" unless ($message =~ /[a-zA-Z]/);
88 CXGN::Page->new()->message_page($text, $message);
92 foreach my $k (keys %distinct_objects) {
93 #print STDERR " DUMP:" .Data::Dumper::Dumper($distinct_objects{$k});
94 #print STDERR " STORING OBJECT: ".ref($distinct_objects{$k})."\n";
96 my $id = $distinct_objects{$k}->store();
97 $self->set_insert_id($k, $id);
101 =head2 add_field
103 Usage:
104 Desc:
105 Ret:
106 Args: a hash with the following keys:
107 screen_name
108 field_name
109 contents
110 length
111 object
112 getter
113 setter
114 set_if
115 validate
116 required fields: field_name.
117 Side Effects:
118 Example:
120 =cut
122 sub add_field {
124 my $self = shift;
125 my %args = @_;
126 $self->get_static_form->add_field(%args);
127 my $field = CXGN::Page::Form::Hidden->new(%args);
128 if(exists $args{validate})
130 $field->set_validate($args{validate});
132 if (!exists($self->{fields})) { $self->{fields}=(); }
133 push @{$self->{fields}}, $field;
138 =head2 function add_textarea
140 Synopsis:
141 Arguments: a hash with the following keys:
142 screen_name
143 field_name
144 contents
145 length
146 object
147 getter
148 setter
149 set_if
150 validate
151 required fields: field_name.
152 Returns:
153 Side effects:
154 Description:
156 =cut
158 sub add_textarea {
159 my $self = shift;
160 my %args = @_;
162 $self->get_static_form->add_textarea(%args);
163 my $field = CXGN::Page::Form::Hidden->new(%args);
165 if(exists $args{validate})
167 $field->set_validate($args{validate});
169 $self->add_field_obj($field);
172 =head2 add_select
174 Usage:
175 Desc:
176 Ret:
177 Args: display_name
178 field_name
179 contents
180 length
181 object
182 getter
183 setter
184 set_if
185 validate
186 select_list_ref
187 select_id_list_ref
189 Side Effects:
190 Example:
192 =cut
194 sub add_select {
195 my $self = shift;
196 my %args = @_;
198 #foreach my $k (keys %args) { print "Args to add_select $k, $args{$k}\n<br />"; }
199 $self->get_static_form->add_select(%args);
200 my $select = CXGN::Page::Form::Hidden->new(%args);
202 if(exists $args{validate})
204 $select->set_validate($args{validate});
206 if (!exists($self->{fields})) { $self->{fields}=(); }
207 push @{$self->{fields}}, $select;
210 =head2 add_hidden
212 Usage:
213 Desc:
214 Ret:
215 Args:
216 Side Effects:
217 Example:
219 =cut
221 sub add_hidden {
222 my $self = shift;
223 my %args = @_;
224 $self->get_static_form->add_hidden(%args);
225 my $hidden = CXGN::Page::Form::EditableHidden->new(%args);
227 if(exists $args{validate})
229 $hidden->set_validate($args{validate});
231 $self->add_field_obj($hidden);
234 =head2 add_checkbox
236 Usage:
237 Desc:
238 Ret:
239 Args:
240 Side Effects:
241 Example:
243 =cut
245 sub add_checkbox
247 my $self = shift;
248 my %args = @_;
250 $self->get_static_form->add_checkbox(%args);
251 my $checkbox = CXGN::Page::Form::Hidden->new(%args);
253 $self->add_field_obj($checkbox);
256 =head2 add_radio_list
258 Usage:
259 Desc:
260 Ret:
261 Args:
262 Side Effects:
263 Example:
265 =cut
267 sub add_radio_list
269 my $self = shift;
270 my %args = @_;
272 $self->get_static_form->add_radio_list(%args);
273 my $radio = CXGN::Page::Form::Hidden->new(%args);
275 $self->add_field_obj($radio);
278 =head2 add_multiselect
280 Usage:
281 Desc:
282 Ret:
283 Args:
284 Side Effects:
285 Example:
287 =cut
289 sub add_multiselect
291 my $self = shift;
292 my %args = @_;
294 $self->get_static_form->add_multiselect(%args);
295 my $radio = CXGN::Page::Form::Hidden->new(%args);
297 $self->add_field_obj($radio);
300 sub get_form_start {
301 my $self = shift;
302 return "<form method=\"" . $self->get_submit_method() . "\" action=\"\">"; #must have action parameter for xhtml 1.0+ -- Evan, 1/7/07
305 sub get_form_end {
306 my $self = shift;
307 return "<input type=\"submit\" value=\"" . $self->get_submit_button_text() . "\" />
309 </form>";
312 =head2 get_submit_method
314 'get' or 'post'
316 =cut
318 sub get_submit_method
320 my $self = shift;
321 return $self->{form_submit_method};
324 =head2 set_submit_method
326 'get' or 'post' (case does not matter)
328 default is 'get' if no or invalid argument
330 =cut
332 sub set_submit_method
334 my ($self, $method) = @_;
335 $method = 'get' if(!defined($method) or $method !~ /^get|post$/i);
336 $self->{form_submit_method} = lc($method); #lowercase
339 sub get_reset_button_text
341 my $self = shift;
342 return $self->{reset_button_text};
345 =head2 set_reset_button_text
347 Args: new button text (default is 'reset form')
349 [1 / 9 / 07] Not currently possible to remove the reset button.
351 =cut
353 sub set_reset_button_text
355 my ($self, $text) = @_;
356 $self->{reset_button_text} = $text;
359 sub get_submit_button_text
361 my $self = shift;
362 return $self->{submit_button_text};
365 =head2 set_submit_button_text
367 Args: new button text (default is 'store')
369 =cut
371 sub set_submit_button_text
373 my ($self, $text) = @_;
374 $self->{submit_button_text} = $text;
377 =head2 as_table_string
379 Usage:
380 Desc:
381 Ret:
382 Args:
383 Note: as table does not call validate itself to give some
384 more control on the appearance (you don't wan't the
385 new input field to appear with error messages).
386 Side Effects:
387 Example:
389 =cut
391 sub as_table_string {
392 my $self = shift;
393 my $string = "";
396 # the static form
397 my $static= $self->get_static_form();
399 $string .= $static->as_table_string();
401 ### the confirmStore form:
402 my %error_hash = $self->get_error_hash();
403 my %html = $self->get_field_hash();
404 my $has_required_field=0;
406 $string .= $self->get_form_start();
407 $string .= qq { <table> };
408 foreach my $f ($self->get_fields()) {
409 my $error = "";
410 if (exists($error_hash{$f->get_field_name()})) {
411 $error=$self->get_error_message($error_hash{$f->get_field_name()})."<br />";
414 my $required_field = "";
415 if ($f->get_validate()) {
416 $required_field = qq { <font color="red">*</font> };
417 $has_required_field=1;
420 # print everything except the hidden fields
422 if (ref($f)!~/hidden/i) {
423 $string .= "<tr><td>$error".($f->get_display_name())."$required_field</td><td width=\"20\">&nbsp;</td><td>".($html{$f->get_field_name()})."</td></tr>\n";
428 $string .= qq { <tr><td colspan="3" align="center"> };
430 # print the hidden fields
432 foreach my $f ($self->get_fields()) {
433 if (ref($f)=~/hidden/i) {
434 $string .= $html{$f->get_field_name()};
437 if ($has_required_field) {
438 $string .= qq { (<font color="red">*</font> denotes required field.)<br /><br /> };
440 $string .= qq { </td></tr></table> };
441 $string .= $self->get_form_end();
443 $string .= $static->get_form_end();
446 return $string;
449 =head2 get_static_form
451 Usage:
452 Desc:
453 Ret:
454 Args:
455 Side Effects:
456 Example:
458 =cut
460 sub get_static_form {
461 my $self=shift;
462 return $self->{static_form};
466 =head2 set_static_form
468 Usage:
469 Desc:
470 Ret:
471 Args:
472 Side Effects:
473 Example:
475 =cut
477 sub set_static_form {
478 my $self=shift;
479 $self->{static_form}=shift;