Bug 13748: Acquisition wizard: some strings not translatable
[koha.git] / cataloguing / value_builder / callnumber-KU.pl
blob4a0dd773c139a5bf21bfa3ecbe555f58f285ae09
1 #!/usr/bin/perl
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 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.
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.
20 use strict;
21 use warnings;
22 use C4::Auth;
23 use CGI qw ( -utf8 );
24 use C4::Context;
26 =head1 DESCRIPTION
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
40 =cut
42 sub plugin_parameters {
45 sub plugin_javascript {
46 my ($dbh,$record,$tagslib,$field_number,$tabloop) = @_;
47 my $res="
48 <script type='text/javascript'>
49 function Focus$field_number() {
50 return 1;
53 function Blur$field_number() {
54 return 1;
57 function Clic$field_number() {
58 var code = document.getElementById('$field_number');
59 var url = '../cataloguing/plugin_launcher.pl?plugin_name=callnumber-KU.pl&code=' + code.value;
60 var req = \$.get(url);
61 req.done(function(resp){
62 code.value = resp;
63 return 1;
64 });
65 return 1;
67 </script>
70 return ($field_number,$res);
73 my $BASE_CALLNUMBER_RE = qr/^(\w+) (\w+)$/;
74 sub plugin {
75 my ($input) = @_;
76 my $code = $input->param('code');
78 my ($template, $loggedinuser, $cookie) = get_template_and_user({
79 template_name => "cataloguing/value_builder/ajax.tt",
80 query => $input,
81 type => "intranet",
82 authnotrequired => 0,
83 flagsrequired => {editcatalogue => '*'},
84 debug => 1,
85 });
87 my $ret;
88 my ($alpha, $num) = ($code =~ $BASE_CALLNUMBER_RE);
89 if (defined $num) { # otherwise no point
90 my ($num_alpha, $num_num) = ($num =~ m/^(\D+)?(\d+)?$/);
91 $num_alpha ||= '';
92 my $pad_len = 4 - length($num);
94 if ($pad_len > 0) {
95 my $num_padded = $num_num;
96 $num_padded .= "0" x ($pad_len - 1) if $pad_len > 1;
97 $num_padded .= "1";
98 my $padded = "$alpha $num_alpha" . $num_padded;
100 my $dbh = C4::Context->dbh;
101 if ( my $first = $dbh->selectrow_array("SELECT itemcallnumber
102 FROM items
103 WHERE itemcallnumber = ?", undef, $padded) ) {
104 my $icn = $dbh->selectcol_arrayref("SELECT DISTINCT itemcallnumber
105 FROM items
106 WHERE itemcallnumber LIKE ?
107 AND itemcallnumber > ?
108 ORDER BY itemcallnumber", undef, "$alpha $num_alpha%", $first);
109 my $next = $num_padded + 1;
110 my $len = length($num_padded);
111 foreach (@$icn) {
112 my ($num1) = ( m/(\d+)$/o );
113 if ($num1 > $next) { # a hole in numbering found, stop
114 last;
116 $next++;
118 $ret = "$alpha $num_alpha" . sprintf("%0${len}d", $next) if length($next) <= $len; # no overflow
120 else {
121 $ret = $padded;
126 $template->param(
127 return => $ret || $code
129 output_html_with_http_headers $input, $cookie, $template->output;