Bug 7276 : Follow up, adding a sub to clear the cache
[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 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 blurcallback010 =
53                 {
54                     success: function(o){
55                         var elems = document.getElementsByTagName('input');
56                         for( i = 0 ; elems[i] ; i++ )
57                         {
58                             if(elems[i].id.match(/^tag_210_subfield_c/)) {
59                                 elems[i].value = o.responseText;
60                                 return 1;
61                             }
62                         }
63                     }
64                 }
65                 var transaction = YAHOO.util.Connect.asyncRequest('GET',url, blurcallback010, null);
66                 return 1;
67         }
68
69         function Clic$field_number() {
70             return 1;
71         }
72     </script>
73     ";
74
75     return ($field_number,$res);
76 }
77
78 sub plugin {
79     my ($input) = @_;
80     my $isbn = $input->param('isbn');
81
82     my ($template, $loggedinuser, $cookie)
83             = get_template_and_user({template_name => "cataloguing/value_builder/ajax.tmpl",
84                                     query => $input,
85                                     type => "intranet",
86                                     authnotrequired => 0,
87                                     flagsrequired => {editcatalogue => '*'},
88                                     debug => 1,
89                                     });
90
91
92     my $dbh = C4::Context->dbh;
93     my $len = 0;
94     my $sth = $dbh->prepare('SELECT publishercode FROM biblioitems WHERE REPLACE(isbn, "-", "") LIKE ? OR isbn LIKE ? LIMIT 1');
95     
96     $isbn =~ s/-//g;
97     if (length ($isbn) == 13){
98         $isbn = substr($isbn, 3, length($isbn)-3);
99     }
100     
101     if(length($isbn) <= 10){
102
103         $len = 5;
104         if ( substr( $isbn, 0, 1 ) <= 7 ){
105             $len = 1;
106         }elsif ( substr( $isbn, 0, 2 ) <= 94 ){
107             $len = 2;
108         }elsif ( substr( $isbn, 0, 3 ) <= 995 ){
109             $len = 3;
110         }elsif ( substr( $isbn, 0, 4 ) <= 9989 ){
111             $len = 4 ;
112         }
113
114         my $x = substr( $isbn, $len );
115         my $seg2 = "";
116         
117         if ( substr( $x, 0, 2 ) <= 19 ) {    
118             $seg2 = substr( $x, 0, 2 );
119         }
120         elsif ( substr( $x, 0, 3 ) <= 699 ) {
121             $seg2 = substr( $x, 0, 3 );
122         }
123         elsif ( substr( $x, 0, 4 ) <= 8399 ) {
124             $seg2 = substr( $x, 0, 4 );
125         }
126         elsif ( substr( $x, 0, 5 ) <= 89999 ) {
127             $seg2 = substr( $x, 0, 5 );
128         }
129         elsif ( substr( $x, 0, 6 ) <= 9499999 ) {
130             $seg2 = substr( $x, 0, 6 );
131         }
132         else {
133             $seg2 = substr( $x, 0, 7 );
134         }
135
136         while($len--){
137             $seg2 = "_".$seg2;
138         }
139         
140         $len = 10 -length($seg2);
141         
142         while($len--){
143             $seg2 .= "_";
144         }
145
146         $sth->execute($seg2, "978$seg2");
147     }
148     
149     if( (my $publishercode) = $sth->fetchrow )
150     {
151         $template->param(return => $publishercode);
152     }
153     output_html_with_http_headers $input, $cookie, $template->output;
154 }
155 1;