Bug 15758: Koha::Libraries - Remove GetBranches
[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 use C4::Output;
27
28
29 sub plugin_javascript {
30     my ($dbh,$record,$tagslib,$field_number,$tabloop) = @_;
31     my $res="
32     <script type='text/javascript'>
33         function Blur$field_number() {
34                 var isbn = document.getElementById('$field_number');
35                 var url = '../cataloguing/plugin_launcher.pl?plugin_name=unimarc_field_010.pl&isbn=' + isbn.value;
36                 var req = \$.get(url);
37                 req.done(function(resp){
38                         var elems = document.getElementsByTagName('input');
39                         for( i = 0 ; elems[i] ; i++ )
40                         {
41                             if(elems[i].id.match(/^tag_210_subfield_c/)) {
42                                 elems[i].value = resp;
43                                 return 1;
44                             }
45                         }
46                 });
47                 return 1;
48         }
49
50     </script>
51     ";
52
53     return ($field_number,$res);
54 }
55
56 sub plugin {
57     my ($input) = @_;
58     my $isbn = $input->param('isbn');
59
60     my ($template, $loggedinuser, $cookie)
61             = get_template_and_user({template_name => "cataloguing/value_builder/ajax.tt",
62                                     query => $input,
63                                     type => "intranet",
64                                     authnotrequired => 0,
65                                     flagsrequired => {editcatalogue => '*'},
66                                     debug => 1,
67                                     });
68
69
70     my $dbh = C4::Context->dbh;
71     my $len = 0;
72     my $sth = $dbh->prepare('SELECT publishercode FROM biblioitems WHERE REPLACE(isbn, "-", "") LIKE ? OR isbn LIKE ? LIMIT 1');
73     
74     $isbn =~ s/-//g;
75     if (length ($isbn) == 13){
76         $isbn = substr($isbn, 3, length($isbn)-3);
77     }
78     
79     if(length($isbn) <= 10){
80
81         $len = 5;
82         if ( substr( $isbn, 0, 1 ) <= 7 ){
83             $len = 1;
84         }elsif ( substr( $isbn, 0, 2 ) <= 94 ){
85             $len = 2;
86         }elsif ( substr( $isbn, 0, 3 ) <= 995 ){
87             $len = 3;
88         }elsif ( substr( $isbn, 0, 4 ) <= 9989 ){
89             $len = 4 ;
90         }
91
92         my $x = substr( $isbn, $len );
93         my $seg2 = "";
94         
95         if ( substr( $x, 0, 2 ) <= 19 ) {    
96             $seg2 = substr( $x, 0, 2 );
97         }
98         elsif ( substr( $x, 0, 3 ) <= 699 ) {
99             $seg2 = substr( $x, 0, 3 );
100         }
101         elsif ( substr( $x, 0, 4 ) <= 8399 ) {
102             $seg2 = substr( $x, 0, 4 );
103         }
104         elsif ( substr( $x, 0, 5 ) <= 89999 ) {
105             $seg2 = substr( $x, 0, 5 );
106         }
107         elsif ( substr( $x, 0, 6 ) <= 9499999 ) {
108             $seg2 = substr( $x, 0, 6 );
109         }
110         else {
111             $seg2 = substr( $x, 0, 7 );
112         }
113
114         while($len--){
115             $seg2 = "_".$seg2;
116         }
117         
118         $len = 10 -length($seg2);
119         
120         while($len--){
121             $seg2 .= "_";
122         }
123
124         $sth->execute($seg2, "978$seg2");
125     }
126     
127     if( (my $publishercode) = $sth->fetchrow )
128     {
129         $template->param(return => $publishercode);
130     }
131     output_html_with_http_headers $input, $cookie, $template->output;
132 }