Revision 392 (by ahitrov, 2013/10/30 12:24:07) Inline autocomplete bug fixes
Encode bug in structures saved in fields resolved. Now we can store hashes and lists without additional serialization
<% $json %>
<%once>

    use JSON::XS;

</%once>
<%args>

	$class	=> undef
	$table	=> undef
	$min	=> 2
	$term	=> undef
	$show	=> 'name'
	$search_by	=> 'name'

</%args>
<%init>

    my @result;

    if ( ($table || $class) && $term && length( Encode::decode('utf-8', $term) ) >= $min ) {
	my $search_string = "%$term%";
	my %opts;
	if ( $class ) {
		$opts{class} = $class;
	} else {
		$opts{table} = $table;
	}
	$opts{$search_by} = $search_string;
	$opts{ilike} = 1;
	while ( my ($key, $val) = each %ARGS ) {
		next	if $key =~ /^(class|table|min|term|show|search_by)$/;
		$opts{$key} = $val;
	}
	my $docs = $keeper->get_documents( %opts, return_mode => 'array_ref', light => 1, limit => 30 );
	if ( ref $docs eq 'ARRAY' && @$docs ) {
		@result = map { {id => $_->id, label => Encode::decode('utf-8', $_->$show), value => Encode::decode('utf-8', $_->$show)} } @$docs;
	}
    }

    my $json = encode_json \@result;

</%init>