]> git.decadent.org.uk Git - maypole.git/blobdiff - lib/Apache/MVC/Model/CDBI.pm
This gives us the edit method. (Which also creates new things)
[maypole.git] / lib / Apache / MVC / Model / CDBI.pm
index c06703a1fb433d0d6262df880670f28bfa62fc89..f9918bc9b84cf1b5f93019d204fd5b21bd8aff54 100644 (file)
@@ -2,9 +2,40 @@ package Apache::MVC::Model::CDBI;
 use base 'Apache::MVC::Model::Base';
 use Class::DBI::AsForm;
 use Class::DBI::FromCGI;
+use CGI::Untaint;
 
 sub description { "A poorly defined class" }
 
 sub column_names { my $class = shift; map { $_ => ucfirst $_ } $class->columns }
 
+sub do_edit :Exported {
+    my ($self, $r) = @_;
+    my $h = CGI::Untaint->new(%{$r->{params}});
+    my $obj;
+    if (@{$r->{args}}) {
+        # We have something to edit
+        $obj = $self->retrieve($r->{args}[0]);
+        $obj->update_from_cgi($h);
+        warn "Updating an object ($obj) with ".Dumper($h); use Data::Dumper;
+    } else {
+        $obj = $self->create_from_cgi($h);
+    }
+    if (my %errors = $obj->cgi_update_errors) {
+        # Set it up as it was:
+        warn "There were errors: ".Dumper(\%errors)."\n";
+        $r->{template_args}{cgi_params} = \%params;
+        $r->{template_args}{errors} = \%errors;
+        $r->{template} = "edit";
+    } else {
+        $r->{template} = "view";
+    }
+    return $obj;
+}
+
+sub adopt {
+    my ($self, $child) = @_;
+    $child->autoupdate(1);
+    $child->columns( Stringify => qw/ name / );
+}
+
 1;