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