From: biopete Date: Mon, 17 Jul 2006 06:04:39 +0000 (+0000) Subject: add_to_from_cgi is cool. X-Git-Tag: 2.11~10 X-Git-Url: https://git.decadent.org.uk/gitweb/?a=commitdiff_plain;h=e1ed80e9b0929d184fabfb6d4df9a8db4580eb4e;p=maypole.git add_to_from_cgi is cool. git-svn-id: http://svn.maypole.perl.org/Maypole/trunk@510 48953598-375a-da11-a14b-00016c27c3ee --- diff --git a/ex/fancy_example/BeerDB.pm b/ex/fancy_example/BeerDB.pm index 5b30188..cb72574 100644 --- a/ex/fancy_example/BeerDB.pm +++ b/ex/fancy_example/BeerDB.pm @@ -44,7 +44,6 @@ BeerDB::Beer->untaint_columns( BeerDB::Pub->untaint_columns(printable => [qw/name notes url/]); BeerDB::Drinker->untaint_columns( printable => [qw/handle created/] ); BeerDB::Pint->untaint_columns( printable => [qw/date_and_time/]); -BeerDB::Person->untaint_columns( printable => [qw/first_name sur_name dob username password email/]); # Required Fields @@ -58,7 +57,6 @@ BeerDB->config->{person}{required_cols} = [qw/first_name sur_name dob email/]; # Columns to display sub BeerDB::Handpump::display_columns { qw/pub beer/ } -sub BeerDB::Person::display_columns { qw/first_name last_name dob email/ } BeerDB->config->{loader}->relationship($_) for ( "a brewery produces beers", @@ -76,4 +74,5 @@ sub BeerDB::Handpump::stringify_self { return $self->beer . " @ " . $self->pub; } + 1; diff --git a/ex/fancy_example/BeerDB/Base.pm b/ex/fancy_example/BeerDB/Base.pm index 23b1dd1..aaafce1 100644 --- a/ex/fancy_example/BeerDB/Base.pm +++ b/ex/fancy_example/BeerDB/Base.pm @@ -2,10 +2,10 @@ package BeerDB::Base; use base qw/Maypole::Model::CDBI/; use strict; use warnings; +use Data::Dumper; # Overide list to add display_columns to cgi # Perhaps do this in AsForm? - sub list : Exported { use Data::Dumper; my ($self, $r) = @_; @@ -15,6 +15,24 @@ sub list : Exported { $r->template_args->{classmetadata}{cgi} = { $self->to_cgi(@cols) }; } +# Override view to make inputs and process form to add to related +sub view : Exported { + my ($self, $r, $obj) = @_; + $self->_croak( "Object method only") unless $obj; + + if ($r->params->{submit}) { + my @related = $obj->add_to_from_cgi($r, { required => [$self->related ]}); + if (my $errs = $obj->cgi_update_errors) { + $r->template_args->{errors} = $errs; + } + } + + # Inputs to add to related on the view page + # Now done on the view template + # my %cgi = $self->to_cgi($self->related); + #$r->template_args->{classmetadata}{cgi} = \%cgi ; +} + # Template switcheroo bug bit me -- was seeing view page but the view action was never # being executed after an edit. diff --git a/ex/fancy_example/BeerDB/Drinker.pm b/ex/fancy_example/BeerDB/Drinker.pm index 5119dcf..db798fd 100644 --- a/ex/fancy_example/BeerDB/Drinker.pm +++ b/ex/fancy_example/BeerDB/Drinker.pm @@ -40,21 +40,6 @@ sub list : Exported { #$r->template_args->{classmetadata}{search_cgi} = $self->search_inputs; } -sub view : Exported { - my ($self, $r, $obj) = @_; - $self->_croak( "Object method only") unless $obj; - - if ($r->params->{submit}) { - my ($pint) = $obj->add_to_from_cgi($r); - if (my $errs = $obj->cgi_update_errors) { - $r->template_args->{errors} = $errs; - } - } - - my %cgi = $self->to_cgi('pints'); - delete $cgi{pints}{drinker}; # = $obj->to_field(drinker => 'link_hidden', {r => $r}); - $r->template_args->{classmetadata}{cgi} = \%cgi ; -} diff --git a/ex/fancy_example/templates/custom/display_inputs b/ex/fancy_example/templates/custom/display_inputs index 201227c..6baf703 100644 --- a/ex/fancy_example/templates/custom/display_inputs +++ b/ex/fancy_example/templates/custom/display_inputs @@ -32,18 +32,25 @@ values on errors FOR c IN request.config.$tbl.required_cols; required.$c = 1; END; + %] -[% FOR col = classmetadata.columns; +[% +SET heading_shown = 0; +FOR col = classmetadata.columns; NEXT IF !classmetadata.cgi.$col; NEXT IF col == "id" OR col == classmetadata.table _ "_id"; - # Display foreign inputs last IF (mykeys = classmetadata.cgi.$col.keys); foreign.push(col); names.push(classmetadata.colnames.$col); NEXT; END; + IF ! heading_shown; + heading = classmetadata.moniker | ucfirst; + "

$heading

