1 package SL::Controller::Helper::ParseFilter;
5 use Exporter qw(import);
6 our @EXPORT = qw(parse_filter);
9 use SL::Helper::DateTime;
10 use List::MoreUtils qw(uniq);
14 date => sub { DateTime->from_lxoffice($_[0]) },
15 number => sub { $::form->parse_amount(\%::myconfig, $_[0]) },
16 percent => sub { $::form->parse_amount(\%::myconfig, $_[0]) / 100 },
17 head => sub { $_[0] . '%' },
18 tail => sub { '%' . $_[0] },
19 substr => sub { '%' . $_[0] . '%' },
23 enable => sub { ;;;; },
25 $_ => sub { +{ $_ => $_[0] } },
26 } qw(similar match imatch regex regexp like ilike rlike is is_not ne eq lt gt le ge),
30 my ($filter, %params) = @_;
32 my $hint_objects = $params{with_objects} || [];
34 my ($flattened, $objects) = _pre_parse($filter, $hint_objects, '', %params);
36 my $query = _parse_filter($flattened, %params);
38 _launder_keys($filter, $params{launder_to}) unless $params{no_launder};
41 ($query && @$query ? (query => $query) : ()),
42 ($objects && @$objects ? ( with_objects => [ uniq @$objects ]) : ());
46 my ($filter, $launder_to) = @_;
47 $launder_to ||= $filter;
48 return unless ref $filter eq 'HASH';
49 for my $key (keys %$filter) {
52 if ('' eq ref $filter->{$orig}) {
53 $launder_to->{$key} = $filter->{$orig};
54 } elsif ('ARRAY' eq ref $filter->{$orig}) {
55 $launder_to->{$key} = [ @{ $filter->{$orig} } ];
57 $launder_to->{$key} ||= { };
58 _launder_keys($filter->{$key}, $launder_to->{$key});
64 my ($filter, $with_objects, $prefix, %params) = @_;
66 return (undef, $with_objects) unless 'HASH' eq ref $filter;
71 while (my ($key, $value) = each %$filter) {
72 next if !defined $value || $value eq ''; # 0 is fine
73 if ('HASH' eq ref $value) {
74 my ($query, $more_objects) = _pre_parse($value, $with_objects, _prefix($prefix, $key));
75 push @result, @$query if $query;
76 push @$with_objects, _prefix($prefix, $key), ($more_objects ? @$more_objects : ());
78 push @result, _prefix($prefix, $key) => $value;
82 return \@result, $with_objects;
86 my ($flattened, %params) = @_;
88 return () unless 'ARRAY' eq ref $flattened;
90 my %sorted = ( @$flattened );
92 my @keys = sort { length($b) <=> length($a) } keys %sorted;
94 next unless $key =~ /^(.*\b)::$/;
95 $sorted{$1 . '::' . delete $sorted{$key} } = delete $sorted{$1} if $sorted{$1} && $sorted{$key};
99 while (my ($key, $value) = each %sorted) {
100 ($key, $value) = _apply_all($key, $value, qr/\b:(\w+)/, { %filters, %{ $params{filters} || {} } });
101 ($key, $value) = _apply_all($key, $value, qr/\b::(\w+)/, { %methods, %{ $params{methods} || {} } });
102 $result{$key} = $value;
108 join '.', grep $_, @_;
112 my ($value, $name, $filters) = @_;
113 return $value unless $name && $filters->{$name};
114 return [ map { _apply($_, $name, $filters) } @$value ] if 'ARRAY' eq ref $value;
115 return $filters->{$name}->($value);
119 my ($key, $value, $re, $subs) = @_;
121 while ($key =~ s/$re//) {
122 $value = _apply($value, $1, $subs);
134 SL::Controller::Helper::ParseFilter - Convert a form filter spec into a RDBO get_all filter
138 use SL::Controller::Helper::ParseFilter;
139 SL::DB::Object->get_all(parse_filter($::form->{filter}));
142 SL::DB::Object->get_all(parse_filter($::form->{filter},
143 with_objects => [ qw(part customer) ]));
147 A search filter will usually search for things in relations of the actual
148 search target. A search for sales orders may be filtered by the name of the
149 customer. L<Rose::DB::Object> alloes you to search for these by filtering them prefixed with their table:
152 'customer.name' => 'John Doe',
153 'department.description' => [ ilike => '%Sales%' ],
154 'orddate' => [ lt => DateTime->today ],
157 Unfortunately, if you specify them in you form as these strings, the form
158 parser will convert them into nested structures like this:
168 And the substring match requires you to recognize the ilike, and modify the value.
170 C<parse_filter> tries to ease this by recognizing these structures and
171 providing suffixes for common search patterns.
177 =item C<parse_filter \%FILTER, [ %PARAMS ]>
179 First argument is the filter from form. It is highly recommended that you put
180 all filter attributes into a named container as to not confuse them with the
183 Nested structures will be parsed and interpreted as foreign references. For
184 example if you search for L<Order>s, this input will search for those with a
185 specific L<Salesman>:
187 [% L.select_tag('filter.salesman.id', ...) %]
189 Additionally you can add modifier to the name to set a certain method:
191 [% L.input_tag('filter.department.description:substr::ilike', ...) %]
193 This will add the "% .. %" wildcards for substr matching in SQL, and add an
194 C<< ilike => $value >> block around it to match case insensitively.
196 As a rule all value filters require a single colon and must be placed before
197 match method suffixes, which are appended with 2 colons. See below for a full
200 The reason for the method being last is that it is possible to specify the
201 method in another input. Suppose you want a date input and a separate
202 before/after/equal select, you can use the following:
204 [% L.date_tag('filter.appointed_date:date', ... ) %]
208 [% L.select_tag('filter.appointed_date::', ... ) %]
210 The special empty method will be used to set the method for the previous
217 Unfortunately Template cannot parse the postfixes if you want to
218 rerender the filter. For this reason all colons filter keys are by
219 default laundered into underscores, so you can use them like this:
221 [% L.input_tag('filter.price:number::lt', filter.price_number__lt) %]
223 All of your original entries will stay intactg. If you don't want this to
224 happen pass C<< no_launder => 1 >> as a parameter. Additionally you can pass a
225 different target for the laundered values with the C<launder_to> parameter. It
226 takes an hashref and will deep copy all values in your filter to the target. So
227 if you have a filter that looks liek this:
230 'price:number::lt' => '2,30',
236 parse_filter($filter, launder_to => $laundered_filter = { })
238 the original filter will be unchanged, and C<$laundered_filter> will end up
242 'price_number__lt' => '2,30',
246 =head1 FILTERS (leading with :)
248 The following filters are built in, and can be used.
254 Parses the input string with C<< DateTime->from_lxoffice >>
258 Pasres the input string with C<< Form->parse_amount >>
262 Parses the input string with C<< Form->parse_amount / 100 >>
266 Adds "%" at the end of the string.
270 Adds "%" at the end of the string.
274 Adds "% .. %" around the search string.
278 =head2 METHODS (leading with ::)
290 All these are recognized like the L<Rose::DB::Object> methods.
294 =head1 BUGS AND CAVEATS
296 This will not properly handle multiple versions of the same object in different
299 Suppose you want all L<SL::DB::Order>s which have either themselves a certain
300 customer, or are linked to a L<SL::DB::Invoice> with this customer, the
301 following will not work as you expect:
304 L.input_tag('customer.name:substr::ilike', ...)
305 L.input_tag('invoice.customer.name:substr::ilike', ...)
307 This will sarch for orders whoe invoice has the _same_ customer, which matches
308 both inputs. This is because tables are aliased by their name and not by their
309 position in with_objects.
317 Additional filters shoud be pluggable.
323 Sven Schöling E<lt>s.schoeling@linet-services.deE<gt>