From: Mike Rylander Date: Wed, 18 Apr 2012 17:24:24 +0000 (-0400) Subject: Teach QueryParser about search filter callbacks, and if a callback returns a value... X-Git-Url: http://git.equinoxoli.org/?p=transitory.git;a=commitdiff_plain;h=712720c419d6f42576426cff43feac773da65b09 Teach QueryParser about search filter callbacks, and if a callback returns a value, parse it at the current recursion level Signed-off-by: Mike Rylander Signed-off-by: Bill Erickson Signed-off-by: Mike Rylander --- diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/QueryParser.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/QueryParser.pm index 4cb51ed..7ee0402 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/QueryParser.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/Storage/QueryParser.pm @@ -63,6 +63,14 @@ sub filters { return $parser_config{$class}{filters}; } +sub filter_callbacks { + my $class = shift; + $class = ref($class) || $class; + + $parser_config{$class}{filter_callbacks} ||= {}; + return $parser_config{$class}{filter_callbacks}; +} + sub modifiers { my $class = shift; $class = ref($class) || $class; @@ -100,9 +108,11 @@ sub add_search_filter { my $pkg = shift; $pkg = ref($pkg) || $pkg; my $filter = shift; + my $callback = shift; return $filter if (grep { $_ eq $filter } @{$pkg->filters}); push @{$pkg->filters}, $filter; + $pkg->filter_callbacks->{$filter} = $callback if ($callback); return $filter; } @@ -568,7 +578,17 @@ sub decompose { my $negate = ($1 eq $pkg->operator('disallowed')) ? 1 : 0; $_ = $'; - $struct->new_filter( $2 => [ split '[,]+', $3 ], $negate ); + + my $filter = $2; + my $params = [ split '[,]+', $3 ]; + + if ($pkg->filter_callbacks->{$filter}) { + my $replacement = $pkg->filter_callbacks->{$filter}->($self, $struct, $filter, $params, $negate); + $_ = "$replacement $_" if ($replacement); + } else { + $struct->new_filter( $filter => $params, $negate ); + } + $last_type = ''; } elsif ($self->filter_count && /$filter_as_class_re/) { # found a filter @@ -576,7 +596,16 @@ sub decompose { my $negate = ($1 eq $pkg->operator('disallowed')) ? 1 : 0; $_ = $'; - $struct->new_filter( $2 => [ split '[,]+', $3 ], $negate ); + + my $filter = $2; + my $params = [ split '[,]+', $3 ]; + + if ($pkg->filter_callbacks->{$filter}) { + my $replacement = $pkg->filter_callbacks->{$filter}->($self, $struct, $filter, $params, $negate); + $_ = "$replacement $_" if ($replacement); + } else { + $struct->new_filter( $filter => $params, $negate ); + } $last_type = ''; } elsif ($self->modifier_count && /$modifier_re/) { # found a modifier