X-Git-Url: https://git.decadent.org.uk/gitweb/?a=blobdiff_plain;f=lib%2FMaypole%2FModel%2FCDBI.pm;h=6ae19f53fa12db0ee355dcebb6425e26b30fe256;hb=12d8a77a713d5ed4f08414e5f34e96d45f60e2d3;hp=171c9164b21de239d2dcde9e75f20200eebb2765;hpb=2225b4184a391bab5f1d9498d5b695ba0c2f09f2;p=maypole.git diff --git a/lib/Maypole/Model/CDBI.pm b/lib/Maypole/Model/CDBI.pm index 171c916..6ae19f5 100644 --- a/lib/Maypole/Model/CDBI.pm +++ b/lib/Maypole/Model/CDBI.pm @@ -1,6 +1,10 @@ package Maypole::Model::CDBI; use base qw(Maypole::Model::Base Class::DBI); use Class::DBI::AsForm; +# use Maypole::Form::CDBI; +use CGI::Untaint; +# use Maypole::Form; + use Class::DBI::FromCGI; use Class::DBI::Loader; use Class::DBI::AbstractSearch; @@ -8,7 +12,7 @@ use Class::DBI::Plugin::RetrieveAll; use Class::DBI::Pager; use Lingua::EN::Inflect::Number qw(to_PL); -use CGI::Untaint; + use strict; =head1 NAME @@ -17,11 +21,62 @@ Maypole::Model::CDBI - Model class based on Class::DBI =head1 DESCRIPTION -This is a master model class which uses C to do all the hard +This is a master model class which uses L to do all the hard work of fetching rows and representing them as objects. It is a good model to copy if you're replacing it with other database abstraction modules. +It implements a base set of methods required for a Maypole Data Model. +See L for these: + +=over 4 + +=item adopt + +=item class_of + +=item do_edit + +=item list + +=item related + +=item setup_database + +=item fetch_objects + +=back + +=head1 Additional Actions + +=over + +=item delete + +Unsuprisingly, this command causes a database record to be forever lost. + +=item search + +The search action + +=back + +=head1 Helper Methods + +=over + +=item order + +=item stringify_column + +=item do_pager + +=item related_class + +Given an accessor name as a method, this function returns the class this accessor returns. + +=back + =cut sub related { @@ -29,39 +84,91 @@ sub related { return keys %{ $self->meta_info('has_many') || {} }; } -sub do_edit : Exported { - my ( $self, $r ) = @_; - my $h = CGI::Untaint->new( %{ $r->{params} } ); - my $creating = 0; - my ($obj) = @{ $r->objects || [] }; - if ($obj) { +sub related_class { + my ( $self, $r, $accessor ) = @_; + my $meta = $self->meta_info; + my @rels = keys %$meta; + my $related; + foreach (@rels) { + $related = $meta->{$_}{$accessor}; + last if $related; + } + return unless $related; - # We have something to edit - $obj->update_from_cgi( $h => - { required => $r->{config}{ $r->{table} }{required_cols} || [], } - ); - } - else { - $obj = - $self->create_from_cgi( $h => - { required => $r->{config}{ $r->{table} }{required_cols} || [], } - ); - $creating++; - } - if ( my %errors = $obj->cgi_update_errors ) { + my $mapping = $related->{args}->{mapping}; + if ( $mapping and @$mapping ) { + return $related->{foreign_class}->meta_info('has_a')->{$$mapping[0]}->{foreign_class}; + } + else { + return $related->{foreign_class}; + } + } + +sub do_edit : Exported +{ + my ($self, $r, $obj) = @_; + + my $config = $r->config; + my $table = $r->table; + + my $required_cols = $config->{$table}->{required_cols} || []; + + ($obj, my $fatal, my $creating) = $self->_do_update_or_create($r, $obj, $required_cols); + + # handle errors, if none, proceed to view the newly created/updated object + my %errors = $fatal ? (FATAL => $fatal) : $obj->cgi_update_errors; + + if (%errors) + { # Set it up as it was: - $r->{template_args}{cgi_params} = $r->{params}; - $r->{template_args}{errors} = \%errors; - $r->{template} = "edit"; - undef $obj if $creating; # Couldn't create + $r->template_args->{cgi_params} = $r->params; + $r->template_args->{errors} = \%errors; + + undef $obj if $creating; + $r->template("edit"); } - else { - $r->{template} = "view"; + else + { + $r->template("view"); } - $r->objects( [$obj] ); + + $r->objects( $obj ? [$obj] : []); } +# drb - I've (probably temporarily) split this out from do_edit, so it's +# reported by Mp::P::Trace +sub _do_update_or_create +{ + my ($self, $r, $obj, $required_cols) = @_; + + my $fatal; + my $creating = 0; + my $h = CGI::Untaint->new( %{$r->params} ); + + # update or create + if ($obj) + { + # We have something to edit + eval { $obj->update_from_cgi( $h => {required => $required_cols} ) }; + $fatal = $@; + } + else + { + eval { + $obj = $self->create_from_cgi( $h => {required => $required_cols} ) + }; + + if ($fatal = $@) + { + warn "$fatal" if $r->debug; + } + $creating++; + } + + return $obj, $fatal, $creating; +} + sub delete : Exported { return shift->SUPER::delete(@_) if caller ne "Maypole::Model::Base"; my ( $self, $r ) = @_; @@ -75,6 +182,7 @@ sub stringify_column { my $class = shift; return ( $class->columns("Stringify"), + ( grep { /^(name|title)$/i } $class->columns ), ( grep { /(name|title)/i } $class->columns ), ( grep { !/id$/i } $class->primary_columns ), )[0]; @@ -97,7 +205,8 @@ sub search : Exported { my $oper = "like"; # For now my %params = %{ $r->{params} }; my %values = map { $_ => { $oper, $params{$_} } } - grep { $params{$_} and $fields{$_} } keys %params; + grep { defined $params{$_} && length ($params{$_}) && $fields{$_} } + keys %params; $r->template("list"); if ( !%values ) { return $self->list($r) } @@ -106,7 +215,7 @@ sub search : Exported { $r->objects( [ $self->search_where( - \%values, ( $order ? { order => $order } : () ) + \%values, ( $order ? { order_by => $order } : () ) ) ] ); @@ -124,12 +233,12 @@ sub do_pager { sub order { my ( $self, $r ) = @_; - my $order; my %ok_columns = map { $_ => 1 } $self->columns; - if ( $order = $r->query->{order} and $ok_columns{$order} ) { - $order .= ( $r->query->{o2} eq "desc" && " DESC" ); - } - $order; + my $q = $r->query; + my $order = $q->{order}; + return unless $order and $ok_columns{$order}; + $order .= ' DESC' if $q->{o2} and $q->{o2} eq 'desc'; + return $order; } sub list : Exported { @@ -145,29 +254,42 @@ sub list : Exported { } sub setup_database { - my ( $self, $config, $namespace, $dsn, $u, $p, $opts ) = @_; + my ( $class, $config, $namespace, $dsn, $u, $p, $opts ) = @_; $dsn ||= $config->dsn; $u ||= $config->user; $p ||= $config->pass; $opts ||= $config->opts; $config->dsn($dsn); + warn "No DSN set in config" unless $dsn; $config->loader || $config->loader( Class::DBI::Loader->new( namespace => $namespace, dsn => $dsn, user => $u, password => $p, - options => $opts, + %$opts, ) ); $config->{classes} = [ $config->{loader}->classes ]; $config->{tables} = [ $config->{loader}->tables ]; + warn( 'Loaded tables: ' . join ',', @{ $config->{tables} } ) + if $namespace->debug; } sub class_of { my ( $self, $r, $table ) = @_; - return $r->config->loader->_table2class($table); + return $r->config->loader->_table2class($table); # why not find_class ? } -1; +sub fetch_objects { + my ($class, $r)=@_; + my @pcs = $class->primary_columns; + if ( $#pcs ) { + my %pks; + @pks{@pcs}=(@{$r->{args}}); + return $class->retrieve( %pks ); + } + return $class->retrieve( $r->{args}->[0] ); +} +1;