1 package Maypole::Model::CDBI;
2 use base qw(Maypole::Model::Base Class::DBI);
3 use Class::DBI::AsForm;
4 use Class::DBI::FromCGI;
5 use Class::DBI::Loader;
6 use Class::DBI::AbstractSearch;
7 use Class::DBI::Plugin::RetrieveAll;
10 use Lingua::EN::Inflect::Number qw(to_PL);
16 Maypole::Model::CDBI - Model class based on Class::DBI
20 This is a master model class which uses C<Class::DBI> to do all the hard
21 work of fetching rows and representing them as objects. It is a good
22 model to copy if you're replacing it with other database abstraction
28 my ( $self, $r ) = @_;
29 return keys %{ $self->meta_info('has_many') || {} };
32 sub do_edit : Exported {
33 my ( $self, $r ) = @_;
34 my $h = CGI::Untaint->new( %{ $r->{params} } );
36 my ($obj) = @{ $r->objects || [] };
39 # We have something to edit
40 $obj->update_from_cgi( $h =>
41 { required => $r->{config}{ $r->{table} }{required_cols} || [], }
46 $self->create_from_cgi( $h =>
47 { required => $r->{config}{ $r->{table} }{required_cols} || [], }
51 if ( my %errors = $obj->cgi_update_errors ) {
53 # Set it up as it was:
54 $r->{template_args}{cgi_params} = $r->{params};
55 $r->{template_args}{errors} = \%errors;
56 $r->{template} = "edit";
57 undef $obj if $creating; # Couldn't create
60 $r->{template} = "view";
62 $r->objects( [$obj] );
65 sub delete : Exported {
66 return shift->SUPER::delete(@_) if caller ne "Maypole::Model::Base";
67 my ( $self, $r ) = @_;
68 $_->SUPER::delete for @{ $r->objects || [] };
69 $r->objects( [ $self->retrieve_all ] );
70 $r->{template} = "list";
74 sub stringify_column {
77 $class->columns("Stringify"),
78 ( grep { /(name|title)/i } $class->columns ),
79 ( grep { !/id$/i } $class->primary_columns ),
84 my ( $self, $child ) = @_;
85 $child->autoupdate(1);
86 if ( my $col = $child->stringify_column ) {
87 $child->columns( Stringify => $col );
91 sub search : Exported {
92 return shift->SUPER::search(@_) if caller ne "Maypole::Model::Base";
95 my ( $self, $r ) = @_;
96 my %fields = map { $_ => 1 } $self->columns;
97 my $oper = "like"; # For now
98 my %params = %{ $r->{params} };
99 my %values = map { $_ => { $oper, $params{$_} } }
100 grep { $params{$_} and $fields{$_} } keys %params;
102 $r->template("list");
103 if ( !%values ) { return $self->list($r) }
104 my $order = $self->order($r);
105 $self = $self->do_pager($r);
108 $self->search_where( \%values ),
109 ( $order ? { order => $order } : () )
112 $r->{template_args}{search} = 1;
116 my ( $self, $r ) = @_;
117 if ( my $rows = $r->config->rows_per_page ) {
118 return $r->{template_args}{pager} =
119 $self->pager( $rows, $r->query->{page} );
121 else { return $self }
125 my ( $self, $r ) = @_;
127 my %ok_columns = map { $_ => 1 } $self->columns;
128 if ( $order = $r->query->{order} and $ok_columns{$order} ) {
129 $order .= ( $r->query->{o2} eq "desc" && " DESC" );
134 sub list : Exported {
135 my ( $self, $r ) = @_;
136 my $order = $self->order($r);
137 $self = $self->do_pager($r);
139 $r->objects( [ $self->retrieve_all_sorted_by($order) ] );
142 $r->objects( [ $self->retrieve_all ] );
147 my ($self, $config, $namespace, $dsn, $u, $p, $opts) = @_;
148 $dsn ||= $config->dsn;
149 $u ||= $config->user;
150 $p ||= $config->pass;
151 $opts ||= $config->opts;
153 $config->loader || $config->loader( Class::DBI::Loader->new(
154 namespace => $namespace,
160 $config->{classes} = [ $config->{loader}->classes ];
161 $config->{tables} = [ $config->{loader}->tables ];
165 my ( $self, $r, $table ) = @_;
166 return $r->config->loader->_table2class($table);