Apache-Voodoo
view release on metacpan or search on metacpan
lib/Apache/Voodoo/Table.pm view on Meta::CPAN
my $s = 'search_' .$field;
my $o = 'search_op_'.$field;
next unless defined($params->{$s});
if (defined($params->{$o})) {
push(@search,[$field,$params->{$o},$params->{$s}]);
}
elsif ($params->{$s} =~ /^\d+$/) {
push(@search,[$field,'=',$params->{$s}]);
}
else {
push(@search,[$field,'like',$params->{$s}]);
}
}
return @search;
};
return $self;
}
sub set_configuration {
my $self = shift;
my $conf = shift;
my @errors;
if (!defined($conf->{'table'})) {
push(@errors,"missing table name");
}
elsif ($conf->{'table'} !~ /^[a-z_]\w*$/) {
push(@errors,"bad table name");
}
else {
$self->{'table'} = $conf->{'table'};
}
if (!defined($conf->{'primary_key'})) {
push(@errors,"missing primary key");
}
elsif ($conf->{'primary_key'} !~ /^[a-z_]\w*$/) {
push(@errors,"bad primary key");
}
else {
$self->{'pkey'} = $conf->{'primary_key'};
}
$self->{'pkey_regexp'} = ($conf->{'primary_key_regexp'})?$conf->{'primary_key_regexp'}:'^\d+$';
$self->{'pkey_user_supplied'} = ($conf->{'primary_key_user_supplied'})?1:0;
eval {
$self->{valid} = Apache::Voodoo::Validate->new($conf->{'columns'});
};
if (my $e = Apache::Voodoo::Exception::RunTime::BadConfig->caught()) {
# FIXME hack! need to figure out to store the list of errors as a data structure and override the stringification operation.
my (undef,@e) = split(/\n\t/,"$e");
push(@errors,@e);
}
elsif ($@) {
ref($@)?
$@->rethrow:
Apache::Voodoo::Exception::RunTime->throw($@);
}
$self->{'column_names'} = {};
while (my ($name,$conf) = each %{$conf->{'columns'}}) {
if (defined($conf->{'multiple'})) {
push(@errors,"Column $name allows multiple values but Apache::Voodoo::Table can't handle that currently.");
}
if (defined($conf->{'unique'})) {
push(@{$self->{'unique'}},$name);
}
# keep a local list of column names for query construction.
if (defined($self->{'pkey'}) && $name ne $self->{'pkey'}) {
push(@{$self->{'columns'}},$name);
$self->{'column_names'}->{$self->{'table'}.'.'.$name} = 1;
}
if ($conf->{'type'} eq "date") { push(@{$self->{dates}},$name); }
if ($conf->{'type'} eq "time") { push(@{$self->{times}},$name); }
if (defined($conf->{'references'})) {
my %v;
$v{'fkey'} = $name;
$v{'table'} = $conf->{'references'}->{'table'};
$v{'pkey'} = $conf->{'references'}->{'primary_key'};
$v{'columns'} = $conf->{'references'}->{'columns'};
$v{'slabel'} = $conf->{'references'}->{'select_label'};
$v{'sdefault'} = $conf->{'references'}->{'select_default'};
$v{'sextra'} = $conf->{'references'}->{'select_extra'};
push(@errors,"no table in reference for $name") unless $v{'table'} =~ /\w+/;
push(@errors,"no primary key in reference for $name") unless $v{'pkey'} =~ /\w+/;
push(@errors,"no label for select list in reference for $name") unless $v{'slabel'} =~ /\w+/;
if (defined($v{'columns'})) {
if (ref($v{'columns'})) {
if (ref($v{'columns'}) ne "ARRAY") {
push(@errors,"references => column must either be a scalar or arrayref for $name");
}
}
else {
$v{'columns'} = [ $v{'columns'} ];
}
}
else {
push(@errors,"references => columns must be defined for $name");
}
push(@{$self->{'references'}},\%v);
}
}
$self->{'default_sort'} = $conf->{'list_options'}->{'default_sort'};
while (my ($k,$v) = each %{$conf->{'list_options'}->{'sort'}}) {
$self->{'list_sort'}->{$k} = (ref($v) eq "ARRAY")? join(", ",@{$v}) : $v;
}
foreach (@{$conf->{'list_options'}->{'search'}}) {
push(@{$self->{'list_search_items'}},[$_->[1],$_->[0]]);
$self->{'list_search'}->{$_->[1]} = 1;
}
if ($conf->{'list_options'}->{'group_by'}) {
$self->{'group_by'} = $conf->{'list_options'}->{'group_by'};
$self->{'group_by'} = $conf->{'table'}.".".$self->{'group_by'} unless ($self->{'group_by'} =~ /\./);
}
$self->{'joins'} = [];
$self->{'list_joins'} = [];
$self->{'view_joins'} = [];
if (ref($conf->{'joins'}) eq "ARRAY") {
foreach my $j (@{$conf->{'joins'}}) {
$j->{'columns'} ||= [];
foreach (@{$j->{'columns'}}) {
$self->{'column_names'}->{$j->{'table'}.'.'.$_} = 1;
}
my $context = lc($j->{'context'}) || '';
$context = ($context =~ /^(list|view)$/i)?$context."_":'';
push(@{$self->{$context.'joins'}},
{
table => $j->{'table'},
type => $j->{'type'} || 'LEFT',
pkey => $j->{'primary_key'},
fkey => $j->{'foreign_key'},
columns => $j->{'columns'},
extra => $j->{'extra'}
}
);
}
}
if ($conf->{'pager'}) {
$self->{'pager'} = $conf->{'pager'};
}
else {
$self->{'pager'} = Apache::Voodoo::Pager->new();
# setup the pagination options
$self->{'pager'}->set_configuration(
'count' => 40,
'window' => 10,
'persist' => [
'pattern',
'limit',
'sort',
'last_sort',
'desc',
@{$conf->{'list_options'}->{'persist'} || []}
]
);
}
if (@errors) {
Apache::Voodoo::Exception::RunTime::BadConfig->throw("Configuration Errors:\n\t".join("\n\t",@errors));
}
}
sub table {
my $self = shift;
if ($_[0]) {
$self->{'table'} = $_[0];
}
return $self->{'table'};
}
sub success {
my $self = shift;
return $self->{'success'};
}
sub edit_details {
my $self = shift;
# if there wasn't a successful edit, then there's no details :)
return unless $self->{'success'};
return $self->{'edit_details'} || [];
}
sub add_details {
my $self = shift;
# if there wasn't a successful add, then there's no details :)
return unless $self->{'success'};
return $self->{'add_details'} || [];
}
sub add_insert_callback {
my $self = shift;
my $sub_ref = shift;
push(@{$self->{'insert_callbacks'}},$sub_ref);
}
sub add_update_callback {
my $self = shift;
my $sub_ref = shift;
push(@{$self->{'update_callbacks'}},$sub_ref);
}
sub list_param_parser {
my $self = shift;
my $sub_ref = shift;
$self->{'list_param_parser'} = $sub_ref;
}
sub validate_add {
my $self = shift;
my $p = shift;
( run in 0.674 second using v1.01-cache-2.11-cpan-39bf76dae61 )