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

(-)a/cataloguing/value_builder/callnumber-KU.pl (-1 / +127 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# Copyright 2010 BibLibre SARL
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 i4 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 largest number AAA 0xxx incremented by 1 or AAA 0000
38
BBB 12 - returns largest number BBB 12xx incremented by 1 or BBB 1200
39
CCC QW - returns largest number CCC QWxx incremented by 1 or 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 $dbh = C4::Context->dbh;
95
        if ( my $max = $dbh->selectrow_array("SELECT MAX(itemcallnumber)
96
                                              FROM items
97
                                              WHERE itemcallnumber LIKE ?", undef, "$code%") ) {
98
            (undef, $num) = ($max =~ $BASE_CALLNUMBER_RE);
99
            my ($num_alpha, $num_num) = ($num =~ m/^(\D+)?(\d+)$/);
100
            my $len = length($num_num);
101
            $num_num++;
102
            if ($len == length($num_num)) { # no overflow
103
                $ret = $num_alpha . $num_num
104
            }
105
        }
106
        else { # pad with 0
107
            my $pad_len = 4 - length($num);
108
            if ($pad_len > 0) {
109
                if ($num =~ m/^\d+$/) {
110
                    $ret = $num. ("0" x $pad_len);
111
                } else {
112
                    $ret = $num ;
113
                    $ret .= "0" x ($pad_len - 1) if $pad_len > 1;
114
                    $ret .= "1";
115
                }
116
            }
117
        }
118
    }
119
    $ret = "$alpha $ret" if $ret;
120
    
121
    $template->param(
122
        return => $ret || $code
123
    );
124
    output_html_with_http_headers $input, $cookie, $template->output;
125
}
126
127
1;

Return to bug 7458