4 # Copyright 2000-2002 Katipo Communications
6 # This file is part of Koha.
8 # Koha is free software; you can redistribute it and/or modify it
9 # under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 3 of the License, or
11 # (at your option) any later version.
13 # Koha is distributed in the hope that it will be useful, but
14 # WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with Koha; if not, see <http://www.gnu.org/licenses>.
23 use Try::Tiny qw( catch try );
25 use C4::Auth qw( get_template_and_user haspermission );
26 use C4::Output qw( output_html_with_http_headers );
27 use C4::Circulation qw( barcodedecode );
30 use List::MoreUtils qw( uniq );
31 use Encode qw( encode_utf8 );
34 use Koha::DateUtils qw( dt_from_string );
35 use Koha::Exceptions::Exception;
39 use Koha::Item::Attributes;
40 use Koha::BackgroundJob::BatchDeleteItem;
41 use Koha::BackgroundJob::BatchUpdateItem;
42 use Koha::UI::Form::Builder::Item;
43 use Koha::UI::Table::Builder::Items;
46 my $dbh = C4::Context->dbh;
47 my $error = $input->param('error');
48 my @itemnumbers = $input->multi_param('itemnumber');
49 my $biblionumber = $input->param('biblionumber');
50 my $op = $input->param('op');
51 my $del = $input->param('del');
52 my $del_records = $input->param('del_records');
53 my $src = $input->param('src');
54 my $use_default_values = $input->param('use_default_values');
55 my $exclude_from_local_holds_priority = $input->param('exclude_from_local_holds_priority');
60 $template_name = "tools/batchMod.tt";
61 $template_flag = { tools => '*' };
64 $template_name = ($del) ? "tools/batchMod-del.tt" : "tools/batchMod-edit.tt";
65 $template_flag = ($del) ? { tools => 'items_batchdel' } : { tools => 'items_batchmod' };
68 my ($template, $loggedinuser, $cookie)
69 = get_template_and_user({template_name => $template_name,
72 flagsrequired => $template_flag,
75 $template->param( searchid => scalar $input->param('searchid'), );
77 # Does the user have a restricted item edition permission?
78 my $uid = $loggedinuser ? Koha::Patrons->find( $loggedinuser )->userid : undef;
79 my $restrictededition = $uid ? haspermission($uid, {'tools' => 'items_batchmod_restricted'}) : undef;
80 # In case user is a superlibrarian, edition is not restricted
81 $restrictededition = 0 if ($restrictededition != 0 && C4::Context->IsSuperLibrarian());
86 my %cookies = parse CGI::Cookie($cookie);
87 my $sessionID = $cookies{'CGISESSID'}->value;
91 if ( $op eq "action" ) {
96 record_ids => \@itemnumbers,
97 delete_biblios => $del_records,
100 Koha::BackgroundJob::BatchDeleteItem->new->enqueue($params);
101 $nextop = 'enqueued';
102 $template->param( job_id => $job_id, );
109 code => 'cannot_enqueue_job',
112 $template->param( view => 'errors' );
116 else { # modification
118 my @item_columns = Koha::Items->columns;
121 my ( $columns_with_regex );
122 my @subfields_to_blank = $input->multi_param('disable_input');
123 my @more_subfields = $input->multi_param("items.more_subfields_xml");
124 for my $item_column (@item_columns) {
125 my @attributes = ($item_column);
126 my $cgi_param_prefix = 'items.';
127 if ( $item_column eq 'more_subfields_xml' ) {
129 $cgi_param_prefix = 'items.more_subfields_xml_';
130 for my $subfield (@more_subfields) {
131 push @attributes, $subfield;
135 for my $attr (@attributes) {
137 my $cgi_var_name = $cgi_param_prefix
139 ; # We need to deal correctly with encoding on subfield codes
141 if ( grep { $attr eq $_ } @subfields_to_blank ) {
144 $new_item_data->{$attr} = undef;
146 elsif ( my $regex_search =
147 $input->param( $cgi_var_name . '_regex_search' ) )
149 $columns_with_regex->{$attr} = {
150 search => $regex_search,
152 $input->param( $cgi_var_name . '_regex_replace' ),
154 $input->param( $cgi_var_name . '_regex_modifiers' )
159 grep { $_ ne "" } uniq $input->multi_param($cgi_var_name);
163 $new_item_data->{$attr} = join '|', @v;
169 record_ids => \@itemnumbers,
170 regex_mod => $columns_with_regex,
171 new_values => $new_item_data,
172 exclude_from_local_holds_priority => (
173 defined $exclude_from_local_holds_priority
174 && $exclude_from_local_holds_priority ne ""
176 ? $exclude_from_local_holds_priority
182 Koha::BackgroundJob::BatchUpdateItem->new->enqueue($params);
183 $nextop = 'enqueued';
184 $template->param( job_id => $job_id, );
190 code => 'cannot_enqueue_job',
193 $template->param( view => 'errors' );
200 messages => \@messages,
203 #-------------------------------------------------------------------------------
204 # build screen with existing items. and "new" one
205 #-------------------------------------------------------------------------------
208 my $filefh = $input->upload('uploadfile');
209 my $filecontent = $input->param('filecontent');
210 my ( @notfoundbarcodes, @notfounditemnumbers);
212 my $split_chars = C4::Context->preference('BarcodeSeparators');
214 binmode $filefh, ':encoding(UTF-8)';
216 while (my $content=<$filefh>){
217 $content =~ s/[\r\n]*$//;
218 push @contentlist, $content if $content;
221 if ($filecontent eq 'barcode_file') {
222 @contentlist = grep /\S/, ( map { split /[$split_chars]/ } @contentlist );
223 @contentlist = uniq @contentlist;
224 # Note: adding lc for case insensitivity
225 my %itemdata = map { lc($_->{barcode}) => $_->{itemnumber} } @{ Koha::Items->search({ barcode => \@contentlist }, { columns => [ 'itemnumber', 'barcode' ] } )->unblessed };
226 @itemnumbers = map { exists $itemdata{lc $_} ? $itemdata{lc $_} : () } @contentlist;
227 @notfoundbarcodes = grep { !exists $itemdata{lc $_} } @contentlist;
229 elsif ( $filecontent eq 'itemid_file') {
230 @contentlist = uniq @contentlist;
231 my %itemdata = map { $_->{itemnumber} => 1 } @{ Koha::Items->search({ itemnumber => \@contentlist }, { columns => [ 'itemnumber' ] } )->unblessed };
232 @itemnumbers = grep { exists $itemdata{$_} } @contentlist;
233 @notfounditemnumbers = grep { !exists $itemdata{$_} } @contentlist;
236 if (defined $biblionumber && !@itemnumbers){
237 my $biblio = Koha::Biblios->find($biblionumber);
238 @itemnumbers = $biblio ? $biblio->items->get_column('itemnumber') : ();
240 if ( my $list = $input->param('barcodelist') ) {
241 my @barcodelist = grep /\S/, ( split /[$split_chars]/, $list );
242 @barcodelist = uniq @barcodelist;
244 @barcodelist = map { barcodedecode( $_ ) } @barcodelist;
246 # Note: adding lc for case insensitivity
247 my %itemdata = map { lc($_->{barcode}) => $_->{itemnumber} } @{ Koha::Items->search({ barcode => \@barcodelist }, { columns => [ 'itemnumber', 'barcode' ] } )->unblessed };
248 @itemnumbers = map { exists $itemdata{lc $_} ? $itemdata{lc $_} : () } @barcodelist;
249 @notfoundbarcodes = grep { !exists $itemdata{lc $_} } @barcodelist;
253 # Flag to tell the template there are valid results, hidden or not
254 if(scalar(@itemnumbers) > 0){ $template->param("itemresults" => 1); }
255 # Only display the items if there are no more than pref MaxItemsToProcessForBatchMod or MaxItemsToDisplayForBatchDel
256 my $max_display_items = $del
257 ? C4::Context->preference("MaxItemsToDisplayForBatchDel")
258 : C4::Context->preference("MaxItemsToDisplayForBatchMod");
259 $template->param("too_many_items_process" => scalar(@itemnumbers)) if !$del && scalar(@itemnumbers) > C4::Context->preference("MaxItemsToProcessForBatchMod");
260 if (scalar(@itemnumbers) <= ( $max_display_items // 1000 ) ) {
263 $template->param("too_many_items_display" => scalar(@itemnumbers));
264 # Even if we do not display the items, we need the itemnumbers
265 $template->param(itemnumbers_array => \@itemnumbers);
268 # now, build the item form for entering a new item
270 # Getting list of subfields to keep when restricted batchmod edit is enabled
271 my @subfields_to_allow = $restrictededition ? split ' ', C4::Context->preference('SubfieldsToAllowForRestrictedBatchmod') : ();
273 my $subfields = Koha::UI::Form::Builder::Item->new->edit_form(
275 restricted_editition => $restrictededition,
278 ? ( subfields_to_allow => \@subfields_to_allow )
281 ignore_not_allowed_subfields => 1,
282 kohafields_to_ignore => ['items.barcode'],
283 prefill_with_default_values => $use_default_values,
284 default_branches_empty => 1,
288 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
290 subfields => $subfields,
291 notfoundbarcodes => \@notfoundbarcodes,
292 notfounditemnumbers => \@notfounditemnumbers
295 } # -- End action="show"
297 if ( $display_items ) {
299 Koha::UI::Table::Builder::Items->new( { itemnumbers => \@itemnumbers } )
302 items => $items_table->{items},
303 item_header_loop => $items_table->{headers},
310 ( $op ? ( $op => 1 ) : () ),
312 biblionumber => $biblionumber,
315 output_html_with_http_headers $input, $cookie, $template->output;