2 # Copyright 2000-2002 Katipo Communications
3 # Parts copyright 2008-2010 Foundations Bible College
5 # This file is part of Koha.
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
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.
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.
22 no warnings 'redefine'; # otherwise loading up multiple plugins fills the log with subroutine redefine warnings
31 plugin_parameters : other parameters added when the plugin is called by the dopop function
35 sub plugin_parameters {
36 # my ($dbh,$record,$tagslib,$i,$tabloop) = @_;
42 plugin_javascript : the javascript function called when the user enters the subfield.
43 contain 3 javascript functions :
44 * one called when the field is entered (OnFocus). Named FocusXXX
45 * one called when the field is leaved (onBlur). Named BlurXXX
46 * one called when the ... link is clicked (<a href="javascript:function">) named ClicXXX
50 * a variable containing the 3 scripts.
51 the 3 scripts are inserted after the <input> in the html code
55 sub plugin_javascript {
56 my ($dbh,$record,$tagslib,$field_number,$tabloop) = @_;
57 my $function_name= "barcode".(int(rand(100000))+1);
60 my ($year, $mon, $day) = split('-', C4::Dates->today('iso'));
61 my ($tag,$subfield) = GetMarcFromKohaField("items.barcode", '');
62 my ($loctag,$locsubfield) = GetMarcFromKohaField("items.homebranch", '');
67 my $autoBarcodeType = C4::Context->preference("autoBarcode");
68 warn "Barcode type = $autoBarcodeType" if $DEBUG;
69 if ((not $autoBarcodeType) or $autoBarcodeType eq 'OFF') {
70 # don't return a value unless we have the appropriate syspref set
71 return ($function_name,
72 "<script type=\"text/javascript\">
73 // autoBarcodeType OFF (or not defined)
74 function Focus$function_name() { return 0;}
75 function Clic$function_name() { return 0;}
76 function Blur$function_name() { return 0;}
79 if ($autoBarcodeType eq 'annual') {
80 $query = "select max(cast( substring_index(barcode, '-',-1) as signed)) from items where barcode like ?";
81 my $sth=$dbh->prepare($query);
82 $sth->execute("$year%");
83 while (my ($count)= $sth->fetchrow_array) {
84 warn "Examining Record: $count" if $DEBUG;
85 $nextnum = $count if $count;
88 $nextnum = sprintf("%0*d", "4",$nextnum);
89 $nextnum = "$year-$nextnum";
91 elsif ($autoBarcodeType eq 'incremental') {
92 # not the best, two catalogers could add the same barcode easily this way :/
93 $query = "select max(abs(barcode)) from items";
94 my $sth = $dbh->prepare($query);
96 while (my ($count)= $sth->fetchrow_array) {
101 elsif ($autoBarcodeType eq 'hbyymmincr') { # Generates a barcode where hb = home branch Code, yymm = year/month catalogued, incr = incremental number, reset yearly -fbcit
102 $year = substr($year, -2);
103 $query = "SELECT MAX(CAST(SUBSTRING(barcode,-4) AS signed)) AS number FROM items WHERE barcode REGEXP ?";
104 my $sth = $dbh->prepare($query);
105 $sth->execute("^[a-zA-Z]{1,}$year");
106 while (my ($count)= $sth->fetchrow_array) {
107 $nextnum = $count if $count;
108 $nextnum = 0 if $nextnum == 9999; # this sequence only allows for cataloging 10000 books per month
109 warn "Existing incremental number = $nextnum" if $DEBUG;
112 $nextnum = sprintf("%0*d", "4",$nextnum);
113 $nextnum = $year . $mon . $nextnum;
114 warn "New hbyymmincr Barcode = $nextnum" if $DEBUG;
116 for (i=0 ; i<document.f.field_value.length ; i++) {
117 if (document.f.tag[i].value == '$loctag' && document.f.subfield[i].value == '$locsubfield') {
121 if (\$('#' + id).val() == '' || force) {
122 \$('#' + id).val(document.f.field_value[fnum].value + '$nextnum');
127 # default js body (if not filled by hbyymmincr)
128 $scr or $scr = <<END_OF_JS;
129 if (\$('#' + id).val() == '' || force) {
130 \$('#' + id).val('$nextnum');
134 my $js = <<END_OF_JS;
135 <script type="text/javascript">
138 function Blur$function_name(index) {
139 //barcode validation might go here
142 function Focus$function_name(subfield_managed, id, force) {
147 function Clic$function_name(id) {
148 return Focus$function_name('not_relavent', id, 1);
153 return ($function_name, $js);