X-Git-Url: https://git.decadent.org.uk/gitweb/?a=blobdiff_plain;f=lib%2FApache%2FMVC%2FModel%2FCDBI.pm;h=695e1cbd7dec1fe0e6e6f9ed33863113df0b2c90;hb=d635ca733a7199c4f9893e8d5a50e96a1295f092;hp=0a0e92384c7552980cf70b66d22cb6db63d4fb19;hpb=fa685ce517bd35c12ed6681803d9d0d6b1793159;p=maypole.git diff --git a/lib/Apache/MVC/Model/CDBI.pm b/lib/Apache/MVC/Model/CDBI.pm index 0a0e923..695e1cb 100644 --- a/lib/Apache/MVC/Model/CDBI.pm +++ b/lib/Apache/MVC/Model/CDBI.pm @@ -1,4 +1,62 @@ 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 CGI::Untaint; + +sub description { "A poorly defined class" } + +sub column_names { my $class = shift; map { $_ => ucfirst $_ } $class->columns } + +sub get_objects { + my ($self, $r) = @_; + return $self->retrieve(shift @{$r->{args}}); +} + +sub related { + my ($self, $r) = @_; + # 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) = @{$r->objects}; + if ($obj) { + # We have something to edit + $obj->update_from_cgi($h); + warn "Updating an object ($obj) with ".Dumper($h); use Data::Dumper; + } else { + $obj = $self->create_from_cgi($h); + } + 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}{errors} = \%errors; + $r->{template} = "edit"; + } else { + $r->{template} = "view"; + } + $r->objects([ $obj ]); +} + +sub delete :Exported { + my ($self, $r) = @_; + $_->SUPER::delete for @{ $r->objects }; + $r->objects([ $self->retrieve_all ]); + $r->{template} = "list"; +} + +sub adopt { + my ($self, $child) = @_; + $child->autoupdate(1); + $child->columns( Stringify => qw/ name / ); +} 1;