Line # Revision Author
1 386 ahitrov <% $json %>
2 <%once>
3
4 use JSON::XS;
5
6 </%once>
7 <%args>
8
9 $class => undef
10 $table => undef
11 $min => 2
12 $term => undef
13 $show => 'name'
14 $search_by => 'name'
15
16 </%args>
17 <%init>
18
19 my @result;
20
21 if ( ($table || $class) && $term && length( Encode::decode('utf-8', $term) ) >= $min ) {
22 my $search_string = "%$term%";
23 my %opts;
24 if ( $class ) {
25 $opts{class} = $class;
26 } else {
27 $opts{table} = $table;
28 }
29 $opts{$search_by} = $search_string;
30 $opts{ilike} = 1;
31 my $docs = $keeper->get_documents( %opts, return_mode => 'array_ref', light => 1, limit => 30 );
32 if ( ref $docs eq 'ARRAY' && @$docs ) {
33 @result = map { {id => $_->id, label => Encode::decode('utf-8', $_->$show), value => Encode::decode('utf-8', $_->$show)} } @$docs;
34 }
35 }
36
37 my $json = encode_json \@result;
38
39 </%init>