]> git.decadent.org.uk Git - maypole.git/blob - lib/Apache/MVC/Model/CDBI.pm
This gives us the edit method. (Which also creates new things)
[maypole.git] / lib / Apache / MVC / Model / CDBI.pm
1 package Apache::MVC::Model::CDBI;
2 use base 'Apache::MVC::Model::Base';
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 do_edit :Exported {
12     my ($self, $r) = @_;
13     my $h = CGI::Untaint->new(%{$r->{params}});
14     my $obj;
15     if (@{$r->{args}}) {
16         # We have something to edit
17         $obj = $self->retrieve($r->{args}[0]);
18         $obj->update_from_cgi($h);
19         warn "Updating an object ($obj) with ".Dumper($h); use Data::Dumper;
20     } else {
21         $obj = $self->create_from_cgi($h);
22     }
23     if (my %errors = $obj->cgi_update_errors) {
24         # Set it up as it was:
25         warn "There were errors: ".Dumper(\%errors)."\n";
26         $r->{template_args}{cgi_params} = \%params;
27         $r->{template_args}{errors} = \%errors;
28         $r->{template} = "edit";
29     } else {
30         $r->{template} = "view";
31     }
32     return $obj;
33 }
34
35 sub adopt {
36     my ($self, $child) = @_;
37     $child->autoupdate(1);
38     $child->columns( Stringify => qw/ name / );
39 }
40
41 1;