Apache-Voodoo
view release on metacpan or search on metacpan
lib/Apache/Voodoo/Table.pm view on Meta::CPAN
push(@{$self->{'add_details'}},[$_,'',$values->{$_}]);
}
if ($self->{'pkey_user_supplied'}) {
$c .= ",".$self->{'pkey'};
$q .= ",?";
push(@v,$params->{$self->{'pkey'}});
}
my $insert_statement = "INSERT INTO $self->{'table'} ($c) VALUES ($q)";
$dbh->do($insert_statement, undef, @v);
$self->{'success'} = 1;
return 1;
}
}
# populate drop downs (also maintaining previous state).
foreach (@{$self->{'references'}}) {
my $query = "SELECT
$_->{'pkey'},
$_->{'slabel'}
FROM
$_->{'table'}
$_->{'sextra'}";
my $res = $dbh->selectall_arrayref($query);
$errors->{$_->{'fkey'}} = $self->prep_select($res,$errors->{$_->{'fkey'}} || $_->{'sdefault'});
}
# If we get here the user is just loading the page
# for the first time or had errors.
return $errors;
}
sub edit {
my $self = shift;
my $p = shift;
my $additional_constraint = shift;
$self->{'success'} = 0;
$self->{'edit_details'} = [];
my $dbh = $p->{'dbh'};
my $params = $p->{'params'};
# make sure our additional constraint won't break the sql
$additional_constraint =~ s/^\s*(where|and|or)\s+//go;
if (length($additional_constraint)) {
$additional_constraint = "AND $additional_constraint";
}
unless ($params->{$self->{'pkey'}} =~ /$self->{'pkey_regexp'}/) {
return $self->display_error("Invalid ID");
}
# find the record to be updated
my $res = $dbh->selectall_arrayref("
SELECT ".
join(",",@{$self->{'columns'}}). "
FROM
$self->{'table'}
WHERE
$self->{'pkey'} = ?
$additional_constraint",
undef,
$params->{$self->{'pkey'}});
unless (defined($res->[0])) {
return $self->display_error("No record with that ID found");
}
my %original_values;
for (my $i=0; $i <= $#{$self->{'columns'}}; $i++) {
$original_values{$self->{'columns'}->[$i]} = $res->[0]->[$i];
}
my $errors = {};
if ($params->{'cm'} eq "update") {
my ($values,$errors) = $self->validate_edit($p);
if (scalar keys %{$errors}) {
$errors->{'has_errors'} = 1;
# copy values into template
$errors->{$self->{'pkey'}} = $params->{$self->{'pkey'}};
foreach (keys(%{$values})) {
$errors->{$_} = $values->{$_};
}
}
else {
# copy clean dates,times into params for insertion
foreach (@{$self->{'dates'}},@{$self->{'times'}}) {
$values->{$_->{'name'}} = $values->{$_->{'name'}."_CLEAN"};
}
# let's figure out what they changed so caller can do something with that info if they want
foreach (@{$self->{'columns'}}) {
if ($values->{$_} ne $original_values{$_}) {
push(@{$self->{'edit_details'}},[$_,$original_values{$_},$values->{$_}]);
}
}
my $update_statement = "
UPDATE
$self->{'table'}
SET ".
join("=?,",@{$self->{'columns'}})."=?
WHERE
$self->{'pkey'} = ?
$additional_constraint";
# $self->debug($update_statement);
# $self->debug((map {$values->{$_}} @{$self->{'columns'}}),$params->{$self->{'pkey'}});
$dbh->do($update_statement,
undef,
(map { $values->{$_} } @{$self->{'columns'}}),
( run in 2.547 seconds using v1.01-cache-2.11-cpan-39bf76dae61 )