(bug #2829) mistake un isbn field
[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 with
18 # Koha; if not, write to the Free Software Foundation, Inc., 59 Temple Place,
19 # Suite 330, Boston, MA  02111-1307 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 => 1},
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 ? LIMIT 1');
93     
94     if (length ($isbn) == 13){
95         $isbn = substr(3, length($isbn));
96     }
97
98     if(length($isbn) <=10){
99         $len = 5;
100         $len = 1 if ( substr( $isbn, 0, 1 ) <= 7 );
101         $len = 2 if ( substr( $isbn, 0, 2 ) <= 94 );
102         $len = 3 if ( substr( $isbn, 0, 3 ) <= 995 );
103         $len = 4 if ( substr( $isbn, 0, 4 ) <= 9989 );
104
105         my $x = substr( $isbn, $len );
106         my $seg2;
107  
108         if ( substr( $x, 0, 2 ) <= 19 ) {    
109             $seg2 = substr( $x, 0, 2 );
110         }
111         elsif ( substr( $x, 0, 3 ) <= 699 ) {
112             $seg2 = substr( $x, 0, 3 );
113         }
114         elsif ( substr( $x, 0, 4 ) <= 8399 ) {
115             $seg2 = substr( $x, 0, 4 );
116         }
117         elsif ( substr( $x, 0, 5 ) <= 89999 ) {
118             $seg2 = substr( $x, 0, 5 );
119         }
120         elsif ( substr( $x, 0, 6 ) <= 9499999 ) {
121             $seg2 = substr( $x, 0, 6 );
122         }
123         else {
124             $seg2 = substr( $x, 0, 7 );
125         }
126
127         while($len--){
128             $seg2 = "_".$seg2;
129         }
130     
131         $seg2 .= "%";
132         warn $seg2;
133         $sth->execute($seg2);
134     }
135     
136     if( (my $publishercode) = $sth->fetchrow )
137     {
138         $template->param(return => $publishercode);
139     }
140     output_html_with_http_headers $input, $cookie, $template->output;
141 }
142 1;