Merge remote-tracking branch 'origin/new/bug_7143'
[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 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 use C4::Auth;
23 use CGI;
24 use C4::Context;
25
26 =head1 DESCRIPTION
27
28 Is used for callnumber computation.
29
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.
33
34 Some examples of legal values that trigger auto allocation:
35
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
39
40 =cut
41
42 sub plugin_parameters {
43 }
44
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;
51         }
52
53         function Blur$field_number() {
54                 return 1;
55         }
56
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 blurcallbackcallnumber = {
61                     success: function(o) {
62                         var field = document.getElementById('$field_number');
63                         field.value = o.responseText;
64                         return 1;
65                     }
66                 }
67                 var transaction = YAHOO.util.Connect.asyncRequest('GET',url, blurcallbackcallnumber, null);
68             return 1;
69         }
70     </script>
71     ";
72
73     return ($field_number,$res);
74 }
75
76 my $BASE_CALLNUMBER_RE = qr/^(\w+) (\w+)$/;
77 sub plugin {
78     my ($input) = @_;
79     my $code = $input->param('code');
80
81     my ($template, $loggedinuser, $cookie) = get_template_and_user({
82         template_name   => "cataloguing/value_builder/ajax.tmpl",
83         query           => $input,
84         type            => "intranet",
85         authnotrequired => 0,
86         flagsrequired   => {editcatalogue => '*'},
87         debug           => 1,
88     });
89
90     my $ret;
91     my ($alpha, $num) = ($code =~ $BASE_CALLNUMBER_RE);
92     if (defined $num) { # otherwise no point
93         my ($num_alpha, $num_num) = ($num =~ m/^(\D+)?(\d+)?$/);
94         $num_alpha ||= '';
95         my $pad_len = 4 - length($num);
96
97         if ($pad_len > 0) {
98             my $num_padded = $num_num;
99             $num_padded .= "0" x ($pad_len - 1) if $pad_len > 1;
100             $num_padded .= "1";
101             my $padded = "$alpha $num_alpha" . $num_padded;
102
103             my $dbh = C4::Context->dbh;
104             if ( my $first = $dbh->selectrow_array("SELECT itemcallnumber
105                                                     FROM items
106                                                     WHERE itemcallnumber = ?", undef, $padded) ) {
107                 my $icn = $dbh->selectcol_arrayref("SELECT DISTINCT itemcallnumber
108                                                     FROM items
109                                                     WHERE itemcallnumber LIKE ?
110                                                       AND itemcallnumber >   ?
111                                                     ORDER BY itemcallnumber", undef, "$alpha $num_alpha%", $first);
112                 my $next = $num_padded + 1;
113                 my $len = length($num_padded);
114                 foreach (@$icn) {
115                     my ($num1) = ( m/(\d+)$/o );
116                     if ($num1 > $next) { # a hole in numbering found, stop
117                         last;
118                     }
119                     $next++;
120                 }
121                 $ret = "$alpha $num_alpha" . sprintf("%0${len}d", $next) if length($next) <= $len; # no overflow
122             }
123             else {
124                 $ret = $padded;
125             }
126         }
127     }
128
129     $template->param(
130         return => $ret || $code
131     );
132     output_html_with_http_headers $input, $cookie, $template->output;
133 }
134
135 1;