Bug 24879: Add check_cookie_auth when missing
[koha.git] / cataloguing / value_builder / callnumber-KU.pl
1 #!/usr/bin/perl
2
3 # Converted to new plugin style (Bug 13437)
4
5 # Copyright 2012 CatalystIT Ltd
6 #
7 # This file is part of Koha.
8 #
9 # Koha is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3 of the License, or
12 # (at your option) any later version.
13 #
14 # Koha is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with Koha; if not, see <http://www.gnu.org/licenses>.
21
22 use Modern::Perl;
23 use CGI qw ( -utf8 );
24
25 use C4::Auth qw( get_template_and_user );
26 use C4::Context;
27 use C4::Output qw( output_html_with_http_headers );
28
29 =head1 DESCRIPTION
30
31 Is used for callnumber computation.
32
33 User must supply a letter prefix (unspecified length) followed by an empty space followed by a "number".
34 "Number" is 4 character long, and is either a number sequence which is 01 padded.
35 If input does not conform with this format any processing is omitted.
36
37 Some examples of legal values that trigger auto allocation:
38
39 AAA 0  - returns first unused number AAA 0xxx starting with AAA 0001
40 BBB 12 - returns first unused number BBB 12xx starting with BBB 1201
41 CCC QW - returns first unused number CCC QWxx starting with CCC QW01
42
43 =cut
44
45 my $builder = sub {
46     my ( $params ) = @_;
47     my $res="
48     <script>
49         function Click$params->{id}(ev) {
50                 ev.preventDefault();
51                 var code = document.getElementById(ev.data.id);
52                 var url = '../cataloguing/plugin_launcher.pl?plugin_name=callnumber-KU.pl&code=' + code.value;
53                 var req = \$.get(url);
54                 req.done(function(resp){
55                     code.value = resp;
56                     return 1;
57                 });
58             return 1;
59         }
60     </script>
61     ";
62     return $res;
63 };
64
65 my $launcher = sub {
66     my ( $params ) = @_;
67     my $input = $params->{cgi};
68     my $code = $input->param('code');
69
70     my ($template, $loggedinuser, $cookie) = get_template_and_user({
71         template_name   => "cataloguing/value_builder/ajax.tt",
72         query           => $input,
73         type            => "intranet",
74         flagsrequired   => {editcatalogue => '*'},
75     });
76
77     my $BASE_CALLNUMBER_RE = qr/^(\w+) (\w+)$/;
78     my $ret;
79     my ($alpha, $num) = ($code =~ $BASE_CALLNUMBER_RE);
80     if (defined $num) { # otherwise no point
81         my ($num_alpha, $num_num) = ($num =~ m/^(\D+)?(\d+)?$/);
82         $num_alpha ||= '';
83         my $pad_len = 4 - length($num);
84
85         if ($pad_len > 0) {
86             my $num_padded = $num_num;
87             $num_padded .= "0" x ($pad_len - 1) if $pad_len > 1;
88             $num_padded .= "1";
89             my $padded = "$alpha $num_alpha" . $num_padded;
90
91             my $dbh = C4::Context->dbh;
92             if ( my $first = $dbh->selectrow_array("SELECT itemcallnumber
93                                                     FROM items
94                                                     WHERE itemcallnumber = ?", undef, $padded) ) {
95                 my $icn = $dbh->selectcol_arrayref("SELECT DISTINCT itemcallnumber
96                                                     FROM items
97                                                     WHERE itemcallnumber LIKE ?
98                                                       AND itemcallnumber >   ?
99                                                     ORDER BY itemcallnumber", undef, "$alpha $num_alpha%", $first);
100                 my $next = $num_padded + 1;
101                 my $len = length($num_padded);
102                 foreach (@$icn) {
103                     my ($num1) = ( m/(\d+)$/o );
104                     if ($num1 > $next) { # a hole in numbering found, stop
105                         last;
106                     }
107                     $next++;
108                 }
109                 $ret = "$alpha $num_alpha" . sprintf("%0${len}d", $next) if length($next) <= $len; # no overflow
110             }
111             else {
112                 $ret = $padded;
113             }
114         }
115     }
116
117     $template->param(
118         return => $ret || $code
119     );
120     output_html_with_http_headers $input, $cookie, $template->output;
121 };
122
123 return { builder => $builder, launcher => $launcher };