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