]> git.decadent.org.uk Git - maypole.git/blob - lib/Apache/MVC/Model/CDBI.pm
88ba3da1334cd3383057cb69345f214614d5447a
[maypole.git] / lib / Apache / MVC / Model / CDBI.pm
1 package Apache::MVC::Model::CDBI;
2 use base qw(Apache::MVC::Model::Base Class::DBI);
3 use Class::DBI::AsForm;
4 use Class::DBI::FromCGI;
5 use CGI::Untaint;
6
7 sub description { "A poorly defined class" }
8
9 sub column_names { my $class = shift; map { $_ => ucfirst $_ } $class->columns }
10
11 sub get_objects {
12     my ($self, $r) = @_;
13     return $self->retrieve(shift @{$r->{args}});
14 }
15
16 sub do_edit :Exported {
17     my ($self, $r) = @_;
18     my $h = CGI::Untaint->new(%{$r->{params}});
19     my ($obj) = @{$self->objects};
20     if ($obj) {
21         # We have something to edit
22         $obj->update_from_cgi($h);
23         warn "Updating an object ($obj) with ".Dumper($h); use Data::Dumper;
24     } else {
25         $obj = $self->create_from_cgi($h);
26     }
27     if (my %errors = $obj->cgi_update_errors) {
28         # Set it up as it was:
29         warn "There were errors: ".Dumper(\%errors)."\n";
30         $r->{template_args}{cgi_params} = \%params;
31         $r->{template_args}{errors} = \%errors;
32         $r->{template} = "edit";
33     } else {
34         $r->{template} = "view";
35     }
36     $r->objects([ $obj ]);
37 }
38
39 sub delete :Exported {
40     my ($self, $r) = @_;
41     $_->SUPER::delete for @{ $r->objects };
42     $r->objects([ $self->retrieve_all ]);
43     $r->{template} = "list";
44 }
45
46 sub adopt {
47     my ($self, $child) = @_;
48     $child->autoupdate(1);
49     $child->columns( Stringify => qw/ name / );
50 }
51
52 1;