Bug 20640: (follow-up) Degrade gracefully on error
[koha.git] / acqui / ajax-getauthvaluedropbox.pl
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Copyright 2012 BibLibre
6 #
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
11 #
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20 =head1 NAME
21
22 ajax-getauthvaluedropbox.pl - returns an authorised values dropbox
23
24 =head1 DESCRIPTION
25
26 this script returns an authorised values dropbox
27
28 =head1 CGI PARAMETERS
29
30 =over 4
31
32 =item name
33
34 The name of the dropbox.
35
36 =item category
37
38 The category of authorised values.
39
40 =item default
41
42 Default value for the dropbox.
43
44 =back
45
46 =cut
47
48 use Modern::Perl;
49
50 use CGI qw ( -utf8 );
51 use C4::Charset;
52 use C4::Auth qw/check_api_auth/;
53 use Koha::AuthorisedValues;
54
55 my $query = CGI->new();
56 binmode STDOUT, ':encoding(UTF-8)';
57
58 my ($status, $cookie, $sessionID) = check_api_auth($query, { catalogue => '*'} );
59 unless ($status eq "ok") {
60     print $query->header(-type => 'text/plain', -status => '403 Forbidden');
61     print '<option></option>';
62     exit 0;
63 }
64
65 my $input = new CGI;
66 my $name = $input->param('name');
67 my $category = $input->param('category');
68 my $default = $input->param('default');
69 $default = C4::Charset::NormalizeString($default);
70 my $branch_limit = C4::Context->userenv ? C4::Context->userenv->{"branch"} : "";
71
72 my $avs = Koha::AuthorisedValues->search(
73     {
74         branchcode => $branch_limit,
75         category => $category,
76     },
77     {
78         group_by => 'lib',
79         order_by => [ 'category', 'lib', 'lib_opac' ],
80     }
81 );
82 my $html = qq|<select id="$name" name="$name">|;
83 while ( my $av = $avs->next ) {
84     if ( $av->authorised_value eq $default ) {
85         $html .= q|<option value="| . $av->authorised_value . q|" selected="selected">| . $av->lib . q|</option>|;
86     } else {
87         $html .= q|<option value="| . $av->authorised_value . q|">| . $av->lib . q|</option>|;
88     }
89 }
90 $html .= qq|</select>|;
91
92 binmode STDOUT, ':encoding(UTF-8)';
93 print $input->header(-type => 'text/plain', -charset => 'UTF-8');
94 print $html;