Apache-Voodoo
view release on metacpan or search on metacpan
lib/Apache/Voodoo.pm view on Meta::CPAN
push(@return,"$key=$o->{$key}") if length($o->{$key});
}
}
return join("&",@return);
}
sub prep_select {
my $self = shift;
my $list = shift;
my $select = shift;
unless (ref($select)) {
$select = [ $select ];
}
my %selected = map { $_ => 1 } @{$select};
return [
map {
{
"ID" => $_->[0],
"ID." . $_->[0] => 1,
"NAME" => $_->[1],
"NAME." . $_->[1] => 1,
"SELECTED" => (defined $selected{$_->[0]})?'SELECTED':0
}
} @{$list}
];
}
sub safe_text {
# return $_[1] =~ /^[\w\s\.\,\/\[\]\{\}\+\=\-\(\)\:\;\&\?\*\'\!]*$/;
return $_[1] =~ /^[\w\s\.\,\/\[\]\{\}\+\=\-\(\)\:\;\&\?\!\*]*$/;
}
sub sanitize_text {
my $self = shift;
my $text = shift;
# return $_[1] =~ /^[\w\s\.\,\/\[\]\{\}\+\=\-\(\)\:\;\&\?\*\'\!]*$/;
$text =~ s/[^\w\s\.\,\/\[\]\{\}\+\=\-\(\)\:\;\&\?\!\*]/ /g;
return $text;
}
sub trim {
my $self = shift;
my $param = shift || "";
$param =~ s/^\s*//o;
$param =~ s/\s*$//o;
return $param;
}
################################################################################
# Database Interaction
################################################################################
# deprecated, dbi uses exceptions now.
sub db_error {
my @caller = caller(1);
my $query = $DBI::lasth->{'Statement'};
$query = join("\n", map { $_ =~ s/^\s*//; $_} split(/\n/,$query));
my $errstr = "\n";
$errstr .= "==================== DB ERROR ====================\n";
$errstr .= "TIME: ". scalar(localtime) . "\n";
$errstr .= "PACKAGE: $caller[0]\n";
$errstr .= "FILE: $caller[1]\n";
$errstr .= "SUBROUTINE: $caller[3]\n";
$errstr .= "LINE: $caller[2]\n\n";
$errstr .= "$DBI::errstr\n";
$errstr .= "===================== QUERY ======================\n";
$errstr .= "$query\n";
$errstr .= "==================================================\n";
# don't really care for this, but there doesn't seem to be any way to
# terminate this request.
die $errstr;
}
sub date_to_sql {
my $self = shift;
my $date = shift;
# Get rid of all spaces in the date
$date =~ s/\s//go;
# date missing. return null;
return undef unless (length($date));
# Split the date up into month day year
my ($m,$d,$y) = split(/[\/-]/,$date,3);
# assume two digit years belong in 2000
if ($y < 1000) { $y += 2000; }
return sprintf("%04d-%02d-%02d",$y,$m,$d);
}
sub last_insert_id {
my $self = shift;
my $dbh = shift;
my $res = $dbh->selectall_arrayref("SELECT LAST_INSERT_ID()") || $self->db_error();
return $res->[0]->[0];
}
# this sub is for use with the callback structure of Apache::Voodoo::Table.
# $params is injected with a arrayref of column to translate
#
# since $params is a reference, the actual columns as seen by the db
# are added to $params and they get back out that way.
# all return values are just error messages (if any)
sub month_year_to_sql {
my $self = shift;
my $conn = shift;
my $params = shift;
( run in 2.444 seconds using v1.01-cache-2.11-cpan-8f98c5d2c55 )