X-Git-Url: https://git.decadent.org.uk/gitweb/?a=blobdiff_plain;f=lib%2FApache%2FMVC%2FModel%2FCDBI.pm;h=854118e1559f2007fda5983a31a41b2708a604ae;hb=5cccc5a89004f9c514e9d611b5e2a02e9aa28ece;hp=8e0b9812bd1763cd1ddb5ed3e8f373a2f06a2866;hpb=2f277c6096d3a93e2f0cd3e2b7bd1c4245398d5f;p=maypole.git diff --git a/lib/Apache/MVC/Model/CDBI.pm b/lib/Apache/MVC/Model/CDBI.pm index 8e0b981..854118e 100644 --- a/lib/Apache/MVC/Model/CDBI.pm +++ b/lib/Apache/MVC/Model/CDBI.pm @@ -1,22 +1,39 @@ package Apache::MVC::Model::CDBI; -use base 'Apache::MVC::Model::Base'; +use base qw(Apache::MVC::Model::Base Class::DBI); +use Lingua::EN::Inflect::Number qw(to_PL); use Class::DBI::AsForm; use Class::DBI::FromCGI; +use Class::DBI::AbstractSearch; +use Class::DBI::Pager; use CGI::Untaint; +use strict; -sub description { "A poorly defined class" } +=head1 NAME -sub column_names { my $class = shift; map { $_ => ucfirst $_ } $class->columns } +Apache::MVC::Model::CDBI - Model class based on Class::DBI -sub get_objects { +=head1 DESCRIPTION + +This is a master model class which uses C 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. + +=cut + +sub related { my ($self, $r) = @_; - return $self->retrieve(shift @{$r->{args}}); + # Has-many methods; XXX this is a hack + map {to_PL($_)} + grep { exists $r->{config}{ok_tables}{$_} } + map {$_->table} + keys %{shift->__hasa_list || {}} } sub do_edit :Exported { my ($self, $r) = @_; my $h = CGI::Untaint->new(%{$r->{params}}); - my ($obj) = @{$self->objects}; + my ($obj) = @{$r->objects}; if ($obj) { # We have something to edit $obj->update_from_cgi($h); @@ -27,7 +44,7 @@ sub do_edit :Exported { if (my %errors = $obj->cgi_update_errors) { # Set it up as it was: warn "There were errors: ".Dumper(\%errors)."\n"; - $r->{template_args}{cgi_params} = \%params; + $r->{template_args}{cgi_params} = $r->{params}; $r->{template_args}{errors} = \%errors; $r->{template} = "edit"; } else { @@ -38,7 +55,7 @@ sub do_edit :Exported { sub delete :Exported { my ($self, $r) = @_; - $self->delete for @{ $r->objects }; + $_->SUPER::delete for @{ $r->objects }; $r->objects([ $self->retrieve_all ]); $r->{template} = "list"; } @@ -49,4 +66,28 @@ sub adopt { $child->columns( Stringify => qw/ name / ); } +sub search :Exported { + return shift->SUPER::search(@_) if caller eq "Class::DBI"; # oops + my ($self, $r) = @_; + my %fields = map {$_ => 1 } $self->columns; + my $oper = "like"; # For now + use Carp; Carp::confess("Urgh") unless ref $r; + my %params = %{$r->{params}}; + my %values = map { $_ => {$oper, $params{$_} } } + grep { $params{$_} and $fields{$_} } keys %params; + + $r->objects([ %values ? $self->search_where(%values) : $self->retrieve_all ]); + $r->template("list"); + $r->{template_args}{search} = 1; +} + +sub list :Exported { + my ($self, $r) = @_; + if ( my $rows = $r->config->{rows_per_page}) { + $self = $self->pager($rows, $r->query->{page}); + $r->{template_args}{pager} = $self; + } + $r->objects([ $self->retrieve_all ]); +} 1; +