]> git.decadent.org.uk Git - maypole.git/blobdiff - lib/Apache/MVC/Model/CDBI.pm
Make untainting, editing, and other things work.
[maypole.git] / lib / Apache / MVC / Model / CDBI.pm
index 0a0e92384c7552980cf70b66d22cb6db63d4fb19..695e1cbd7dec1fe0e6e6f9ed33863113df0b2c90 100644 (file)
@@ -1,4 +1,62 @@
 package Apache::MVC::Model::CDBI;
-use base 'Apache::MVC::Model::Base';
+use base qw(Apache::MVC::Model::Base Class::DBI);
+use Lingua::EN::Inflect::Number qw(to_PL);
+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 get_objects {
+    my ($self, $r) = @_;
+    return $self->retrieve(shift @{$r->{args}});
+}
+
+sub related {
+    my ($self, $r) = @_;
+    # Has-many methods; XXX this is a hack
+    map {to_PL($_)} 
+    grep { exists $r->{config}{ok_tables}{$_} }
+    map {$_->table}
+    keys %{shift->__hasa_list || {}}
+}
+
+sub do_edit :Exported {
+    my ($self, $r) = @_;
+    my $h = CGI::Untaint->new(%{$r->{params}});
+    my ($obj) = @{$r->objects};
+    if ($obj) {
+        # We have something to edit
+        $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";
+    }
+    $r->objects([ $obj ]);
+}
+
+sub delete :Exported {
+    my ($self, $r) = @_;
+    $_->SUPER::delete for @{ $r->objects };
+    $r->objects([ $self->retrieve_all ]);
+    $r->{template} = "list";
+}
+
+sub adopt {
+    my ($self, $child) = @_;
+    $child->autoupdate(1);
+    $child->columns( Stringify => qw/ name / );
+}
 
 1;