source: trunk/LATMOS-Accounts-Web/lib/LATMOS/Accounts/Web/Controller/Json/RefVal.pm @ 1694

Last change on this file since 1694 was 1694, checked in by nanardon, 8 years ago

speedup office web page

File size: 1.6 KB
Line 
1package LATMOS::Accounts::Web::Controller::Json::RefVal;
2use Moose;
3use namespace::autoclean;
4
5BEGIN { extends 'Catalyst::Controller'; }
6
7=head1 NAME
8
9LATMOS::Accounts::Web::Controller::Json::RefVal - Catalyst Controller
10
11=head1 DESCRIPTION
12
13Catalyst Controller.
14
15=head1 METHODS
16
17=cut
18
19
20=head2 index
21
22=cut
23
24sub index :Path :Args(2) {
25    my ( $self, $c, $otype, $attr ) = @_;
26
27    my $base = $c->model('Accounts')->db;
28
29    my $attribute = $base->attribute($otype, $attr);
30    my $term = $c->req->param('term') || '';
31
32    my @values = $attribute->can_values;
33
34    if (my $oref = $attribute->reference) {
35        if ($term) {
36            my %objs = ();
37            foreach my $attr (@{ $c->config->{objects}{$oref}{quick_search} || [ 'name' ]}) {
38                foreach ($base->search_objects(
39                        $oref, "$attr~" . $term, 'exported=true')) {
40                    $objs{$_} = 1;
41                }
42            }
43            @values = grep { $objs{$_} } @values;
44        }
45
46        if (scalar(@values) <= 20) {
47            $c->stash->{data} = [
48                map { my $o = $base->get_object($attribute->reference, $_); { label => "$o", value => $_ } }
49                @values ];
50        } else {
51            $c->stash->{data} = [ map { { label => $_, value => $_ } } @values ];
52        }
53    } else {
54        $c->stash->{data} = [ map { { label => $_, value => $_ } } grep { $_ =~ /\Q$term\E/i } @values ];
55    }
56}
57
58
59
60=encoding utf8
61
62=head1 AUTHOR
63
64olivier
65
66=head1 LICENSE
67
68This library is free software. You can redistribute it and/or modify
69it under the same terms as Perl itself.
70
71=cut
72
73__PACKAGE__->meta->make_immutable;
74
751;
Note: See TracBrowser for help on using the repository browser.