X-Git-Url: https://git.decadent.org.uk/gitweb/?a=blobdiff_plain;f=lib%2FMaypole%2FModel%2FCDBI%2FAsForm.pm;h=3c35dfc5fa4b23b978cb886986cd528123f1d863;hb=3ed98309a0852fc198f9e4d0e6f70c5510c8282f;hp=39cda98cd80f77a0cbceb0dc41201510d6ee153e;hpb=28823167d12d4cd1419cc6a58900c0fc5819e1af;p=maypole.git diff --git a/lib/Maypole/Model/CDBI/AsForm.pm b/lib/Maypole/Model/CDBI/AsForm.pm index 39cda98..3c35dfc 100644 --- a/lib/Maypole/Model/CDBI/AsForm.pm +++ b/lib/Maypole/Model/CDBI/AsForm.pm @@ -24,32 +24,7 @@ our @EXPORT = _select_guts unselect_element search_inputs make_param_foreign ); -our $VERSION = '.09'; - -# 11-05-05 - added _to_link_hidden to make a link to the hidden object -# - fixed _to_hidden when called with no args. Hides self obj. -# 11-04-05 - _to_textfield: tries to call "deflate4edit" if column is has_a -# 11-08-05 - Changed Version to .08 - - - -# 1-10-06 -- fixed bug in to_textfiled that was stringifyingf CDBI objects -# # -# -# 1-20-06 - to_select - call db_Main with has a class. -# 1-24-06 - to_select_from_many now named _to_select_from_many . Old deprecated -# - hasmany_class removed in favor of model's related_class method. -# - took out do_select. That is a model action. -# - use search_columns instead of search_fields now. -# - use to_field('column', 'select', {args}) instead of a_select_box. -# -- took out make_hidden_element.was my own personal hack -# -- added _box from DH's FormView to calculate decent textarea size -# -- Refactor to_field into _from_* method calls. -# -# 1-25-06 -- Added _to_checkbox and _to_radio from FView -# 1-27-06 -- Refactored into yet more exported methods -# 1-28-06 -- select constraints where, join order by - +our $VERSION = '2.11'; =head1 NAME @@ -78,7 +53,7 @@ Maypole::Model:CDBI::AsForm - Produce HTML form elements for database columns __PACKAGE__->has_a('beer', 'BeerDB::Beer'); package BeerDB::Drinker; __PACKAGE__->has_many('pints', 'BeerDB::Pint'); - + # NEED to do mapping my $sel = BeerDB::Drinker->to_field('pints', 'select') # multiple my $sel = $Drunk->to_field('pints', 'select'); # Already had beers selected @@ -115,8 +90,7 @@ __PACKAGE__->has_many('contacts' => 'Contact', - # Random uses - + # Random uses =head1 DESCRIPTION @@ -142,7 +116,8 @@ Uses fields specified in search_fields, makes foreign inputs if necessary. =cut -# TODO -- use search_columns +# TODO -- use search_columns + sub search_inputs { my ($class, $r) = @_; warn "In model search_inputs " if $class->model_debug; @@ -194,6 +169,12 @@ sub unselect_element { } +=head2 a_select_box + + Returns a HTML::Element representing a select box, based on the arguments + +=cut + # make a select box from args sub a_select_box { my ($self, $name, $vals, $selected_val, $contents) = @_; @@ -334,32 +315,31 @@ Override at will. =cut sub _field_from_column { - my ($self, $field, $args) = @_; - my $class = ref $self || $self; - # Get column type - unless ($args->{column_type}) { - if ($class->can('column_type')) { - $args->{column_type} = $class->column_type($field); - } - else { - # Right, have some of this - eval "package $class; Class::DBI::Plugin::Type->import()"; - $args->{column_type} = $class->column_type($field); - } + my ($self, $field, $args) = @_; + my $class = ref $self || $self; + # Get column type + unless ($args->{column_type}) { + if ($class->can('column_type')) { + $args->{column_type} = $class->column_type($field); + } else { + # Right, have some of this + eval "package $class; Class::DBI::Plugin::Type->import()"; + $args->{column_type} = $class->column_type($field); } - my $type = $args->{column_type}; - - return $self->_to_textfield($field) - if $type and $type =~ /(VAR)?CHAR/i; #common type - return $self->_to_textarea($field, $args) - if $type and $type =~ /^(TEXT|BLOB)$/i; - return $self->_to_enum_select($field, $args) - if $type and $type =~ /^ENUM\((.*?)\)$/i; - return $self->_to_bool_select($field, $args) - if $type and $type =~ /^BOOL/i; - return $self->_to_readonly($field, $args) - if $type and $type =~ /^readonly$/i; - return; + } + my $type = $args->{column_type}; + + return $self->_to_textfield($field) + if $type and $type =~ /(VAR)?CHAR/i; #common type + return $self->_to_textarea($field, $args) + if $type and $type =~ /^(TEXT|BLOB)$/i; + return $self->_to_enum_select($field, $args) + if $type and $type =~ /^ENUM\((.*?)\)$/i; + return $self->_to_bool_select($field, $args) + if $type and $type =~ /^BOOL/i; + return $self->_to_readonly($field, $args) + if $type and $type =~ /^readonly$/i; + return; } @@ -563,13 +543,10 @@ sub _to_select { # Get items to select from $args->{items} = _select_items($args); -use Data::Dumper; -warn "Just got items. They are " . Dumper($args->{items}); - - # Make select HTML element - $a = $self->_select_guts($col, $args); + # Make select HTML element + $a = $self->_select_guts($col, $args); - # Return + # Return $OLD_STYLE && return $a->as_HTML; $a; @@ -598,7 +575,6 @@ sub _select_items { $sql .= " WHERE " . $args->{where} if $args->{where}; $sql .= " ORDER BY " . $args->{order_by} if $args->{order_by}; $sql .= " LIMIT " . $args->{limit} if $args->{limit}; -warn "_select_items sql is : $sql"; return $fclass->db_Main->selectall_arrayref($sql); @@ -964,8 +940,7 @@ sub _options_from_arrays { my $content = ($fclass and $stringify and $fclass->can($stringify)) ? $fclass->$stringify($_) : join('/', @{$_}); -use Data::Dumper; -warn "Content is $content"; + $opt->push_content( $content ); push @res, $opt; } @@ -993,17 +968,15 @@ sub _options_from_hashes { my $fclass = $args->{class} || ''; my $stringify = $args->{stringify} || ''; my @res; - for (@$items) { - my $val = $_->{$pk}; + for my $item (@$items) { + my $val = $item->{$pk}; my $opt = HTML::Element->new("option", value => $val ); $opt->attr(selected => "selected") if $selected->{$val}; - my $content = $fclass and $stringify and $fclass->can($stringify) ? - $fclass->$stringify($_) : - join(' ', @$_); + my $content = ( $fclass && $stringify && $fclass->can($stringify)) ? $fclass->$stringify($item) : join(' ', @$item); $opt->push_content( $content ); - push @res, $opt; + push @res, $opt; } - return @res; + return @res; } # @@ -1160,6 +1133,3 @@ L, L, L. =cut - - -