1 | package LATMOS::Accounts::Web::Controller::Json::RefVal; |
---|
2 | use Moose; |
---|
3 | use namespace::autoclean; |
---|
4 | |
---|
5 | BEGIN { extends 'Catalyst::Controller'; } |
---|
6 | |
---|
7 | =head1 NAME |
---|
8 | |
---|
9 | LATMOS::Accounts::Web::Controller::Json::RefVal - Catalyst Controller |
---|
10 | |
---|
11 | =head1 DESCRIPTION |
---|
12 | |
---|
13 | Catalyst Controller. |
---|
14 | |
---|
15 | =head1 METHODS |
---|
16 | |
---|
17 | =cut |
---|
18 | |
---|
19 | |
---|
20 | =head2 index |
---|
21 | |
---|
22 | =cut |
---|
23 | |
---|
24 | sub 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 | |
---|
64 | olivier |
---|
65 | |
---|
66 | =head1 LICENSE |
---|
67 | |
---|
68 | This library is free software. You can redistribute it and/or modify |
---|
69 | it under the same terms as Perl itself. |
---|
70 | |
---|
71 | =cut |
---|
72 | |
---|
73 | __PACKAGE__->meta->make_immutable; |
---|
74 | |
---|
75 | 1; |
---|