Bug 7045: Use <<tag>> for default value placeholders
[koha.git] / cataloguing / value_builder / callnumber-KU.pl
1 #!/usr/bin/perl
2
3 # Copyright 2012 CatalystIT Ltd
4 #
5 # This file is part of Koha.
6 #
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.
11 #
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.
16 #
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>.
19
20 use strict;
21 use warnings;
22 use C4::Auth;
23 use CGI qw ( -utf8 );
24 use C4::Context;
25 use C4::Output;
26
27 =head1 DESCRIPTION
28
29 Is used for callnumber computation.
30
31 User must supply a letter prefix (unspecified length) followed by an empty space followed by a "number".
32 "Number" is 4 character long, and is either a number sequence which is 01 padded.
33 If input does not conform with this format any processing is omitted.
34
35 Some examples of legal values that trigger auto allocation:
36
37 AAA 0  - returns first unused number AAA 0xxx starting with AAA 0001
38 BBB 12 - returns first unused number BBB 12xx starting with BBB 1201
39 CCC QW - returns first unused number CCC QWxx starting with CCC QW01
40
41 =cut
42
43 sub plugin_javascript {
44     my ($dbh,$record,$tagslib,$field_number,$tabloop) = @_;
45     my $res="
46     <script type='text/javascript'>
47         function Clic$field_number() {
48                 var code = document.getElementById('$field_number');
49                 var url = '../cataloguing/plugin_launcher.pl?plugin_name=callnumber-KU.pl&code=' + code.value;
50                 var req = \$.get(url);
51                 req.done(function(resp){
52                     code.value = resp;
53                     return 1;
54                 });
55             return 1;
56         }
57     </script>
58     ";
59
60     return ($field_number,$res);
61 }
62
63 my $BASE_CALLNUMBER_RE = qr/^(\w+) (\w+)$/;
64 sub plugin {
65     my ($input) = @_;
66     my $code = $input->param('code');
67
68     my ($template, $loggedinuser, $cookie) = get_template_and_user({
69         template_name   => "cataloguing/value_builder/ajax.tt",
70         query           => $input,
71         type            => "intranet",
72         authnotrequired => 0,
73         flagsrequired   => {editcatalogue => '*'},
74         debug           => 1,
75     });
76
77     my $ret;
78     my ($alpha, $num) = ($code =~ $BASE_CALLNUMBER_RE);
79     if (defined $num) { # otherwise no point
80         my ($num_alpha, $num_num) = ($num =~ m/^(\D+)?(\d+)?$/);
81         $num_alpha ||= '';
82         my $pad_len = 4 - length($num);
83
84         if ($pad_len > 0) {
85             my $num_padded = $num_num;
86             $num_padded .= "0" x ($pad_len - 1) if $pad_len > 1;
87             $num_padded .= "1";
88             my $padded = "$alpha $num_alpha" . $num_padded;
89
90             my $dbh = C4::Context->dbh;
91             if ( my $first = $dbh->selectrow_array("SELECT itemcallnumber
92                                                     FROM items
93                                                     WHERE itemcallnumber = ?", undef, $padded) ) {
94                 my $icn = $dbh->selectcol_arrayref("SELECT DISTINCT itemcallnumber
95                                                     FROM items
96                                                     WHERE itemcallnumber LIKE ?
97                                                       AND itemcallnumber >   ?
98                                                     ORDER BY itemcallnumber", undef, "$alpha $num_alpha%", $first);
99                 my $next = $num_padded + 1;
100                 my $len = length($num_padded);
101                 foreach (@$icn) {
102                     my ($num1) = ( m/(\d+)$/o );
103                     if ($num1 > $next) { # a hole in numbering found, stop
104                         last;
105                     }
106                     $next++;
107                 }
108                 $ret = "$alpha $num_alpha" . sprintf("%0${len}d", $next) if length($next) <= $len; # no overflow
109             }
110             else {
111                 $ret = $padded;
112             }
113         }
114     }
115
116     $template->param(
117         return => $ret || $code
118     );
119     output_html_with_http_headers $input, $cookie, $template->output;
120 }