package Apache::MVC::Model::CDBI;
use base qw(Apache::MVC::Model::Base Class::DBI);
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 do_edit :Exported {
    my ($self, $r) = @_;
    my $h = CGI::Untaint->new(%{$r->{params}});
    my ($obj) = @{$self->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;
