View | Details | Raw Unified | Return to bug 7458
Collapse All | Expand All

(-)a/cataloguing/value_builder/callnumber-KU.pl (-1 / +140 lines)
Line 0 Link Here
0
- 
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 0 padded
32
or two letters which are 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 0000
38
BBB 12 - returns first unused number BBB 12xx starting with BBB 1200
39
CCC QW - returns first unused number CCC QWxx starting with CCC QW01
40
41
=cut
42
43
sub plugin_parameters {
44
}
45
46
sub plugin_javascript {
47
    my ($dbh,$record,$tagslib,$field_number,$tabloop) = @_;
48
    my $res="
49
    <script type='text/javascript'>
50
        function Focus$field_number() {
51
            return 1;
52
        }
53
54
        function Blur$field_number() {
55
                return 1;
56
        }
57
58
        function Clic$field_number() {
59
                var code = document.getElementById('$field_number');
60
                var url = '../cataloguing/plugin_launcher.pl?plugin_name=callnumber-KU.pl&code=' + code.value;
61
                var blurcallbackcallnumber = {
62
                    success: function(o) {
63
                        var field = document.getElementById('$field_number');
64
                        field.value = o.responseText;
65
                        return 1;
66
                    }
67
                }
68
                var transaction = YAHOO.util.Connect.asyncRequest('GET',url, blurcallbackcallnumber, null);
69
            return 1;
70
        }
71
    </script>
72
    ";
73
74
    return ($field_number,$res);
75
}
76
77
my $BASE_CALLNUMBER_RE = qr/^(\w+) (\w+)$/;
78
sub plugin {
79
    my ($input) = @_;
80
    my $code = $input->param('code');
81
82
    my ($template, $loggedinuser, $cookie) = get_template_and_user({
83
        template_name   => "cataloguing/value_builder/ajax.tmpl",
84
        query           => $input,
85
        type            => "intranet",
86
        authnotrequired => 0,
87
        flagsrequired   => {editcatalogue => '*'},
88
        debug           => 1,
89
    });
90
91
    my $ret;
92
    my ($alpha, $num) = ($code =~ $BASE_CALLNUMBER_RE);
93
    if (defined $num) { # otherwise no point
94
        my ($num_alpha, $num_num) = ($num =~ m/^(\D+)?(\d+)?$/);
95
        $num_alpha ||= '';
96
        my $pad_len = 4 - length($num);
97
        
98
        if ($pad_len > 0) {
99
            my $num_padded = $num_num;
100
            if ($num_alpha) {
101
                $num_padded .= "0" x ($pad_len - 1) if $pad_len > 1;
102
                $num_padded .= "1";
103
            } else {
104
                $num_padded .= "0" x $pad_len;
105
            }
106
            my $padded = "$alpha $num_alpha" . $num_padded;
107
108
            my $dbh = C4::Context->dbh;
109
            if ( my $first = $dbh->selectrow_array("SELECT itemcallnumber
110
                                                    FROM items
111
                                                    WHERE itemcallnumber = ?", undef, $padded) ) {
112
                my $icn = $dbh->selectcol_arrayref("SELECT itemcallnumber
113
                                                    FROM items
114
                                                    WHERE itemcallnumber LIKE ?
115
                                                      AND itemcallnumber >   ?
116
                                                    ORDER BY itemcallnumber", undef, "$alpha $num_alpha%", $first);
117
                my $next = $num_padded + 1;
118
                my $len = length($num_padded);
119
                foreach (@$icn) {
120
                    my ($num1) = ( m/(\d+)$/o );
121
                    if ($num1 > $next) { # a hole in numbering found, stop
122
                        last;
123
                    }
124
                    $next++;
125
                }
126
                $ret = "$alpha $num_alpha" . sprintf("%0${len}d", $next) if length($next) <= $len; # no overflow
127
            }
128
            else {
129
                $ret = $padded;
130
            }
131
        }
132
    }
133
    
134
    $template->param(
135
        return => $ret || $code
136
    );
137
    output_html_with_http_headers $input, $cookie, $template->output;
138
}
139
140
1;

Return to bug 7458