|
Line 0
Link Here
|
| 0 |
- |
1 |
#!/usr/bin/perl |
|
|
2 |
|
| 3 |
# Converted to new plugin style (Bug 13437) |
| 4 |
|
| 5 |
# Copyright 2012 BibLibre SARL |
| 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::Output qw( output_html_with_http_headers ); |
| 27 |
use C4::Koha qw( GetAuthorisedValues ); |
| 28 |
|
| 29 |
=head1 DESCRIPTION |
| 30 |
|
| 31 |
This plugin is based on authorised values from INVENTORY. |
| 32 |
It is used for stocknumber computation. |
| 33 |
|
| 34 |
If no prefix is submitted, or the prefix does contain only |
| 35 |
numbers, it returns the inserted code (= keep the field unchanged). |
| 36 |
|
| 37 |
If a prefix is submitted, we look for the highest stocknumber |
| 38 |
with this prefix and return it incremented. |
| 39 |
|
| 40 |
In this case, a stocknumber has this form (e.g. "PREFIX 9678570"): |
| 41 |
PREFIX containing letters, a space separator and incremential value. |
| 42 |
|
| 43 |
=cut |
| 44 |
|
| 45 |
my $builder = sub { |
| 46 |
my ($params) = @_; |
| 47 |
|
| 48 |
my @options_array; |
| 49 |
my $av = GetAuthorisedValues("INVENTORY_SPACING"); |
| 50 |
|
| 51 |
push @options_array, "<option value=''>Select an authorised value</option>"; |
| 52 |
for my $r (@$av) { |
| 53 |
push @options_array, "<option value='$r->{authorised_value}'>$r->{authorised_value}</option>"; |
| 54 |
} |
| 55 |
|
| 56 |
my $options = join '', @options_array; |
| 57 |
|
| 58 |
my $res = qq{ |
| 59 |
<script> |
| 60 |
function Click$params->{id}(ev) { |
| 61 |
ev.preventDefault(); |
| 62 |
|
| 63 |
var select = document.createElement('select'); |
| 64 |
const csrf_token = document.querySelector('meta[name="csrf-token"]').getAttribute('content'); |
| 65 |
select.id = 'inventory_spacing_select'; |
| 66 |
|
| 67 |
select.innerHTML = "$options"; |
| 68 |
var anchor = ev.target; |
| 69 |
anchor.parentNode.replaceChild(select, anchor); |
| 70 |
|
| 71 |
select.addEventListener('click', function() { |
| 72 |
var code = select.value; |
| 73 |
\$.ajax({ |
| 74 |
url: '/cgi-bin/koha/cataloguing/plugin_launcher.pl', |
| 75 |
type: 'POST', |
| 76 |
data: { |
| 77 |
'plugin_name': 'stocknumberAV_spacing.pl', |
| 78 |
'code': code, |
| 79 |
'csrf_token': csrf_token |
| 80 |
}, |
| 81 |
success: function(data) { |
| 82 |
if (ev.data && ev.data.id) { |
| 83 |
var field = document.getElementById(ev.data.id); |
| 84 |
if (field) { |
| 85 |
field.value = data; |
| 86 |
return 1; |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
}); |
| 91 |
}); |
| 92 |
} |
| 93 |
</script> |
| 94 |
}; |
| 95 |
|
| 96 |
return $res; |
| 97 |
}; |
| 98 |
|
| 99 |
my $launcher = sub { |
| 100 |
my ($params) = @_; |
| 101 |
my $input = $params->{cgi}; |
| 102 |
my $code = $input->param('code'); |
| 103 |
|
| 104 |
my ( $template, $loggedinuser, $cookie ) = get_template_and_user( |
| 105 |
{ |
| 106 |
template_name => "cataloguing/value_builder/ajax.tt", |
| 107 |
query => $input, |
| 108 |
type => "intranet", |
| 109 |
flagsrequired => { editcatalogue => '*' }, |
| 110 |
} |
| 111 |
); |
| 112 |
|
| 113 |
# If a prefix is submited, we look for the highest stocknumber with this prefix, and return it incremented |
| 114 |
$code =~ s/ *$//g; |
| 115 |
if ( $code =~ m/^[a-zA-Z]+$/ ) { |
| 116 |
my $av = Koha::AuthorisedValues->find( |
| 117 |
{ |
| 118 |
'category' => 'INVENTORY_SPACING', |
| 119 |
'authorised_value' => $code |
| 120 |
} |
| 121 |
); |
| 122 |
if ($av) { |
| 123 |
$av->lib( $av->lib + 1 ); |
| 124 |
$av->store; |
| 125 |
$template->param( return => $code . ' ' . sprintf( '%s', ( $av->lib ) ), ); |
| 126 |
} else { |
| 127 |
$template->param( return => "There is no defined value for $code" ); |
| 128 |
} |
| 129 |
|
| 130 |
# The user entered a custom value, we don't touch it, this could be handled in js |
| 131 |
} else { |
| 132 |
$template->param( return => $code, ); |
| 133 |
} |
| 134 |
|
| 135 |
output_html_with_http_headers $input, $cookie, $template->output; |
| 136 |
}; |
| 137 |
|
| 138 |
return { builder => $builder, launcher => $launcher }; |