X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FController%2FHelper%2FParseFilter.pm;h=cdd3eb7a75db7b7af7d080dbd03f8e487ee67622;hb=ad033a768bbe46566afa159008c42ff7fc6bb35a;hp=55e65e1b567b812076cfca5f4cdb7eea60d3d9a3;hpb=303a4d5d9c401254c2cf623a702a9e7676fb639f;p=kivitendo-erp.git diff --git a/SL/Controller/Helper/ParseFilter.pm b/SL/Controller/Helper/ParseFilter.pm index 55e65e1b5..cdd3eb7a7 100644 --- a/SL/Controller/Helper/ParseFilter.pm +++ b/SL/Controller/Helper/ParseFilter.pm @@ -10,6 +10,7 @@ use SL::Helper::DateTime; use List::MoreUtils qw(uniq); use SL::MoreCommon qw(listify); use Data::Dumper; +use Text::ParseWords; my %filters = ( date => sub { DateTime->from_lxoffice($_[0]) }, @@ -42,10 +43,10 @@ sub parse_filter { _add_uniq($objects, $_) for @$auto_objects; } - my $query = _parse_filter($flattened, $objects, %params); - _launder_keys($filter, $params{launder_to}) unless $params{no_launder}; + my $query = _parse_filter($flattened, $objects, %params); + return ($query && @$query ? (query => $query) : ()), ($objects && @$objects ? ( with_objects => [ uniq @$objects ]) : ()); @@ -106,7 +107,7 @@ sub _parse_filter { if ($key =~ s/:multi//) { my @multi; my $orig_key = $key; - for my $value (split / /, $value) { + for my $value (parse_line('\s+', 0, $value)) { ($key, $value) = _apply_all($key, $value, qr/\b:(\w+)/, { %filters, %{ $params{filters} || {} } }); ($key, $value) = _apply_all($key, $value, qr/\b::(\w+)/, { %methods, %{ $params{methods} || {} } }); ($key, $value) = _dispatch_custom_filters($params{class}, $with_objects, $key, $value) if $params{class}; @@ -148,25 +149,38 @@ sub _dispatch_custom_filters { die 'unrecognized filters' if $key =~ /:/; my @tokens = split /\./, $key; - my $last_token = pop @tokens; my $curr_class = $class->object_class; - for my $token (@tokens) { + # our key will consist of dot-delimited tokens + # like this: order.part.unit.name + # each of these tokens except the last one is one of: + # - a relationship in the parent object + # - a custom filter + # + # the last token must be + # - a custom filter + # - a column in the parent object + # + # find first token which is not a relationship, + # so we can pass the rest on + my $i = 0; + while ($i < $#tokens) { eval { - $curr_class = $curr_class->meta->relationship($token)->class; - 1; + $curr_class = $curr_class->meta->relationship($tokens[$i])->class; + ++$i; } or do { - require Carp; - Carp::croak("Could not resolve the relationship '$token' in '$key' while building the filter request"); + last; } } my $manager = $curr_class->meta->convention_manager->auto_manager_class_name; - my $obj_path = join '.', @tokens; - my $obj_prefix = join '.', @tokens, ''; + my $obj_path = join '.', @tokens[0..$i-1]; + my $obj_prefix = join '.', @tokens[0..$i-1], ''; + my $key_token = $tokens[$i]; + my @additional_tokens = @tokens[$i+1..$#tokens]; if ($manager->can('filter')) { - ($key, $value, my $obj) = $manager->filter($last_token, $value, $obj_prefix); + ($key, $value, my $obj) = $manager->filter($key_token, $value, $obj_prefix, $obj_path, @additional_tokens); _add_uniq($with_objects, $obj) if $obj; } else { _add_uniq($with_objects, $obj_path) if $obj_path; @@ -256,7 +270,7 @@ SL::Controller::Helper::ParseFilter - Convert a form filter spec into a RDBO get A search filter will usually search for things in relations of the actual search target. A search for sales orders may be filtered by the name of the -customer. L alloes you to search for these by filtering them prefixed with their table: +customer. L allows you to search for these by filtering them prefixed with their table: query => [ 'customer.name' => 'John Doe', @@ -264,7 +278,7 @@ customer. L alloes you to search for these by filtering them p 'orddate' => [ lt => DateTime->today ], ] -Unfortunately, if you specify them in you form as these strings, the form +Unfortunately, if you specify them in your form as these strings, the form parser will convert them into nested structures like this: $::form = bless { @@ -296,7 +310,7 @@ specific L: [% L.select_tag('filter.salesman.id', ...) %] -Additionally you can add modifier to the name to set a certain method: +Additionally you can add a modifier to the name to set a certain method: [% L.input_tag('filter.department.description:substr::ilike', ...) %] @@ -312,7 +326,7 @@ list of modifiers. =head1 LAUNDERING Unfortunately Template cannot parse the postfixes if you want to -rerender the filter. For this reason all colons filter keys are by +rerender the filter. For this reason all colon filter keys are by default laundered into underscores, so you can use them like this: [% L.input_tag('filter.price:number::lt', filter.price_number__lt) %] @@ -325,7 +339,7 @@ these will get copied into a _ suffixed version as hashes: All of your original entries will stay intact. If you don't want this to happen pass C<< no_launder => 1 >> as a parameter. Additionally you can pass a different target for the laundered values with the C parameter. It -takes an hashref and will deep copy all values in your filter to the target. So +takes a hashref and will deep copy all values in your filter to the target. So if you have a filter that looks like this: $filter = { @@ -381,7 +395,7 @@ will expand to: ] ] -For more abuot custom filters, see L. +For more about custom filters, see L. =head1 FILTERS (leading with :) @@ -449,7 +463,7 @@ following will not work as you expect: L.input_tag('customer.name:substr::ilike', ...) L.input_tag('invoice.customer.name:substr::ilike', ...) -This will sarch for orders whose invoice has the _same_ customer, which matches +This will search for orders whose invoice has the _same_ customer, which matches both inputs. This is because tables are aliased by their name and not by their position in with_objects. @@ -459,7 +473,7 @@ position in with_objects. =item * -Additional filters shoud be pluggable. +Additional filters should be pluggable. =back