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>.
30 use C4::BackgroundJob;
35 use List::MoreUtils qw/uniq/;
37 use Koha::AuthorisedValues;
45 my $dbh = C4::Context->dbh;
46 my $error = $input->param('error');
47 my @itemnumbers = $input->multi_param('itemnumber');
48 my $biblionumber = $input->param('biblionumber');
49 my $op = $input->param('op');
50 my $del = $input->param('del');
51 my $del_records = $input->param('del_records');
52 my $completedJobID = $input->param('completedJobID');
53 my $runinbackground = $input->param('runinbackground');
54 my $src = $input->param('src');
55 my $use_default_values = $input->param('use_default_values');
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' };
69 my ($template, $loggedinuser, $cookie)
70 = get_template_and_user({template_name => $template_name,
74 flagsrequired => $template_flag,
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());
83 $template->param(del => $del);
87 my @errors; # store errors found while checking data BEFORE saving item.
88 my $items_display_hashref;
89 our $tagslib = &GetMarcStructure(1);
91 my $deleted_items = 0; # Number of deleted items
92 my $deleted_records = 0; # Number of deleted records ( with no items attached )
93 my $not_deleted_items = 0; # Number of items that could not be deleted
94 my @not_deleted; # List of the itemnumbers that could not be deleted
95 my $modified_items = 0; # Numbers of modified items
96 my $modified_fields = 0; # Numbers of modified fields
98 my %cookies = parse CGI::Cookie($cookie);
99 my $sessionID = $cookies{'CGISESSID'}->value;
102 #--- ----------------------------------------------------------------------------
103 if ($op eq "action") {
104 #-------------------------------------------------------------------------------
105 my @tags = $input->multi_param('tag');
106 my @subfields = $input->multi_param('subfield');
107 my @values = $input->multi_param('field_value');
108 my @disabled = $input->multi_param('disable_input');
109 # build indicator hash.
110 my @ind_tag = $input->multi_param('ind_tag');
111 my @indicator = $input->multi_param('indicator');
113 # Is there something to modify ?
114 # TODO : We shall use this var to warn the user in case no modification was done to the items
115 my $values_to_modify = scalar(grep {!/^$/} @values);
116 my $values_to_blank = scalar(@disabled);
119 # Once the job is done
120 if ($completedJobID) {
121 # If we have a reasonable amount of items, we display them
122 if (scalar(@itemnumbers) <= ( C4::Context->preference("MaxItemsToDisplayForBatchDel") // 1000 ) ) {
123 $items_display_hashref=BuildItemsData(@itemnumbers);
125 # Else, we only display the barcode
126 my @simple_items_display = map {
128 my $item = Koha::Items->find($itemnumber);
130 itemnumber => $itemnumber,
131 barcode => $item ? ( $item->barcode // q{} ) : q{},
132 biblionumber => $item ? $item->biblio->biblionumber : q{},
135 $template->param("simple_items_display" => \@simple_items_display);
138 # Setting the job as done
139 my $job = C4::BackgroundJob->fetch($sessionID, $completedJobID);
141 # Calling the template
142 add_saved_job_results_to_template($template, $completedJobID);
145 # While the job is getting done
147 # Job size is the number of items we have to process
148 my $job_size = scalar(@itemnumbers);
151 # If we asked for background processing
152 if ($runinbackground) {
153 $job = put_in_background($job_size);
156 #initializing values for updates
157 my ( $itemtagfield, $itemtagsubfield) = &GetMarcFromKohaField("items.itemnumber", "");
158 if ($values_to_modify){
159 my $xml = TransformHtmlToXml(\@tags,\@subfields,\@values,\@indicator,\@ind_tag, 'ITEM');
160 $marcitem = MARC::Record::new_from_xml($xml, 'UTF-8');
162 if ($values_to_blank){
163 foreach my $disabledsubf (@disabled){
164 if ($marcitem && $marcitem->field($itemtagfield)){
165 $marcitem->field($itemtagfield)->update( $disabledsubf => "" );
168 $marcitem = MARC::Record->new();
169 $marcitem->append_fields( MARC::Field->new( $itemtagfield, '', '', $disabledsubf => "" ) );
176 foreach my $itemnumber(@itemnumbers){
178 $job->progress($i) if $runinbackground;
179 my $itemdata = Koha::Items->find($itemnumber);
180 next unless $itemdata; # Should have been tested earlier, but just in case...
181 $itemdata = $itemdata->unblessed;
183 my $return = DelItemCheck( $itemdata->{'biblionumber'}, $itemdata->{'itemnumber'});
187 $not_deleted_items++;
189 { biblionumber => $itemdata->{'biblionumber'},
190 itemnumber => $itemdata->{'itemnumber'},
191 barcode => $itemdata->{'barcode'},
192 title => $itemdata->{'title'},
197 # If there are no items left, delete the biblio
198 if ( $del_records ) {
199 my $itemscount = Koha::Biblios->find( $itemdata->{'biblionumber'} )->items->count;
200 if ( $itemscount == 0 ) {
201 my $error = DelBiblio($itemdata->{'biblionumber'});
202 $deleted_records++ unless ( $error );
206 if ($values_to_modify || $values_to_blank) {
207 my $localmarcitem = Item2Marc($itemdata);
209 my $modified = UpdateMarcWith( $marcitem, $localmarcitem );
212 if ( my $item = ModItemFromMarc( $localmarcitem, $itemdata->{biblionumber}, $itemnumber ) ) {
213 LostItem($itemnumber, 'batchmod') if $item->{itemlost} and not $itemdata->{itemlost};
217 if ( $runinbackground ) {
218 $modified_items++ if $modified;
219 $modified_fields += $modified;
221 modified_items => $modified_items,
222 modified_fields => $modified_fields,
232 #-------------------------------------------------------------------------------
233 # build screen with existing items. and "new" one
234 #-------------------------------------------------------------------------------
237 my $filefh = $input->upload('uploadfile');
238 my $filecontent = $input->param('filecontent');
239 my ( @notfoundbarcodes, @notfounditemnumbers);
243 binmode $filefh, ':encoding(UTF-8)';
244 while (my $content=<$filefh>){
245 $content =~ s/[\r\n]*$//;
246 push @contentlist, $content if $content;
249 @contentlist = uniq @contentlist;
250 if ($filecontent eq 'barcode_file') {
251 my $existing_items = Koha::Items->search({ itemnumber => \@contentlist });
252 @itemnumbers = $existing_items->get_column('itemnumber');
253 my %exists = map {$_=>1} @{$existing_items->get_column('barcode')};
254 @notfoundbarcodes = grep { !$exists{$_} } @contentlist;
256 elsif ( $filecontent eq 'itemid_file') {
257 @itemnumbers = Koha::Items->search({ itemnumber => \@contentlist })->get_column('itemnumber');
258 my %exists = map {$_=>1} @itemnumbers;
259 @notfounditemnumbers = grep { !$exists{$_} } @contentlist;
262 if (defined $biblionumber){
263 my @all_items = GetItemsInfo( $biblionumber );
264 foreach my $itm (@all_items) {
265 push @itemnumbers, $itm->{itemnumber};
268 if ( my $list=$input->param('barcodelist')){
269 push my @barcodelist, uniq( split(/\s\n/, $list) );
271 my $existing_items = Koha::Items->search({ barcode => \@barcodelist });
272 @itemnumbers = $existing_items->get_column('itemnumber');
273 my @barcodes = $existing_items->get_column('barcode');
274 my %exists = map {$_=>1} @barcodes;
275 @notfoundbarcodes = grep { !$exists{$_} } @barcodelist;
279 # Flag to tell the template there are valid results, hidden or not
280 if(scalar(@itemnumbers) > 0){ $template->param("itemresults" => 1); }
281 # Only display the items if there are no more than pref MaxItemsToProcessForBatchMod or MaxItemsToDisplayForBatchDel
282 my $max_display_items = $del
283 ? C4::Context->preference("MaxItemsToDisplayForBatchDel")
284 : C4::Context->preference("MaxItemsToDisplayForBatchMod");
285 $template->param("too_many_items_process" => scalar(@itemnumbers)) if !$del && scalar(@itemnumbers) >= C4::Context->preference("MaxItemsToProcessForBatchMod");
286 if (scalar(@itemnumbers) <= ( $max_display_items // 1000 ) ) {
287 $items_display_hashref=BuildItemsData(@itemnumbers);
289 $template->param("too_many_items_display" => scalar(@itemnumbers));
290 # Even if we do not display the items, we need the itemnumbers
291 $template->param(itemnumbers_array => \@itemnumbers);
293 # now, build the item form for entering a new item
296 my $branch_limit = C4::Context->userenv ? C4::Context->userenv->{"branch"} : "";
298 my $libraries = Koha::Libraries->search({}, { order_by => ['branchname'] })->unblessed;# build once ahead of time, instead of multiple times later.
300 # Adding a default choice, in case the user does not want to modify the branch
301 my $nochange_branch = { branchname => '', value => '', selected => 1 };
302 unshift (@$libraries, $nochange_branch);
304 my $pref_itemcallnumber = C4::Context->preference('itemcallnumber');
306 # Getting list of subfields to keep when restricted batchmod edit is enabled
307 my $subfieldsToAllowForBatchmod = C4::Context->preference('SubfieldsToAllowForRestrictedBatchmod');
308 my $allowAllSubfields = (
309 not defined $subfieldsToAllowForBatchmod
310 or $subfieldsToAllowForBatchmod eq q||
312 my @subfieldsToAllow = split(/ /, $subfieldsToAllowForBatchmod);
314 foreach my $tag (sort keys %{$tagslib}) {
315 # loop through each subfield
316 foreach my $subfield (sort keys %{$tagslib->{$tag}}) {
317 next if IsMarcStructureInternal( $tagslib->{$tag}{$subfield} );
318 next if (not $allowAllSubfields and $restrictededition && !grep { $tag . '$' . $subfield eq $_ } @subfieldsToAllow );
319 next if ($tagslib->{$tag}->{$subfield}->{'tab'} ne "10");
320 # barcode and stocknumber are not meant to be batch-modified
321 next if $tagslib->{$tag}->{$subfield}->{'kohafield'} eq 'items.barcode';
322 next if $tagslib->{$tag}->{$subfield}->{'kohafield'} eq 'items.stocknumber';
325 my $index_subfield = int(rand(1000000));
326 if ($subfield eq '@'){
327 $subfield_data{id} = "tag_".$tag."_subfield_00_".$index_subfield;
329 $subfield_data{id} = "tag_".$tag."_subfield_".$subfield."_".$index_subfield;
331 $subfield_data{tag} = $tag;
332 $subfield_data{subfield} = $subfield;
333 $subfield_data{marc_lib} ="<span id=\"error$i\" title=\"".$tagslib->{$tag}->{$subfield}->{lib}."\">".$tagslib->{$tag}->{$subfield}->{lib}."</span>";
334 $subfield_data{mandatory} = $tagslib->{$tag}->{$subfield}->{mandatory};
335 $subfield_data{repeatable} = $tagslib->{$tag}->{$subfield}->{repeatable};
337 if ( $use_default_values) {
338 $value = $tagslib->{$tag}->{$subfield}->{defaultvalue};
339 # get today date & replace YYYY, MM, DD if provided in the default value
340 my $today = dt_from_string;
341 my $year = $today->year;
342 my $month = $today->month;
343 my $day = $today->day;
344 $value =~ s/YYYY/$year/g;
345 $value =~ s/MM/$month/g;
346 $value =~ s/DD/$day/g;
348 $subfield_data{visibility} = "display:none;" if (($tagslib->{$tag}->{$subfield}->{hidden} > 4) || ($tagslib->{$tag}->{$subfield}->{hidden} < -4));
349 # testing branch value if IndependentBranches.
351 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} ) {
352 my @authorised_values;
354 # builds list, depending on authorised value...
356 if ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "branches" ) {
357 foreach my $library (@$libraries) {
358 push @authorised_values, $library->{branchcode};
359 $authorised_lib{$library->{branchcode}} = $library->{branchname};
363 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "itemtypes" ) {
364 push @authorised_values, "";
365 my $itemtypes = Koha::ItemTypes->search_with_localization;
366 while ( my $itemtype = $itemtypes->next ) {
367 push @authorised_values, $itemtype->itemtype;
368 $authorised_lib{$itemtype->itemtype} = $itemtype->translated_description;
374 elsif ( $tagslib->{$tag}->{$subfield}->{authorised_value} eq "cn_source" ) {
375 push @authorised_values, "" unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
377 my $class_sources = GetClassSources();
378 my $default_source = C4::Context->preference("DefaultClassificationSource");
380 foreach my $class_source (sort keys %$class_sources) {
381 next unless $class_sources->{$class_source}->{'used'} or
382 ($value and $class_source eq $value) or
383 ($class_source eq $default_source);
384 push @authorised_values, $class_source;
385 $authorised_lib{$class_source} = $class_sources->{$class_source}->{'description'};
389 #---- "true" authorised value
392 push @authorised_values, ""; # unless ( $tagslib->{$tag}->{$subfield}->{mandatory} );
394 my @avs = Koha::AuthorisedValues->search({ category => $tagslib->{$tag}->{$subfield}->{authorised_value}, branchcode => $branch_limit },{order_by=>'lib'});
395 for my $av ( @avs ) {
396 push @authorised_values, $av->authorised_value;
397 $authorised_lib{$av->authorised_value} = $av->lib;
401 $subfield_data{marc_value} = {
403 id => "tag_".$tag."_subfield_".$subfield."_".$index_subfield,
404 name => "field_value",
405 values => \@authorised_values,
406 labels => \%authorised_lib,
409 # it's a thesaurus / authority field
411 elsif ( $tagslib->{$tag}->{$subfield}->{authtypecode} ) {
412 $subfield_data{marc_value} = {
414 id => $subfield_data{id},
416 authtypecode => $tagslib->{$tag}->{$subfield}->{authtypecode},
419 elsif ( $tagslib->{$tag}->{$subfield}->{value_builder} ) { # plugin
420 require Koha::FrameworkPlugin;
421 my $plugin = Koha::FrameworkPlugin->new( {
422 name => $tagslib->{$tag}->{$subfield}->{'value_builder'},
426 my $pars= { dbh => $dbh, record => $temp, tagslib => $tagslib,
427 id => $subfield_data{id}, tabloop => \@loop_data };
428 $plugin->build( $pars );
429 if( !$plugin->errstr ) {
430 $subfield_data{marc_value} = {
432 id => $subfield_data{id},
434 javascript => $plugin->javascript,
435 noclick => $plugin->noclick,
438 warn $plugin->errstr;
439 $subfield_data{marc_value} = { # supply default input form
441 id => $subfield_data{id},
446 elsif ( $tag eq '' ) { # it's an hidden field
447 $subfield_data{marc_value} = {
449 id => $subfield_data{id},
453 elsif ( $tagslib->{$tag}->{$subfield}->{'hidden'} ) { # FIXME: shouldn't input type be "hidden" ?
454 $subfield_data{marc_value} = {
456 id => $subfield_data{id},
460 elsif ( length($value) > 100
461 or (C4::Context->preference("marcflavour") eq "UNIMARC" and
462 300 <= $tag && $tag < 400 && $subfield eq 'a' )
463 or (C4::Context->preference("marcflavour") eq "MARC21" and
464 500 <= $tag && $tag < 600 )
466 # oversize field (textarea)
467 $subfield_data{marc_value} = {
469 id => $subfield_data{id},
473 # it's a standard field
474 $subfield_data{marc_value} = {
476 id => $subfield_data{id},
480 # $subfield_data{marc_value}="<input type=\"text\" name=\"field_value\">";
481 push (@loop_data, \%subfield_data);
484 } # -- End foreach tag
488 # what's the next op ? it's what we are not in : an add if we're editing, otherwise, and edit.
491 notfoundbarcodes => \@notfoundbarcodes,
492 notfounditemnumbers => \@notfounditemnumbers
495 } # -- End action="show"
497 $template->param(%$items_display_hashref) if $items_display_hashref;
501 $template->param( $op => 1 ) if $op;
503 if ($op eq "action") {
505 #my @not_deleted_loop = map{{itemnumber=>$_}}@not_deleted;
508 not_deleted_items => $not_deleted_items,
509 deleted_items => $deleted_items,
510 delete_records => $del_records,
511 deleted_records => $deleted_records,
512 not_deleted_loop => \@not_deleted
516 foreach my $error (@errors) {
517 $template->param($error => 1) if $error;
519 $template->param(src => $src);
520 $template->param(biblionumber => $biblionumber);
521 output_html_with_http_headers $input, $cookie, $template->output;
525 # ---------------- Functions
529 # now, build existiing item list
530 my %witness; #---- stores the list of subfields used at least once, with the "meaning" of the code
532 #---- finds where items.itemnumber is stored
533 my ( $itemtagfield, $itemtagsubfield) = &GetMarcFromKohaField("items.itemnumber", "");
534 my ($branchtagfield, $branchtagsubfield) = &GetMarcFromKohaField("items.homebranch", "");
535 foreach my $itemnumber (@itemnumbers){
536 my $itemdata = Koha::Items->find($itemnumber);
537 next unless $itemdata; # Should have been tested earlier, but just in case...
538 $itemdata = $itemdata->unblessed;
539 my $itemmarc=Item2Marc($itemdata);
541 foreach my $field (grep {$_->tag() eq $itemtagfield} $itemmarc->fields()) {
542 # loop through each subfield
543 my $itembranchcode=$field->subfield($branchtagsubfield);
544 if ($itembranchcode && C4::Context->preference("IndependentBranches")) {
546 my $userenv = C4::Context->userenv();
547 unless (C4::Context->IsSuperLibrarian() or (($userenv->{'branch'} eq $itembranchcode))){
548 $this_row{'nomod'}=1;
551 my $tag=$field->tag();
552 foreach my $subfield ($field->subfields) {
553 my ($subfcode,$subfvalue)=@$subfield;
554 next if ($tagslib->{$tag}->{$subfcode}->{tab} ne 10
555 && $tag ne $itemtagfield
556 && $subfcode ne $itemtagsubfield);
558 $witness{$subfcode} = $tagslib->{$tag}->{$subfcode}->{lib} if ($tagslib->{$tag}->{$subfcode}->{tab} eq 10);
559 if ($tagslib->{$tag}->{$subfcode}->{tab} eq 10) {
560 $this_row{$subfcode}=GetAuthorisedValueDesc( $tag,
561 $subfcode, $subfvalue, '', $tagslib)
565 $this_row{itemnumber} = $subfvalue if ($tag eq $itemtagfield && $subfcode eq $itemtagsubfield);
569 # grab title, author, and ISBN to identify bib that the item
570 # belongs to in the display
571 my $biblio = Koha::Biblios->find( $itemdata->{biblionumber} );
572 $this_row{title} = $biblio->title;
573 $this_row{author} = $biblio->author;
574 $this_row{isbn} = $biblio->biblioitem->isbn;
575 $this_row{biblionumber} = $biblio->biblionumber;
576 $this_row{holds} = $biblio->holds->count;
577 $this_row{item_holds} = Koha::Holds->search( itemnumber => $itemnumber )->count;
580 push(@big_array, \%this_row);
583 @big_array = sort {$a->{0} cmp $b->{0}} @big_array;
585 # now, construct template !
586 # First, the existing items for display
588 my @witnesscodessorted=sort keys %witness;
589 for my $row ( @big_array ) {
591 my @item_fields = map +{ field => $_ || '' }, @$row{ @witnesscodessorted };
592 $row_data{item_value} = [ @item_fields ];
593 $row_data{itemnumber} = $row->{itemnumber};
594 #reporting this_row values
595 $row_data{'nomod'} = $row->{'nomod'};
596 $row_data{bibinfo} = $row->{bibinfo};
597 $row_data{author} = $row->{author};
598 $row_data{title} = $row->{title};
599 $row_data{isbn} = $row->{isbn};
600 $row_data{biblionumber} = $row->{biblionumber};
601 $row_data{holds} = $row->{holds};
602 $row_data{item_holds} = $row->{item_holds};
603 my $is_on_loan = C4::Circulation::IsItemIssued( $row->{itemnumber} );
604 $row_data{onloan} = $is_on_loan ? 1 : 0;
605 push(@item_value_loop,\%row_data);
607 my @header_loop=map { { header_value=> $witness{$_}} } @witnesscodessorted;
609 return { item_loop => \@item_value_loop, item_header_loop => \@header_loop };
612 #BE WARN : it is not the general case
613 # This function can be OK in the item marc record special case
614 # Where subfield is not repeated
615 # And where we are sure that field should correspond
618 my ($marcfrom,$marcto)=@_;
619 my ( $itemtag, $itemtagsubfield) = &GetMarcFromKohaField("items.itemnumber", "");
620 my $fieldfrom=$marcfrom->field($itemtag);
621 my @fields_to=$marcto->field($itemtag);
623 foreach my $subfield ( $fieldfrom->subfields() ) {
624 foreach my $field_to_update ( @fields_to ) {
625 if ( $subfield->[1] ) {
626 unless ( $field_to_update->subfield($subfield->[0]) eq $subfield->[1] ) {
628 $field_to_update->update( $subfield->[0] => $subfield->[1] );
633 $field_to_update->delete_subfield( code => $subfield->[0] );
641 my ($tagfield,$insubfield,$record) = @_;
644 foreach my $field ($record->field($tagfield)) {
645 my @subfields = $field->subfields();
646 foreach my $subfield (@subfields) {
647 if (@$subfield[0] eq $insubfield) {
648 $result .= @$subfield[1];
649 $indicator = $field->indicator(1).$field->indicator(2);
653 return($indicator,$result);
656 # ----------------------------
657 # Background functions
660 sub add_results_to_template {
661 my $template = shift;
663 $template->param(map { $_ => $results->{$_} } keys %{ $results });
666 sub add_saved_job_results_to_template {
667 my $template = shift;
668 my $completedJobID = shift;
669 my $job = C4::BackgroundJob->fetch($sessionID, $completedJobID);
670 my $results = $job->results();
671 add_results_to_template($template, $results);
673 my $fields = $job->get("modified_fields");
674 my $items = $job->get("modified_items");
676 modified_items => $items,
677 modified_fields => $fields,
681 sub put_in_background {
682 my $job_size = shift;
684 my $job = C4::BackgroundJob->new($sessionID, "test", '/cgi-bin/koha/tools/batchMod.pl', $job_size);
685 my $jobID = $job->id();
688 if (my $pid = fork) {
690 # return job ID as JSON
692 # prevent parent exiting from
693 # destroying the kid's database handle
694 # FIXME: according to DBI doc, this may not work for Oracle
695 $dbh->{InactiveDestroy} = 1;
697 my $reply = CGI->new("");
698 print $reply->header(-type => 'text/html');
699 print '{"jobID":"' . $jobID . '"}';
701 } elsif (defined $pid) {
703 # close STDOUT to signal to Apache that
704 # we're now running in the background
708 # fork failed, so exit immediately
709 warn "fork failed while attempting to run tools/batchMod.pl as a background job";