"; + SET heading_shown = 1; + END; %] [% # Base case starts here @@ -84,9 +91,19 @@ values on errors [% USE this = Class(classmetadata.name); FOR col IN foreign; - fclass = this.related_class(request, col); + # has_many mapping throws a stick in our spokes because related_class returns the mapped + # class. Sometimes we just want the has_many class. + + # In case of Pub Handpumps maps to Beer and we want to add Handpump to Pub, we dont + # want the mapped data . + # In case of "Create New Handpump" -- we want the mapped data probably so we get + # Beer inputs and Pub select box. + + fclass_rel_meta = this.related_meta(request, col); + fclass = fclass_rel_meta.foreign_class; # ignor args.mapping fclass_meta = this.get_classmetadata(fclass); - fclass_meta.cgi = classmetadata.cgi.$col; + fclass_meta.cgi = classmetadata.cgi.$col; + # USE Dumper; Dumper.dump(fclass_meta); INCLUDE display_inputs col_prefix = col _ "__AF__" _ col_prefix errors = errors.$col diff --git a/ex/fancy_example/templates/custom/display_inputs.recursive b/ex/fancy_example/templates/custom/display_inputs.recursive deleted file mode 100644 index 5c7d565..0000000 --- a/ex/fancy_example/templates/custom/display_inputs.recursive +++ /dev/null @@ -1,97 +0,0 @@ -[%# - -=head1 display_inputs - -This *RECURSIVELY* displays inputs for a hash of html elements - -Vars it needs: - classmetadata-- the hash of bunch of data: - cgi -- inputs keyed on column names - table -- table inputs are for - columns -- list in order want displayed inputs - colnames -- hash of what to label inputs - -errors -- hash of errors keyed on columns - - -TODO -- make it recognize a general submit button for redisplaying -values on errors - -=cut - -# -%] - -[% # some variables - foreign = []; - names = []; - # get hash of related classes keyed on accessor for Foreign Inputs - USE this = Class(classmetadata.name); - tbl = classmetadata.table; - required = { }; - FOR c IN request.config.$tbl.required_cols; - required.$c = 1; - END; -%] - -[% FOR col = classmetadata.columns; - NEXT IF !classmetadata.cgi.$col; - NEXT IF col == "id"; - - # Display foreign inputs last - IF (mykeys = classmetadata.cgi.$col.keys); - foreign.push(col); - names.push(classmetadata.colnames.$col); - NEXT; - END; -%] - -[% # Base case starts here - - SET elem = classmetadata.cgi.$col; #.clone; # not sure why clone - IF elem.type == 'hidden'; - elem.as_XML; - NEXT; - ELSIF cgi_params; - param_col = col_prefix _ col; - IF elem.tag == "textarea"; - elem = elem.push_content(cgi_params.$param_col); - ELSIF elem.tag == "select"; - oldval = set_selected(elem, cgi_params.$col); - ELSE; - oldval = elem.attr("value", cgi_params.$param_col); - END; - END; -%] - - - - [% IF errors.$col %] - [% errors.$col | html %] - [% END %] -[% END; %] - - - -[% USE this = Class(classmetadata.name); - FOR col IN foreign; - fclass = this.related_class(request, col); - fclass_meta = this.get_classmetadata(fclass); - fclass_meta.cgi = classmetadata.cgi.$col; - INCLUDE display_inputs - col_prefix = col _ "__AF__" _ col_prefix - errors = errors.$col - heading = names.shift - classmetadata = fclass_meta; # localize - END; -%] - diff --git a/ex/fancy_example/templates/custom/display_search_inputs b/ex/fancy_example/templates/custom/display_search_inputs index ee1d586..9985bfb 100644 --- a/ex/fancy_example/templates/custom/display_search_inputs +++ b/ex/fancy_example/templates/custom/display_search_inputs @@ -20,6 +20,7 @@ errors -- hash of errors keyed on columns SET cgi = classmetadata.cgi; SET delimiter = this.foreign_input_delimiter; FOR field IN srch_fields; + NEXT IF !cgi.$field; # Recursivly call this tmeplate if we have foreign field # (hash of foreign inputs should come with it) IF ( cgi.$field.keys ); diff --git a/ex/fancy_example/templates/drinker/view b/ex/fancy_example/templates/drinker/view deleted file mode 100644 index fd85e88..0000000 --- a/ex/fancy_example/templates/drinker/view +++ /dev/null @@ -1,54 +0,0 @@ -[%# - -=for doc - -Drinker C template displays drinker and from to drink beer. - -=cut - -#%] -[% PROCESS macros %] -[% INCLUDE header %] -[% view_item(object); %] - -[%# Form to drink a pint. We made sure to only make inputs for pint. could do it - # here like so. - USE this = Class(classmetadata.name); - classmetadata.cgi = this.to_field(pints); - -%] - - -[%# - -=for doc - -The C template also displays a list of other objects related to the first -one via C style relationships; this is done by calling the -C method - see L - to return -a list of has-many accessors. Next it calls each of those accessors, and -displays the results in a table. - -#%] -
Back to listing -[% view_related(object); %] - -[% - button(object, "edit"); - button(object, "delete"); -%] -[% INCLUDE footer %]