Fix FSF address in directory cataloguing/
[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 under the
9 # terms of the GNU General Public License as published by the Free Software
10 # Foundation; either version 2 of the License, or (at your option) any later
11 # version.
12 #
13 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
14 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
15 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
16 #
17 # You should have received a copy of the GNU General Public License along
18 # with Koha; if not, write to the Free Software Foundation, Inc.,
19 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20
21 use strict;
22 use warnings;
23 use C4::Auth;
24 use CGI;
25 use C4::Context;
26
27
28 =head1
29
30 plugin_parameters : other parameters added when the plugin is called by the dopop function
31
32 =cut
33
34 sub plugin_parameters {
35     my ($dbh,$record,$tagslib,$i,$tabloop) = @_;
36     return "";
37 }
38
39 sub plugin_javascript {
40     my ($dbh,$record,$tagslib,$field_number,$tabloop) = @_;
41     my $res="
42     <script type='text/javascript'>
43         function Focus$field_number() {
44             return 1;
45         }
46
47         function Blur$field_number() {
48                 var isbn = document.getElementById('$field_number');
49                 var url = '../cataloguing/plugin_launcher.pl?plugin_name=unimarc_field_010.pl&isbn=' + isbn.value;
50                 var blurcallback010 =
51                 {
52                     success: function(o){
53                         var elems = document.getElementsByTagName('input');
54                         for( i = 0 ; elems[i] ; i++ )
55                         {
56                             if(elems[i].id.match(/^tag_210_subfield_c/)) {
57                                 elems[i].value = o.responseText;
58                                 return 1;
59                             }
60                         }
61                     }
62                 }
63                 var transaction = YAHOO.util.Connect.asyncRequest('GET',url, blurcallback010, null);
64                 return 1;
65         }
66
67         function Clic$field_number() {
68             return 1;
69         }
70     </script>
71     ";
72
73     return ($field_number,$res);
74 }
75
76 sub plugin {
77     my ($input) = @_;
78     my $isbn = $input->param('isbn');
79
80     my ($template, $loggedinuser, $cookie)
81             = get_template_and_user({template_name => "cataloguing/value_builder/ajax.tmpl",
82                                     query => $input,
83                                     type => "intranet",
84                                     authnotrequired => 0,
85                                     flagsrequired => {editcatalogue => '*'},
86                                     debug => 1,
87                                     });
88
89
90     my $dbh = C4::Context->dbh;
91     my $len = 0;
92     my $sth = $dbh->prepare('SELECT publishercode FROM biblioitems WHERE isbn LIKE ? OR isbn LIKE ? LIMIT 1');
93     
94     $isbn =~ s/-//g;
95     warn $isbn;
96     if (length ($isbn) == 13){
97         $isbn = substr($isbn, 3, length($isbn)-3);
98     }
99     
100     if(length($isbn) <= 10){
101
102         $len = 5;
103         if ( substr( $isbn, 0, 1 ) <= 7 ){
104             $len = 1;
105         }elsif ( substr( $isbn, 0, 2 ) <= 94 ){
106             $len = 2;
107         }elsif ( substr( $isbn, 0, 3 ) <= 995 ){
108             $len = 3;
109         }elsif ( substr( $isbn, 0, 4 ) <= 9989 ){
110             $len = 4 ;
111         }
112
113         my $x = substr( $isbn, $len );
114         my $seg2 = "";
115         
116         if ( substr( $x, 0, 2 ) <= 19 ) {    
117             $seg2 = substr( $x, 0, 2 );
118         }
119         elsif ( substr( $x, 0, 3 ) <= 699 ) {
120             $seg2 = substr( $x, 0, 3 );
121         }
122         elsif ( substr( $x, 0, 4 ) <= 8399 ) {
123             $seg2 = substr( $x, 0, 4 );
124         }
125         elsif ( substr( $x, 0, 5 ) <= 89999 ) {
126             $seg2 = substr( $x, 0, 5 );
127         }
128         elsif ( substr( $x, 0, 6 ) <= 9499999 ) {
129             $seg2 = substr( $x, 0, 6 );
130         }
131         else {
132             $seg2 = substr( $x, 0, 7 );
133         }
134
135         while($len--){
136             $seg2 = "_".$seg2;
137         }
138         
139         $len = 10 -length($seg2);
140         
141         while($len--){
142             $seg2 .= "_";
143         }
144
145         $sth->execute($seg2, "978$seg2");
146     }
147     
148     if( (my $publishercode) = $sth->fetchrow )
149     {
150         $template->param(return => $publishercode);
151     }
152     output_html_with_http_headers $input, $cookie, $template->output;
153 }
154 1;