Bug 9978: Replace license header with the correct license (GPLv3+)
[koha.git] / cataloguing / value_builder / unimarc_field_010.pl
1 #!/usr/bin/perl
2
3
4 # Copyright 2008 Biblibre SARL
5 #
6 # This file is part of Koha.
7 #
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
12 #
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
17 #
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
20
21 use strict;
22 use warnings;
23 use C4::Auth;
24 use CGI qw ( -utf8 );
25 use C4::Context;
26
27
28 sub plugin_javascript {
29     my ($dbh,$record,$tagslib,$field_number,$tabloop) = @_;
30     my $res="
31     <script type='text/javascript'>
32         function Blur$field_number() {
33                 var isbn = document.getElementById('$field_number');
34                 var url = '../cataloguing/plugin_launcher.pl?plugin_name=unimarc_field_010.pl&isbn=' + isbn.value;
35                 var req = \$.get(url);
36                 req.done(function(resp){
37                         var elems = document.getElementsByTagName('input');
38                         for( i = 0 ; elems[i] ; i++ )
39                         {
40                             if(elems[i].id.match(/^tag_210_subfield_c/)) {
41                                 elems[i].value = resp;
42                                 return 1;
43                             }
44                         }
45                 });
46                 return 1;
47         }
48
49     </script>
50     ";
51
52     return ($field_number,$res);
53 }
54
55 sub plugin {
56     my ($input) = @_;
57     my $isbn = $input->param('isbn');
58
59     my ($template, $loggedinuser, $cookie)
60             = get_template_and_user({template_name => "cataloguing/value_builder/ajax.tt",
61                                     query => $input,
62                                     type => "intranet",
63                                     authnotrequired => 0,
64                                     flagsrequired => {editcatalogue => '*'},
65                                     debug => 1,
66                                     });
67
68
69     my $dbh = C4::Context->dbh;
70     my $len = 0;
71     my $sth = $dbh->prepare('SELECT publishercode FROM biblioitems WHERE REPLACE(isbn, "-", "") LIKE ? OR isbn LIKE ? LIMIT 1');
72     
73     $isbn =~ s/-//g;
74     if (length ($isbn) == 13){
75         $isbn = substr($isbn, 3, length($isbn)-3);
76     }
77     
78     if(length($isbn) <= 10){
79
80         $len = 5;
81         if ( substr( $isbn, 0, 1 ) <= 7 ){
82             $len = 1;
83         }elsif ( substr( $isbn, 0, 2 ) <= 94 ){
84             $len = 2;
85         }elsif ( substr( $isbn, 0, 3 ) <= 995 ){
86             $len = 3;
87         }elsif ( substr( $isbn, 0, 4 ) <= 9989 ){
88             $len = 4 ;
89         }
90
91         my $x = substr( $isbn, $len );
92         my $seg2 = "";
93         
94         if ( substr( $x, 0, 2 ) <= 19 ) {    
95             $seg2 = substr( $x, 0, 2 );
96         }
97         elsif ( substr( $x, 0, 3 ) <= 699 ) {
98             $seg2 = substr( $x, 0, 3 );
99         }
100         elsif ( substr( $x, 0, 4 ) <= 8399 ) {
101             $seg2 = substr( $x, 0, 4 );
102         }
103         elsif ( substr( $x, 0, 5 ) <= 89999 ) {
104             $seg2 = substr( $x, 0, 5 );
105         }
106         elsif ( substr( $x, 0, 6 ) <= 9499999 ) {
107             $seg2 = substr( $x, 0, 6 );
108         }
109         else {
110             $seg2 = substr( $x, 0, 7 );
111         }
112
113         while($len--){
114             $seg2 = "_".$seg2;
115         }
116         
117         $len = 10 -length($seg2);
118         
119         while($len--){
120             $seg2 .= "_";
121         }
122
123         $sth->execute($seg2, "978$seg2");
124     }
125     
126     if( (my $publishercode) = $sth->fetchrow )
127     {
128         $template->param(return => $publishercode);
129     }
130     output_html_with_http_headers $input, $cookie, $template->output;
131 }