X-Git-Url: https://git.decadent.org.uk/gitweb/?a=blobdiff_plain;f=lib%2FMaypole%2FModel%2FCDBI%2FAsForm.pm;h=cd295f3e06335e502cbd720839ee3625f688608c;hb=745badbb1417451398a0f983c450fd8725794f65;hp=9beaa8dc7288af5a50a6545b623cb2cb116bd5fd;hpb=d3bd3060bf3f11775d9f40ee983f71949404f794;p=maypole.git diff --git a/lib/Maypole/Model/CDBI/AsForm.pm b/lib/Maypole/Model/CDBI/AsForm.pm index 9beaa8d..cd295f3 100644 --- a/lib/Maypole/Model/CDBI/AsForm.pm +++ b/lib/Maypole/Model/CDBI/AsForm.pm @@ -53,25 +53,95 @@ Maypole::Model:CDBI::AsForm - Produce HTML form elements for database columns end_form; } -# Example of has_many select -package Job; -__PACKAGE__->has_a('job_employer' => 'Employer'); -__PACKAGE__->has_a('contact' => 'Contact') - -package Contact; -__PACKAGE__->has_a('cont_employer' => 'Employer'); -__PACKAGE__->has_many('jobs' => 'Job', - { join => { job_employer => 'cont_employer' }, - constraint => { 'finshed' => 0 }, - order_by => "created ASC", - } -); - -package Employer; -__PACKAGE__->has_many('jobs' => 'Job',); -__PACKAGE__->has_many('contacts' => 'Contact', - order_by => 'name DESC', -); + + . . . + + # Somewhere else in a Maypole application about beer... + + + + + $beer->to_field('brewery', 'textfield', { + name => 'brewery_id', value => $beer->brewery, + # however, no need to set value since $beer is object + }); + + # Rate a beer + $beer->to_field(rating => select => { + items => [1 , 2, 3, 4, 5], + }); + + # Select a Brewery to visit in the UK + Brewery->to_field(brewery_id => { + items => [ Brewery->search_like(location => 'UK') ], + }); + + # Make a select for a boolean field + $Pub->to_field('open' , { items => [ {'Open' => 1, 'Closed' => 0 } ] }); + + $beer->to_field('brewery', { + selected => $beer->brewery, # again not necessary since caller is obj. + }); + + + $beer->to_field('brewery', 'link_hidden', {r => $r, uri => 'www.maypole.perl.org/brewery/view/'.$beer->brewery}); + # an html link that is also a hidden input to the object. R is required to + # make the uri unless you pass a uri + + + + ##################################################### + # Templates Usage + +
+ + ... + + + + . . . + + + + . . . + +
+ + + ##################################################### + # Advanced Usage + + # has_many select + package Job; + __PACKAGE__->has_a('job_employer' => 'Employer'); + __PACKAGE__->has_a('contact' => 'Contact') + + package Contact; + __PACKAGE__->has_a('cont_employer' => 'Employer'); + __PACKAGE__->has_many('jobs' => 'Job', + { join => { job_employer => 'cont_employer' }, + constraint => { 'finshed' => 0 }, + order_by => "created ASC", + } + ); + + package Employer; + __PACKAGE__->has_many('jobs' => 'Job',); + __PACKAGE__->has_many('contacts' => 'Contact', + order_by => 'name DESC', + ); # Choose some jobs to add to a contact (has multiple attribute). @@ -81,6 +151,9 @@ __PACKAGE__->has_many('contacts' => 'Contact', # Choose a job from $contact->jobs my $job_sel = $contact->to_field('jobs'); + 1; + + =head1 DESCRIPTION @@ -242,7 +315,7 @@ sub to_cgi { This maps an individual column to a form element. The C argument can be used to force the field type into any you want. All that you need is a method named "_to_$how" in your class. Your class inherits many from -AsForm already. Override them at will. +AsForm already. If C is specified but the class cannot call the method it maps to, then AsForm will issue a warning and the default input will be made. @@ -258,8 +331,8 @@ sub to_field { unless ($how) { $how = $args->{how} || ''; } #warn "In to_field field is $field how is $how. args ar e" . Dumper($args) . " \n"; # Set sensible default value - unless ($args->{default}) { - my $def = $self->column_default($field); + if ($field and not defined $args->{default}) { + my $def = $self->column_default($field) ; # exclude defaults we don't want actually put as value for input if (defined $def) { $def = $def =~ /(^0000-00-00.*$|^0[0]*$|^0\.00$|CURRENT_TIMESTAMP|NULL)/i ? '' : $def ; @@ -445,18 +518,6 @@ sub _field_from_relationship { return; } - - - #NOOO! maybe select from has_many -# if ($rel_type eq 'has_many' and ref $self) { -# $args->{items} ||= [$self->$field]; -# # arg name || fclass pk name || field -# if (not $args->{name}) { -# $args->{name} = eval{$fclass->primary_column->name} || $field; -# } -# return $self->_to_select($field, $args); -# } - # # maybe foreign inputs my %local_cols = map { $_ => 1 } $self->columns; # includes is_a cols if ($fclass_is_cdbi and (not $local_cols{$field} or $rel_name eq 'has_own')) @@ -476,8 +537,11 @@ Override at will. sub _field_from_column { my ($self, $field, $args) = @_; - return unless $field; - my $class = ref $self || $self; + # this class and pk are default class and field at this point + my $class = $args->{class} || $self; + $class = ref $class || $class; + $field ||= ($class->primary_columns)[0]; # TODO + # Get column type unless ($args->{column_type}) { if ($class->can('column_type')) { @@ -507,6 +571,9 @@ sub _field_from_column { sub _to_textarea { my ($self, $col, $args) = @_; + my $class = $args->{class} || $self; + $class = ref $class || $class; + $col ||= ($class->primary_columns)[0]; # TODO # pjs added default $args ||= {}; my $val = $args->{value}; @@ -671,6 +738,7 @@ sub _to_select { if not $args->{selected} and ref $self; } $col = $args->{class}->primary_column; + $args->{name} ||= $col; } # Related Class maybe ? elsif ($rel_meta = $self->related_meta('r:)', $col) ) { @@ -710,11 +778,10 @@ sub _to_select { } # We could say :Col is name and we are selecting out of class arg. # DIE for now - else { - #$args->{name} = $col; - die "Usage _to_select. $col not related to any class to select from. "; + #else { + # die "Usage _to_select. $col not related to any class to select from. "; - } + #} # Set arguments unless ( defined $args->{column_nullable} ) { @@ -983,7 +1050,9 @@ sub _to_foreign_inputs { } # Ignore our fkey in them to prevent infinite recursion - my $me = eval {$rel_meta->{args}{foreign_column}} || ''; + my $me = eval {$rel_meta->{args}{foreign_key}} || + eval {$rel_meta->{args}{foreign_column}} + || ''; # what uses foreign_column has_many or might_have my $constrained = $rel_meta->{args}{constraint}; my %inputs; foreach ( @$fields ) { @@ -1242,13 +1311,13 @@ sub _options_from_hashes { my $fclass = $args->{class} || ''; my $stringify = $args->{stringify} || ''; my @res; - for (@$items) { - my $val = defined $_->{$pk} ? $_->{$pk} : ''; + for my $item (@$items) { + my $val = defined $item->{$pk} ? $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(' ', keys %$_); + join(' ', map {$item->{$_} } keys %$item); $opt->push_content( $content ); push @res, $opt; }