]> git.decadent.org.uk Git - maypole.git/blob - lib/Apache/MVC/Model/CDBI.pm
And now we have paging support. (And some better docs)
[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 Class::DBI::AbstractSearch;
7 use Class::DBI::Pager;
8 use CGI::Untaint;
9 use strict;
10
11 =head1 NAME
12
13 Apache::MVC::Model::CDBI - Model class based on Class::DBI
14
15 =head1 DESCRIPTION
16
17 This is a master model class which uses C<Class::DBI> to do all the hard
18 work of fetching rows and representing them as objects. It is a good
19 model to copy if you're replacing it with other database abstraction
20 modules.
21
22 =cut
23
24 sub related {
25     my ($self, $r) = @_;
26     # Has-many methods; XXX this is a hack
27     map {to_PL($_)} 
28     grep { exists $r->{config}{ok_tables}{$_} }
29     map {$_->table}
30     keys %{shift->__hasa_list || {}}
31 }
32
33 sub do_edit :Exported {
34     my ($self, $r) = @_;
35     my $h = CGI::Untaint->new(%{$r->{params}});
36     my ($obj) = @{$r->objects};
37     if ($obj) {
38         # We have something to edit
39         $obj->update_from_cgi($h);
40         warn "Updating an object ($obj) with ".Dumper($h); use Data::Dumper;
41     } else {
42         $obj = $self->create_from_cgi($h);
43     }
44     if (my %errors = $obj->cgi_update_errors) {
45         # Set it up as it was:
46         warn "There were errors: ".Dumper(\%errors)."\n";
47         $r->{template_args}{cgi_params} = $r->{params};
48         $r->{template_args}{errors} = \%errors;
49         $r->{template} = "edit";
50     } else {
51         $r->{template} = "view";
52     }
53     $r->objects([ $obj ]);
54 }
55
56 sub delete :Exported {
57     my ($self, $r) = @_;
58     $_->SUPER::delete for @{ $r->objects };
59     $r->objects([ $self->retrieve_all ]);
60     $r->{template} = "list";
61 }
62
63 sub adopt {
64     my ($self, $child) = @_;
65     $child->autoupdate(1);
66     $child->columns( Stringify => qw/ name / );
67 }
68
69 sub search :Exported {
70     return shift->SUPER::search(@_) if caller eq "Class::DBI"; # oops
71     my ($self, $r) = @_;
72     my %fields = map {$_ => 1 } $self->columns;
73     my $oper = "like"; # For now
74     use Carp; Carp::confess("Urgh") unless ref $r;
75     my %params = %{$r->{params}};
76     my %values = map { $_ => {$oper, $params{$_} } }
77                  grep { $params{$_} and $fields{$_} } keys %params;
78
79     $r->objects([ %values ? $self->search_where(%values) : $self->retrieve_all ]);
80     $r->template("list");
81     $r->{template_args}{search} = 1;
82 }
83
84 sub list :Exported {
85     my ($self, $r) = @_;
86     if ( my $rows = $r->config->{rows_per_page}) {
87         $self = $self->pager($rows, $r->query->{page});
88         $r->{template_args}{pager} = $self;
89     } 
90     $r->objects([ $self->retrieve_all ]);
91 }
92 1;
93