Merge remote-tracking branch 'origin/new/bug_7805'
[koha.git] / cataloguing / value_builder / barcode_manual.pl
1 #!/usr/bin/perl
2 # Copyright 2000-2002 Katipo Communications
3 # Parts copyright 2008-2010 Foundations Bible College
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 2 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use strict;
21 use warnings;
22 no warnings 'redefine'; # otherwise loading up multiple plugins fills the log with subroutine redefine warnings
23
24 use C4::Context;
25 require C4::Barcodes::ValueBuilder;
26 require C4::Dates;
27
28 my $DEBUG = 0;
29
30 =head1
31
32 plugin_parameters : 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 =head1
42
43 plugin_javascript : the javascript function called when the user enters the subfield.
44 contain 3 javascript functions :
45 * one called when the field is entered (OnFocus). Named FocusXXX
46 * one called when the field is leaved (onBlur). Named BlurXXX
47 * one called when the ... link is clicked (<a href="javascript:function">) named ClicXXX
48
49 returns :
50 * XXX
51 * a variable containing the 3 scripts.
52 the 3 scripts are inserted after the <input> in the html code
53
54 =cut
55
56 sub plugin_javascript {
57     my ($dbh,$record,$tagslib,$field_number,$tabloop) = @_;
58     my $function_name= "barcode".(int(rand(100000))+1);
59     my %args;
60
61     $args{dbh} = $dbh;
62
63 # find today's date
64     ($args{year}, $args{mon}, $args{day}) = split('-', C4::Dates->today('iso'));
65     ($args{tag},$args{subfield})       =  GetMarcFromKohaField("items.barcode", '');
66     ($args{loctag},$args{locsubfield}) =  GetMarcFromKohaField("items.homebranch", '');
67
68     my $nextnum;
69     my $scr;
70     my $autoBarcodeType = C4::Context->preference("autoBarcode");
71     warn "Barcode type = $autoBarcodeType" if $DEBUG;
72     if ((not $autoBarcodeType) or $autoBarcodeType eq 'OFF') {
73 # don't return a value unless we have the appropriate syspref set
74         return ($function_name,
75                 "<script type=\"text/javascript\">
76                 // autoBarcodeType OFF (or not defined)
77                 function Focus$function_name() { return 0;}
78                 function  Clic$function_name() { return 0;}
79                 function  Blur$function_name() { return 0;}
80                 </script>");
81     }
82     if ($autoBarcodeType eq 'annual') {
83         ($nextnum, $scr) = C4::Barcodes::ValueBuilder::annual::get_barcode(\%args);
84     }
85     elsif ($autoBarcodeType eq 'incremental') {
86         ($nextnum, $scr) = C4::Barcodes::ValueBuilder::incremental::get_barcode(\%args);
87     }
88     elsif ($autoBarcodeType eq 'hbyymmincr') {      # Generates a barcode where hb = home branch Code, yymm = year/month catalogued, incr = incremental number, reset yearly -fbcit
89         ($nextnum, $scr) = C4::Barcodes::ValueBuilder::hbyymmincr::get_barcode(\%args);
90     }
91
92 # default js body (if not filled by hbyymmincr)
93     $scr or $scr = <<END_OF_JS;
94     if (\$('#' + id).val() == '') {
95         \$('#' + id).val('$nextnum');
96     }
97 END_OF_JS
98
99         my $js  = <<END_OF_JS;
100     <script type="text/javascript">
101         //<![CDATA[
102
103         function Blur$function_name(index) {
104             //barcode validation might go here
105         }
106
107     function Focus$function_name(subfield_managed, id, force) {
108         return 0;
109     }
110
111     function Clic$function_name(id) {
112         $scr
113             return 0;
114     }
115     //]]>
116     </script>
117 END_OF_JS
118         return ($function_name, $js);
119 }
120
121 =head1
122
123 plugin: useless here
124
125 =cut
126
127 sub plugin {
128 # my ($input) = @_;
129     return "";
130 }
131
132 1;