From fd44a844f551caa56dfaaacb4ba2c6e69d9a4157 Mon Sep 17 00:00:00 2001 From: Simon Cozens Date: Thu, 29 Jan 2004 14:10:53 +0000 Subject: [PATCH] This gives us the edit method. (Which also creates new things) git-svn-id: http://svn.maypole.perl.org/Maypole/trunk@16 48953598-375a-da11-a14b-00016c27c3ee --- lib/Apache/MVC.pm | 4 +++- lib/Apache/MVC/Model/Base.pm | 7 +++++++ lib/Apache/MVC/Model/CDBI.pm | 31 +++++++++++++++++++++++++++++++ 3 files changed, 41 insertions(+), 1 deletion(-) diff --git a/lib/Apache/MVC.pm b/lib/Apache/MVC.pm index 7869cfa..25a8adb 100644 --- a/lib/Apache/MVC.pm +++ b/lib/Apache/MVC.pm @@ -51,6 +51,8 @@ sub init { for my $subclass (@{$config->{classes}}) { no strict 'refs'; push @{$subclass."::ISA"}, $class->config->{model}; + $config->{model}->adopt($subclass) + if $config->{model}->can("adopt"); } $class->view_object($class->config->{view}->new); $class->init_done(1); @@ -97,7 +99,7 @@ sub parse_location { $self->{action} = shift @pi; $self->{args} = \@pi; - $self->{params} = $self->{ar}->content; + $self->{params} = { $self->{ar}->content }; } sub is_applicable { diff --git a/lib/Apache/MVC/Model/Base.pm b/lib/Apache/MVC/Model/Base.pm index 1fe178f..89feb7c 100644 --- a/lib/Apache/MVC/Model/Base.pm +++ b/lib/Apache/MVC/Model/Base.pm @@ -12,6 +12,13 @@ sub view :Exported { return $self->retrieve(shift @{$r->{args}}); } +sub edit :Exported { + my ($self, $r) = @_; + return $self->retrieve(shift @{$r->{args}}); +} + +sub do_edit { die "This is an abstract method" } + sub list :Exported { my ($self, $r) = @_; return $self->retrieve_all; diff --git a/lib/Apache/MVC/Model/CDBI.pm b/lib/Apache/MVC/Model/CDBI.pm index c06703a..f9918bc 100644 --- a/lib/Apache/MVC/Model/CDBI.pm +++ b/lib/Apache/MVC/Model/CDBI.pm @@ -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; -- 2.39.5