From: Sven Schöling Date: Fri, 24 May 2013 12:28:33 +0000 (+0200) Subject: ParseFilter: _collapse_indirect_filters ausgelagert und algorithmus auf array umgesch... X-Git-Tag: release-3.1.0beta1~401^2~14 X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=9fac28d83f55524f85cfa32cab463e0b8c063d92;p=kivitendo-erp.git ParseFilter: _collapse_indirect_filters ausgelagert und algorithmus auf array umgeschrieben --- diff --git a/SL/Controller/Helper/ParseFilter.pm b/SL/Controller/Helper/ParseFilter.pm index 3bf1b6595..5a1c93cd9 100644 --- a/SL/Controller/Helper/ParseFilter.pm +++ b/SL/Controller/Helper/ParseFilter.pm @@ -90,21 +90,48 @@ sub _parse_filter { return () unless 'ARRAY' eq ref $flattened; - my %sorted = ( @$flattened ); + $flattened = _collapse_indirect_filters($flattened); + + my @result; + for (my $i = 0; $i < scalar @$flattened; $i += 2) { + my ($key, $value) = ($flattened->[$i], $flattened->[$i+1]); + ($key, $value) = _apply_all($key, $value, qr/\b:(\w+)/, { %filters, %{ $params{filters} || {} } }); + ($key, $value) = _apply_all($key, $value, qr/\b::(\w+)/, { %methods, %{ $params{methods} || {} } }); + push @result, $key, $value; + } + return \@result; +} + +sub _collapse_indirect_filters { + my ($flattened) = @_; + + die 'flattened filter array length is uneven, should be possible to use as hash' if @$flattened % 2; + + my (%keys_to_delete, %keys_to_move, @collapsed); + + # search keys matching /::$/; + for (my $i = 0; $i < scalar @$flattened; $i += 2) { + my ($key, $value) = ($flattened->[$i], $flattened->[$i+1]); - my @keys = sort { length($b) <=> length($a) } keys %sorted; - for my $key (@keys) { next unless $key =~ /^(.*\b)::$/; - $sorted{$1 . '::' . delete $sorted{$key} } = delete $sorted{$1} if $sorted{$1} && $sorted{$key}; + + $keys_to_delete{$key}++; + $keys_to_move{$1} = $1 . '::' . $value; } - my %result; - while (my ($key, $value) = each %sorted) { - ($key, $value) = _apply_all($key, $value, qr/\b:(\w+)/, { %filters, %{ $params{filters} || {} } }); - ($key, $value) = _apply_all($key, $value, qr/\b::(\w+)/, { %methods, %{ $params{methods} || {} } }); - $result{$key} = $value; + for (my $i = 0; $i < scalar @$flattened; $i += 2) { + my ($key, $value) = ($flattened->[$i], $flattened->[$i+1]); + + if ($keys_to_move{$key}) { + push @collapsed, $keys_to_move{$key}, $value; + next; + } + if (!$keys_to_delete{$key}) { + push @collapsed, $key, $value; + } } - return [ %result ]; + + return \@collapsed; } sub _prefix {