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

(-)a/cataloguing/value_builder/callnumber-KU.pl (-1 / +130 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 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 $dbh = C4::Context->dbh;
95
        my $pad_len = 4 - length($num);
96
        for (my $i = $pad_len - 1; $i >= 0; $i--) {
97
            my $padded = $code . ("0" x $i);
98
            if ( my $max = $dbh->selectrow_array("SELECT MAX(itemcallnumber)
99
                                                  FROM items
100
                                                  WHERE itemcallnumber LIKE ?", undef, "$padded%") ) {
101
                my (undef, $num1) = ($max =~ $BASE_CALLNUMBER_RE);
102
                my ($num_alpha, $num_num) = ($num1 =~ m/^(\D+)?(\d+)$/);
103
                my $len = length($num_num);
104
                $num_num++;
105
                if ($len == length($num_num)) { # no overflow
106
                    $ret = "$alpha $num_alpha" . $num_num;
107
                    last;
108
                }
109
            }
110
            else {
111
                $ret = $padded ;
112
                $pad_len -= $i;
113
                if ($num =~ m/^\d+$/) {
114
                    $ret .= "0" x $pad_len;
115
                } else {
116
                    $ret .= "0" x ($pad_len - 1) if $pad_len > 1;
117
                    $ret .= "1";
118
                }
119
                last;
120
            }
121
        }
122
    }
123
    
124
    $template->param(
125
        return => $ret || $code
126
    );
127
    output_html_with_http_headers $input, $cookie, $template->output;
128
}
129
130
1;

Return to bug 7458