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 |
392 |
ahitrov |
while ( my ($key, $val) = each %ARGS ) { |
32 |
|
|
next if $key =~ /^(class|table|min|term|show|search_by)$/; |
33 |
|
|
$opts{$key} = $val; |
34 |
|
|
} |
35 |
386 |
ahitrov |
my $docs = $keeper->get_documents( %opts, return_mode => 'array_ref', light => 1, limit => 30 ); |
36 |
|
|
if ( ref $docs eq 'ARRAY' && @$docs ) { |
37 |
|
|
@result = map { {id => $_->id, label => Encode::decode('utf-8', $_->$show), value => Encode::decode('utf-8', $_->$show)} } @$docs; |
38 |
|
|
} |
39 |
|
|
} |
40 |
|
|
|
41 |
|
|
my $json = encode_json \@result; |
42 |
|
|
|
43 |
|
|
</%init> |