]> git.decadent.org.uk Git - maypole.git/blob - lib/Maypole/Model/CDBI/DFV.pm
fixed bug in DFV model when updating object
[maypole.git] / lib / Maypole / Model / CDBI / DFV.pm
1 package Maypole::Model::CDBI::DFV;
2 use strict;
3
4 =head1 NAME
5
6 Maypole::Model::CDBI::DFV - Class::DBI::DFV model for Maypole.
7
8 =head1 SYNOPSIS
9
10     package Foo;
11     use 'Maypole::Application';
12
13     Foo->config->model("Maypole::Model::CDBI::DFV");
14     Foo->setup([qw/ Foo::SomeTable Foo::Other::Table /]);
15
16     # Look ma, no untainting
17
18     sub Foo::SomeTable::SomeAction : Exported {
19
20         . . .
21
22     }
23
24 =head1 DESCRIPTION
25
26 This module allows you to use Maypole with previously set-up
27 L<Class::DBI> classes that use Class::DBI::DFV;
28
29 Simply call C<setup> with a list reference of the classes you're going to use,
30 and Maypole will work out the tables and set up the inheritance relationships
31 as normal.
32
33 Better still, it will also set use your DFV profile to validate input instead
34 of CGI::Untaint. For teh win!!
35
36 =cut
37
38 use Class::C3;
39 use Data::FormValidator;
40 use Data::Dumper;
41
42 use Maypole::Config;
43 use Maypole::Model::CDBI::AsForm;
44
45 use base qw(Maypole::Model::CDBI::Base);
46
47 Maypole::Config->mk_accessors(qw(table_to_class _COLUMN_INFO));
48
49 =head1 METHODS
50
51 =head2 setup
52
53   This method is inherited from Maypole::Model::Base and calls setup_database,
54   which uses Class::DBI::Loader to create and load Class::DBI classes from
55   the given database schema.
56
57 =head2 setup_database
58
59   This method loads the model classes for the application
60
61 =cut
62
63 sub setup_database {
64     my ( $self, $config, $namespace, $classes ) = @_;
65     $config->{classes}        = $classes;
66     foreach my $class (@$classes) {
67       $namespace->load_model_subclass($class);
68     }
69     $namespace->model_classes_loaded(1);
70     $config->{table_to_class} = { map { $_->table => $_ } @$classes };
71     $config->{tables}         = [ keys %{ $config->{table_to_class} } ];
72 }
73
74 =head2 class_of
75
76   returns class for given table
77
78 =cut
79
80 sub class_of {
81     my ( $self, $r, $table ) = @_;
82     return $r->config->{table_to_class}->{$table};
83 }
84
85 =head2 adopt
86
87 This class method is passed the name of a model class that represensts a table
88 and allows the master model class to do any set-up required.
89
90 =cut
91
92 sub adopt {
93     my ( $self, $child ) = @_;
94     if ( my $col = $child->stringify_column ) {
95         $child->columns( Stringify => $col );
96     }
97 }
98
99 =head2 check_params
100
101   Checks parameters against the DFV profile for the class, returns the results
102   of DFV's check.
103
104   my $dfv_results = __PACKAGE__->check_params($r->params);
105
106 =cut
107
108 sub check_params {
109   my ($class,$params) = @_;
110   return Data::FormValidator->check($params, $class->dfv_profile);
111 }
112
113
114 =head1 Action Methods
115
116 Action methods are methods that are accessed through web (or other public) interface.
117
118 Inherited from L<Maypole::Model::CDBI::Base> except do_edit (below)
119
120 =head2 do_edit
121
122 If there is an object in C<$r-E<gt>objects>, then it should be edited
123 with the parameters in C<$r-E<gt>params>; otherwise, a new object should
124 be created with those parameters, and put back into C<$r-E<gt>objects>.
125 The template should be changed to C<view>, or C<edit> if there were any
126 errors. A hash of errors will be passed to the template.
127
128 =cut
129
130 sub do_edit : Exported {
131   my ($class, $r, $obj) = @_;
132
133   my $config   = $r->config;
134   my $table    = $r->table;
135
136   # handle cancel button hit
137   if ( $r->params->{cancel} ) {
138     $r->template("list");
139     $r->objects( [$class->retrieve_all] );
140     return;
141   }
142
143
144   my $errors;
145   if ($obj) {
146     ($obj,$errors) = $class->_do_update($r,$obj);
147   } else {
148     ($obj,$errors) = $class->_do_create($r);
149   }
150
151   # handle errors, if none, proceed to view the newly created/updated object
152   if (ref $errors) {
153     # pass errors to template
154     $r->template_args->{errors} = $errors;
155     foreach my $error (keys %$errors) {
156       $r->template_args->{errors}{ucfirst($error)} = $errors->{$error}
157     }
158
159     # Set it up as it was:
160     $r->template_args->{cgi_params} = $r->params;
161     $r->template("edit");
162   } else {
163     $r->template("view");
164   }
165
166   $r->objects( $obj ? [$obj] : []);
167 }
168
169 sub _do_update {
170   my ($class,$r,$obj) = @_;
171   my $errors;
172   my $dfv_results = Data::FormValidator->check($r->{params}, $class->dfv_profile);
173
174   # handle dfv errors
175   if ( $dfv_results->has_missing ) {   # missing fields
176     foreach my $field ( $dfv_results->missing ) {
177       $errors->{$field} = "$field is required";
178     }
179   }
180   if ( $dfv_results->has_invalid ) {   # Print the name of invalid fields
181     foreach my $field ( $dfv_results->invalid ) {
182       $errors->{$field} =  "$field is invalid: " . $dfv_results->invalid( $field );
183     }
184   }
185
186   my $this_class_params = { map { $_ => $r->{params}{$_} }  $class->columns  };
187   delete $this_class_params->{route_id};
188
189   # update or make other related (must_have, might_have, has_many  etc )
190   unless ($errors) {
191     foreach my $accssr ( grep ( !(exists $this_class_params->{$_}) , keys %{$r->{params}} ) ) {
192       # get related object if it exists
193       my $rel_meta = $class->related_meta('r',$accssr);
194       if (!$rel_meta) {
195         $r->warn("[_do_update] No relationship for $accssr in " . ref($class));
196         next;
197       }
198
199       my $rel_type  = $rel_meta->{name};
200       my $fclass    = $rel_meta->{foreign_class};
201       my ($rel_obj,$errs);
202       $rel_obj = $fclass->retrieve($r->params->{$accssr});
203       # update or create related object
204       ($rel_obj, $errs) = ($rel_obj)
205         ? $fclass->_do_update($r, $rel_obj)
206           : $obj->_create_related($accssr, $r->params);
207       $errors->{$accssr} = $errs if ($errs);
208     }
209   }
210
211   unless ($errors) {
212     $obj->set( %$this_class_params );
213     $obj->update;
214   }
215
216   return ($obj,$errors);
217 }
218
219 sub _do_create {
220   my ($class,$r) = @_;
221   my $errors;
222   my $this_class_params = { map { $_ => $r->{params}{$_} }  $class->columns  };
223   my $obj;
224
225   my $dfv_results = Data::FormValidator->check($r->{params}, $class->dfv_profile);
226   if ($dfv_results->success) {
227     $obj = $class->create($this_class_params);
228   } else {
229     # handle dfv errors
230     if ( $dfv_results->has_missing ) {   # missing fields
231       foreach my $field ( $dfv_results->missing ) {
232         $errors->{$field} = "$field is required";
233       }
234     }
235     if ( $dfv_results->has_invalid ) {   # Print the name of invalid fields
236       foreach my $field ( $dfv_results->invalid ) {
237         $errors->{$field} =  "$field is invalid: " . $dfv_results->invalid( $field );
238       }
239     }
240   }
241
242   # Make other related (must_have, might_have, has_many  etc )
243   unless ($errors) {
244     foreach my $accssr ( grep ( !(exists $this_class_params->{$_}) , keys %{$r->{params}} ) ) {
245       my ($rel_obj, $errs) = $obj->_create_related($accssr, $r->{params}{$accssr});
246       $errors->{$accssr} = $errs if ($errs);
247     }
248   }
249   return ($obj,$errors);
250 }
251
252
253 sub _create_related {
254   # self is object or class, accssr is accssr to relationship, params are
255   # data for relobject, and created is the array ref to store objs
256   my ( $self, $accssr, $params )  = @_;
257   $self->_croak ("Can't make related object without a parent $self object") unless (ref $self);
258   my $created = [];
259   my $rel_meta = $self->related_meta('r',$accssr);
260   if (!$rel_meta) {
261     $self->_carp("[_create_related] No relationship for $accssr in " . ref($self));
262     return;
263   }
264
265   my $rel_type  = $rel_meta->{name};
266   my $fclass    = $rel_meta->{foreign_class};
267
268   my ($rel, $errs);
269
270   # Set up params for might_have, has_many, etc
271   if ($rel_type ne 'has_own' and $rel_type ne 'has_a') {
272     # Foreign Key meta data not very standardized in CDBI
273     my $fkey= $rel_meta->{args}{foreign_key} || $rel_meta->{foreign_column};
274     unless ($fkey) { die " Could not determine foreign key for $fclass"; }
275     my %data = (%$params, $fkey => $self->id);
276     %data = ( %data, %{$rel_meta->{args}->{constraint} || {}} );
277     ($rel, $errs) =  $fclass->_do_create(\%data);
278   }
279   else {
280     ($rel, $errs) =  $fclass->_do_create($params);
281     unless ($errs) {
282       $self->$accssr($rel->id);
283       $self->update;
284     }
285   }
286   return ($rel, $errs);
287 }
288
289
290 =head2 do_delete
291
292 Inherited from Maypole::Model::CDBI::Base.
293
294 This action deletes records
295
296 =head2 do_search
297
298 Inherited from Maypole::Model::CDBI::Base.
299
300 This action method searches for database records.
301
302 =head2 list
303
304 Inherited from Maypole::Model::CDBI::Base.
305
306 The C<list> method fills C<$r-E<gt>objects> with all of the
307 objects in the class. The results are paged using a pager.
308
309 =cut
310
311 sub _column_info {
312   my $class = shift;
313
314   # get COLUMN INFO from DB
315   $class->SUPER::_column_info() unless (ref $class->COLUMN_INFO);
316
317   # update with required columns from DFV Profile
318   my $profile = $class->dfv_profile;
319   $class->required_columns($profile->{required});
320
321   return $class->COLUMN_INFO;
322 }
323
324
325
326 =head1 SEE ALSO
327
328 L<Maypole::Model::Base>
329
330 L<Maypole::Model::CDBI::Base>
331
332 =head1 AUTHOR
333
334 Aaron Trevena.
335
336 =head1 LICENSE
337
338 You may distribute this code under the same terms as Perl itself.
339
340 =cut
341
342 1;
343
344