| 1 | use warnings; | ||
| 2 | use strict; | ||
| 3 | |||
| 4 | package Jifty::Web::Form::Field::Checkboxes; | ||
| 5 | |||
| 6 | use base qw/Jifty::Web::Form::Field/; | ||
| 7 | |||
| 8 | =head1 NAME | ||
| 9 | |||
| 10 | Jifty::Web::Form::Field::Checkboxes - Add a list of checkboxes for | ||
| 11 | multiple-choice selection | ||
| 12 | |||
| 13 | =head1 METHODS | ||
| 14 | |||
| 15 | =head2 render_widget | ||
| 16 | |||
| 17 | Renders the whole checkbox collection. | ||
| 18 | |||
| 19 | =cut | ||
| 20 | |||
| 21 | sub render_widget { | ||
| 22 | my $self = shift; | ||
| 23 | $self->_render_checkboxes(0); | ||
| 24 | } | ||
| 25 | |||
| 26 | |||
| 27 | =head2 render_value | ||
| 28 | |||
| 29 | Renders the whole checkbox collection in disabled mode. | ||
| 30 | |||
| 31 | =cut | ||
| 32 | |||
| 33 | sub render_value { | ||
| 34 | my $self = shift; | ||
| 35 | $self->_render_checkboxes(1); | ||
| 36 | } | ||
| 37 | |||
| 38 | |||
| 39 | |||
| 40 | sub _render_checkboxes { | ||
| 41 | my $self = shift; | ||
| 42 | my $readonly = shift; | ||
| 43 | |||
| 44 | my %checked; | ||
| 45 | |||
| 46 | my $current_value = $self->current_value; | ||
| 47 | if( defined($current_value) ) { | ||
| 48 | if( ref($current_value) eq 'ARRAY' ) { | ||
| 49 | for my $value (@$current_value) { | ||
| 50 | if( ref($value) eq 'HASH' ) { | ||
| 51 | $value = $value->{'value'}; | ||
| 52 | } | ||
| 53 | $checked{$value} = 1; | ||
| 54 | } | ||
| 55 | } | ||
| 56 | } | ||
| 57 | |||
| 58 | Jifty->web->out('<ul class="checkboxlist">'); | ||
| 59 | |||
| 60 | for my $opt ($self->available_values) { | ||
| 61 | |||
| 62 | my $display = ref($opt) ? $opt->{'display'} : $opt; | ||
| 63 | my $value = ref($opt) ? $opt->{'value'} : $opt; | ||
| 64 | $value = '' if !defined($value); | ||
| 65 | |||
| 66 | my $id = $self->element_id . "-" . $value; | ||
| 67 | $id =~ s/\s+/_/; | ||
| 68 | my $field = qq! <li class="checkboxlistitem"> !; | ||
| 69 | $field .= qq! <input type="checkbox" !; | ||
| 70 | $field .= qq! name="@{[ $self->input_name ]}"!; | ||
| 71 | $field .= qq! id="@{[ $id ]}"!; | ||
| 72 | $field .= qq! title="@{[ $self->title ]}"! if ($self->title); | ||
| 73 | $field .= qq! value="@{[ $value ]}"!; | ||
| 74 | $field .= $self->_widget_class; | ||
| 75 | |||
| 76 | $field .= qq! checked="checked"! if $checked{$value}; | ||
| 77 | $field .= qq! disabled="disabled" readonly="readonly"! if $readonly; | ||
| 78 | |||
| 79 | $field .= $self->javascript; | ||
| 80 | $field .= qq! /><label for="@{[ $id ]}"!; | ||
| 81 | $field .= $self->_widget_class; | ||
| 82 | $field .= qq!>$display</label></li>\n!; | ||
| 83 | |||
| 84 | Jifty->web->out($field); | ||
| 85 | } | ||
| 86 | Jifty->web->out('</ul>'); | ||
| 87 | } | ||
| 88 | |||
| 89 | |||
| 90 | |||
| 91 | |||
| 92 | 1; |