X-Git-Url: https://git.decadent.org.uk/gitweb/?a=blobdiff_plain;f=lib%2FMaypole%2FModel%2FCDBI%2FAsForm.pm;h=f1fe978b1c36e7273f883d54a364cda003ec97ae;hb=c3973978e1373a262d13da63c9e9ecfde4b72cc7;hp=3c35dfc5fa4b23b978cb886986cd528123f1d863;hpb=3ed98309a0852fc198f9e4d0e6f70c5510c8282f;p=maypole.git diff --git a/lib/Maypole/Model/CDBI/AsForm.pm b/lib/Maypole/Model/CDBI/AsForm.pm index 3c35dfc..f1fe978 100644 --- a/lib/Maypole/Model/CDBI/AsForm.pm +++ b/lib/Maypole/Model/CDBI/AsForm.pm @@ -1,6 +1,16 @@ package Maypole::Model::CDBI::AsForm; -use 5.006; +#TODO -- +# lots of doc +# _to_select_or_create -- select input stays +# _to_create_or_select -- create input trumps +# + +# TESTED and Works -- +# has_many select -- $obj->to_field($has_many_col); # select one form many +# -- $class->to_field($has_many_col); # foreign inputs +# $class->search_inputs; / + use strict; use warnings; @@ -9,22 +19,22 @@ use base 'Exporter'; use Data::Dumper; use Class::DBI::Plugin::Type (); use HTML::Element; +use Carp qw/cluck/; our $OLD_STYLE = 0; # pjs -- Added new methods to @EXPORT -our @EXPORT = - qw( - to_cgi to_field _to_textarea _to_textfield _to_select +our @EXPORT = + qw( + to_cgi to_field foreign_input_delimiter search_inputs unselect_element + _field_from_how _field_from_relationship _field_from_column + _to_textarea _to_textfield _to_select _select_guts _to_foreign_inputs _to_enum_select _to_bool_select - _to_select_from_many _to_select_from_related _to_select_from_objs _to_hidden _to_link_hidden _rename_foreign_input _to_readonly _options_from_objects _options_from_arrays _options_from_hashes - _options_from_scalars - _field_from_how _field_from_relationship _field_from_column - _select_guts unselect_element search_inputs make_param_foreign + _options_from_array _options_from_hash ); -our $VERSION = '2.11'; +our $VERSION = '.10'; =head1 NAME @@ -47,50 +57,34 @@ Maypole::Model:CDBI::AsForm - Produce HTML form elements for database columns end_form; } - . . . somewhere use to_field($col, $how, $args) - package BeerDB::Pint; - __PACKAGE__->has_a('drinker', 'BeerDB::Drinker'); - __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 - +# Example of has_many select package Job; - -__PACKAGE__->has_a('employer' => 'Employer'); +__PACKAGE__->has_a('job_employer' => 'Employer'); __PACKAGE__->has_a('contact' => 'Contact') package Contact; - -__PACKAGE__->has_a('employer_also' => 'Employer'); +__PACKAGE__->has_a('cont_employer' => 'Employer'); __PACKAGE__->has_many('jobs' => 'Job', - { join => { employer => 'employer_also' }, + { 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', ); - # Below gives select boxes with the multiple attribute. - my $select_jobs_for_new_contact = - Contact->to_field('jobs', 'select'); # Uses constraint and order by - - my $edit_jobs_for_existing_contact = - $contact->to_field('jobs', 'select'); - - + # Choose some jobs to add to a contact (has multiple attribute). + my $job_sel = Contact->to_field('jobs'); # Uses constraint and order by + - # Random uses + # Choose a job from $contact->jobs + my $job_sel = $contact->to_field('jobs'); + =head1 DESCRIPTION @@ -102,165 +96,288 @@ into textareas, and fields with a has-a relationship to other C tables are turned into select drop-downs populated with objects from the joined class. -=head1 METHODS -The module is a mix-in which adds two additional methods to your -C-derived class. +=head1 ARGUMENTS HASH +This provides a convenient way to tweak AsForm's behavior in exceptional or +not so exceptional instances. Below describes the arguments hash and +example usages. -=head2 search_inputs -Returns hashref of search inputs elements to use in cgi. + $beer->to_field($col, $how, $args); + $beer->to_field($col, $args); -Uses fields specified in search_fields, makes foreign inputs if necessary. +Not all _to_* methods pay attention to all arguments. For example, '_to_textfield' does not look in $args->{'items'} at all. + +=over -=cut +=item name -- the name the element will have , this trumps the derived name. -# TODO -- use search_columns + $beer->to_field('brewery', 'readonly', { + name => 'brewery_id' + }); + +=item value -- the initial value the element will have, trumps derived value -sub search_inputs { - my ($class, $r) = @_; - warn "In model search_inputs " if $class->model_debug; - $class = ref $class || $class; - #my $accssr_class = { $class->accessor_classes }; - my %cgi; - my $sfs = [$class->search_columns]; - - foreach my $field ( @$sfs ) { - if ( ref $field eq "HASH" ) { # foreign search fields - my ($accssr, $cols) = each %$field; - unless ( @$cols ) { - # default to search fields for related - #$cols = $accssr_class->{$accssr}->search_columns; - die ("$class search_fields error: Must specify at least one column to search in the foreign object named '$accssr'"); - } - my $fcgi = $class->_to_foreign_inputs($accssr, $cols); - # unset the default values for a select box - foreach (keys %$fcgi) { - $class->unselect_element($fcgi->{$_}); - } - $cgi{$accssr} = $fcgi; - } else { - $cgi{$field} = $class->to_field($field); - $class->unselect_element($cgi{$field}); - } - } - return \%cgi; -} + $beer->to_field('brewery', 'textfield', { + name => 'brewery_id', value => $beer->brewery, + # however, no need to set value since $beer is object + }); + +=item items -- array of items generally used to make select box options +Can be array of objects, hashes, arrays, or strings, or just a hash. + # 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') ], + }); -=head2 unselect_element + # Make a select for a boolean field + $Pub->to_field('open' , { items => [ {'Open' => 1, 'Closed' => 0 } ] }); -Unselects all options in a HTML::Element of type select. -It does nothing if element is not a select element. +=item selected -- something representing which item is selected in a select box -=cut + $beer->to_field('brewery', { + selected => $beer->brewery, # again not necessary since caller is obj. + }); -sub unselect_element { - my ($self, $el) = @_; - #unless (ref $el eq 'HTML::Element') { - #$self->_croak ('Need an HTML::Element to unselect. You gave a ' . Dumper($el)); } - if ($el->tag eq 'select') { - foreach my $opt ($el->content_list) { - $opt->attr('selected', undef) if $opt->attr('selected'); - } - } -} +Can be an simple scalar id, an object, or an array of either +=item class -- the class for which the input being made for field pertains to. -=head2 a_select_box +This in almost always derived in cases where it may be difficult to derive, -- + # Select beers to serve on handpump + Pub->to_field(handpumps => select => { + class => 'Beer', order_by => 'name ASC', multiple => 1, + }); - Returns a HTML::Element representing a select box, based on the arguments +=item column_type -- a string representing column type + + $pub->to_field('open', 'bool_select', { + column_type => "bool('Closed', 'Open'), + }); -=cut +=item column_nullable -- flag saying if column is nullable or not -# make a select box from args -sub a_select_box { - my ($self, $name, $vals, $selected_val, $contents) = @_; - die "Usage: Need a name and array ref of values to make a select boxes" unless ($name && $vals); - $selected_val ||= ""; - $contents ||= $vals ; - - my $a = HTML::Element->new('select', 'name' => $name); - my $i = 0; - my $c; - foreach my $v ( @$vals ) { - my $opt = HTML::Element->new('option', 'value' => $v); - $opt->attr('selected' => 'selected') if $v eq $selected_val; - $c = $contents->[$i++] || $v; - $opt->push_content($c); - $a->push_content($opt); - } - $a; -} +Generally this can be set to get or not get a null/empty option added to +a select box. AsForm attempts to call "$class->column_nullable" to set this +and it defaults to true if there is no shuch method. + + $beer->to_field('brewery', { column_nullable => 1 }); +=item r or request -- the mapyole request object +=item uri -- uri for a link , used in methods such as _to_link_hidden -=head2 make_param_foreign + $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 -Makes a new foreign parameter out of parameter and accessor -Just puts accssr__FOREIGN__ in front of param name +=item order_by, constraint, join -=cut +These are used in making select boxes. order_by is a simple order by clause +and constraint and join are hashes used to limit the rows selected. The +difference is that join uses methods of the object and constraint uses +static values. You can also specify these in the relationship arguments. -sub make_param_foreign { - my ($self, $r, $p, $accssr) = @_; - $r->params->{$accssr . '__FOREIGN__' . $p} = $r->params->{$p}; -} + BeerDB::LondonBeer->has_a('brewery', 'BeerDB::Brewery', + order_by => 'brewery_name ASC', + constraint => {location => 'London'}, + 'join' => {'brewery_tablecolumn => 'beer_obj_column'}, + ); + +=item no_hidden_constraints -- -=head2 to_cgi +Tell AsForm not to make hidden inputs for relationship constraints. It does +this sometimes when making foreign inputs . + +=back -This returns a hash mapping all the column names of the class to -HTML::Element objects representing form widgets. +=head2 to_cgi -pjs -- added a columns list argument to specify which columns to make -inputs for. + $self->to_cgi([@columns, $args]); + +This returns a hash mapping all the column names to HTML::Element objects +representing form widgets. It takes two opitonal arguments -- a list of +columns and a hashref of hashes of arguments for each column. If called with an object like for editing, the inputs will have the object's values. + + $self->to_cgi(); # uses $self->columns; # most used + $self->to_cgi(qw/brewery style rating/); # sometimes + # and on rare occassions this is desireable if you have a lot of fields + # and dont want to call to_field a bunch of times just to tweak one or + # two of them. + $self->to_cgi(@cols, {brewery => { + how => 'textfield' # too big for select + }, + style => { + column_nullable => 0, + how => 'select', + items => ['Ale', 'Lager'] + } + } =cut sub to_cgi { my ($class, @columns) = @_; # pjs -- added columns arg - @columns = $class->columns unless (@columns); - map { $_ => $class->to_field($_) } @columns; + my $args = {}; + if (not @columns) { + @columns = $class->columns; + # Eventually after stabalization, we could add display_columns + #keys map { $_ => 1 } ($class->display_columns, $class->columns); + } + else { + if ( ref $columns[-1] eq 'HASH' ) { $args = pop @columns; } + } + map { $_ => $class->to_field($_, $args->{$_}) } @columns; } - -=head2 to_field($field [, $how]) +=head2 to_field($field [, $how][, $args]) This maps an individual column to a form element. The C argument -can be used to force the field type into one of C, C