Bug 16996: (follow-up) Do not explode if mandatory fields are missing
[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::Koha;
52 use C4::Charset;
53 use C4::Auth qw/check_api_auth/;
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
71 binmode STDOUT, ':encoding(UTF-8)';
72 print $input->header(-type => 'text/plain', -charset => 'UTF-8');
73 my $avs = C4::Koha::GetAuthvalueDropbox($category, $default);
74 my $html = qq|<select id="$name" name="$name">|;
75 for my $av ( @$avs ) {
76     if ( $av->{default} ) {
77         $html .= qq|<option value="$av->{value}" selected="selected">$av->{label}</option>|;
78     } else {
79         $html .= qq|<option value="$av->{value}">$av->{label}</option>|;
80     }
81 }
82 $html .= qq|</select>|;
83
84 print $html;