3 # Copyright 2012 CatalystIT Ltd
5 # This file is part of Koha.
7 # Koha is free software; you can redistribute it and/or modify it
8 # under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 3 of the License, or
10 # (at your option) any later version.
12 # Koha is distributed in the hope that it will be useful, but
13 # WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with Koha; if not, see <http://www.gnu.org/licenses>.
28 Is used for callnumber computation.
30 User must supply a letter prefix (unspecified length) followed by an empty space followed by a "number".
31 "Number" is 4 character long, and is either a number sequence which is 01 padded.
32 If input does not conform with this format any processing is omitted.
34 Some examples of legal values that trigger auto allocation:
36 AAA 0 - returns first unused number AAA 0xxx starting with AAA 0001
37 BBB 12 - returns first unused number BBB 12xx starting with BBB 1201
38 CCC QW - returns first unused number CCC QWxx starting with CCC QW01
42 sub plugin_javascript {
43 my ($dbh,$record,$tagslib,$field_number,$tabloop) = @_;
45 <script type='text/javascript'>
46 function Clic$field_number() {
47 var code = document.getElementById('$field_number');
48 var url = '../cataloguing/plugin_launcher.pl?plugin_name=callnumber-KU.pl&code=' + code.value;
49 var req = \$.get(url);
50 req.done(function(resp){
59 return ($field_number,$res);
62 my $BASE_CALLNUMBER_RE = qr/^(\w+) (\w+)$/;
65 my $code = $input->param('code');
67 my ($template, $loggedinuser, $cookie) = get_template_and_user({
68 template_name => "cataloguing/value_builder/ajax.tt",
72 flagsrequired => {editcatalogue => '*'},
77 my ($alpha, $num) = ($code =~ $BASE_CALLNUMBER_RE);
78 if (defined $num) { # otherwise no point
79 my ($num_alpha, $num_num) = ($num =~ m/^(\D+)?(\d+)?$/);
81 my $pad_len = 4 - length($num);
84 my $num_padded = $num_num;
85 $num_padded .= "0" x ($pad_len - 1) if $pad_len > 1;
87 my $padded = "$alpha $num_alpha" . $num_padded;
89 my $dbh = C4::Context->dbh;
90 if ( my $first = $dbh->selectrow_array("SELECT itemcallnumber
92 WHERE itemcallnumber = ?", undef, $padded) ) {
93 my $icn = $dbh->selectcol_arrayref("SELECT DISTINCT itemcallnumber
95 WHERE itemcallnumber LIKE ?
96 AND itemcallnumber > ?
97 ORDER BY itemcallnumber", undef, "$alpha $num_alpha%", $first);
98 my $next = $num_padded + 1;
99 my $len = length($num_padded);
101 my ($num1) = ( m/(\d+)$/o );
102 if ($num1 > $next) { # a hole in numbering found, stop
107 $ret = "$alpha $num_alpha" . sprintf("%0${len}d", $next) if length($next) <= $len; # no overflow
116 return => $ret || $code
118 output_html_with_http_headers $input, $cookie, $template->output;