Bug 32437: Honor overlay setting in staged batch when adding to a basket
[srvgit] / cataloguing / value_builder / unimarc_field_009_ppn.pl
1 #!/usr/bin/perl
2
3 # Converted to new plugin style (Bug 13437)
4
5 # Copyright 2000-2002 Katipo Communications
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21
22 use Modern::Perl;
23 use LWP::Simple qw();
24 use LWP::UserAgent;
25 use JSON;
26 use C4::Auth qw ( get_template_and_user );
27 use C4::Output qw ( output_html_with_http_headers );
28
29 my $res;
30 my $builder = sub {
31     my ( $params ) = @_;
32     my $res= qq|
33         <script>
34             jQuery(document).ready(function () {
35                 const input = document.getElementById('$params->{id}');
36                 const isbn_input = jQuery('input[id^="tag_010_subfield_a_"]');
37                 const issn_input = jQuery('input[id^="tag_011_subfield_a_"]');
38                 const ean_input = jQuery('input[id^="tag_073_subfield_a_"]');
39
40                 isbn_input.on('change', function () {
41                     update_ppn('isbn', this.value);
42                 });
43                 issn_input.on('change', function () {
44                     update_ppn('issn', this.value);
45                 });
46                 ean_input.on('change', function () {
47                     update_ppn('ean', this.value);
48                 });
49
50                 jQuery(input).on('focus', function () {
51                     const isbn = isbn_input.val().trim();
52                     const issn = issn_input.val().trim();
53                     const ean = ean_input.val().trim();
54                     if (isbn !== '') {
55                         update_ppn('isbn', isbn);
56                     } else if (issn !== '') {
57                         update_ppn('issn', issn);
58                     } else if (ean !== '') {
59                         update_ppn('ean', ean);
60                     }
61                 });
62
63                 function update_ppn(search_type, search_text) {
64                     if (input.value.trim() === '') {
65                         get_ppn(search_type, search_text).then(function (ppn) {
66                             input.value = ppn;
67                         });
68                     }
69                 }
70
71                 function get_ppn(search_type, search_text) {
72                     const url = '/cgi-bin/koha/cataloguing/plugin_launcher.pl?plugin_name=unimarc_field_009_ppn.pl&' + search_type + '=' + encodeURIComponent(search_text);
73
74                     return jQuery.get(url);
75                 }
76             });
77         </script>
78     |;
79     return $res;
80 };
81
82 my $launcher = sub {
83     my ( $params ) = @_;
84     my $input = $params->{cgi};
85     my $isbn = $input->param('isbn');
86     my $issn = $input->param('issn');
87     my $ean = $input->param('ean');
88
89     my ($template, $loggedinuser, $cookie) = get_template_and_user({
90         template_name   => "cataloguing/value_builder/ajax.tt",
91         query           => $input,
92         type            => "intranet",
93         flagsrequired   => {editcatalogue => '*'},
94     });
95
96     my $url;
97     if ( $isbn ) {
98         $url = "https://www.sudoc.fr/services/isbn2ppn/$isbn&format=text/json";
99     } elsif ( $issn ) {
100         $url = "https://www.sudoc.fr/services/issn2ppn/$issn&format=text/json";
101     } elsif ( $ean ) {
102         $url = "https://www.sudoc.fr/services/ean2ppn/$ean&format=text/json";
103     }
104
105     if ($url) {
106         my $json = LWP::Simple::get($url);
107         if (defined $json) {
108             my $response = JSON->new->utf8->decode($json);
109             my $result = $response->{sudoc}->{query}->{result};
110             my $ppn = ref $result eq 'ARRAY' ? $result->[0]->{ppn} : $result->{ppn};
111             $template->param( return => $ppn );
112         }
113     }
114
115     output_html_with_http_headers $input, $cookie, $template->output;
116 };
117
118 return { builder => $builder, launcher => $launcher };