]> git.decadent.org.uk Git - maypole.git/blob - lib/Maypole/Model/CDBI.pm
Do list properly after delete.
[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     } else {
43         $obj = $self->create_from_cgi($h);
44     }
45     if (my %errors = $obj->cgi_update_errors) {
46         # Set it up as it was:
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     return shift->SUPER::delete(@_) if caller ne "Maypole::Model::Base";
58     my ($self, $r) = @_;
59     $_->SUPER::delete for @{ $r->objects || [] };
60     $r->objects([ $self->retrieve_all ]);
61     $r->{template} = "list";
62     $self->list;
63 }
64
65 sub stringify_column {
66     my $class = shift;
67     return ($class->columns("Stringify"),
68                 (grep { $_ ne "id" } $class->primary_columns),
69                 (grep { $_ eq "name" } $class->columns)
70                )[0];
71 }
72
73 sub adopt {
74     my ($self, $child) = @_;
75     $child->autoupdate(1);
76     if (my $col = $child->stringify_column) {
77         $child->columns( Stringify => $col );
78     }
79 }
80
81 sub search :Exported {
82     return shift->SUPER::search(@_) if caller ne "Maypole::Model::Base";
83                                     # A real CDBI search.
84     my ($self, $r) = @_;
85     my %fields = map {$_ => 1 } $self->columns;
86     my $oper = "like"; # For now
87     my %params = %{$r->{params}};
88     my %values = map { $_ => {$oper, $params{$_} } }
89                  grep { $params{$_} and $fields{$_} } keys %params;
90
91     $self = $self->do_pager($r);
92     $r->objects([ %values ? $self->search_where(%values) : $self->retrieve_all ]);
93     $r->template("list");
94     $r->{template_args}{search} = 1;
95 }
96
97 sub do_pager {
98     my ($self, $r) = @_;
99     if ( my $rows = $r->config->{rows_per_page}) {
100         return $r->{template_args}{pager} = $self->pager($rows, $r->query->{page});
101     } else { return $self } 
102 }
103
104 sub list :Exported {
105     my ($self, $r) = @_;
106     my %ok_columns = map {$_ => 1} $self->columns;
107     $self = $self->do_pager($r);
108     my $order;
109     if ($order = $r->query->{order} and $ok_columns{$order}) {
110         $r->objects([ $self->retrieve_all_sorted_by( $order.
111             ($r->query->{o2} eq "desc" && " DESC")
112         )]);
113     } else {
114         $r->objects([ $self->retrieve_all ]);
115     }
116 }
117
118 sub setup_database {
119     my ($self, $config, $namespace, $dsn, $u, $p) = @_;
120     $config->{dsn} = $dsn;
121     $config->{loader} = Class::DBI::Loader->new(
122         namespace => $namespace,
123         dsn => $dsn,
124         user => $u,
125         password => $p,
126     );
127     $config->{classes} = [ $config->{loader}->classes ];
128     $config->{tables}  = [ $config->{loader}->tables ];
129 }
130
131 sub class_of {
132     my ($self, $r, $table) = @_;
133     return $r->config->{loader}->_table2class($table);
134 }
135
136 1;
137