Gossamer Forum
Home : General : Perl Programming :

problem passing a variable to sub

Quote Reply
problem passing a variable to sub
i have a subroutine that works fine when i am modifying or adding a single record. but when i select modify multiple, it hangs up. i need to look at each date field for each record that is being modified. i can't figure out how to pass those variables. here's the code for the multiple routine:

Code:
sub modify_multi_records {
#----------------------------------------------------------------------
# Modify multiple records
#
my $self = shift;
return $self->home($self->_language('PER_MOD')) unless( $self->{user}->{modify_p} );

#------------demo code----------------

# Format the cgi for searching
$self->format_search_cgi;

# Hash to handle errors if there are any errors.
my $errors = {};
my $errcode = {};

# Need to know the names of the columns for this Table.
my @columns = keys %{$self->{db}->cols};
# Need to know the number of records modified
my $rec_modified = 0;

# For through the record numbers. These are the values of the
# check boxes
my $modify = ( ref $self->{cgi}->{modify} eq 'ARRAY' ) ? $self->{cgi}->{modify} : [$self->{cgi}->{modify}];

return $self->modify_search_form($self->_language('MUL_MOD_SUC',0)) if ( !$self->{cgi}->{modify} );

my $pk;
# Check if users can delete only their own records
# 06/23/2011 add Unrestricted status
# if ( $self->{cfg}->{'auth_modify_own'} and $self->{cfg}->{'auth_user_field'} and !$self->{user}->{admin_p} ) {
if ( $self->{cfg}->{'auth_modify_own'} and $self->{cfg}->{'auth_user_field'}) {
unless ( $self->{user}->{admin_p} || ($self->{user}->{'Status'} eq 'Unrestricted')) {
$pk = $self->{db}->{schema}->{pk};
( ref $pk ) or return $self->modify_form ($self->_language('ERR_PK'));
}
} # end if modify own
# Setup the language for GT::SQL.
local $GT::SQL::ERRORS->{ILLEGALVAL} = $self->_language('ADD_ILLEGALVAL');
local $GT::SQL::ERRORS->{UNIQUE} = $self->_language('ADD_UNIQUE');
local $GT::SQL::ERRORS->{NOTNULL} = $self->_language('ADD_NOTNULL');

foreach my $rec_num ( @$modify ) {

# The hash ref, we need, to modify a record.
my $change = {};

# Check if users can delete only their own records
# 06/23/2011 add Unrestricted status
# if ( $self->{cfg}->{'auth_modify_own'} and $self->{cfg}->{'auth_user_field'} and !$self->{user}->{admin_p} ) {
if ( $self->{cfg}->{'auth_modify_own'} and $self->{cfg}->{'auth_user_field'}) {
unless ( $self->{user}->{admin_p} || ($self->{user}->{'Status'} eq 'Unrestricted')) {
my $cond;
%$cond = map {$_ => $self->{cgi}->{"$rec_num-$_"}} @$pk;
my $result = $self->{db}->get($cond);
( $result ) or return $self->modify_search_form($self->_language('SRC_NOTFOUND'));

my $userid = $result->{$self->{cfg}->{'auth_user_field'}};
($userid eq $self->{user}->{'Username'}) or return $self->modify_form($self->_language('ERR_MODIFY_OWN'));

# 7/24/2010 don't reset username field on modify
# $self->{cgi}->{"$rec_num-$self->{cfg}->{'auth_user_field'}"} = $self->{user}->{Username}
}


# For through the column names to build our modification hash
foreach my $column ( @columns ) {
$change->{$column} = $self->{cgi}->{"$rec_num-$column"} if exists $self->{cgi}->{"$rec_num-$column"};
}

### i think this is where my new code should go?
### my ($msg,$tmp) = mysub($??????);
### more stuff depending on $msg

# Make the changes and capture any errors.
my $ret = $self->{db}->modify($change);
if ( defined ($ret) ) {
$rec_modified++;
}
else {
if ( $GT::SQL::error ){
my $error = $GT::SQL::error;
$error =~ s/\n/<br>\n<li>/g;
$errors->{$rec_num} = "<li>$error";
}
$errcode->{$rec_num} = $GT::SQL::errcode if ( $GT::SQL::errcode );
}
}
} # end modify own?

# Return the results page with the proper arguments depending on if we got an error or not.
return ( keys %{$errors} ) ? $self->modify_multi_results($rec_modified, $errors, $errcode) : $self->modify_multi_results($rec_modified);
}
Quote Reply
Re: [delicia] problem passing a variable to sub In reply to
maybe i've found a better way to do it. there's a sub that checks values for add/modify record. but all it does is return an error if the value is empty and is set NOT NULL or if the record has a regex and it doesn't match. if i can get the sub to actually change a value, that would work for me!

here's the line that calls the sub:
$self->_check_value ($col, $cols{$col}, $set->{$col});

here's the sub:

Code:
sub _check_value {
# -------------------------------------------------------------------
# Checks to see if a value is valid.
#
my ($self, $name, $column, $value) = @_;

my ($regex);
if ($column->{not_null} and not defined $value) {
$self->error ('ILLEGALVAL', 'WARN', $column->{form_display} || $name, $value);
}
if (($column->{type} eq 'ENUM') and $value) {
foreach (@{$column->{values}}) { $regex .= quotemeta($_) . "|"; }
chop $regex;
$regex = '^' . $regex . '$';
}
else {
$regex = $column->{regex} || '';
}

# 06/27/2011 allow blank entries
# only test regex if not_null
if (!$column->{not_null} && !$value) {
return 1;
}

if ($regex) {
if (! ref $value) {
eval {
if ($value !~ /$regex/) {
$self->error ("ILLEGALVAL", 'WARN', $column->{form_display} || $name, $value);
}
1;
} or $self->error ("REGEXFAIL", 'WARN', $regex);
}
}
if (ref $self->{_error} and @{$self->{_error}}) {
$GT::SQL::error = join "\n", @{$self->{_error}};
return;
}

#### THIS IS MY CODE
#### I NEED $value set to $tmp
if (($column->{type} eq 'DATE') and $value) {
unless ($value eq '0000-00-00') {
my ($msg,$tmp) = universal_date($value);
if ($msg eq 'ok') { $value = $tmp; return 1; }
if ($msg eq 'bad') {
$self->error ("ILLEGALVAL", 'WARN', $column->{form_display} || $name, $value);
return;
}
}
}
###
return 1;
}