added sol100 and chado cvterm pages to validate_all.t
[sgn.git] / lib / CXGN / Page / Form / MultiSelect.pm
blob949ab4143cf810e2aa9e04509b721f2e5487f012
1 =head1 NAME
3 CXGN::Page::Form::MultiSelect;
5 =head1 DESCRIPTION
7 Implements a static (non-editable) multiple-selection list on a static form. For more information, see L<CXGN::Page::Form>.
9 Currently a MultiSelect displays as a multiple-selection drop-down menu. It could also be a series of checkboxes with the same field name;
10 implement that yourself if you need it.
12 =head1 AUTHOR(S)
14 Evan Herbst
16 =cut
18 use strict;
19 use CXGN::Page::Form::ElementI;
21 package CXGN::Page::Form::MultiSelect;
23 use base qw/ CXGN::Page::Form::ElementI /;
25 =head2 new
27 Usage:
28 Desc:
29 Ret:
30 Args: a hashref with the following keys:
31 display_name (name of the field for display purposes)
32 field_name (name of the variable)
33 choices (an arrayref of IDs for the strings to be shown)
34 labels (an arrayref of strings to be shown)
35 contents (an arrayref of values that will be evaluated as Booleans to get the initial selection)
36 object (the object this field maps to)
37 getter (the getter function for this field in the object)
38 setter (the setter function for this field in the object)
39 Side Effects:
40 Example:
42 =cut
44 sub new
46 my $class = shift;
47 my %params = @_;
48 my $self = $class->SUPER::new(%params);
49 #store the parameters that aren't handled by our superclass
50 foreach my $param (qw(choices labels))
52 my $set_func = "set_$param";
53 $self->$set_func($params{$param});
55 return $self;
58 =head2 render
60 Usage:
61 Desc:
62 Ret:
63 Args:
64 Side Effects:
65 Example:
67 =cut
69 sub render
71 my $self = shift;
72 #render as a comma-separated list of labels
73 my @selected_labels = ();
74 for(my $i = 0; $i < scalar($self->get_choice_array()); $i++)
76 if($self->is_selected($i))
78 push @selected_labels, $self->get_label($i);
81 return join(", ", @selected_labels);
84 =head2 set_from_external
86 Args: string with all values for this field separated by \0
88 =cut
90 sub set_from_external
92 my ($self, $value_string) = @_;
93 my %values = map {$_ => 1} split(/\0/, $value_string); #map only values that appear to 1
94 my @choices = $self->get_choice_array();
95 for(my $i = 0; $i < scalar(@choices); $i++)
97 if($values{$choices[$i]})
99 $self->set_selected($i, 1);
104 =head2 get_choice
106 Args: choice index
108 =cut
110 sub get_choice
112 my ($self, $i) = @_;
113 return $self->{choices}->[$i];
116 =head2 get_choice_array
118 Ret: array (not arrayref) of choices
120 =cut
122 sub get_choice_array
124 my $self = shift;
125 return @{$self->{choices}};
128 =head2 set_choices
130 Args: arrayref of choices
132 =cut
134 sub set_choices
136 my ($self, $choices) = @_;
137 $self->{choices} = $choices;
140 =head2 get_label
142 Args: label index
144 =cut
146 sub get_label
148 my ($self, $i) = @_;
149 return $self->{labels}->[$i];
152 =head2 set_labels
154 Args: arrayref of labels
156 =cut
158 sub set_labels
160 my ($self, $labels) = @_;
161 $self->{labels} = $labels;
164 =head2 is_selected
166 Args: index to check
168 =cut
170 sub is_selected
172 my ($self, $i) = @_;
173 return $self->{contents}->[$i];
176 =head2 set_selected
178 Args: index, an expression that will be evaluated in Boolean context
180 =cut
182 sub set_selected
184 my ($self, $i, $val) = @_;
185 $self->{contents}->[$i] = ($val ? 1 : 0);
188 return 1;