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