]> git.decadent.org.uk Git - maypole.git/blob - lib/Apache/MVC/Model/CDBI.pm
Link to has-many objects in the view page.
[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 Lingua::EN::Inflect::Number qw(to_PL);
4 use Class::DBI::AsForm;
5 use Class::DBI::FromCGI;
6 use CGI::Untaint;
7
8 sub description { "A poorly defined class" }
9
10 sub column_names { my $class = shift; map { $_ => ucfirst $_ } $class->columns }
11
12 sub get_objects {
13     my ($self, $r) = @_;
14     return $self->retrieve(shift @{$r->{args}});
15 }
16
17 sub related {
18     my ($self, $r) = @_;
19     # Has-many methods; XXX this is a hack
20     map {to_PL($_)} 
21     grep { exists $r->{config}{ok_tables}{$_} }
22     map {$_->table}
23     keys %{shift->__hasa_list}
24 }
25
26 sub do_edit :Exported {
27     my ($self, $r) = @_;
28     my $h = CGI::Untaint->new(%{$r->{params}});
29     my ($obj) = @{$self->objects};
30     if ($obj) {
31         # We have something to edit
32         $obj->update_from_cgi($h);
33         warn "Updating an object ($obj) with ".Dumper($h); use Data::Dumper;
34     } else {
35         $obj = $self->create_from_cgi($h);
36     }
37     if (my %errors = $obj->cgi_update_errors) {
38         # Set it up as it was:
39         warn "There were errors: ".Dumper(\%errors)."\n";
40         $r->{template_args}{cgi_params} = \%params;
41         $r->{template_args}{errors} = \%errors;
42         $r->{template} = "edit";
43     } else {
44         $r->{template} = "view";
45     }
46     $r->objects([ $obj ]);
47 }
48
49 sub delete :Exported {
50     my ($self, $r) = @_;
51     $_->SUPER::delete for @{ $r->objects };
52     $r->objects([ $self->retrieve_all ]);
53     $r->{template} = "list";
54 }
55
56 sub adopt {
57     my ($self, $child) = @_;
58     $child->autoupdate(1);
59     $child->columns( Stringify => qw/ name / );
60 }
61
62 1;