package II3562149e53818b6637b38680e87ba918::perllist; use strict; use CGI qw/checkbox_group/; use Zen::PerlHandler; sub get { my ($form, $f) = @_; my ($type, $handler, $par, $addon) = split ',', Zen::PerlHandler->getStatList, 4; $addon = qq(class="form-control"); my $data = Zen::PerlHandler->get($handler)->do($form, $f); my (@values, %labels); for (@$data) { push @values, $_->[0]; $labels{$_->[0]} = $_->[1]; } buildList($type, $form->pfx.$f, $form->getDecorated->{$f}, \@values, \%labels, $par, $addon); } sub set { my ($form, $f, $v) = @_; my ($type, $list, $par, $addon) = split ',', Zen::PerlHandler->getStatList, 4; my $object = $form->getDecorated; $object->{$f} = $type =~ /checkbox|multiple/ ? join(',', grep {length} split(',', $v)) : $v; } sub buildList { my ($type, $name, $default, $values, $labels, $par, $addon) = @_; if ($par && $type =~ /select|radio/) { my ($value, $label) = split '=', $par; unshift @$values, $value; $labels->{$value} = $label; } for my $k (keys %$labels) { $labels->{$k} = " $labels->{$k}"; } my $code; if ($type eq 'select') { $addon .= qq( class="custom-select"); my %sel; $sel{$default} = 'selected'; $code = qq(<select name="$name" $addon>); for (@$values) { $code .= qq(<option value="$_" $sel{$_}> $labels->{$_}); delete $sel{$_} if $sel{$_}; } $code .= qq(</select>); } elsif ($type eq 'radio') { my %sel; $sel{$default} = 'checked'; $code = ''; my $idix = 0; my $wrapper = qq(<div class="custom-control custom-radio">); if ($par) { my ($top_v, $top_t) = split '=', $par; $code .= qq($wrapper<input id="rb-$name-$idix" type=radio name="$name" value="$top_v" $sel{$top_v} class="custom-control-input"><label for="rb-$name-$idix" class="custom-control-label">$top_t</label></div>\n); } for (@$values) { $code .= qq($wrapper<input id="rb-$name-$idix" type=radio name="$name" value="$_" $sel{$_} class="custom-control-input"><label for="rb-$name-$idix" class="custom-control-label">$labels->{$_}</label></div>\n); $idix += 1; } } elsif ($type eq 'multiple') { my $size = $par || @$values; my %sel; $addon .= qq( class="custom-select"); $sel{$_} = 'selected' for grep { length $_ } split ',', $default; $code = qq(<input type=hidden name="$name" value="">\n); $code .= qq(<select name="$name" size="$size" multiple $addon>\n); $code .= qq(<option value="$_" $sel{$_}> $labels->{$_}\n) for grep { length $_ } @$values; $code .= qq(</select>\n); } elsif ($type eq 'checkbox') { $code = qq(<input type=hidden name="$name" value="">\n); for (grep { length $_ } @$values) { my $checked = $_ ~~ [split ',', $default] ? 'checked' : ''; $code .= qq( <div class="custom-control custom-checkbox"> <input name="$name" id="checkbox_$_" type="checkbox" class="custom-control-input" value="$_" $checked> <label for="checkbox_$_" class="custom-control-label">$labels->{$_}</label></div> ); } } $code =~ s/&lt;/</g; $code =~ s/&gt;/>/g; $code =~ s/&amp;/&/g; $code =~ s/&quot;/"/g; $code; } 1; 