From: biopete Date: Tue, 11 Apr 2006 00:28:59 +0000 (+0000) Subject: Added _options_from_hash so you can say: X-Git-Tag: 2.11~47 X-Git-Url: https://git.decadent.org.uk/gitweb/?a=commitdiff_plain;h=883d0367cf16313f19a9747df2e1d2e16ea75fb5;p=maypole.git Added _options_from_hash so you can say: $beer->to_field('brewery', 'select', {items => { 'Option Content1 ' => 'Opt Val', ... }); git-svn-id: http://svn.maypole.perl.org/Maypole/trunk@473 48953598-375a-da11-a14b-00016c27c3ee --- diff --git a/lib/Maypole/Model/CDBI/AsForm.pm b/lib/Maypole/Model/CDBI/AsForm.pm index ef67a06..59bf438 100644 --- a/lib/Maypole/Model/CDBI/AsForm.pm +++ b/lib/Maypole/Model/CDBI/AsForm.pm @@ -32,7 +32,7 @@ our @EXPORT = _to_foreign_inputs _to_enum_select _to_bool_select _to_hidden _to_link_hidden _rename_foreign_input _to_readonly _options_from_objects _options_from_arrays _options_from_hashes - _options_from_scalars _to_select_or_create + _options_from_array _options_from_hash _to_select_or_create ); our @EXPORTOK = @@ -653,7 +653,7 @@ sub _to_select { my ($self, $col, $args) = @_; $args ||= {}; # Do we have items already ? Go no further. - if ($args->{items} and @{$args->{items}}) { + if ($args->{items} and ref $args->{items}) { my $a = $self->_select_guts($col, $args); $OLD_STYLE && return $a->as_HTML; if ($args->{multiple}) { $a->attr('multiple', 'multiple');} @@ -1083,9 +1083,12 @@ sub _hash_selected { Internal api method to make the actual select box form elements. 3 types of lists making for -- + Hash, Array, Array of CDBI objects. Array of scalars , - Array or Array refs with cols from class. + Array or Array refs with cols from class, + Array of hashes + =cut @@ -1106,29 +1109,39 @@ sub _select_guts { $a->push_content($null_element); } - my $items = $args->{items}; - my $proto = $items->[0]; - my $type = ref $proto || ''; - - # Objects - if (not $type) { - $a->push_content($self->_options_from_scalars($items, $args)); - } - elsif($type !~ /ARRAY|HASH/i) { - # make select of objects - $a->push_content($self->_options_from_objects($items, $args)); - } - elsif ($type =~ /ARRAY/i) { - $a->push_content($self->_options_from_arrays($items, $args)); + my $items = $args->{items}; + my $type = ref $items; + my $proto = eval { ref $items->[0]; } || ""; + warn "Type is $type, proto is $proto\n"; + # Single Hash + if ($type eq 'HASH') { + $a->push_content($self->_options_from_hash($items, $args)); + } + # Single Array + elsif ( $type eq 'ARRAY' and not ref $items->[0] ) { + $a->push_content($self->_options_from_array($items, $args)); + } + # Array of Objects + elsif( $type eq 'ARRAY' and $proto !~ /ARRAY|HASH/i ) { + # make select of objects + $a->push_content($self->_options_from_objects($items, $args)); } - elsif ($type =~ /HASH/i) { - $a->push_content($self->_options_from_hashes($items, $args)); + # Array of Arrays + elsif ( $type eq 'ARRAY' and $proto eq 'ARRAY' ) { + $a->push_content($self->_options_from_arrays($items, $args)); } - else { - die "You passed a weird type of data structure to me. Here it is: $type"; + # Array of Hashes + elsif ( $type eq 'ARRAY' and $proto eq 'HASH' ) { + $a->push_content($self->_options_from_hashes($items, $args)); + } + else { + die "You passed a weird type of data structure to me. Here it is: " . + Dumper($items ); } return $a; + + } @@ -1181,20 +1194,39 @@ sub _options_from_arrays { return @res; } -sub _options_from_scalars { + +sub _options_from_array { my ($self, $items, $args) = @_; - my $selected = $args->{selected} || {}; + my $selected = $args->{selected} || {}; my @res; - for (@$items) { - my $opt = HTML::Element->new("option", value => $_ ); - #$opt->attr(selected => "selected") if $selected =~/^$id$/; - $opt->attr(selected => "selected") if $selected->{$_}; - $opt->push_content( $_ ); - push @res, $opt; + for (@$items) { + my $opt = HTML::Element->new("option", value => $_ ); + #$opt->attr(selected => "selected") if $selected =~/^$id$/; + $opt->attr(selected => "selected") if $selected->{$_}; + $opt->push_content( $_ ); + push @res, $opt; + } + return @res; +} + +sub _options_from_hash { + my ($self, $items, $args) = @_; + my $selected = $args->{selected} || {}; + my @res; + + my @values = values %$items; + # hash Key is the option content and the hash value is option value + for (sort keys %$items) { + my $opt = HTML::Element->new("option", value => $items->{$_} ); + #$opt->attr(selected => "selected") if $selected =~/^$id$/; + $opt->attr(selected => "selected") if $selected->{$items->{$_}}; + $opt->push_content( $_ ); + push @res, $opt; } return @res; } + sub _options_from_hashes { my ($self, $items, $args) = @_; my $selected = $args->{selected} || {};