X-Git-Url: http://wagnertech.de/git?a=blobdiff_plain;f=SL%2FController%2FHelper%2FParseFilter.pm;h=491eb63b58b9a6682b13b6b2f9280c892ed5f712;hb=223e6d0cf51b9847fd164b76c5a7fb77219f1855;hp=db59408436a5ebdc43b755062f97c1d7d2f45cf0;hpb=fe546cb027a1604ce2ba0f93b1bd241794fb66e4;p=kivitendo-erp.git diff --git a/SL/Controller/Helper/ParseFilter.pm b/SL/Controller/Helper/ParseFilter.pm index db5940843..491eb63b5 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]) }, @@ -101,16 +102,45 @@ sub _parse_filter { my @result; for (my $i = 0; $i < scalar @$flattened; $i += 2) { my ($key, $value) = ($flattened->[$i], $flattened->[$i+1]); + my ($type, $op) = $key =~ m{:(.+)::(.+)}; + + if ($key =~ s/:multi//) { + my @multi; + my $orig_key = $key; + 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}; + ($key, $value) = _apply_value_filters($key, $value, $type, $op); + push @multi, $key, $value; + $key = $orig_key; + } + ($key, $value) = (and => \@multi); + } else { + ($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}; + ($key, $value) = _apply_value_filters($key, $value, $type, $op); + } - ($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}; - - push @result, $key, $value; + push @result, $key, $value if defined $key; } return \@result; } +sub _apply_value_filters { + my ($key, $value, $type, $op) = @_; + + return ($key, $value) unless $key && $value && $type && $op && (ref($value) eq 'HASH'); + + if (($type eq 'date') && ($op eq 'le')) { + my $date = delete $value->{le}; + $value->{lt} = $date->add(days => 1); + } + + return ($key, $value); +} + sub _dispatch_custom_filters { my ($class, $with_objects, $key, $value) = @_; @@ -137,7 +167,7 @@ sub _dispatch_custom_filters { my $obj_prefix = join '.', @tokens, ''; if ($manager->can('filter')) { - ($key, $value, my $obj) = $manager->filter($last_token, $value, $obj_prefix); + ($key, $value, my $obj) = $manager->filter($last_token, $value, $obj_prefix, $obj_path); _add_uniq($with_objects, $obj) if $obj; } else { _add_uniq($with_objects, $obj_path) if $obj_path; @@ -227,7 +257,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', @@ -235,7 +265,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 { @@ -267,7 +297,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', ...) %] @@ -283,7 +313,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) %] @@ -296,7 +326,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 = { @@ -352,7 +382,7 @@ will expand to: ] ] -For more abuot custom filters, see L. +For more about custom filters, see L. =head1 FILTERS (leading with :) @@ -420,7 +450,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. @@ -430,7 +460,7 @@ position in with_objects. =item * -Additional filters shoud be pluggable. +Additional filters should be pluggable. =back